Nate Holt's Blog

July 5, 2009

Nate’s Simple AutoLisp – Lesson 002

Filed under: Tutorials — nateholt @ 11:22 am

 

 

(note: this originally appeared in July 2006 on the old blog)

Warning/Disclaimer: I’m neither a teacher nor am I versed in the theory behind AutoLisp programming. But I invite you to come along for a ride. This stuff is not only fun, but it can be useful ! Each lesson should take 5-10 minutes.

So, here we go…

Lesson 002 – First peek at Block Inserts

AutoCAD Electrical makes heavy use of standard AutoCAD blocks with attributes. There is a lot of neat stuff here, so let’s get started.

Just so we can stick together, download this sample drawing here and open it up. Its file name is lesson_002.dwg.

Let’s go back to where we left off in the last lesson. We were picking a LINE entity with entsel and opening it up with entget to examine its internals. Let’s enter the same AutoLisp expression as before but this time let’s pick on a block insert entity instead of a line entity.

1. At the Command: prompt type this… (entget (car (entsel))) [Enter]

2. Pick on the limit switch symbol LS406 in the upper left-hand part of the drawing. Now you’ll see a bunch of data displayed in your command window. It will look something like this:

nate_lesson002a

This is a list of sublists of data defining the base part of the block insert. Each element of this list starts with a group code number followed by the value for that group code.

Group code 0 gives the entity type. Find this entry: (0 . “INSERT”) in the data returned. This tells us that we picked and opened a block Insert instance.

Group code 8 identifies the base layer that this block instance is inserted on. In the example here it is layer “SYMS”.

Group code 2 gives the block name, “HLS11”.

Group code 10 for an INSERT entity gives the block’s insertion X,Y,Z coordinate. For our picked instance of “HLS11” the returned data says that it is inserted at XY coordinate 3.875,16.0.

Group codes 41-43 give the block insert’s X,Y,Z scale factor values and 50 gives the rotation angle.

Let’s figure out how to write a single-line AutoLisp expression to just give us the block name of the picked entity. Somehow we have to extract the group code 2 value from this big list of data.

There is a base AutoLisp function called assoc that can be of great help to us here. We can pass the group code number we want and the full list of data to search. The assoc function will return just the part of the list that matches the specific group code we pass to the assoc function. Let’s try it. We want to get the insert’s block name, this is carried in group code 2:

3. Type this at the AutoCAD Command: prompt…. (assoc 2 (entget (car (entsel)))) [Enter] and pick on the limit switch block insert. You will see this returned to the command line prompt:

nate_lesson002b

Hmm… we’re close. But we need to get rid of the first part of this list and just leave the block name. There is another AutoLisp function, (cdr …. ) that discards the first element of a list. Let’s try it.

4. Type this at the Command: prompt… (cdr (assoc 2 (entget (car (entsel))))) [Enter]. Make sure you end with five “)” to keep the “(” and “)” counts balanced. Pick on the limit switch again. Now we get a nice clean return.

nate_lesson002c

Okay, time’s up. Next lesson we’ll eliminate the need to type in this AutoLisp expression into the command line every time we want to run this “program”. We’ll create a text file version of this program and learn how to “APPLOAD” it. This will let us quickly run it whenever we want.

Advertisements

1 Comment »

  1. […] previous postings: Lesson 001, Lesson 002, Lesson 003, Lesson 004, and Lesson […]

    Pingback by Nate’s Simple AutoLISP – Lesson 006 « AutoCAD Electrical Etcetera — September 26, 2009 @ 4:55 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: