Macro to create layer and set it to active state

Library for macros
jeremyrz
Posts: 8
Joined: Thu Nov 02, 2023 11:22 am
Answers: 0

Macro to create layer and set it to active state

Unread post by jeremyrz »

Hye,

I'm expected to create a Macro that could create a Layer, and set it in an active state, before adding a BOM in this layer, in my assembly drawing.
I've search on site like, Codestacks or cadCoder, but nothing that i can really use to do this.

A have allready find a macro to add a BOM, i just nead the part with Layer.

If someone could help me.
Thanks a lot.
User avatar
SPerman
Posts: 2035
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2207
x 1860
Contact:

Re: Macro to create layer and set it to active state

Unread post by SPerman »

-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
jeremyrz
Posts: 8
Joined: Thu Nov 02, 2023 11:22 am
Answers: 0

Re: Macro to create layer and set it to active state

Unread post by jeremyrz »

Hye SPerman,

Thanks you for the links.
I've allready seen this kind of help on solidworks, but i don't know how to use it.
Especially for the "Declaration" field. Where i put that in my VBA macro ?
What mean the "_" at the end of each line ?
VBA
VBA
looking forward to hearing from you
User avatar
zwei
Posts: 701
Joined: Mon Mar 15, 2021 9:17 pm
Answers: 18
Location: Malaysia
x 185
x 600

Re: Macro to create layer and set it to active state

Unread post by zwei »

Example macro below:

Code: Select all

'==================== Preamble ===================='
Option Explicit
Dim swApp               As SldWorks.SldWorks
Dim swModel             As SldWorks.ModelDoc2
Dim swDraw              As SldWorks.DrawingDoc



Dim boolstatus          As Boolean
Dim lErrors             As Long
Dim lWarnings           As Long


'=================================================='

'==================== MainCode ===================='
Sub main()

    Set swApp = Application.SldWorks
    
    'Get active document
    Set swModel = swApp.ActiveDoc
    
    'Display message and terminate if not drawing
    If Not swModel.GetType() = swDocDRAWING Then
        'If not drawing
        MsgBox "Macro only work on drawing file.", vbCritical, "ERROR"
        End
    End If
    Set swDraw = swModel
    
    'Create Layer
    boolstatus = swDraw.CreateLayer("TestLayer", "TestDescription", 255, swLineCONTINUOUS, swLW_THICK, True)
    
    'Activate Layer
    boolstatus = swDraw.SetCurrentLayer("TestLayer")
    
    Set swModel = Nothing
End Sub
'=================================================='
You will need to change the two line below manually:
'Create Layer
boolstatus = swDraw.CreateLayer("TestLayer", "TestDescription", 255, swLineCONTINUOUS, swLW_THICK, True)

'Activate Layer
boolstatus = swDraw.SetCurrentLayer("TestLayer")
Far too many items in the world are designed, constructed and foisted upon us with no understanding-or even care-for how we will use them.
Tera
Posts: 225
Joined: Fri Mar 19, 2021 4:58 am
Answers: 2
x 481
x 90

Re: Macro to create layer and set it to active state

Unread post by Tera »

jeremyrz wrote: Fri Nov 03, 2023 6:37 am What mean the "_" at the end of each line ?
In VBA, When a line of code is too long, you can divide it to several lines by adding a "_ " at the end of the code.

So if you have a line like :

Code: Select all

If swFeat.GetTypeName = "SolidBodyFolder" Or swFeat.GetTypeName = "CutListFolder" Or swFeat.GetTypeName = "SubWeldFolder" Then
You can add an _ to the end and continue in the next line.
Sometimes it makes your code much better readable.

Code: Select all

If swFeat.GetTypeName = "SolidBodyFolder" Or _
        swFeat.GetTypeName = "CutListFolder" Or _
        swFeat.GetTypeName = "SubWeldFolder" Then
jeremyrz
Posts: 8
Joined: Thu Nov 02, 2023 11:22 am
Answers: 0

Re: Macro to create layer and set it to active state

Unread post by jeremyrz »

Tanks you Tera.

And thank you Zwei, you helped me find a solution.
However, some problems remain:

- The BOM, which in the VBA program is generated after layer creation, is not created with the active layer, but in another layer "0".
The new layer becomes active in SW, but the BOM does not take it into account. Nor is the BOM created with the layer
defined in Options/Document properties/Tables/BOM.
You can see the Vba lines below to test it.

What could be the solution ?

Const ANCHOR_TYPE As Integer = swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_TopRight
Const BOM_TYPE As Integer = swBomType_e.swBomType_PartsOnly
Const TABLE_TEMPLATE_SIMPLE As String = "\\vmisw2022\DataSolidWorks\ModelesVMI\Tables\Nomenclatures-Qte-CodeSAP.sldbomtbt"
Const TABLE_TEMPLATE_STD As String = "\\vmisw2022\DataSolidWorks\ModelesVMI\Tables\NomenclaturesSAP.sldbomtbt"
'Const TABLE_TEMPLATE_BASE_FR As String = "\\vmisw2022\DataSolidWorks\ModelesVMI\Tables\NomenclaturesSAP_SIMP_FR.sldbomtbt"
'Const TABLE_TEMPLATE_BASE_EN As String = "\\vmisw2022\DataSolidWorks\ModelesVMI\Tables\NomenclaturesSAP_SIMP_EN.sldbomtbt"
'Const TABLE_TEMPLATE_BASE_AL As String = "\\vmisw2022\DataSolidWorks\ModelesVMI\Tables\NomenclaturesSAP_SIMP_AL.sldbomtbt"
'Const TABLE_TEMPLATE_BASE_ES As String = "\\vmisw2022\DataSolidWorks\ModelesVMI\Tables\NomenclaturesSAP_SIMP_ES.sldbomtbt"
Const INDENTED_NUMBERING_TYPE As Integer = swNumberingType_e.swNumberingType_Flat
Const DETAILED_CUT_LIST As Boolean = False
Const FOLLOW_ASSEMBLY_ORDER As Boolean = True
Const ALL_SHEETS As Boolean = False


Dim boolstatus As Boolean
Dim lErrors As Long
Dim lWarnings As Long

Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc


Sub main()

Set swApp = Application.SldWorks
Set swDraw = swApp.ActiveDoc

'Get active document
Set swModel = swApp.ActiveDoc

Dim boolstatus As Boolean
Dim lErrors As Long
Dim lWarnings As Long



'Display message and terminate if not drawing
If Not swModel.GetType() = swDocDRAWING Then
'If not drawing
MsgBox "Macro only work on drawing file.", vbCritical, "ERROR"
End
End If
Set swDraw = swModel

'Création calque BOM_SIMPLE
boolstatus = swDraw.CreateLayer("BOM_SIMPLE", "Nomenclature Ss Description", 255, swLineCONTINUOUS, swLW_THICK, True)

'Active calque BOM_SIMPLE
boolstatus = swDraw.SetCurrentLayer("BOM_SIMPLE")

Set swModel = Nothing

'If ALL_SHEETS Then

'Dim vSheetNames As Variant
'vSheetNames = swDraw.GetSheetNames

'Dim activeSheetName As String
' activeSheetName = swDraw.GetCurrentSheet().GetName

'Dim i As Integer

'For i = 0 To UBound(vSheetNames)
'Dim swSheet As SldWorks.sheet
' Set swSheet = swDraw.sheet(CStr(vSheetNames(i)))
'InsertBomTable swDraw, swSheet
'Next

'swDraw.ActivateSheet activeSheetName

'Else
InsertBomTable swDraw, swDraw.GetCurrentSheet
'End If

End Sub

Sub InsertBomTable(draw As SldWorks.DrawingDoc, sheet As SldWorks.sheet)

If False = draw.ActivateSheet(sheet.GetName()) Then
Err.Raise vbError, "", "Failed to activate sheet " & sheet.GetName
End If

Dim vViews As Variant
vViews = sheet.GetViews

Dim swView As SldWorks.View

Set swView = vViews(0)

Dim swBomTableAnn As SldWorks.BomTableAnnotation

Set swBomTableAnn = swView.InsertBomTable4(True, 0, 0, ANCHOR_TYPE, BOM_TYPE, "", TABLE_TEMPLATE_SIMPLE, False, INDENTED_NUMBERING_TYPE, DETAILED_CUT_LIST)

If Not swBomTableAnn Is Nothing Then
swBomTableAnn.BomFeature.FollowAssemblyOrder2 = FOLLOW_ASSEMBLY_ORDER
Else
Err.Raise vbError, "", "Failed to insert BOM table into " & swView.Name
End If

End Sub

- Is it possible, once the BOM has been created with the new layer, to hide this layer?

Thanks you so much. :D
User avatar
zwei
Posts: 701
Joined: Mon Mar 15, 2021 9:17 pm
Answers: 18
Location: Malaysia
x 185
x 600

Re: Macro to create layer and set it to active state

Unread post by zwei »

jeremyrz wrote: Tue Nov 07, 2023 6:24 am - The BOM, which in the VBA program is generated after layer creation, is not created with the active layer, but in another layer "0".
The new layer becomes active in SW, but the BOM does not take it into account. Nor is the BOM created with the layer
defined in Options/Document properties/Tables/BOM.
You can see the Vba lines below to test it.

What could be the solution ?
InsertBOM does not set the layer, you will need to set the layer after you insert the code

Code: Select all

Set swTable = swBomTableAnn
swTable.GetAnnotation.Layer = "BOM_SIMPLE"
- Is it possible, once the BOM has been created with the new layer, to hide this layer?

Thanks you so much. :D
If you mean turning off the layer visibility, then yes. You can even do it before inserting the bom if you want...

Code: Select all

'Turn layer visibility off
    Set swLayerMgr = swModel.GetLayerManager
    Set swLayer = swLayerMgr.GetLayer("BOM_SIMPLE")
    swLayer.Visible = False

I had updated your code as shown below:
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc

Dim swTable As SldWorks.TableAnnotation
Dim swLayerMgr As SldWorks.LayerMgr
Dim swLayer As SldWorks.Layer


Dim boolstatus As Boolean
Dim lErrors As Long
Dim lWarnings As Long

Const ANCHOR_TYPE As Integer = swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_TopRight
Const BOM_TYPE As Integer = swBomType_e.swBomType_PartsOnly
Const TABLE_TEMPLATE_SIMPLE As String = "\\vmisw2022\DataSolidWorks\ModelesVMI\Tables\Nomenclatures-Qte-CodeSAP.sldbomtbt"
Const TABLE_TEMPLATE_STD As String = "\\vmisw2022\DataSolidWorks\ModelesVMI\Tables\NomenclaturesSAP.sldbomtbt"
'Const TABLE_TEMPLATE_BASE_FR As String = "\\vmisw2022\DataSolidWorks\ModelesVMI\Tables\NomenclaturesSAP_SIMP_FR.sldbomtbt"
'Const TABLE_TEMPLATE_BASE_EN As String = "\\vmisw2022\DataSolidWorks\ModelesVMI\Tables\NomenclaturesSAP_SIMP_EN.sldbomtbt"
'Const TABLE_TEMPLATE_BASE_AL As String = "\\vmisw2022\DataSolidWorks\ModelesVMI\Tables\NomenclaturesSAP_SIMP_AL.sldbomtbt"
'Const TABLE_TEMPLATE_BASE_ES As String = "\\vmisw2022\DataSolidWorks\ModelesVMI\Tables\NomenclaturesSAP_SIMP_ES.sldbomtbt"
Const INDENTED_NUMBERING_TYPE As Integer = swNumberingType_e.swNumberingType_Flat
Const DETAILED_CUT_LIST As Boolean = False
Const FOLLOW_ASSEMBLY_ORDER As Boolean = True
Const ALL_SHEETS As Boolean = False


Sub main()

Set swApp = Application.SldWorks
Set swDraw = swApp.ActiveDoc

'Get active document
Set swModel = swApp.ActiveDoc

'Display message and terminate if not drawing
If Not swModel.GetType() = swDocDRAWING Then
'If not drawing
MsgBox "Macro only work on drawing file.", vbCritical, "ERROR"
End
End If
Set swDraw = swModel

'Création calque BOM_SIMPLE
boolstatus = swDraw.CreateLayer("BOM_SIMPLE", "Nomenclature Ss Description", 255, swLineCONTINUOUS, swLW_THICK, True)

'Active calque BOM_SIMPLE
boolstatus = swDraw.SetCurrentLayer("BOM_SIMPLE")



'If ALL_SHEETS Then

'Dim vSheetNames As Variant
'vSheetNames = swDraw.GetSheetNames

'Dim activeSheetName As String
' activeSheetName = swDraw.GetCurrentSheet().GetName

'Dim i As Integer

'For i = 0 To UBound(vSheetNames)
'Dim swSheet As SldWorks.sheet
' Set swSheet = swDraw.sheet(CStr(vSheetNames(i)))
'InsertBomTable swDraw, swSheet
'Next

'swDraw.ActivateSheet activeSheetName

'Else
InsertBomTable swDraw, swDraw.GetCurrentSheet
'End If

'Set BOM Layer
Set swTable = swBomTableAnn
swTable.GetAnnotation.Layer = "BOM_SIMPLE"

'Set turn off layer visibility
Set swLayerMgr = swModel.GetLayerManager
Set swLayer = swLayerMgr.GetLayer("BOM_SIMPLE")
swLayer.Visible = False


Set swModel = Nothing


End Sub

Sub InsertBomTable(draw As SldWorks.DrawingDoc, sheet As SldWorks.sheet)

If False = draw.ActivateSheet(sheet.GetName()) Then
Err.Raise vbError, "", "Failed to activate sheet " & sheet.GetName
End If

Dim vViews As Variant
vViews = sheet.GetViews

Dim swView As SldWorks.View

Set swView = vViews(0)

Dim swBomTableAnn As SldWorks.BomTableAnnotation

Set swBomTableAnn = swView.InsertBomTable4(True, 0, 0, ANCHOR_TYPE, BOM_TYPE, "", TABLE_TEMPLATE_SIMPLE, False, INDENTED_NUMBERING_TYPE, DETAILED_CUT_LIST)

If Not swBomTableAnn Is Nothing Then
swBomTableAnn.BomFeature.FollowAssemblyOrder2 = FOLLOW_ASSEMBLY_ORDER
Else
Err.Raise vbError, "", "Failed to insert BOM table into " & swView.Name
End If

End Sub
Far too many items in the world are designed, constructed and foisted upon us with no understanding-or even care-for how we will use them.
jeremyrz
Posts: 8
Joined: Thu Nov 02, 2023 11:22 am
Answers: 0

Re: Macro to create layer and set it to active state

Unread post by jeremyrz »

Hello Zwei,

Image
It seems that the modifications you made generate a compilation error when I run the macro.
And since I'm not a VBA specialist at all, I can't find the solution to this error.
Could you please test it at home to see if you get the same thing?

Thanks a lot!
Attachments
image.png
User avatar
SPerman
Posts: 2035
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2207
x 1860
Contact:

Re: Macro to create layer and set it to active state

Unread post by SPerman »

The problem (for me at least) occurs here:

Set swTable = swBomTableAnn

swBomTableAnn is defined in the subroutine, but not in the main function.
-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
User avatar
zwei
Posts: 701
Joined: Mon Mar 15, 2021 9:17 pm
Answers: 18
Location: Malaysia
x 185
x 600

Re: Macro to create layer and set it to active state

Unread post by zwei »

Sperman is correct, i missed out the sub routine, move the code that set the table layer to the subroutine should fix the issue
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc

Dim swTable As SldWorks.TableAnnotation
Dim swLayerMgr As SldWorks.LayerMgr
Dim swLayer As SldWorks.Layer


Dim boolstatus As Boolean
Dim lErrors As Long
Dim lWarnings As Long

Const ANCHOR_TYPE As Integer = swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_TopRight
Const BOM_TYPE As Integer = swBomType_e.swBomType_PartsOnly
Const TABLE_TEMPLATE_SIMPLE As String = "\\vmisw2022\DataSolidWorks\ModelesVMI\Tables\Nomenclatures-Qte-CodeSAP.sldbomtbt"
Const TABLE_TEMPLATE_STD As String = "\\vmisw2022\DataSolidWorks\ModelesVMI\Tables\NomenclaturesSAP.sldbomtbt"
'Const TABLE_TEMPLATE_BASE_FR As String = "\\vmisw2022\DataSolidWorks\ModelesVMI\Tables\NomenclaturesSAP_SIMP_FR.sldbomtbt"
'Const TABLE_TEMPLATE_BASE_EN As String = "\\vmisw2022\DataSolidWorks\ModelesVMI\Tables\NomenclaturesSAP_SIMP_EN.sldbomtbt"
'Const TABLE_TEMPLATE_BASE_AL As String = "\\vmisw2022\DataSolidWorks\ModelesVMI\Tables\NomenclaturesSAP_SIMP_AL.sldbomtbt"
'Const TABLE_TEMPLATE_BASE_ES As String = "\\vmisw2022\DataSolidWorks\ModelesVMI\Tables\NomenclaturesSAP_SIMP_ES.sldbomtbt"
Const INDENTED_NUMBERING_TYPE As Integer = swNumberingType_e.swNumberingType_Flat
Const DETAILED_CUT_LIST As Boolean = False
Const FOLLOW_ASSEMBLY_ORDER As Boolean = True
Const ALL_SHEETS As Boolean = False


Sub main()

Set swApp = Application.SldWorks
Set swDraw = swApp.ActiveDoc

'Get active document
Set swModel = swApp.ActiveDoc

'Display message and terminate if not drawing
If Not swModel.GetType() = swDocDRAWING Then
'If not drawing
MsgBox "Macro only work on drawing file.", vbCritical, "ERROR"
End
End If
Set swDraw = swModel

'Création calque BOM_SIMPLE
boolstatus = swDraw.CreateLayer("BOM_SIMPLE", "Nomenclature Ss Description", 255, swLineCONTINUOUS, swLW_THICK, True)

'Active calque BOM_SIMPLE
boolstatus = swDraw.SetCurrentLayer("BOM_SIMPLE")



'If ALL_SHEETS Then

'Dim vSheetNames As Variant
'vSheetNames = swDraw.GetSheetNames

'Dim activeSheetName As String
' activeSheetName = swDraw.GetCurrentSheet().GetName

'Dim i As Integer

'For i = 0 To UBound(vSheetNames)
'Dim swSheet As SldWorks.sheet
' Set swSheet = swDraw.sheet(CStr(vSheetNames(i)))
'InsertBomTable swDraw, swSheet
'Next

'swDraw.ActivateSheet activeSheetName

'Else
InsertBomTable swDraw, swDraw.GetCurrentSheet
'End If



'Set turn off layer visibility
Set swLayerMgr = swModel.GetLayerManager
Set swLayer = swLayerMgr.GetLayer("BOM_SIMPLE")
swLayer.Visible = False


Set swModel = Nothing


End Sub

Sub InsertBomTable(draw As SldWorks.DrawingDoc, sheet As SldWorks.sheet)

If False = draw.ActivateSheet(sheet.GetName()) Then
Err.Raise vbError, "", "Failed to activate sheet " & sheet.GetName
End If

Dim vViews As Variant
vViews = sheet.GetViews

Dim swView As SldWorks.View

Set swView = vViews(0)

Dim swBomTableAnn As SldWorks.BomTableAnnotation

Set swBomTableAnn = swView.InsertBomTable4(True, 0, 0, ANCHOR_TYPE, BOM_TYPE, "", TABLE_TEMPLATE_SIMPLE, False, INDENTED_NUMBERING_TYPE, DETAILED_CUT_LIST)

If Not swBomTableAnn Is Nothing Then
swBomTableAnn.BomFeature.FollowAssemblyOrder2 = FOLLOW_ASSEMBLY_ORDER

'Set BOM Layer
Set swTable = swBomTableAnn
swTable.GetAnnotation.Layer = "BOM_SIMPLE"


Else
Err.Raise vbError, "", "Failed to insert BOM table into " & swView.Name
End If



End Sub
Far too many items in the world are designed, constructed and foisted upon us with no understanding-or even care-for how we will use them.
jeremyrz
Posts: 8
Joined: Thu Nov 02, 2023 11:22 am
Answers: 0

Re: Macro to create layer and set it to active state

Unread post by jeremyrz »

Hello Zwei and Superman

Thanks for your help, it works very well.
UU
Post Reply