AnsweredAssumed Answered

How to reload feature tree in drawing per macro?

Question asked by Stefan Jaekel on Dec 23, 2020
Latest reply on Jan 4, 2021 by Stefan Jaekel



perhaps someone can help me with my macros?

We used to put on our drawings the excel-based BOM.

From now on we want to delete the excel-based BOM and use the SW BOM.

I wrote a macro to delete all the excel-based BOMs in the drawing.

Another macro puts the SW BOM on the drawing.

The SW BOM is placed correctly on the drawing but:

   The SW BOM is not shown in the feature tree!

Why not?

I tryed to place the SW BOM manually on the drawing, but with the same result.

There must be something wrong in the macro "Delete excel-based BOM" but I can't figure out what.

Manualy deleting the excel-based BOM and inserting the SW BOM via macro works fine.

Technically both macros are working. Only the feature tree does not update.


If I type something in the filterfield above the feature tree and remove the entry from filterfield, suddenly the BOM ist displayed.


Perhaps anyone has come across the same problem an can help me.


VBA macro "Delete excel-based BOM":


Option Explicit
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swFeatMgr As SldWorks.FeatureManager
Dim swFeat As SldWorks.Feature
Dim swSubFeat As SldWorks.Feature
Dim swSubSubFeat As SldWorks.Feature
Dim swView As SldWorks.View
Dim oViewBOMObj As BomTable
Dim swViewBomTable As SldWorks.BomTable
Dim swdraw As SldWorks.DrawingDoc
Dim bRet As Boolean
Dim SheetNames As Variant

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swFeatMgr = swModel.FeatureManager
Set swdraw = swModel
SheetNames = swdraw.GetSheetNames

swFeatMgr.EnableFeatureTree = False
swModel.ClearSelection2 True
Debug.Print "File = " & swModel.GetPathName
Set swFeat = swModel.FirstFeature
Do While Not swFeat Is Nothing
If swFeat.GetTypeName = "DrSheet" Then
swdraw.ActivateSheet (swFeat.Name)
End If
Debug.Print " " & swFeat.Name & " [" & swFeat.GetTypeName & "]"
Set swSubFeat = swFeat.GetFirstSubFeature
Do While Not swSubFeat Is Nothing
Debug.Print " " & swSubFeat.Name & " [" & swSubFeat.GetTypeName & "]"
Set swSubSubFeat = swSubFeat.GetFirstSubFeature
Do While Not swSubSubFeat Is Nothing And InStr(swSubFeat.GetTypeName, "View") > 0
If swSubSubFeat.GetTypeName = "BomFeature" Then
Debug.Print " " & swSubSubFeat.Name & " [" & swSubSubFeat.GetTypeName & "]" & " [" & swSubSubFeat.GetID & "]"
Set swView = swdraw.FeatureByName(swSubFeat.Name).GetSpecificFeature()
Set oViewBOMObj = swView.GetBomTable
Set swViewBomTable = oViewBOMObj
End If
Set swSubSubFeat = swSubSubFeat.GetNextSubFeature
If Not oViewBOMObj Is Nothing Then
bRet = swViewBomTable.Select(False, 0)
bRet = swModel.DeleteSelection(False)
swModel.ForceRebuild3 True
Set oViewBOMObj = Nothing
End If
Set swSubFeat = swSubFeat.GetNextSubFeature
Set swFeat = swFeat.GetNextFeature
swModel.ForceRebuild3 True
Set swModel = Nothing
swdraw.ActivateSheet (SheetNames(0))
End Sub


I think there is an problem with looping through the features.

Because when I run the macro without any deleting, the feature tree is "frozen" and a new SW BOM will not be shown.


If I expand the view with the excelbased BOM and run the "Delete excel-based BOM" macro, the excel-based BOM is still present.


















When I try to select the excel-based BOM, SW crashes.

Typing something in the filterfield forces the recalculating of the featuretree and the excel-based BOM entry has vanished.


After running the macro "Insert BOM":

After filtering:


Any help is much apprechiated.


Thank you, have nice a christmas, a happy new year and stay healthy!