Using LISp to edit my Block with an Excel Sheet
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Why is AutoCAD not detecting my block name, "1A-Plan_A". Here is the routine I am running, and the properties dialog box of the block in discussion please help me resolve this issue.
;; -----------------------------------------
;; UPDATE ATTRIBUTES FOR BLOCK: 1A-PLAN_A
;; ATTRIBUTES: NA, NB, ND1, ND2, NE1, NE2, NHAD, NSHAD
;; -----------------------------------------
(defun StrSplit (str delim / pos lst)
(while (setq pos (vl-string-search delim str))
(setq lst (cons (substr str 1 pos) lst))
(setq str (substr str (+ pos (strlen delim) 1)))
)
(reverse (cons str lst))
)
(defun ReadCSV (path / file line rows)
(setq file (open path "r"))
(while (setq line (read-line file))
(setq rows (cons (StrSplit line ",") rows))
)
(close file)
(reverse rows)
)
(defun GetAttributes (blk)
(vl-remove-if
'null
(mapcar
'(lambda (x)
(if (= (vla-get-ObjectName x) "AcDbAttribute")
x
)
)
(vlax-safearray->list
(vlax-variant-value (vla-getAttributes blk))
)
)
)
)
(defun c:UpdatePlanA ( / csv data headers rows ss blk att tag idx row rowIndex)
(setq csv (getfiled "Select CSV file" "" "csv" 0))
(if (not csv)
(progn (princ "\nCanceled.") (exit))
)
;; Read CSV
(setq data (ReadCSV csv))
(setq headers (mapcar 'strcase (car data)))
(setq rows (cdr data))
;; Select block by exact name
(setq ss (ssget "X" '((2 . "1A-PLAN_A"))))
(if (not ss)
(progn (princ "\nNo blocks named 1A-PLAN_A found.") (exit))
)
(setq rowIndex 0)
;; Update each block
(repeat (sslength ss)
(setq blk (vlax-ename->vla-object (ssname ss rowIndex)))
(setq row (nth rowIndex rows))
(setq attlist (GetAttributes blk))
(foreach att attlist
(setq tag (strcase (vla-get-TagString att)))
(setq idx (vl-position tag headers))
(if idx
(vla-put-TextString att (nth idx row))
)
)
(setq rowIndex (1+ rowIndex))
)
(princ "\n✓ Block 1A-PLAN_A updated.")
(princ)
)