Need lisp for loading multiple blocks into a drawing

Need lisp for loading multiple blocks into a drawing

StanThe
Enthusiast Enthusiast
4,402 Views
18 Replies
Message 1 of 19

Need lisp for loading multiple blocks into a drawing

StanThe
Enthusiast
Enthusiast

I need a lisp that will read a csv or xls file that has names of blocks, and load them from a part folder into my drawing.

 

SH
0 Likes
Accepted solutions (2)
4,403 Views
18 Replies
Replies (18)
Message 2 of 19

Kent1Cooper
Consultant
Consultant

@StanThe wrote:

I need a lisp that will read a csv or xls file that has names of blocks, and load them from a part folder into my drawing.


 

By "load" do you mean INSERT Block references, or just bring in as Block definitions  for future Insertion?

 

That's best done with a "plain" .CSV file, which AutoLisp can read easily enough, but what's the internal format of that?  Is each Block name on a line of its own [all in one column  as viewed in Excel], or are there multiple names on a line with comma delimiters [in cells in the same row], or a mix?  A small sample file would be helpful.

Kent Cooper, AIA
0 Likes
Message 3 of 19

StanThe
Enthusiast
Enthusiast

Sent an excel type file of what i would be asking for and a pic of what my drawing would look like when lisp was done.

I have a folder with a lot of parts in it (drawings) that i want to bring into my drawing as blocks.

Hopefully I'm making sense. Thanks!

SH
0 Likes
Message 4 of 19

StanThe
Enthusiast
Enthusiast

Each drawing number will be in it's own cell down the A column in a csv file and the drawings i want to bring in to my open drawing are on our server.

Thanks!

SH
0 Likes
Message 5 of 19

Sea-Haven
Mentor
Mentor

No need for coding do in excel, if you use =concatenate you can make a column of the commands required to run in Autocad, Note you need a space in some of the columns as part of the text. A little bit of experimentation and you will get it.

 

Just copy and paste the column to Autocad command line.

 

 

 

0 Likes
Message 6 of 19

Kent1Cooper
Consultant
Consultant
Accepted solution

Try something like this [untested] :

(defun C:WHATEVER (/ file LL blkLL blkUR)
  (setq
    file (open "X:/Your/Path/And/Filename.csv" "r")
    LL '(0 0)
  ); setq
  (while (setq blk (read-line file))
    (command "_.insert" blk "_scale" 1 (getvar 'viewctr) 0)
    (vla-getboundingbox (vlax-ename->vla-object (entlast)) 'minpt 'maxpt)
    (setq
      blkLL (vlax-safearray->list minpt)
      blkUR (vlax-safearray->list maxpt)
    ); setq
    (command "_.move" (entlast) "" "_none" blkLL LL)
    (setq LL (polar LL 0 (+ (- (car blkUR) (car blkLL)) YourSpaceBetween)))
  ); while
  (close file)
  (princ)
); defun

Change the bluish italic parts to appropriate things; the YourSpaceBetween must be a number, in drawing units.

Kent Cooper, AIA
0 Likes
Message 7 of 19

StanThe
Enthusiast
Enthusiast

Automated the path a bit. Works great!

SH
0 Likes
Message 8 of 19

StanThe
Enthusiast
Enthusiast

Ran into a big problem. If it doesn't find one of the numbers it errors and stops.

Any way to tell it to say if it found the number or not and to keep looking?

SH
0 Likes
Message 9 of 19

Kent1Cooper
Consultant
Consultant
Accepted solution

@StanThe wrote:

Ran into a big problem. If it doesn't find one of the numbers it errors and stops.

Any way to tell it to say if it found the number or not and to keep looking?


If the names are separate drawing files in a place in the Support File Search Path list where AutoCAD knows to look, try this [untested]:

(defun C:WHATEVER (/ file LL blkLL blkUR)
  (setq
    file (open "X:/Your/Path/And/Filename.csv" "r")
    LL '(0 0)
  ); setq
  (while (setq blk (read-line file))
(if (findfile (strcat blk ".dwg"))
(progn ; then (command "_.insert" blk "_scale" 1 (getvar 'viewctr) 0) (vla-getboundingbox (vlax-ename->vla-object (entlast)) 'minpt 'maxpt) (setq blkLL (vlax-safearray->list minpt) blkUR (vlax-safearray->list maxpt) ); setq (command "_.move" (entlast) "" "_none" blkLL LL) (setq LL (polar LL 0 (+ (- (car blkUR) (car blkLL)) YourSpaceBetween)))
); progn
); if ); while (close file) (princ) ); defun

 

 

Kent Cooper, AIA
0 Likes
Message 10 of 19

StanThe
Enthusiast
Enthusiast

You are phenomenal Kent! Thank you!

SH
0 Likes
Message 11 of 19

Automohan
Advocate
Advocate

I have seen many of the codes with using so much of enters

 

 (setq
    file (open "X:/Your/Path/And/Filename.csv" "r")
    LL '(0 0)
  ); setq

 

See the below code with one line "simple look"

 

 (setq file (open "X:/Your/Path/And/Filename.csv" "r") LL '(0 0));setq

 

Another example:

 

(setq
          blkLL (vlax-safearray->list minpt)
          blkUR (vlax-safearray->list maxpt)
        ); setq
(setq blkLL (vlax-safearray->list minpt)
      blkUR (vlax-safearray->list maxpt)); setq

 

 Is there any reason for using to much of enters

"Save Energy"
Did you find this reply helpful? If so please use the Accept as Solution
0 Likes
Message 12 of 19

Kent1Cooper
Consultant
Consultant

You could have collapsed your second example into a single line, too.  Why not?  Word wrapping of long lines is a consideration.  I could ask you the corresponding question:  Why do you some indent things so far?

 

I usually do it this way when setting more than one variable in one (setq) function just for clarity.  Every variable is on its own line of code along with whatever determines the value set into it.  It makes it easier to see what's happening if you need to add another variable or eliminate one, or change the order if one variable needs to reference another.

 

I also prefer having every closing right parenthesis that is not on the same line as its opening left parenthesis at the same position horizontally as its opening one, again for clarity of the relationships:

 

(setq

  A (one thing)

  B (another)

  C

    (somefunction

      argumentA

      argumentB

      argumentC

    ); somefunction

  D (whatever)

); setq

 

Examples in the AutoLisp reference that carry code to multiple lines do it that way.

 

But it's not necessary.  You can put an entire file full of AutoLisp function on a single line if you don't need any commenting in it.

Kent Cooper, AIA
0 Likes
Message 13 of 19

Automohan
Advocate
Advocate

of course

 

(setq blkLL (vlax-safearray->list minpt) blkUR (vlax-safearray->list maxpt)); setq

 

below is better looking - easily understood "2 no of setq"

 

(setq blkLL (vlax-safearray->list minpt)
(setq blkUR (vlax-safearray->list maxpt)

 

Dear Kent1cooper;

I am not only pointing you about this matter, all the Autolisp experts are doing the same way,

All of your codes are with comments nicely explaining to the new learners,
because of the comments lines are increasing

 

I was not telling to reduces the code no. of  lines as much you can but telling to reduces unnecessary lines

 

If the code reached more than 100 lines & the monitor display is limit view both ways then you have to work with scroll bars & move the eye up & down to fix bugs . . . 

"Save Energy"
Did you find this reply helpful? If so please use the Accept as Solution
0 Likes
Message 14 of 19

hak_vz
Advisor
Advisor

@Automohan  Code visibility can sometimes be a problem.

In editor Notepad++ and many other you have option to temporarily close block of code

In attached picture whole WHILE block is folder to single line and can be collapsed when needed.

Untitled.png

 

Check link for further explanation.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 15 of 19

DonatasAzaravicius
Advocate
Advocate

You can try my lisp for block import and export.

It needs OpenDCL installed.

Can be downloaded from here https://github.com/Azaravicius/BlockImportExport

No need for csv files, can copy-past directly from exel or to exel. Also have function to import messing block (all blocks) from external drawing to current drawing.

Main window.

BIE-Main.png

0 Likes
Message 16 of 19

marko_ribar
Advisor
Advisor

Only accidently, I saw your LSP - thought is like ordinary thread - answers to actual requests by OP's...

 

I looked at your LSP - it works very well, but I corrected some minor lacks :

 

This part :

...

; Check if LastDirSelected exist, if not set it to C:\\
(if (= (DA:GetCfgValue "LastDirSelected") "")
  (DA:SetCfgValue "LastDirSelected" "C:\\")
)

...

 

Should be something like this :

...

; Check if LastDirSelected exist, if not set it to C:\\
(if (or (not (DA:GetCfgValue "LastDirSelected")) (= (DA:GetCfgValue "LastDirSelected") ""))
  (DA:SetCfgValue "LastDirSelected" "C:\\")
)

...

 

Because - if not preset "LastDirSelected", (DA:GetCfgValue "LastDirSelected") returns not "", but nil ... Look at (get_cfg) function in Alisp Reference...

 

It is important that all those syntaxes with this presetting coded return values set, or later (getfiled) function that calls (DA:GetCfgValue "LastDirSelected") will be nil, making complete routine to fail when clicked on import/browse for folder - main option of left portion of Dialog Box...

 

Also corrected minor typo that may cause also problem - in next (getfiled) call , you coded ".csv" as file type - just checked - it should actually be only "csv" (without ".") - that's how (getfiled) accepts file extensions... Previous (getfiled) was good "dwg;dwt;dws"...

 

That's all from what I saw...

 

But, anyway, I don't know what will I/or someone else use it for...

It's little new to me...

 

HTH. M.R.

In attachment is *.LSP that is good - works for me... *.ODCL file is untact (same) - you can download it from GitHub...

Marko Ribar, d.i.a. (graduated engineer of architecture)
0 Likes
Message 17 of 19

DonatasAzaravicius
Advocate
Advocate

Thanks for message.

 

Because DA:GetCfgValue is wrapper around getcfg function, I think I will edit inside this wrapper to return "" if getcfg returns nil. It will be less code to fix and write. I even wrote in description what DA:GetCfgValue returns nil, but forgot about it.

 

Could not find getfiled with ".csv". With "find" function I found only one ".csv" and it was not in getfiled, but was checking extension returned from vl-filename-extension

 

I use this lisp often (after I made it). In my work I need to move blocks between AutoCAD and Excel a lot. I work with AutoCAD Civil 3D and use TIN Surface a lot. Need to create TIN Surfaces from block data, or get TIN Surface elevations in block places.

This is version 2, because I had made version 1 long ago and it used only csv files. Re-made everything on version 2 and added copy-past.

0 Likes
Message 18 of 19

Sea-Haven
Mentor
Mentor

TIN Surface elevations in block places.

 

What is the problem use block XYZ insertion point then just make Acad point and use CIV3D add "Autocad object Point" surface option. Same if attributes hold XYZ. This is old stuff for problem of elevations are text not a point XYZ. 

 

If you want to go over the top there is sample code in CIV3D program files if you look hard enough for make surface from XYZ directly, VL code.

0 Likes
Message 19 of 19

DonatasAzaravicius
Advocate
Advocate

Hi,

at work I make TIN Surfaces using drill holes information and topographic data. Drill hole information needs to be shown on drawing too and I (everyone in company) use block with attributes to show it. This become standard.

I need to insert block in drill hole place with many attributes with different data. Make many TIN Surfaces from data. Use this TIN Surfaces to make more TIN Surfaces (modify made TIN Surface with Edit or with Grading, or make Volume TIN Surfaces), get elevations from modified TIN Surfaces or new TIN surfaces in drill holes places and move them to Excel. Here may be hundreds of drill holes.

I have even made lisp to add surface elevation data to specific block attribute.

Also every drill hole have name, so I need to know witch drill hole is in witch place and block with attributes works well.

In Excel drill holes are in 1D space, good for editing and math. In AutoCAD they are in 2D space, good to know nearby drill holes around specific drill hole. So moving data between Excel and AutoCAD as blocks with attributes is essential in my work.

0 Likes