Accessing Layers

AutoCAD has a Layers collection that contains all the Layer objects in the drawing. You can create as many layers as you want by adding new Layer objects to the Layers collection.

You access the Layers collection via a Document object. In the following code, ThisDrawing is used as the active document:

Dim objLayers As AcadLayers

Set objLayers = ThisDrawing.Layers

To set a reference to an existing Layer object, use the Item method of the Layers collection as follows:

Dim objLayer As AcadLayer

Set objLayer = objLayers.Item(2)

Set objLayer = objLayers.Item("My Layer")

The parameter of this method is either an integer representing the position of the desired Layer object within the Layers collection or a string representing the name of the desired Layer object. If you use an index number, it must be between 0 and the value of the Layers.Count property minus 1.

Like in other AutoCAD collections, Item is the default method for Layers. This means that the method name may be omitted, and the parameter passed straight to the Layers reference. Some programmers prefer this, as it's simpler to type and read. The following code does the same thing as the prior example using the default method to specify the Layer object:

Dim objLayer As AcadLayer

Set objLayer = objLayers(2)

Set objLayer = objLayers("My Layer")

However, I recommend that you avoid using default methods and instead strive to use explicit properties and methods. Microsoft is making a strong commitment to explicit coding in all of its current and future programming technologies, such as .NET. In fact, .NET doesn't support default methods or implicit data types by default.

Was this article helpful?

0 0
100 Bowling Tips

100 Bowling Tips

Playing bowling with your friends can help you decide if it is indeed the hobby that you want to invest your time on today. Aside from that, it can help you get a better feel of the sport. More importantly, when you play with your friends, it would become a more fun activity, which you can look forward to each week.

Get My Free Ebook

Post a comment