;;; Design Agency | BESS Layout Studio ;;; DAFENCE creates or extends one planar DA-FENCE polyline in model space. ;;; Load in AutoCAD LT for Windows 2024 or later, or full desktop AutoCAD ;;; with compatible AutoLISP support. Geometry is committed once, on Finish. ;;; Coordinates picked in the current UCS are projected onto world XY at Z=0. (defun daf:plan (ucs / w) (setq w (trans ucs 1 0)) (list (car w) (cadr w))) (defun daf:ucs (xy) (trans (list (car xy) (cadr xy) 0.0) 0 1)) (defun daf:layer-ready (/ data) (setq data (tblsearch "LAYER" "DA-FENCE")) (or (null data) (and (= 0 (logand 5 (cdr (assoc 70 data)))) (> (cdr (assoc 62 data)) 0)))) (defun daf:points (data / result entry) (foreach entry data (if (= (car entry) 10) (setq result (cons (list (cadr entry) (caddr entry)) result)))) (reverse result)) (defun daf:ordinary (data / normal curved entry) (setq normal (cdr (assoc 210 data))) (foreach entry data (if (and (= (car entry) 42) (/= (cdr entry) 0.0)) (setq curved T))) (and (= (cdr (assoc 0 data)) "LWPOLYLINE") (= (strcase (cdr (assoc 8 data))) "DA-FENCE") (zerop (logand 1 (cdr (assoc 70 data)))) (>= (cdr (assoc 90 data)) 2) (or (null normal) (equal normal '(0.0 0.0 1.0) 0.00000001)) (or (null (assoc 38 data)) (equal (cdr (assoc 38 data)) 0.0 0.00000001)) (or (null (assoc 39 data)) (equal (cdr (assoc 39 data)) 0.0 0.00000001)) (null curved))) (defun daf:pick-existing (/ sel data pts picked first lastpt prepend) (setq sel (entsel "\nSelect the open DA-FENCE polyline near the end to extend: ")) (if sel (progn (setq data (entget (car sel) '("*"))) (if (daf:ordinary data) (progn (setq pts (daf:points data) picked (daf:plan (cadr sel)) first (car pts) lastpt (last pts) prepend (< (distance first picked) (distance lastpt picked))) (list (car sel) data (if prepend first lastpt) (if prepend lastpt first) prepend (length pts))) (prompt "\nChoose an open DA-FENCE lightweight polyline with straight segments on world XY."))))) (defun daf:segment (a b) (grdraw (daf:ucs a) (daf:ucs b) 7 1)) (defun daf:preview (reversed / points) (redraw) (setq points reversed) (while (cdr points) (daf:segment (car points) (cadr points)) (setq points (cdr points)))) (defun daf:by-length (base / lengthValue degrees radians) (initget 6) (setq lengthValue (getreal "\nLeg length in drawing units: ")) (if lengthValue (progn (setq degrees (getreal "\nWorld plan angle in degrees (0 east, 90 north): ")) (if degrees (progn (setq radians (* pi (/ degrees 180.0))) (list (+ (car base) (* lengthValue (cos radians))) (+ (cadr base) (* lengthValue (sin radians))))))))) (defun daf:draft (base otherEnd existingCount / points count done closed answer next) (setq points (list base) count existingCount) (while (not done) (initget 1024 "Length Undo Close") (setq answer (getpoint (daf:ucs (car points)) "\nNext corner or [Length/Undo/Close] : ") next nil) (cond ((null answer) (setq done T)) ((equal answer "Undo") (if (cdr points) (progn (setq points (cdr points) count (1- count)) (daf:preview points)) (prompt "\nNo new leg to undo."))) ((equal answer "Close") (if (>= count 3) (setq closed T done T) (prompt "\nA closed fence needs at least three vertices."))) ((equal answer "Length") (setq next (daf:by-length (car points)))) (T (setq next (daf:plan answer)))) (if next (cond ((equal next (car points) 0.00000001) (prompt "\nChoose a different point.")) ((and (>= count 3) (equal next otherEnd 0.00000001)) (prompt "\nChoose Close to return to the first end.")) ((>= count 10000) (prompt "\nThe studio limit is 10,000 vertices per path. Press Enter to finish.")) (T (daf:segment (car points) next) (setq points (cons next points) count (1+ count)))))) (redraw) (if (or closed (> count existingCount)) (list (reverse points) closed))) (defun daf:vertex-tags (points / tags xy) (foreach xy points (setq tags (cons (cons 10 xy) tags))) (reverse tags)) (defun daf:entity (points closed) (append (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(8 . "DA-FENCE") '(100 . "AcDbPolyline") (cons 90 (length points)) (cons 70 (if closed 1 0)) '(38 . 0.0)) (daf:vertex-tags points) '((210 0.0 0.0 1.0)))) (defun daf:extended-data (data added prepend closed / result listToInsert active inserted tag code) ;; Preserve old vertex widths, ids, XDATA, handle, style and dictionary. ;; New vertices have the drawing's ordinary zero-bulge defaults. (setq listToInsert (daf:vertex-tags (if prepend (reverse added) added))) (foreach tag data (setq code (car tag)) (cond ((= code 90) (setq tag (cons 90 (+ (cdr tag) (length added))))) ((and (= code 70) closed) (setq tag (cons 70 (logior 1 (cdr tag)))))) (if (and (not inserted) (or (and prepend (= code 10)) (and (not prepend) active (not (member code '(10 40 41 42 91)))))) (progn (setq result (append (reverse listToInsert) result) inserted T))) (if (= code 10) (setq active T)) (setq result (cons tag result))) (if (not inserted) (setq result (append (reverse listToInsert) result))) (reverse result)) (defun c:DAFENCE (/ *error* start existing draft result) (defun *error* (message) (redraw) (if (and message (not (wcmatch (strcase message) "*CANCEL*,*QUIT*,*EXIT*"))) (princ (strcat "\nDAFENCE: " message))) (princ)) (cond ((= 1 (getvar "CVPORT")) (prompt "\nRun DAFENCE in model space.")) ((not (member (getvar "INSUNITS") '(1 2 4 5 6 7 14 21))) (prompt "\nSet the drawing's actual insertion units first. The studio accepts common metric and imperial units.")) ((not (daf:layer-ready)) (prompt "\nTurn on, thaw and unlock DA-FENCE before editing it.")) (T (prompt "\nDAFENCE projects picks onto world XY at Z=0. Enter finishes; Escape discards the draft.") (initget 1024 "Extend") (setq start (getpoint "\nFirst fence point or [Extend] : ")) (cond ((null start) nil) ((equal start "Extend") (setq existing (daf:pick-existing)) (if (listp existing) (if existing (progn (setq draft (daf:draft (nth 2 existing) (nth 3 existing) (nth 5 existing))) (if draft (progn (setq result (entmod (daf:extended-data (cadr existing) (cdr (car draft)) (nth 4 existing) (cadr draft)))) (if result (entupd (car existing))))))))) (T (setq start (daf:plan start) draft (daf:draft start start 1)) (if draft (setq result (entmake (daf:entity (car draft) (cadr draft))))))) (cond (result (prompt "\nFence saved on DA-FENCE.")) (draft (prompt "\nAutoCAD could not save this fence. Check the layer and drawing.")) (T (prompt "\nNo fence changes."))))) (princ)) (princ "\nDesign Agency fence tool loaded. Type DAFENCE to draw or extend a run.") (princ)