- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I’m experiencing a performance issue when inserting a large number of blocks into AutoCAD using the script below.
The script works perfectly — it correctly inserts the blocks, sets the insertion point, and adjusts the visibility state as expected. However, when I try to insert a large number of blocks (for example, using the attached CSV file), the process becomes extremely slow and can take several hours to complete.
I have a few questions:
Is there a way to improve performance and make the insertion process faster?
I noticed that this method creates many anonymous blocks (*U22, *U23, *U123, etc.). Could this be the reason why performance degrades over time? It starts fast but becomes progressively slower.
If the block definition is not already present in the drawing and exists only in an external file (e.g., C://example//PNT_IMPORT.dwg), what would be the best approach to handle this efficiently?
I’ve attached a sample DWG and CSV file for reference.
Thank you in advance for your help.
(defun c:DEMO ( )
(setvar 'REGENMODE 0)
(if
(and
(setq file (getfiled "Select CSV File" (getvar 'DWGPREFIX) "csv" 16))
(setq data (LM:readcsv file))
)
(foreach line data
(setq item line)
(setq pnt (list (atof (nth 9 item)) (atof(nth 8 item)) 0.0 ))
(import:BlkInsert-Short
(car item)
pnt ;;ponto
(nth 1 item) ;visibilidade
(nth 2 item) ;info1
(nth 3 item) ;info2
(nth 4 item) ;info3
(nth 5 item) ;info4
(nth 6 item) ;info5
(nth 7 item) ;info6
)
)
)
(setvar 'REGENMODE 1)
(princ)
)
(defun import:BlkInsert-Short (nomebloco ponto visibility INFO1 INFO2 INFO3 INFO4 INFO5 INFO6 / block atts)
(if (and nomebloco ponto)
(progn
(setq block
(vla-InsertBlock
(if (= 1 (getvar 'cvport))
(vla-get-paperspace (vla-get-activedocument (vlax-get-acad-object)))
(vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object)))
)
(vlax-3d-point (trans ponto 1 0))
nomebloco 1 1 1 0
)
)
(LM:SetVisibilityState block visibility)
;; Se você sabe a ordem exata dos atributos no bloco
(if (vla-get-HasAttributes block)
(progn
(setq atts (vlax-safearray->list (vlax-variant-value (vla-getAttributes block))))
;; Atribui na ordem: ajuste os índices conforme seu bloco
(if (>= (length atts) 4)
(progn
(vla-put-TextString (nth 0 atts) INFO1)
(vla-put-TextString (nth 1 atts) INFO2)
(vla-put-TextString (nth 2 atts) INFO3)
(vla-put-TextString (nth 3 atts) INFO4)
(vla-put-TextString (nth 4 atts) INFO5)
(vla-put-TextString (nth 5 atts) INFO6)
)
)
)
)
)
)
(princ)
)
;; Read CSV - Lee Mac
;; Parses a CSV file into a matrix list of cell values.
;; csv - [str] filename of CSV file to read
(defun LM:readcsv ( csv / des lst sep str )
(if (setq des (open csv "r"))
(progn
(setq sep (cond ((vl-registry-read "HKEY_CURRENT_USER\\Control Panel\\International" "sList")) (",")))
(while (setq str (read-line des))
(setq lst (cons (LM:csv->lst str sep 0) lst))
)
(close des)
)
)
(reverse lst)
)
;; CSV -> List - Lee Mac
;; Parses a line from a CSV file into a list of cell values.
;; str - [str] string read from CSV file
;; sep - [str] CSV separator token
;; pos - [int] initial position index (always zero)
(defun LM:csv->lst ( str sep pos / s )
(cond
( (not (setq pos (vl-string-search sep str pos)))
(if (wcmatch str "\"*\"")
(list (LM:csv-replacequotes (substr str 2 (- (strlen str) 2))))
(list str)
)
)
( (or (wcmatch (setq s (substr str 1 pos)) "\"*[~\"]")
(and (wcmatch s "~*[~\"]*") (= 1 (logand 1 pos)))
)
(LM:csv->lst str sep (+ pos 2))
)
( (wcmatch s "\"*\"")
(cons
(LM:csv-replacequotes (substr str 2 (- pos 2)))
(LM:csv->lst (substr str (+ pos 2)) sep 0)
)
)
( (cons s (LM:csv->lst (substr str (+ pos 2)) sep 0)))
)
)
(defun LM:csv-replacequotes ( str / pos )
(setq pos 0)
(while (setq pos (vl-string-search "\"\"" str pos))
(setq str (vl-string-subst "\"" "\"\"" str pos)
pos (1+ pos)
)
)
str
)
;; Set Dynamic Block Visibility State - Lee Mac
;; Sets the Visibility Parameter of a Dynamic Block (if present) to a specific value (if allowed)
;; blk - [vla] VLA Dynamic Block Reference object
;; val - [str] Visibility State Parameter value
;; Returns: [str] New value of Visibility Parameter, else nil
(defun LM:SetVisibilityState ( blk val / vis )
(if
(and
(setq vis (LM:getvisibilityparametername blk))
(member (strcase val) (mapcar 'strcase (LM:getdynpropallowedvalues blk vis)))
)
(LM:setdynpropvalue blk vis val)
)
)
;-------------------------------------------------------------------------------;
;; Get Visibility Parameter Name - Lee Mac
;; Returns the name of the Visibility Parameter of a Dynamic Block (if present)
;; blk - [vla] VLA Dynamic Block Reference object
;; Returns: [str] Name of Visibility Parameter, else nil
(defun LM:getvisibilityparametername ( blk / vis )
(if
(and
(vlax-property-available-p blk 'effectivename)
(setq blk
(vla-item
(vla-get-blocks (vla-get-document blk))
(vla-get-effectivename blk)
)
)
(= :vlax-true (vla-get-isdynamicblock blk))
(= :vlax-true (vla-get-hasextensiondictionary blk))
(setq vis
(vl-some
'(lambda ( pair )
(if
(and
(= 360 (car pair))
(= "BLOCKVISIBILITYPARAMETER" (cdr (assoc 0 (entget (cdr pair)))))
)
(cdr pair)
)
)
(dictsearch
(vlax-vla-object->ename (vla-getextensiondictionary blk))
"ACAD_ENHANCEDBLOCK"
)
)
)
)
(cdr (assoc 301 (entget vis)))
)
)
;-------------------------------------------------------------------------------;
;; Get Dynamic Block Property Allowed Values - Lee Mac
;; Returns the allowed values for a specific Dynamic Block property.
;; blk - [vla] VLA Dynamic Block Reference object
;; prp - [str] Dynamic Block property name (case-insensitive)
;; Returns: [lst] List of allowed values for property, else nil if no restrictions
(defun LM:getdynpropallowedvalues ( blk prp )
(setq prp (strcase prp))
(vl-some '(lambda ( x ) (if (= prp (strcase (vla-get-propertyname x))) (vlax-get x 'allowedvalues)))
(vlax-invoke blk 'getdynamicblockproperties)
)
)
;-------------------------------------------------------------------------------;
;; Set Dynamic Block Property Value - Lee Mac
;; Modifies the value of a Dynamic Block property (if present)
;; blk - [vla] VLA Dynamic Block Reference object
;; prp - [str] Dynamic Block property name (case-insensitive)
;; val - [any] New value for property
;; Returns: [any] New value if successful, else nil
(defun LM:setdynpropvalue ( blk prp val )
(setq prp (strcase prp))
(vl-some
'(lambda ( x )
(if (= prp (strcase (vla-get-propertyname x)))
(progn
(vla-put-value x (vlax-make-variant val (vlax-variant-type (vla-get-value x))))
(cond (val) (t))
)
)
)
(vlax-invoke blk 'getdynamicblockproperties)
)
)
(defun LM:getvisibilitystate ( blk / vis )
(if (setq vis (LM:getvisibilityparametername blk))
(LM:getdynpropvalue blk vis)
)
)
;; Get Dynamic Block Property Value - Lee Mac
;; Returns the value of a Dynamic Block property (if present)
;; blk - [vla] VLA Dynamic Block Reference object
;; prp - [str] Dynamic Block property name (case-insensitive)
(defun LM:getdynpropvalue ( blk prp )
(setq prp (strcase prp))
(vl-some '(lambda ( x ) (if (= prp (strcase (vla-get-propertyname x))) (vlax-get x 'value)))
(vlax-invoke blk 'getdynamicblockproperties)
)
)Solved! Go to Solution.