Jump to content
Shablab

Import multiple Blocks from CSV/txt file with coordinates

Recommended Posts

Shablab

I want to be able to import 5-10 different blocks that need to be placed at approximately 70 locations each. I want to achieve this by taking a CSV file I have with the following attributes: (NAME, Xcoord, Ycoord, Zcoord) and then place the 'NAME' block at the correct coordinates within the drawing. The blocks are already preloaded within the drawing itself so they can be referenced by name.

 

Share this post


Link to post
Share on other sites
rlx
Posted (edited)

quickly written & totally untested without example dwg / csv

(defun c:t1 ( / SplitStr doc spc csv fp data bn)
  (defun SplitStr ( s d / p ) (if (setq p (vl-string-search d s)) (cons (substr s 1 p) (SplitStr (substr s (+ p 1 (strlen d))) d)) (list s)))
  (if (and (setq doc (vla-get-activedocument (vlax-get-acad-object)) spc (vla-get-block (vla-get-activelayout doc)))
           (setq csv (getfiled "Select csv file" "" "csv" 0) fp (open csv "r")))
    (while (and (setq data (read-line fp)) (setq data (splitstr data ",") bn (car data) pnt (cdr data)))
      (if (tblsearch "block" bn)(vla-InsertBlock spc (vlax-3d-point (mapcar 'atof pnt)) bn 1 1 1 0)))) (princ))

 

Edited by rlx

Share this post


Link to post
Share on other sites
rlx

try now


(defun c:t1 ( / SplitStr doc spc csv fp data bn)
  (defun SplitStr ( s d / p )
    (if (setq p (vl-string-search d s)) (cons (substr s 1 p) (SplitStr (substr s (+ p 1 (strlen d))) d)) (list s)))
  (if (and (setq doc (vla-get-activedocument (vlax-get-acad-object)) spc (vla-get-block (vla-get-activelayout doc)))
             (setq csv (getfiled "Select csv file" "" "csv" 0))(setq fp (open csv "r")) (read-line fp))
    (while (and (setq data (read-line fp)) (setq data (splitstr data ",") bn (car data) pnt (cdr data)))
      (if (tblsearch "block" bn)(vla-InsertBlock spc (vlax-3d-point (mapcar 'atof pnt)) bn 1 1 1 0))))
  (if fp (close fp))
  (princ)
)

 

  • Like 1

Share this post


Link to post
Share on other sites
Shablab
14 hours ago, rlx said:

try now

 


(defun c:t1 ( / SplitStr doc spc csv fp data bn)
  (defun SplitStr ( s d / p )
    (if (setq p (vl-string-search d s)) (cons (substr s 1 p) (SplitStr (substr s (+ p 1 (strlen d))) d)) (list s)))
  (if (and (setq doc (vla-get-activedocument (vlax-get-acad-object)) spc (vla-get-block (vla-get-activelayout doc)))
             (setq csv (getfiled "Select csv file" "" "csv" 0))(setq fp (open csv "r")) (read-line fp))
    (while (and (setq data (read-line fp)) (setq data (splitstr data ",") bn (car data) pnt (cdr data)))
      (if (tblsearch "block" bn)(vla-InsertBlock spc (vlax-3d-point (mapcar 'atof pnt)) bn 1 1 1 0))))
  (if fp (close fp))
  (princ)
)

 

 

That worked beautifully! Thank you so much!

Share this post


Link to post
Share on other sites
rlx
1 minute ago, Shablab said:

That worked beautifully! Thank you so much!

 

you're welcome... really was a small fix. First line in your csv has a header and without having the csv file on my first attempt I didn't account for that. After seeing the file it was simply a matter of sipping the first line 🤓

  • Like 1

Share this post


Link to post
Share on other sites
Fredricc

whats the command in cad when use this lisp ? t1 gets me the option to choose csv, then after choose it nothing happens ? 

Share this post


Link to post
Share on other sites
rlx

are you using shablab's drawing or one of your own? Drawing must have preloaded block's (see first post , last sentence )

Share this post


Link to post
Share on other sites
rlx
2 minutes ago, Fredricc said:

i trying on different drawings

 

the point is , the csv file contains the coordinates and the blocknames of the block to be inserted. These block must be present in the drawing before you can use the lisp routine. If you have purged the drawing, all the block definitions will be gone before you can do anything so you have to use the drawing from 3rd post , and without purging the drawing.

Share this post


Link to post
Share on other sites
Fredricc

ok! was not this type of lisp i was serching for here but in some way i was turned into this thread and tryed it. thx for the info! 

Share this post


Link to post
Share on other sites
BIGAL

A little bit more.

 

A simple defun can be added that checks the block exists if not insert it, then delete it, the block definition stays in the dwg. But the block must be available as a seperate dwg saved in say a block library.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...