Message 1 of 9
nerdy stuff - help in optimizing lisp that dimensions room
Not applicable
04-05-2016
11:50 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi guys:
I am a lisp newbie and just pieced together the following code to help me put room dimensions in multiple rooms. This only works for feet and inches drawings. I was wondering if there is a way to optimize this code... just as a way of learning something new. The curent code works perfectly for my requirements.
Thanks for all the help you can provide.
Best regards,
Akshay
(defun round (num prec) ;; routine to round up or down a number based on precision levels
(* prec
(if (minusp num)
(fix (- (/ num prec) 0.5))
(fix (+ (/ num prec) 0.5))
)
)
)
(defun my-getstring (msg);; routine to not accept a null response for a get string function
(while
(eq ""
(setq response
(vl-string-trim " "
(getstring T msg)
)
)
)
(princ "\nInvalid respose,")
)
response
)
;; below is the main routine.
(defun c:rmdim(/ *error* var val uvar uval resp ent hpent pt1 pt2 rmname hor ver horft verft horin verin dimtxt)
(defun *error* ( msg )
(mapcar 'setvar var val)
(mapcar 'setvar uvar uval)
(if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*")))
(princ (strcat "\nError: " msg))
);end if
);end defun error
(setq var '(cmdecho attreq)
val (mapcar 'getvar var)
uvar '(clayer textsize textstyle)
uval (mapcar 'getvar uvar)
); end setq
(initget "Yes No")
(setq resp (cond ( (getkword "\nDo you want to match text to an entity in drawing?<N>") )( "No" )))
(cond
((= resp "No") (princ "\nUsing current text attributes"))
((= resp "Yes")
(while
(not
(and
(setq
ent (car (entsel "\nPick the text style to match"))
hpent (if ent (entget ent))
); end setq
(OR (= (cdr (assoc 0 hpent)) "MTEXT") (= (cdr (assoc 0 hpent)) "TEXT"))
(= (cdr (assoc 70 (tblsearch "layer" (cdr (assoc 8 hpent))))) 0); on Unlocked Layer
); end and
); end not
(prompt "\nNothing selected, or it is not a Text object -- ")
); end while
(progn
(setvar "clayer" (cdr (assoc 8 hpent)))
(setvar "textstyle" (cdr (assoc 7 hpent)))
(setvar "textsize" (cdr (assoc 40 hpent)))
);close progn
)
);end cond
(while
(setq pt1 (getpoint "\nPick first corner:"))
(setq pt2 (getcorner pt1 "\nPick second corner:"))
(setq rmname (my-getstring "\nRoom Name: "))
(setq hor (abs (- (car pt1) (car pt2))))
(setq ver (abs (- (cadr pt1) (cadr pt2))))
(setq horft (fix (/ hor 12)))
(setq verft (fix (/ ver 12)))
(setq horin (round (rem hor 12) 1))
(setq verin (round (rem ver 12) 1))
(if (= horin 12) (progn
(setq horin 0)
(setq horft (+ horft 1))
);end progn
);endif
(if (= verin 12) (progn
(setq verin 0)
(setq verft (+ verft 1))
);end progn
);endif
(setq dimtxt (strcat rmname "\n" (itoa horft) "'-" (itoa horin) (chr 34) " X " (itoa verft) "'-" (itoa verin) (chr 34)))
(entmake
(list
'(0 . "MTEXT")
(CONS 100 "AcDbEntity")
(CONS 100 "AcDbMText")
(CONS 10 pt1)
(CONS 7 (getvar "textstyle"))
'(71 . 5)
(CONS 1 dimtxt)
) ;_ end list
) ;_ end entmake
(command "move" (entlast) "" pt1 pause)
);end while
(mapcar 'setvar uvar uval)
);end defun rmdim