How to create a new layer and set it as current in VBA for AutoCAD?

How to create a new layer and set it as current in VBA for AutoCAD?

Anonymous
Not applicable
2,858 Views
6 Replies
Message 1 of 7

How to create a new layer and set it as current in VBA for AutoCAD?

Anonymous
Not applicable

I thought this would be pretty simple, but still my method doesn't work.

This is the code that I tried to use:

ThisDrawing.SendCommand "-layer new EnterNameHere make EnterNameHere  "

This should create a layer called EnterNameHere, but it doesn't.

0 Likes
Accepted solutions (2)
2,859 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable
Accepted solution

I found out what the problem was. When entering the name, spaces are used as text instead of the equivalent of hitting the enter-key. I had to use 'vbCr' as a equivalent.

The code below should do the trick:

 

ThisDrawing.SendCommand "-layer new PC" & vbCr & "make PC" & vbCr & vbCr

0 Likes
Message 3 of 7

Ed__Jobe
Mentor
Mentor
Accepted solution

You're better off using the Layers api rather than SendCommand.

 

 

 

Sub LayerTest()
    Dim MyLayer As AcadLayer
    Dim color As AcadAcCmColor
    Set color = AcadApplication.GetInterfaceObject("AutoCAD.AcCmColor.22")
    color.SetRGB 80, 100, 244

    
    Set MyLayer = ThisDrawing.Layers.Add("LayerTest")
    With MyLayer
        .TrueColor = color
        .Freeze = False
        .Lineweight = acLnWt009
    End With
    
End Sub

 

 

 

http://help.autodesk.com/view/OARX/2018/ENU/?guid=GUID-14F28A52-81E5-4CD0-AA79-ADC09E91BB8C  

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 4 of 7

Anonymous
Not applicable

Hi @Ed__Jobe 

 

Thanks for the improvement. It works completely with the exception of assigning the color. But that's not a problem.

Is there any other way to activate the Layer then:

ThisDrawing.SendCommand "-layer make LayerTest" & vbCr & vbCr

0 Likes
Message 5 of 7

Ed__Jobe
Mentor
Mentor

The CLAYER system variable controls the current layer. See the last line below.

 

Sub LayerTest()
    Dim MyLayer As AcadLayer
    Dim color As AcadAcCmColor
    Set color = AcadApplication.GetInterfaceObject("AutoCAD.AcCmColor.22")
    color.SetRGB 80, 100, 244

    
    Set MyLayer = ThisDrawing.Layers.Add("LayerTest")
    With MyLayer
        .TrueColor = color
        .Freeze = False
        .Lineweight = acLnWt009
    End With
    ThisDrawing.SetVariable("clayer") = MyLayer.Name
End Sub

 

 You probably didn't get the color to work because you don't have the right class ID. AutoCAD.AcCmColor.22 is for 2018, which is what I use. Check the registry at HKEY_Classes_Root for your installation.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 6 of 7

Anonymous
Not applicable

Hey @Ed__Jobe ,

The color thing is not important to me.
But even if I only add the last line to my code I get the error message: "Compile error: Argument not optional"

I use AutoCAD 2020 by the way

0 Likes
Message 7 of 7

Ed__Jobe
Mentor
Mentor

Sorry, it should have been:

ThisDrawing.SetVariable "clayer", MyLayer.Name

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes