SolidWorks

Sheet Metal Guy INDUSTRY DIRECTORY: The most comprehensive resource for businesses in the metal forming, fabricating, and welding industries.

 

 
Home


 

 

 

 

 

 


 

Click here for Customizing SolidWorks For Greater Productivity

The GPS Center

Top Selling SolidWorks Book
SolidWorks for the Sheet Metal Guy - Course 1: Part Creation
"The book was easy to follow. I have bought other books to learn how to use SolidWorks. This book took me to a new level."
- Charles Hugo

 

Nate's Simple AutoLisp - Lesson 002
(

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

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

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

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.

Nate's Simple AutoLisp - Lesson 001
Nate's Simple AutoLisp - Lesson 003
 

 
 

Controlling the Machine

 

Nate HoltNate Holt
Coinciding with the acquisition of VIA Development, Nate joined Autodesk in March of 2003 after a decade stint as an entrepreneur following a two-decade stint as a controls engineer and software applications developer at Owens-Corning. Nate is now the lead product architect for AutoCAD Electrical. He loves this stuff.

 
 

 

 

Nate's Simple AutoLisp - Lesson 001

Nate's Simple AutoLisp - Lesson 003

Controlling the Machine

Signup for our free AutoCAD Tips and Tricks

AllAboutAutoCAD.com
AllAboutAutoCAD.com

AllAboutInventor.com

AllAboutInventor.com

AllAboutSolidEdge.com
AllAboutSolidEdge.com
About SolidWorks
AboutSolidWorks.com
SheetMetalGuy.com
SheetMetalGuy.com

Home | About | Contact Us | Privacy Policy | Return Policy | Site Terms
All About Community | All About AutoCAD | All About Inventor | All About Solid Edge | About SolidWorks | Sheet Metal Guy
Copyright 2008 Sheet Metal Guy | info@SheetMetalGuy.com