Hi @m.joudivand. I think I may have something that you can work with then. Below is the code for a VBA macro that can be used for ensuring a specifically named Layer is present in your drawing. It first tries to find a layer by the specified name, and if found, makes sure it is a 'local' Layer, so it can be used within the document. If it is not found, it is created, then all of its settings are set a certain way. Right now, if it is found to already exist, it is not checking its settings, and just assuming that the settings are the way you want. If this may not be the case, you can either restructure the code or add more code for ensuring the settings are the way you want them, even if the layer already exists. This is an odd case, because there is no 'Add()' type method for creating new Layers...you just have to copy an existing Layer, then make any necessary adjustments to it. You can also save this new local style back to the 'global' repository, so that it can be made available for other documents. If you want to do that, there is a method of the Layer object called 'SaveToGlobal', that you can use.
You will need to edit the layer's name and its settings to suit your needs, language, and application.
Sub GetOrCreateLayer()
If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
Call MsgBox("A Drawing document must be active for this code to work. Exiting.", vbCritical, "")
Exit Sub
End If
Dim oDDoc As DrawingDocument
Set oDDoc = ThisDoc.Document
Dim oDStMgr As DrawingStylesManager
Set oDStMgr = oDDoc.StylesManager
Dim oLayers As LayersEnumerator
Set oLayers = oDStMgr.Layers
Dim oTargetLayerName As String
oTargetLayerName = "MyLayer"
Dim oMyLayer As Layer
Dim oLayer As Layer
For Each oLayer In oLayers
'If oLayer.InternalName = oTargetLayerName Then
If oLayer.name = oTargetLayerName Then
If oLayer.StyleLocation = StyleLocationEnum.kLibraryStyleLocation And _
oLayer.StyleLocation <> StyleLocationEnum.kBothStyleLocation Then
Set oMyLayer = oLayer.ConvertToLocal
Else
Set oMyLayer = oLayer
End If
End If
Next
If IsNothing(oMyLayer) Then
Set oMyLayer = oLayers.Item(1).Copy(oTargetLayerName)
'oMyLayer.AutomaticColor = True 'can ony be set to True, not False
oMyLayer.Color = ThisApplication.TransientObjects.CreateColor(255, 0, 0)
oMyLayer.Comments = "My layer comments"
oMyLayer.LineType = LineTypeEnum.kContinuousLineType
oMyLayer.LineWeight = 0.001
oMyLayer.Plot = True
oMyLayer.ScaleByLineWeight = False
oMyLayer.Visible = True
'oMyLayer.SaveToGlobal
End If
End Sub
Wesley Crihfield

(Not an Autodesk Employee)