This website uses cookies to ensure you get the best experience on our website. By using our website you agree on the following Cookie Policy, Privacy Policy, and Terms Of Use
There is no common ::Layer SOLIDWORKS API property to change the layer for any entity, rather this property is added to each interface which supports it (e.g. ISketchSegment::Layer property). This macro checks the type of the entity and calls corresponding SOLIDWORKS API property to change the layer.
Dim swApp As SldWorks.SldWorks
Dim swDraw As SldWorks.DrawingDoc
Dim swSelMgr As SldWorks.SelectionMgr
Sub main()
OnErrorResumeNextSet swApp = Application.SldWorks
Set swDraw = swApp.ActiveDoc
IfNot swDraw IsNothingThenSet swSelMgr = swDraw.SelectionManager
If swSelMgr.GetSelectedObjectCount2(-1) > 0 ThenDim layerName AsString
layerName = InputBox("Specify the layer name to move selected objects to")
Dim swAnn As SldWorks.Annotation
Dim i AsIntegerFor i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
Dim swSelObj AsObjectSet swSelObj = swSelMgr.GetSelectedObject6(i, -1)
IfTypeOf swSelObj Is SldWorks.SketchSegment ThenDim swSkSegment As SldWorks.SketchSegment
Set swSkSegment = swSelObj
swSkSegment.Layer = layerName
ElseIfTypeOf swSelObj Is SldWorks.SketchPoint ThenDim swSkPoint As SldWorks.SketchPoint
Set swSkPoint = swSelObj
swSkPoint.Layer = layerName
ElseIfTypeOf swSelObj Is SldWorks.Note ThenDim swNote As SldWorks.Note
Set swNote = swSelObj
Set swAnn = swNote.GetAnnotation()
swAnn.Layer = layerName
ElseIfTypeOf swSelObj Is SldWorks.DisplayDimension ThenDim swDispDim As SldWorks.DisplayDimension
Set swDispDim = swSelObj
Set swAnn = swDispDim.GetAnnotation
swAnn.Layer = layerName
Else'try to set the layer using late binding
swSelObj.Layer = layerName
EndIfNextElse
MsgBox "Please select annotation, sketch segment or point to move to new layer"EndIfElse
MsgBox "Please open drawing"EndIfEndSub