Importing Blocks from CSV - Coordinates Issue

Importing Blocks from CSV - Coordinates Issue

srwYB8P3
Contributor Contributor
266 Views
3 Replies
Message 1 of 4

Importing Blocks from CSV - Coordinates Issue

srwYB8P3
Contributor
Contributor

I have a LISP command to automatically import multiple blocks at set coordinates and populate an attribute based on data in a CSV file. I'm running AutoCAD LT 2027. 

The CSV file has 3 columns - Block Attribute (ADDRESS), X-coordinate, and Y-coordinate.

To run the command, I load the LISP file and type in the command 'IBDETECTOR', then select the CSV labelled 'Import Data'.

However, when I run the command, it sometimes messes up the coordinates and bunches the blocks together, example shown in screenshot below. 

The bottom block should be at coordinates 195000, 12000, but is actually at 195000, 14700. The top block has imported correctly at coordinates 195000, 15000. 

srwYB8P3_0-1780571469852.png

The strange thing is that if I re-do the command, it sometimes imports the devices at different coordinates. 

DWG, LISP and CSV files attached. 

 

Appreciate any advice on how I can get this to import all blocks at correct coordinates as per the CSV!

0 Likes
Accepted solutions (1)
267 Views
3 Replies
Replies (3)
Message 2 of 4

komondormrex
Mentor
Mentor
Accepted solution

Try to put "_non“ before PT1 in line 22, like ..."_non" PT1...

0 Likes
Message 3 of 4

CADaSchtroumpf
Advisor
Advisor

It also seems to have an incorrect statement in the insert command.

Try this

(defun c:IBDetector ( / fn fp lst l VAL Row n ATT1 X Y PT1 b1 ff1 xx1)
(setq fn (getfiled "Select CSV file" "" "csv" 4))
(setq fp (open fn "r") lst '())
(while (setq l (read-line fp))
(setq lst (cons l lst))
)
(close fp)
(setq VAR (mapcar 'getvar '("ATTREQ" "CMDECHO" "SNAPMODE")))
(mapcar 'setvar '("ATTREQ" "CMDECHO" "SNAPMODE") '(0 0 0))
(foreach Row lst
(while (if (setq n (vl-string-search "," Row 0))
(setq Row (vl-string-subst " " "," Row))
)
)
(setq Row (read (strcat "(" Row ")")))

(setq ATT1 (vl-symbol-name (nth 0 Row)))
(setq X (nth 1 Row))
(setq Y (nth 2 Row))
(setq PT1 (list X Y))

(command "_.-insert" "FF Detectors & MCP" PT1 "1" "0")
(setq b1 (entget (entnext (entlast))))
(setq ff1(assoc 1 b1))
(setq xx1 (cons 1 ATT1))
(entmod(subst xx1 ff1 b1))
)
(mapcar 'setvar '("ATTREQ" "CMDECHO" "SNAPMODE") VAR)
(prin1)
)
0 Likes
Message 4 of 4

Sea-Haven
Mentor
Mentor

 @CADaSchtroumpf Just a suggestion as the csv is 3 values per line I dont make a list of the lines only use a while read, then process the line. 

 

I do use this to split the line into a lst as you read each line, saves a step in your code.

; thanks to Lee-mac for this defun
; 44 is , 34 is space 59 is ;

(defun csv->lst (str ans / pos )
(if (setq pos (vl-string-position ans str))
    (cons (substr str 1 pos) (csv->lst (substr str (+ pos 2)) ans))
    (list str)
    )
)

;(CSV->LST "1,2,3" 44)
; ("1" "2" "3")
(setq ATT1 (vl-symbol-name (nth 0 Row)))

(setq ATT1 (nth 0 Row)) ; should work if row is like I posted.

 

0 Likes