Problem with saving functions as "layer"

Problem with saving functions as "layer"

Hubert_Los
Advocate Advocate
217 Views
1 Reply
Message 1 of 2

Problem with saving functions as "layer"

Hubert_Los
Advocate
Advocate

Hi.

 

I can't save functions as layer, what can i do?

 

Sub main()
    
    Dim oLayer123 As Layer
    oLayer123 = Get_layer()
    
End Sub

Function Get_layer() As Layer
    Dim oDoc As DrawingDocument
    Set oDoc = ThisApplication.ActiveDocument
    Dim styleMgr As DrawingStylesManager
    Set styleMgr = oDoc.StylesManager

    Dim oLayer As Layer

        
On Error GoTo ErrorHandler
    
    Set oLayer = styleMgr.Layers.Item("test_layer")
    Get_layer = oLayer                  ' problem
ErrorHandler:
    Dim oNewLayer As Layer
    Set oLayer = oDoc.StylesManager.Layers.Item(1)
    Set oNewLayer = oLayer.Copy("test_layer")
    
    oNewLayer.Color = ThisApplication.TransientObjects.CreateColor(255, 0, 0)
    oNewLayer.LineWeight = 1
    oNewLayer.LineType = LineTypeEnum.kContinuousLineType
    
    Get_layer = oNewLayer                ' problem
End Function
0 Likes
Accepted solutions (1)
218 Views
1 Reply
Reply (1)
Message 2 of 2

WCrihfield
Mentor
Mentor
Accepted solution

Hi @Hubert_Los.  I tried using a Function within an iLogic rule and it worked, so then I tried it in VBA, because your code appears to be in VBA, and after a bit of tweaking, I got that to work too.  Below is my version of the code that worked in my tests.  (I already had a Layer named "0" (zero), but not one named "NewLayer".)

Sub Main()
    Dim oLyr As Layer
    Set oLyr = GetLayer
    Call MsgBox("oLyr.Name = " & oLyr.Name, , "")
End Sub

Function GetLayer() As Layer
    Dim oDDoc As DrawingDocument
    Set oDDoc = ThisApplication.ActiveDocument
    Dim oLayers As LayersEnumerator
    Set oLayers = oDDoc.StylesManager.Layers
    Dim oLayer As Layer
    On Error GoTo ErrorHandler
    Set oLayer = oLayers.Item("0")
    Set GetLayer = oLayer
    Exit Function
ErrorHandler:
    Set oLayer = oLayers.Item(1).Copy("NewLayer")
    Set GetLayer = oLayer
End Function

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes