Jump to content
deejaydave

Using a text file to add and manipulate Dynamic Blocks

Recommended Posts

deejaydave

I am trying to find out if you can use a text file (converted from a spreadsheet) to add dynamic blocks to a drawing, not just the X, Y coordinates, but the angle (not standard rotation, the dynamic one). The block I am working with reflects “Dip & Strike”. The block has only three (3) dynamic variables, Angle, Basepoint and Visibility Drop List. I want to automate adding multiple blocks without having to change the angle one at a time.

Any help would be appreciated.

Share this post


Link to post
Share on other sites
BIGAL

Go to the source http://WWW.lee-mac.com and get his dynamic block lisp. You could read a text file insert the block then pass all the required values to your dynamic block. If you dont know how to use lisp post a dwg and a text file. A good subject to learn about. Note also can do csv files.

Share this post


Link to post
Share on other sites
deejaydave

Thanks for the reply BIGAL, unfortunately none of Dynamic Block lisp files will help me achieve exactly what I want. I have a feeling that bulk manipulation of dynamic blocks is not a simple task and not easily solved.

Share this post


Link to post
Share on other sites
BIGAL

You can do what you want ! Look at a simple rectangle dynamic block length & width are dynamic so a text file would look like this.

 

 

x y len wid
1 2 12.45 12.56
10 10 10 10

You would read the line and set the variables x,y Len & WId insert dynamic block x,y 1 1 0 then update the dynamic block with correct Length & width.

 

You can cut the middle man out if you want and just read direct form excel, but lets crawl before we run.

 

 

Post your dwg with the block.

Share this post


Link to post
Share on other sites
deejaydave

Thanks. I have attached a drawing containing the block.

 

 

I appreciate the help.

 

 

deejaydave

Dip Block Test.dwg

Share this post


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

Try this

 

; dynamic block insert from csv
; by Alan H July 2018

; thanks to Lee-mac for this defun and the dynamic block put defun
(defun _csv->lst ( str / pos )
(if (setq pos (vl-string-position 44 str))
   (cons (substr str 1 pos) (_csv->lst (substr str (+ pos 2))))
   (list str)
   )
)

;The dtr function converts degrees to radians
;The rtd function converts radians to degrees
(defun dtr (a)
(* pi (/ a 180.0))
)

(defun LM:setdynpropvalue ( blk prp val )
   (setq prp (strcase prp))
   (vl-some
      '(lambda ( x )
           (if (= prp (strcase (vla-get-propertyname x)))
               (progn
                   (vla-put-value x (vlax-make-variant val (vlax-variant-type (vla-get-value x))))
                   (cond (val) (t))
               )
           )
       )
       (vlax-invoke blk 'getdynamicblockproperties)
   )
)

(defun AH:setdyn ( / obj att fo x y ang oldattdia )
(setq oldattdia (getvar 'attdia))
(setvar "attdia" 0)
(setq fo (open (getfiled "Text Export File" "" "csv" 4) "R"))
(while (setq newline(read-line fo))
(setq lst (_csv->lst newline))
(setq x (atof (nth 0 lst)))
(setq y (atof (nth 1 lst)))
(setq ang (nth 2 lst))
(setq dynvar (nth 3 lst))
(command "insert" "DIP_STRIKE" (list x y) 1 1 0)
(setq obj (vlax-ename->vla-object (entlast)))
(LM:setdynpropvalue  obj "Angle1"   ( - 0.0 (dtr (atof ang))))
(LM:setdynpropvalue  obj "Visibility1" dynvar)
)
(close fo)
(command "Regen")
(setvar 'attdia oldattdia)
(princ)
)

 

Save this as say test.csv

100,100,45,With Arrowhead
200,5,32,With pointer
-200,-200,14,ArrowHead Without Strike
300,200,90,Pointer Without Strike

Edited by BIGAL

Share this post


Link to post
Share on other sites
deejaydave

BIGAL

 

 

Thanks for the code. Unfortunately it has done something to the angles, it shows 14° when it should be 346°. It seems to have reverted to AutoCAD's setup, I work on the principal of 0° = N, 180°=S etc.

The code might come in handy for other blocks in the future, so a big thanks for the work.

 

 

Regards

deejaydave

Share this post


Link to post
Share on other sites
BIGAL

We spend a lot of time doing something to try and just saying oh angles are wrong will not get you much support in future, it can be fixed, we have limited info to go on when trying to solve a problem and it may take a couple of attempts, angle in AutoCAD can be controlled. take the 4 examples and post an image of what they should be.

Share this post


Link to post
Share on other sites
deejaydave

I was just being specific, sorry if it sounded otherwise.

 

 

I have attached the test drawing from before with a few annotations (direction of rotation, degree points etc). I hope this helps.

 

 

(Question. The language code used, what is it?)

 

 

Thanks very much for the help.

Regards

deejaydave

Dip Block Test.dwg

Share this post


Link to post
Share on other sites
BIGAL

Hopefully fixed just removed the attribute update and inserted in a different way. Code updated.

 

The code is written in lisp and Visual Lisp, Autocad Lisp has been around since day dot, and visual lisp was introduced years ago adding more functions. The majority here of code is lisp and a bit of VBA and some .Net which is a high level programming approach.

Share this post


Link to post
Share on other sites
harshad

u have data in excel like xyz and angel of that block ??

Share this post


Link to post
Share on other sites
deejaydave

Hi BIGAL

 

 

Thanks for the update. I am not sure if I have done something wrong (which is a possibility), but when I have loaded the lisp file I don't seem to get the option to choose a CSV file like the first iteration. I have copied the updated program text from your message, copied it to Notepad++ and saved it as DIP.LSP. Any thoughts.

 

 

Many thanks

deejaydave

Share this post


Link to post
Share on other sites
BIGAL

I tested again copying code from here a new restart to make sure.

 

Copy just this line only directly to the command line it should open a windows explorer style dialouge to pick the csv.

 

(setq fo (open (getfiled "Text Export File" "" "csv" 4) "R"))

then

(close fo)

 

Copy the code to notepad and resave.

Share this post


Link to post
Share on other sites
deejaydave
I tested again copying code from here a new restart to make sure.

 

Copy just this line only directly to the command line it should open a windows explorer style dialouge to pick the csv.

 

(setq fo (open (getfiled "Text Export File" "" "csv" 4) "R"))

then

(close fo)

Copy the code to notepad and resave.

 

 

 

Am I correct in assuming that I must load the lisp file first, then enter (paste) the first bit of code here to the command line (loading csv), then enter the second bit of code. Is that correct.

Share this post


Link to post
Share on other sites
BIGAL

The 1st line should just open up the select file dialouge window you said this is not working it should start with say your C:drive once you pick a csv file just paste the 2nd line it closes the file so no errors about having a file open to do stuff. You should see something like # on the command line this is saying it found the file you want to use.

Share this post


Link to post
Share on other sites
deejaydave

Hi BIGAL

 

 

I think my last message was not too clear, in which order do I do everything.

Is it load lisp, enter first bit of code to load csv, then enter second bit of code to close file, or should there be another order.

I ask, because whether I load the lisp before or after entering the code, nothing seems to happen. The CSV files loads (I get #), so if you could let me know which order I should be doing this I that would be great.

 

 

Have a good day

deejaydave

Share this post


Link to post
Share on other sites
BIGAL

It sounds like your csv is not the same pattern as mine can you post a sample please.

 

Here is a few more inserts which is what I used with your sample dwg and worked perfect, copy this to notepad and save as say test.csv in "E:\013\ALSP0903\" and try the full code again.

 

100,100,45,With Arrowhead
200,5,32,With pointer
-200,-200,14,ArrowHead Without Strike
300,200,90,Pointer Without Strike
-150,-200,142,ArrowHead Without Strike
180,-200,275,ArrowHead Without Strike
200,200,345,ArrowHead Without Strike

Share this post


Link to post
Share on other sites
deejaydave

I have attached csv file as requested.

 

 

Cheers

deejaydave

dipstrike.csv

Share this post


Link to post
Share on other sites
BIGAL

I have no idea just copied your block to my default template dwg apploaded the lisp and opened your csv file and it all worked.

 

Can you run it then when it crashes do F2 and copy all the command line entries to say notepad save and post file here.

Share this post


Link to post
Share on other sites
deejaydave

Hi

 

 

Thanks for getting back to me, I have attached the text file.

Maybe there is a problem with my cad or my version of cad (ver. 17).

 

 

Many Thanks

deejaydave

CAD_List.txt

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×