Layers control colors, linetypes and lineweights. Everything is on a layer.

The layer collection and layer object conform to the general object structure. in fact it would be an interesting exercise to write a generic structure description where the collection and item names could be interchanged with other types of entities.

To set a reference to an existing class_object use the Item property of the collection. Use either the string name or the index number. All collections are zero-based. The count property of the collection is one-based. To add a new class_object, use the Add property of the collection. If you reference with Item to an object that does not exist, you will receive “Key not found” error. If you add a class_object that already exists, you get a reference to the existing object.

You can iterate through the collection with
For Each class_object in collection

For i= 0 to collection.count – 1
Set class_object = collection.Item(i)


Dim objlayers As AcadLayers
Dim objlayer As AcadLayer
Set objlayers = acadDoc.Layers
Set objlayer = objlayers.Item(“ABC”) ‘error if does not exist
Set objlayer = objlayers.Item(1)
Set objlayer = objlayers.Add(“ABC”) ‘returns reference if already exists

The new layer needs to be current for new entities to adopt its property. The ActiveLayer property of the drawing object is used.
Acaddoc.ActiveLayer = acadDoc.Layers.Item(“ABC”)
Acaddoc.ActiveLayer = objlayer

Application – Documents – Document – Layers – Layer

The Layers collection contains all the Layer objects in the drawing.
Layers collection – AcadLayers
Methods – Add Item
Properties – Count

Layer object – AcadLayer
Method – Delete
Properties – Used, Name, LayerOn, Freeze, Lock, TrueColor, Linetype,
Lineweight, PlotStyleName, Plottable, Description

Sub testnewlayer()
Call newlayer("AAA", 1, acLnWt035)
End Sub

Sub newlayer(strname As String, intcolor As Integer, lineweight As Integer)
Call connect_acad

Dim objlayers As AcadLayers
Dim objlayer As AcadLayer
Set objlayers = acadDoc.Layers

Set objlayer = objlayers.Add(strname)
    objlayer.Color = intcolor
  'note color is not currently a listed item in documentation but still works
    objlayer.lineweight = lineweight
    'acadDoc.ActiveLayer = acadDoc.Layers.Item(strname)
     acadDoc.ActiveLayer = objlayer

'default values of a new layer
'objlayer.Used = True
'objlayer.Name = "ABC"
'objlayer.LayerOn = True
'objlayer.Freeze = False
'objlayer.Lock = False
'objlayer.Color = 7 'this is the old 255 color method no longer documented 
'debug.print objlayer.TrueColor returns- object doesnt support this property or method
'debug.print objlayer.Color returns an integer you can use to read or set color
'objlayer.LineType = "Continuous"
'objlayer.LineWeight = -3
'objlayer.PlotStyleName = "Color_7"
'objlayer.Plottable = True
'objlayer.Description = ""

End Sub

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.