Insert BOM API
Insert BOM API
(OP)
I am attempting to insert a Bill of Materials using a specific table template into a Solidworks drawing via API. My macro so far is thus:
Sub Macro2()
Dim BomTable As Object
Dim BomFeat As Object
Dim Names As Variant
Dim Visible As Variant
Set swApp = CreateObject("SldWorks.Application")
Set Drawing = swApp.ActiveDoc
Set View = Drawing.GetFirstView 'This is the sheet
Set View = View.GetNextView 'This is the first actual view
Filepath = "W:\BLOCKS\Solidworks Templates\Other\HTC BOM.sldbomtbt"
Set BomTable = View.InsertBomTable2(True, 0, 0, swBOMConfigurationAnchor_TopRight, _
swBomType_TopLevelOnly, "", Filepath)
Set BomFeat = BomTable.BomFeature
Names = BomFeat.GetConfigurations(False, Visible)
Visible(0) = True
boolstatus = BomFeat.SetConfigurations(True, Visible, Names)
Drawing.FeatureManager.UpdateFeatureTree
End Sub
The other thing about this macro is that it is embedded in a button in an Excel sheet, if that is a factor. When I try to run this macro, it hangs up at the line "Set BomFeat..." with an error stating that the object variable is not set. What am I missing here? I have used the examples given in the Solidworks API help to make this macro, and it doesn't seem to follow through. Any suggestions?
Sub Macro2()
Dim BomTable As Object
Dim BomFeat As Object
Dim Names As Variant
Dim Visible As Variant
Set swApp = CreateObject("SldWorks.Application")
Set Drawing = swApp.ActiveDoc
Set View = Drawing.GetFirstView 'This is the sheet
Set View = View.GetNextView 'This is the first actual view
Filepath = "W:\BLOCKS\Solidworks Templates\Other\HTC BOM.sldbomtbt"
Set BomTable = View.InsertBomTable2(True, 0, 0, swBOMConfigurationAnchor_TopRight, _
swBomType_TopLevelOnly, "", Filepath)
Set BomFeat = BomTable.BomFeature
Names = BomFeat.GetConfigurations(False, Visible)
Visible(0) = True
boolstatus = BomFeat.SetConfigurations(True, Visible, Names)
Drawing.FeatureManager.UpdateFeatureTree
End Sub
The other thing about this macro is that it is embedded in a button in an Excel sheet, if that is a factor. When I try to run this macro, it hangs up at the line "Set BomFeat..." with an error stating that the object variable is not set. What am I missing here? I have used the examples given in the Solidworks API help to make this macro, and it doesn't seem to follow through. Any suggestions?






RE: Insert BOM API
FYI, I couldn't get the "SetConfigurations" to work at all. I'm not sure what's going on there.
RE: Insert BOM API
Yatanae!
RE: Insert BOM API
Sub Macro2()
'***Get the SldWorks app
Dim swApp As SldWorks.SldWorks
Set swApp = CreateObject("SldWorks.Application")
Set Drawing = swApp.ActiveDoc
Set View = Drawing.GetFirstView 'This is the sheet
Set View = View.GetNextView 'This is the first actual view
'***Insert the BomTableAnnotation
Dim BOMTable As SldWorks.BomTableAnnotation
Set BOMTable = View.InsertBomTable2(True, 0, 0, swBOMConfigurationAnchor_TopRight, _
swBomType_TopLevelOnly, View.ReferencedConfiguration, "W:\BLOCKS\Solidworks Templates\Other\HTC BOM.sldbomtbt")
'***Update to show the BOM feature in the Feature Manager Tree
Drawing.FeatureManager.UpdateFeatureTree
End Sub
RE: Insert BOM API
Using the "Option Explicit" directive will also help you track down problems like this. I believe it's generally considered best practice to always declare all your variables anyway.
RE: Insert BOM API
RE: Insert BOM API
RE: Insert BOM API
RE: Insert BOM API
Regards,
Regg