A little bit of Automation

I was happily surprised to see so many showing interest in doing a little customization of their own and who joined us for the EECast this past Monday. In this weblog, I have added the source code for the sample from the EECast. Also, I wanted to touch on some simple Visual Lisp since we did not get a chance to look at any of this on Monday.

First, the source code from the EECast can be found here.

Simple Visual Lisp

We talked on Monday about IDE’s. That stands for Integrated Development Environment. Visual Lisp also has an integrated development environment. You can get to it by typing in “VLIDE” at the command prompt.

AutoLisp is a LISt Processing language and was first introduced into AutoCAD all the way back in R2.18. Lisp is a functional programming language it means that the function comes first and drives each line.

For example, the lisp phrase (+ 1 2) adds 1 plus 2. The function (add) comes first and then the parameters to that function (1 2) comes next.

Visual Lisp is a superset of AutoLisp which adds easier access and control to objects. However, to be able to use any of the Visual Lisp tools, you first have to load the Visual Lisp component. You do this by typing (VL-LOAD-COM).

Let’s change a selected object’s layer. First we select the object using the AutoLisp function ENTSEL.

(SETQ OBJ (ENTSEL "\nSelect entity: ")) 

The SETQ sets the variable (named OBJ) to the result of the function ENTSEL. The “\n” creates a newline so the coding looks more like a native AutoCAD command. What is returned from the ENTSEL function is a list containing the entity and a sub-list containing the coordinates of the picked point. What we are after is the entity or the first item in a list.

(SETQ ENT (CAR OBJ))

This gets us the entity object and writes the entity to the ENT variable. Now once we have the entity we can convert it to a VL-OBJECT which is a whole lot easier to get properties from and write to.

(SETQ VL-OBJ (VLAX-ENAME->VLA-OBJECT ENT))

Notice I am converting an ENAME (the entity object) to a VLA-OBJECT and writing this to the variable VL-OBJ. What is the object’s layer?

(SETQ LYRNAME (VLA-GET-LAYER VL-OBJ))

Want to change the layer name?

(VLA-PUT-LAYER VL-OBJ "NEWLAYERNAME")

That is simple enough. It took longer to get the entity than it was to edit it!! Once you have the VL-OBJ, I encourage you to use the INSPECT button along the top of the VLIDE. This will allow you to see all the properties that you have available to GET/PUT for each object. Simply select the variable name (VL-OBJ) and click the INSPECT button.

Happy Coding!!

If you like the coding blog post, let me know. How many more I post will be based upon feedback.

Comments are closed.