I'm probably missing something simple. This routine adds a circuit number to a home run in electrical drawings. It works fine in general, but the inserted text sometimes disappears if I run the "wrong" command as the next command (seems to happen if the TEXT command is next) or if I right click to re-run C:IS then press ESC. Does anyone see any flaws in the logic or know a way to fix the text so it doesn't disappear? Thanks for any thoughts or suggestions.
Martin
(defun eehr_id ( / *error* 72code 73code ds end_pt entity entity_ename
memb pt2 quarter_pi rot rot2 ss start_pt tempX)
(defun dxf (1code 1ent) (cdr (assoc 1code 1ent)))
(defun *error* (msg)
(if (member msg '( "console break" "Function cancelled" "quit / exit abort"))
(progn
(setq new (entlast))
(entdel new)
)
(progn
(if msg
(princ (strcat "\nError: " msg))
)
)
)
(princ)
) ;end error
(defun get_string (pt2 c72 c73 / a b c elist newent)
(defun GetKey ( / result )
(while ; repeat until a key is pressed
(null (eq 2 (car (setq result (grread))))) ; 2 as the first element of the
; list returned says the input
; was keyboard. Null returns
; T if the argument equated to
; nil, nil otherwise, so keyboard
; input will exit the loop
)
(chr (cadr result)) ; convert the code returned to
; a character
)
(setq a "_")
(setq b "")
(setq elist ; set up a text entity
(list
'(0 . "TEXT")
(cons 8 "POWER")
(cons 7 (getvar "textstyle"))
(cons 40 (getvar "textsize"))
(cons 62 2) ; set color = yellow
(cons 50 0.0) ; rotation
(cons 10 pt2) ; dummy value
(cons 11 pt2) ; insert point
(cons 72 c72) ; vertical justification
(cons 73 c73) ; horizontal justification
(cons 1 a)
)
)
(entmake elist) ; make the entity
(setq newent (entlast))
(setq newent (entget newent))
(while (/= b "\r") ; "\r" is enter
(setq b "") ; initialize
(while (= b "") ; loop
(setq b (getkey)) ; wait for a keystroke
(if (not (wcmatch b "[a-z],[A-Z],[0-9],-,['#],[' ],[`,],\r,\010"))
; test for letters, numbers, dash, pound, space, comma.
; ESC, and backspace
(setq b "") ; reset if not a valid character
)
)
(if (/= b "\r") ; if not enter
(progn
(if (= b "\010") ; test for backspace
(progn ; its a backspace
(if (> (strlen a) 0) ; can't delete from zero length string
(setq a (substr a 1 (1- (strlen a))))
)
)
(progn
(if (= a "_")
(setq a "")
)
(setq a (strcat a b))
)
)
)
)
(setq newent (subst (cons 1 a) (assoc 1 newent) newent))
; set up to change the text in the database
(entmod newent) ; update the database
)
(setq newent (subst (cons 62 256) (assoc 62 newent) newent))
; set up to change the text color in the database
(entmod newent) ; update the database
a
)
(setvar "cmdecho" 0)
(command "._undo" "be")
(setq ds (getvar "dimscale")) ; get the current scale
(setq ss "")
(while (= ss "")
(setq ss (car (entsel "\nSelect homerun to annotate... ")))
(setq entity (entget ss))
(if (not (and (= (dxf 0 entity) "INSERT") ; is it a block?
(= (substr (dxf 2 entity) 1 2) "EE") ; modern homeruns start with "EE" or "EEHR"
)
)
(setq ss "")
)
)
(setq entity_ename (dxf -1 entity))
(setq entity (entget entity_ename))
(setq tempX (vlax-ename->vla-object entity_ename))
; cast to an activeX entity
(foreach memb (vlax-invoke tempX 'Explode)
; explode the activeX block, then
; process each component
(if (= (vla-get-objectname memb) "AcDbPolyline")
; the arrowhead is a LWPOLYLINE
(progn
(setq new_entity memb) ; store the arrowhead
(setq start_pt (vlax-curve-getStartPoint new_entity))
(setq end_pt (vlax-curve-getEndPoint new_entity))
(vla-Delete memb)
(setq rot (+ (angle start_pt end_pt) 3.7051727))
(setq pt2 (polar start_pt rot (* ds 0.19483940)))
; set a point inside the homerun
(setq rot2 (- (angle end_pt start_pt) 0.365235))
(setq quarter_pi (/ pi 4.0))
(while (< rot2 0.0)
(setq rot2 (+ rot2 (* pi 2.0)))
)
(while (> rot2 (* pi 2.0))
(setq rot2 (- rot2 (* pi 2.0)))
)
(cond
((or (< rot2 quarter_pi) ; ML
(>= rot2 (* quarter_pi 7))
)
(progn
(setq 72code 0)
(setq 73code 2)
(setq pt2 (polar pt2 0.0 (* ds 0.19483940)))
)
)
((and (>= rot2 quarter_pi) ; C
(< rot2 (* quarter_pi 3))
)
(progn
(setq 72code 1)
(setq 73code 0)
(setq pt2 (polar pt2 (/ pi 2.0) (* ds 0.19483940)))
)
)
((and (>= rot2 (* quarter_pi 3)) ; MR
(< rot2 (* quarter_pi 5))
)
(progn
(setq 72code 2)
(setq 73code 2)
(setq pt2 (polar pt2 pi (* ds 0.19483940)))
)
)
((and (>= rot2 (* quarter_pi 5)) ; TC
(< rot2 (* quarter_pi 7))
)
(progn
(setq 72code 1)
(setq 73code 3)
(setq pt2 (polar pt2 (* (/ pi 2.0) 3.0) (* ds 0.19483940)))
)
)
)
(prompt "\nCircuit number... ")
(setq data (get_string pt2 72code 73code))
)
(vla-Delete memb)
)
)
(*error* nil)
(command "._undo" "e")
(princ)
)