Jump to content
numberOCD

Insert Blocks off of List with Attributes and Rotate the Blocks when inserted

Recommended Posts

numberOCD

Good Morning,

I am brand new to AutoLISP and am not picking it up quite as well.

I'm trying to make a LISP that will insert around 1500 blocks at designated coordinates off a list I will make in Excel (or can transfer to a txt). The excel list will have X,Y coordinates, an angle of rotation, and 2 or 3 attributes.

 

This LISP will not be for a long time use, it's mainly to get a large insertion completed for editing as construction comes along, so I only need to make it for functional purpose. If someone can guide me better in how to extract the information from the list into the LISP, that's the main thing I'm having trouble picking up.

 

Thanks for any help!

Share this post


Link to post
Share on other sites
lrm

It might be easier to use a script to add blocks to an AutoCAD drawing if your data is coming from an Excel worksheet. For example, assume your Excel file looks something like the example below with your data in columns B through G.

 

Script1.JPG

 

Enter the following in cell A2 using the Excel Concatenate function to create the command to add a block. It is important to use spaces as noted to force the command to go on to the next argument when it is executed. Note that there is one space before the 1 (for scale) and two spaces after it. Since an Enter cannot be included in this sequence we will uses @@@ as a place holder for it.

Script2.JPG

 

Use fill down to create the insert commands for all the data.

 

Copy Column A from row 2 to the last row (cell A4 in my example) to the clipboard.

 

Open MS Word and paste as text.

 

Use Replace and replace the text @@@ with ^p . This will place the second attribute value on a new line providing an Enter input to the command.

 

Save the file as a .txt file.

 

Change the file name extension to .scr . The filename should be something like: filename.scr

 

In AutoCAD enter the Script command and select the .scr file. This should add multiple blocks at the specified location and angle.

Share this post


Link to post
Share on other sites
7o7

You can use this routine to get information from text file. With A.txt is file and L is the result list. The item's type of L is String.

(setq L nil
     file (open "A.txt" "r"))
(while (setq line (read-line file))
 (setq line (mapcar 'vl-princ-to-string (read (strcat "(" line ")")))
L (append L (list line)))
)
(close file)

Share this post


Link to post
Share on other sites
BIGAL

Irm maybe also rather than adding the Word step wrap the attribute in quotes "Apples" "Banannas". Pasting to Word need to make sure save as a TEXT file with .scr file extension, Word can add hidden characters.

 

Nice clear explanation, well done.

Share this post


Link to post
Share on other sites
lrm

BIGAL, The text string:

 

-insert MyBlock 12.34,20.5 1 30 "Apples" "Bananas"

 

will assign "Apples" "Bananas" to the first attribute of MyBlock. What syntax should be used to get an Enter key between the two values so that Apples is assigned to the first attribute and Bananas to the second attribute? My substitution of ^p added the Return key which did this. It would be cleaner to not have to do the substitution. Thank you.

Share this post


Link to post
Share on other sites
numberOCD

Irm,

This is the kind of method I've been looking for. I used to have a similar idea for drawing polylines with varying y coordinates and increasing x by one unit (5 feet) and I would paste that list into the command line. This remedy will definitely save me a huge deal of time for this task.

 

Thanks a bunch!

Share this post


Link to post
Share on other sites
David Bethel

Or maybe use autolisp syntax :

ins3.jpg

ins5.jpg

Edited by David Bethel
Wrong Scale / Angle order

Share this post


Link to post
Share on other sites
numberOCD

Irm,

Do I need spaces around the ^p? I haven't gotten it to work quite yet, but it is coming along.

 

David,

How would I use your this sequence? I'm not quite familiar with autolisp yet. I copied down the sequence and ran it through Script. Is this the proper method? I got a prompt: "Point or option keyword required." followed by "Specify insertion point or [basepoint/Scale/X/Y/Z/Rotate]:"

Share this post


Link to post
Share on other sites
David Bethel

The simplest way would be to :

 

 

Simply Copy Column "I" to the AutoCAD Command Line

 

OR :

 

  • Copy column "I" into Notepad or any true ASCII editor
  • Save the new file to a known location ( c:\acad\insb.lsp] )
  • The file name and extension are not important in this scenario
  • At the AutoCAD command line type (load "c:/acad/insb.lsp")

 

 

Notice the use of "/" vs the DOS standard for file locations. ( AutoLsip Requires this )

 

Also the BLOCK / INSERT ATTRIBute quantities must be an exact match. In the same I posted MyBlock1 has (3) MyBlock2 has MyBlock3 has (2).

 

There are work arounds for this but it didn't sound like that was a biggie for your use.

insert.xlsx

Share this post


Link to post
Share on other sites
numberOCD

When I run the lsp named Test2.lsp it returns this test window

Notice.jpg

 

This is the only line in the lsp right now

(command "_.INSERT" "SIG" list( 12 36 0) 1 1 32 "781Z" "GYR")

 

Any idea what's wrong?:?

 

Thanks

Share this post


Link to post
Share on other sites
numberOCD

Ok,

I took out the "List" with paranthesis and the Z coordinate and it is working by pasting the line in the command bar up until the Attribute Window. It still prompts me for the Attribute value, and that is one part I need to automate badly.

 

It currently reads this

(command "_.INSERT" "SIG2" "12,36" 1 1 32 "781Z" "GYR")

Share this post


Link to post
Share on other sites
David Bethel

Leaving he Z axis value can lead to really weird results.

 

 

Does the BLOCK have 2 or 3 Attributes?

Share this post


Link to post
Share on other sites
David Bethel
When I run the lsp named Test2.lsp it returns this test window

[ATTACH=CONFIG]49920[/ATTACH]

 

 

Thanks

 

 

Well I guess there are file naming limitations after all

 

It can only have 1 file extension ie .lsp not .lsp.shx

 

Th extension cannot be .shx as the (load) command knows what a shx file is supposed to be

Share this post


Link to post
Share on other sites
lrm

Hi numberOCD,

 

To answer your follow up question to my original post.

 

After selecting and copying column A to the clipboard and pasting it in to MS Word you will have something that looks like this (I have turned on the display of paragraph marks and spaces):

 

Script3.JPG

 

Use Find and Replace as shown (no spaces) to replace @@@ with ^p. The results should look something like this where each attribute beyond the first is on a new line. Make sure there is only one paragraph mark at the end of the last line and no paragraph mark after it.

Script4.JPG

 

When you go to save this file in Word use Save As and then choose the txt format. This will save the file in an ASCII format. Note, you can use Notepad to edit the .scr file but Notepad's Replace function does not include paragraph character (^p).

 

Script5.JPG

AutoCAD expects script files to have the file extension .scr so chnage the file name using the file manager from:

Script6.JPG

to:

Script7.JPG

 

You can then use the script command to enter all the blocks.

 

For a test of this process I created a simple block named MyBlock with two attributes. The names of the attributes is not important but the order is. Please let me know if you have any questions.

~ lrm

Share this post


Link to post
Share on other sites
lrm

Here's a sample of the output of the MyBlockScript using a simple block with two attributes:

Script8.JPG

 

~lrm

Share this post


Link to post
Share on other sites
BIGAL

This works not sure why yours doesn't.

 

(command "_.insert" "labelstn" "0,0,0" 1 1 0 "10" "20" "30" "40" "50")

Share this post


Link to post
Share on other sites
Tharwat

Can you upload a sample Excel file with a sample drawing that obtains the attributed blocks that you are going to use ?

How many different attributed blocks to be inserted ?

Share this post


Link to post
Share on other sites
lrm

Thanks BIGAL for the suggestion. I was trying to create a script that does not use AutoLISP but it is one way around the problem of including the Enter key. It simplifies the process by eliminating the need to do a replace in Word.

 

Here then is the process for converting Excel data for the location and orientation of a bunch of blocks that each include several attributes.

 

1. Layout your spreadsheet similar to the following, leaving column empty. I will be using column A for building the AutoCAD command for each block through the use of the Excel Concatenate function.

 

Script10.JPG

 

2. Type into cell A2 the following expression paying careful attention to “ and spaces. Single quotes are used to start and end a string of text while double quotes “” are used to add a single quote. I have placed red dots where spaces should be included.

 

Script11.JPG

 

3. Use Fill-down from cell A2 to create the other AutoCAD (actually AutoLISP) commands.

4. Copy cells A2 to A4 (or however many rows you have) to the clipboard with Ctrl-C.

5. Open Notepad and Paste the commands into it. The file should look something like the following.

 

Script12.JPG

 

6. Save the file in Notepad so that is has a file name extension of .scr.

7. In an AutoCAD file that includes the definition of the blocks used give the Script command and select the .scr file.

 

Here is the result.

Script13.JPG

8. Enjoy!

 

I have included the Excel, .scr, and AutoCAD file as an example.

~lrm

BlockScript.dwg

BlockData.xlsx

BlockScript.scr

Share this post


Link to post
Share on other sites
7o7

Suppose that you have a txt file like this , call it "D:/A.txt" :

 

MyBlock 12.34 20.5 15 Apples Bananas

MyBlock 14.12 22.5 30 Cherries Donuts

MyBlock 16.11 24.5 45 Eggs Figs

 

You can use this lisp function to insert your block:

(defun c:test()
 (setq L nil
file (open "D:/A.txt" "r"))
 (while (setq line (read-line file))
   (setq line (mapcar 'vl-princ-to-string (read (strcat "(" line ")")))
  L (append L (list line)))
 )
 (close file)
 (foreach line L
   (command "-insert" (car line)
     (list (atof (nth 1 line)) (atof (nth 2 line)))
     1 1 (atof (nth 3 line))
     (nth 4 line) (nth 5 line)))
)

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

×