AfraLISP - Learn AutoLISP for AutoCAD productivity

Beginning Visual LISP - Part 2

by Kenny Ramage

Before we start this section, I think it might be a good idea if you pop along and have a look at the AutoCAD Object Model. In fact, print it out and have it next to you as you work through these tutorials. It's just like having a road map.

Anyway, where were we? Oh, yes. We are now going to try and extract the AutoCAD Support Path from the Object Model using VLisp. We'll take it right from the top just in case you got a bit lost. Here we go. Type all of the following statements into the Console window, pressing "Enter" after each one :

The lines in red are what VLisp returns, don't type them!

Load the VLisp support.

_$ (vl-load-com)

Store a reference to the Application Object

_$ (setq acadObject (vlax-get-acad-object))
#<VLA-OBJECT IAcadApplication 00adc088>

Store a reference to the Preferences Object

_$ (setq prefsObject (vlax-get-property acadObject 'Preferences))
#<VLA-OBJECT IAcadPreferences 018adfdc>

Store a reference to the Files Object

_$ (setq tabnameObject (vlax-get-property prefsObject 'Files))
#<VLA-OBJECT IAcadPreferencesFiles 018adfc0>

Get the Support Path

_$ (setq thePath (vlax-get-property tabnameObject 'SupportPath))

"Jings, that's great, but can we add a new directory to our Support Path?"

Of course we can, but first we need to add our new path to the existing path variable. What should we use? Easy, let's just use the (strcat) function :

_$ (setq thePath (strcat thePath ";" "C:\\TEMP"))

We've added the new directory to the Support Path variable, so now let's update it.

Type in this line:

_$ (vlax-put-property tabnameObject 'SupportPath thePath)

Now, return to AutoCAD and go to "Tools" - "Options" - "Files" - "Support Search Path". Your new directory, C:/TEMP, should have been added to your Support Path. Dead easy Hey? This Vlisp stuff is easy!

Right, we've opened the filing cabinet AutoCAD drawer (the Application Object) and had a look at the characteristics of that drawer. Now we need to delve into the drawer and have a look at one of the document folders stored inside.

_$ (vl-load-com)

_$ (setq acadObject (vlax-get-acad-object))
#<VLA-OBJECT IAcadApplication 00adc088>

Store a reference to the Active Document. (the drawing you fool!)

_$ (setq acadDocument (vla-get-ActiveDocument acadObject))
#<VLA-OBJECT IAcadDocument 00ee0f84>

Double click to select the variable "acadDocument" and add it to the "Watch" Window :


Now double click on "ACADDOCUMENT" Object.


Wow, look at all that! Now double click on <Layers> :

Inspect Layers

Ha, we've now drilled down to all the layers contained in the drawing. In other words, the "Layers Collection".
"Alright, that's great," I can hear you say, "I can see that there are 13 layers in this drawing, but where are the layers? Right enter this :

_$ (vl-load-com)

_$ (setq acadDocument (vla-get-activedocument (vlax-get-acad-object)))
#<VLA-OBJECT IAcadDocument 00ee0f84>

_$ (setq theLayers (vla-get-layers acadDocument))
#<VLA-OBJECT IAcadLayers 018aee84>

We are now proud owners of all the layers in this particular drawing. Would you like to have a wee look at them? Just be very careful and don't drop them!

Enter the following at the Console prompt, pressing "Ctrl" then "Enter" after each line. After the final line, the closing parenthesis, press "Enter" :

_$ (setq i 0)
      (repeat (vla-get-count theLayers)
      (setq aLayer (vla-item theLayers i))
      (princ (vla-get-name aLayer))
      (setq i (1+ i))

All the layer names in your drawing should be listed. Mine looks like this :


Would you like to have a closer look at one of the layers in your drawing? Then type this :

(setq aLayer (vla-item theLayers "2"))
#<VLA-OBJECT IAcadLayer 018aeec4>

We have now accessed Layer "2" from the Layers Collection. Let's list all the properties and methods of this Layer :

_$ (vlax-dump-object aLayer T)

; IAcadLayer: A logical grouping of data, similar to transparent acetate
  overlays on a drawing
; Property values:
; Application (RO) = #<VLA-OBJECT IAcadApplication 00adc088>
; Color = 2
; Document (RO) = #<VLA-OBJECT IAcadDocument 00ee0f84>
; Freeze = 0
; Handle (RO) = "40"
; HasExtensionDictionary (RO) = 0
; LayerOn = -1
; Linetype = "DASHED2"
; Lineweight = -3
; Lock = 0
; Name = "2"
; ObjectID (RO) = 21872128
; ObjectName (RO) = "AcDbLayerTableRecord"
; OwnerID (RO) = 21871632
; PlotStyleName = "Color_511"
; Plottable = -1
; ViewportDefault = 0
; Methods supported:
; Delete ()
; GetExtensionDictionary ()
; GetXData (3)
; SetXData (2)

Want to change your Layers color? Enter this :

_$ (vla-put-color aLayer 4)

This will have changed your Layer "2" color to "Cyan" or color "4", including the color of all Objects within the drawing on Layer "2" with color "ByLayer".

Have a look around at the Document Object. There's stacks to see and drool over. Can you now see the power and capabilities of Visual Lisp?

On the next page we'll go even deeper and start to have a look at creating, selecting and changing drawing entities.