Skip to content

Instantly share code, notes, and snippets.

@autokludge
Created June 12, 2020 00:15
Show Gist options
  • Save autokludge/4be744eed93f8345a1723462e7c88412 to your computer and use it in GitHub Desktop.
Save autokludge/4be744eed93f8345a1723462e7c88412 to your computer and use it in GitHub Desktop.
AutoCAD label rectangle size
(defun c:LabelRec (/ ActDoc CurSpace Ht ss cnt Ent EntData VerPoints tmpEnt Wid Len Pt Str tmpText tmpDist1 tmpDist2)
; original version as posted didn't work
; originally found at https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-that-dimensions-a-rectangle/td-p/4556923
; Label rectangles with length and width in middle of them.
(vl-load-com)
(setq ActDoc (vla-get-ActiveDocument (vlax-get-Acad-Object)))
(if (= (getvar "cvport") 1)
(setq CurSpace (vla-get-PaperSpace ActDoc))
(setq CurSpace (vla-get-ModelSpace ActDoc))
)
(if (and (setq Ht (getreal "\n Enter height of text: ")) (setq ss (ssget '((0 . "*POLYLINE")))))
(progn
(setq cnt 0)
(while (setq Ent (ssname ss cnt))
(setq EntData (entget Ent))
(if (= (cdr (assoc 0 EntData)) "LWPOLYLINE")
(setq VerPoints (vlax-get (vlax-ename->vla-object Ent) 'Coordinates))
(progn
(setq VerPoints nil)
(setq tmpEnt (entnext Ent))
(while (not (equal (cdr (assoc 0 (entget tmpEnt))) "SEQEND"))
(setq tmpPt (cdr (assoc 10 (entget tmpEnt))))
(setq VerPoints (append VerPoints (list (car tmpPt))))
(setq VerPoints (append VerPoints (list (cadr tmpPt))))
(setq tmpEnt (entnext tmpEnt)))
)
)
(if (= (length VerPoints) 8)
(progn
(setq tmpDist1 (distance (list (nth 0 VerPOints) (nth 1 VerPoints)) (list (nth 2 VerPoints) (nth 3 VerPoints))))
(setq tmpDist2 (distance (list (nth 2 VerPOints) (nth 3 VerPoints)) (list (nth 4 VerPoints) (nth 5 VerPoints))))
(if (< tmpDist1 tmpDist2)
(setq Len tmpDist2 Wid tmpDist1)
(setq Len tmpDist1 Wid tmpDist2)
)
(setq Pt (list (/ (+ (nth 0 VerPoints) (nth 4 VerPoints)) 2.0) (/ (+ (nth 1 VerPoints) (nth 5 VerPoints)) 2.0) 0.0))
;;
;; below is number precision
;; unit modes: 1-Scientific,2-Decimal,3-Engineering (feet and decimal inches),4-Architectural (feet and fractional inches),5-Fractional
;; unitPrecision: 0 to 10, number of decimal places. also affects fractionals ie 1/2" 1/4" 1/8" etc
;; read http://help.autodesk.com/view/ACD/2015/ENU/?guid=GUID-D03ABBC2-939A-44DB-8C93-FC63B64DE4A2 for more info on rtos
(setq unitMode 2)
(setq unitPrecision 0)
(setq Str (strcat (rtos Len unitMode unitPrecision) "x" (rtos Wid unitMode unitPrecision)))
;;
(setq tmpText (vla-AddText CurSpace Str (vlax-3d-point Pt) Ht))
(vla-put-Alignment tmpText 4)
(vla-put-TextAlignmentPoint tmpText (vlax-3d-point Pt))
)
)
(setq cnt (1+ cnt)))
)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment