Jump to content

Import multiple Blocks from CSV/txt file with coordinates


Recommended Posts

Posted

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.

 

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
Posted

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 2
Posted
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!

Posted
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
Posted

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

Posted

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

Posted
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.

Posted

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! 

Posted

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.

  • 3 weeks later...
Posted
On 3/22/2019 at 1:51 AM, BIGAL said:

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.

can you somehow pull it out of Tool Palettes with lisp

Posted (edited)

No need for tool palettes, where is the block ?

 

Is it a dwg on its own ? Then just use Insert to drag it into the current dwg you must have full path of dwg location

 

Or is it a block in another dwg. You can use lee-mac Steal.lsp, in a lisp it can be ran with out user input to pull a block out of another dwg.

 

; checks for block

(if (tblsearch "Block" "Holden")
(princ "Holden")
(progn
(command "Insert" "p:\\Autodesk\\vba\\holdencar.dwg" "0,0" 1 1 0)
(command "erase" "last" "")
) ; progn
)

Edited by BIGAL
Posted

Thank you for advice Bigal, and you Rlx for lisp. Yes, I must use Lee's lisp because blocks are in a drawing, just need to change this lisp cause my block names are at the end of line and also do some lists comparing cause my names in list don't correspond to names in dwg where I'm supposed to get'em out.

Posted (edited)

The instruction to call a block from a dwg are in Lee's Steal.lsp you should read 1st, I have used it and it works well, just trying to remember which bit of code its in.

 

; cut a bit out of the code

(if (not steal)(load "Stealv1-6"))
(Steal <Dwg> <ItemList>)  
dwg obvious full pathed dwg name
 (Steal "C:\\My Folder\\MyDrawing.dwg"
    '(   ("Blocks" "Block1" "Block22")   )
)    

Edited by BIGAL
  • Like 1
Posted (edited)

i need your help guys again...when passing block name as a string to the Lee's lisp i get 'Error: bad argument type: stringp KBN' where KBN contains string with block name, so how to pass it, thanx

    (Steal "D:\\Geodezija\\Kartografska zbirka\\1000_BLOKOVI.dwg"
           '(("Blocks"
              (kbn)
             )
            )
    ) ;_  Steal

 

Edited by Tomislav
Posted (edited)
14 minutes ago, Tomislav said:

i need your help guys again...when passing block name as a string to the Lee's lisp i get 'Error: bad argument type: stringp KBN' where KBN contains string with block name, so how to pass it, thanx


    (Steal "D:\\Geodezija\\Kartografska zbirka\\1000_BLOKOVI.dwg"
           '(("Blocks"
              (kbn)
             )
            )
    ) ;_  Steal

 

 

Format is (Steal <Drawing Filename> <Item List>)

Drawing Filename (dwg)

The full filename of the drawing from which items are to be imported.

Item List (lst)

A list of items to be imported, in the following format:

(
    (   <Collection 1>
        (<Item 1> <Item 2>  ...  <Item N>)
    )
    (   <Collection 2>
        (<Item 1> <Item 2>  ...  <Item N>)
    )
    ...
    (   <Collection N>
        (<Item 1> <Item 2>  ...  <Item N>)
    )
)

 

 

Try

    (Steal "D:\\Geodezija\\Kartografska zbirka\\1000_BLOKOVI.dwg"
           (list (list "Blocks" (list kbn)))
    ) ;_  Steal

 

Edited by dlanorh
  • Like 1
Posted (edited)

well that just worked, thank you...i still don't quite get it why list,list,list instead of  '(("Blocks" (kbn)) but must work on it

Edited by Tomislav
Posted
3 hours ago, Tomislav said:

well that just worked, thank you...i still don't quite get it why list,list,list instead of  '(("Blocks" (kbn)) but must work on it

 

The first  quote  '( means everything is literal, and it isn't as you have an evaluation (kbn = string value). This means that you must evaluate everything into the item to be evaluated.

 

Putting brackets round items doesn't make a list

 

(setq a (10 20 30)) => ;error: bad function: 10. This is looking for a function called 10 (defun 10 ( a b / )......) with two parameters to pass in

(setq a '(10 20 30)) => (10 20 30) (type a) => LIST

but

(setq b 20)

(setq a '(10 b 30)) => (10 B 30) (type a) => LIST ; ' means literal b is not evaluated

(setq a (list 10 b 30)) => (10 20 30) (type a) => LIST ;Now b is evaluated

 

 

 

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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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...