Missing blocks

Missing blocks

nicolasR6NGC
Contributor Contributor
4,203 Views
8 Replies
Message 1 of 9

Missing blocks

nicolasR6NGC
Contributor
Contributor

I have a CSV that holds a the location (x coordinate, y coordinate) and the attributes to assign to a block (addresses, and parcel numbers). The LISP that I wrote creates these blocks and the location with the given attributes using entmakex however when I run the LISP the blocks only show when selected through select similar or by filtering them out when selecting the whole file's geometry. I've run regen and regenall however the blocks still won't show outside the property manager. Any help?

Note: this will import 1397 addresses so I would like to use entmakex to save time.

 

LISP:

(defun c:ParcelCreator (/ LM:str->lst read_csv_file draw_pline ; local functions
fname points^ itm)

;; Parse string in CSV ;;
(defun LM:str->lst ( str del / pos )
(if (setq pos (vl-string-search del str))
(cons (substr str 1 pos) (LM:str->lst (substr str (+ pos 1 (strlen del))) del))
(list str)
)
) ; LM:str->lst

(defun read-csv-addy (filename)
(setq file (open filename "r"))
(if (not file)
(progn
(princ "\nError: Unable to open file.") ; Print error message
(exit) ; Stop execution
)
)
(setq data '())
(while (setq line (read-line file))
(setq fields (LM:str->lst line ",")) ; Use LM:str->lst instead of vl-string-split
(if (and fields (= (length fields) 5)) ; Ensure fields exist and have 4 elements
(setq data (cons fields data))
)
)
(close file)
(reverse data)
)

(defun insert-block (blkname polyind x y address apn)
(setq inspt (list (atof x) (atof y) 0)) ; Convert string coordinates to numbers

;; Create block reference
(setq blk-entity (entmakex (list
(cons 0 "INSERT") ; Block reference
(cons 2 blkname) ; Block name
(cons 10 inspt) ; Insertion point
(cons 41 1.0) ; X scale factor
(cons 42 1.0) ; Y scale factor
(cons 43 1.0) ; Z scale factor
(cons 50 0.0) ; Rotation angle
)))

; Now modify the attributes of the inserted block
(setq ent (entnext blk-entity)) ; Get the first attribute
(while ent
(setq entData (entget ent)) ; Get the entity data of the attribute
(setq tag (cdr (assoc 2 entData))) ; Get the tag of the attribute
(if (equal tag "ADDRESS") ; Match the tag of the attribute
(progn
; Modify the attribute value
(setq newEntData (subst (cons 1 address) (assoc 1 entData) entData))
(entmod newEntData) ; Modify the attribute entity
(princ (strcat "Modified attribute " tag address))
)
)
(if (equal tag "APN") ; Match the tag of the attribute
(progn
; Modify the attribute value
(setq newEntData (subst (cons 1 apn) (assoc 1 entData) entData))
(entmod newEntData) ; Modify the attribute entity
(princ (strcat "Modified attribute " tag " to " apn))
)
)
(setq ent (entnext ent)) ; Move to the next attribute
)
(princ "Block inserted and attributes modified.")
)


;; Main execution
(setq mykmzPath (getfiled "Select KMZ File" "" "" 0)) ; Get the KMZ file path
(if mykmzPath ;if KMZ exists go into script
(progn
(setq dir (vl-filename-directory mykmzPath))
(setq Addresspath (strcat dir "\\" "AddressCoords.csv"))
(setq start 1)
(if (setq Aname (findfile Addresspath))
(progn
(setq blkname "ADD_PLACE")
(setq csv-data (read-csv-addy Aname))
;;(print csv-data)
(foreach row csv-data
(if (= start 1)
(progn
(print "here")
(setq start (+ start 1))
)
(progn
(print row) ; Debug output to check row format
(apply 'insert-block (cons blkname row))
)
)
)
)
)
)
)
(print) ; Final print for cleanliness
)

0 Likes
Accepted solutions (1)
4,204 Views
8 Replies
Replies (8)
Message 2 of 9

cadffm
Consultant
Consultant

Hi,

where is the part you create the ATTRIB(s) and SEQEND?

 

Or you create Inserts only, run ATTSYNC for this block

and the other info you are after:

(REDRAW)

 

 

My offtopic opinion: Blocks with (non constant) attdefs ONLY, is not the best idea.

Sebastian

Message 3 of 9

ronjonp
Mentor
Mentor
Accepted solution

@nicolasR6NGC 

I would suggest using vla-insertblock since your block is already defined in the drawing. Then you can fill in the attributes easily using Lee's LM:vl-setattributevalue function.

Message 4 of 9

nicolasR6NGC
Contributor
Contributor

Thanks for the Attsync reminder I ran it instead of regen and the blocks appeared. The problem now is that I have been editing the attributes incorrectly. I'm trying to create the blocks by calling an existing block (ADD_PLACE saved as blkname) that has the tags ADDRESS and APN. I'd like to stick to AutoCAD LISP commands over autocad commands since it runs faster but don't know how to edit the blocks otherwise do you have any reccomendations? 

0 Likes
Message 5 of 9

nicolasR6NGC
Contributor
Contributor

Awesome I will give this a try and report back thank you!

0 Likes
Message 6 of 9

Sea-Haven
Mentor
Mentor

Like @ronjonp insert the block vla-insertblock, then (entlast) which is the block you can then get the attributes of this object which are empty and (Vlax-put att textstring) so filling the attributes its very fast. You can just loop through the attributes as they will be returned in a sequence so no tag names needed.. Do you need code ?

0 Likes
Message 7 of 9

ronjonp
Mentor
Mentor

@Sea-Haven No need for entlast when using vla-insertblock. The result is a vla-object which then can be iterated with (foreach att (vlax-invoke blk 'getattributes))

0 Likes
Message 8 of 9

nicolasR6NGC
Contributor
Contributor

This worked out perfectly thank you so much!

0 Likes
Message 9 of 9

ronjonp
Mentor
Mentor

@nicolasR6NGC Glad you got it sorted! 🍻

0 Likes