Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert BOM API 1

Status
Not open for further replies.

smma

Mechanical
Jun 4, 2007
24
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?
 
Replies continue below

Recommended for you

My guess is that you haven't included the SolidWorks Constant type libraries in the VBA editor. Try replacing swBOMConfigurationAnchor_TopRight and swBomType_TopLevelOnly with their actual value (both 2).

FYI, I couldn't get the "SetConfigurations" to work at all. I'm not sure what's going on there.
 
handleman, interestingly enough, I did get it to work. My first problem was that the filename for the table template was wrong; that is, the path was correct, but the actual file was incorrect. It had two spaces in "HTC BOM" instead of one. I have since corrected. Secondly, and this is an interesting note of candy SWX didn't tell us about, you can use View.ReferencedConfiguration to set the configuration instead of going through the Get/Set Config syntax as advised in SWX's help file. It picked the configuration used on the drawing and then put the template in top level only status perfectly! (BTW, if there is something wrong with going this route rather than the recommended way, somebody please let me know.)

Yatanae!
 
Okay, let me rescind my last post. When I start Visual Basic from SWX and run the code, it works peachy. However, when I copy the code to Visual Basic in Excel and run it, nothing happens; it cycles through the code and doesn't add the BOM in, but it doesn't return any kind of an error either. I have the appropriate references checked in the Object Library, and it recognizes solidworks application code, but it just doesn't do anything. Any ideas? Here's the code below. Run it from SWX and then run it from Excel through VB and see if the same thing doesn't happen.

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
 
Are you sure that the SolidWorks 200x Constant Type Library reference is checked? If it isn't, the constants I referenced in my last post will be interpreted as "0" and you will get the behavior you cite.

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.
 
man, handleman, you're a genus:class:genius! (A little zoological humor there.) I didn't check the Constant Type Library; the only one that was showing was the 2005...since I have 2007, I thought it might have been obsolete or something like that. Well, that's what I get for those assumptions!! Starsies for you!

[medal][medal][medal][afro2][afro2][afro2]
[2thumbsup][2thumbsup][2thumbsup] ?[3eyes]?
 
Hmm, if you don't have a 2007 version of the constant library I wonder if there's something screwy with your install. The old constants don't go obsolete for the most part for backward compatibility so that macros written way back in SW2001 will mostly still work. However, new software functionality adds new constants, so I would imagine the 2005 version may be a bit incomplete.
 
Good 2 know, I'll czech it out.
 
This is why you should always include the Option Explicit statement in your macros; it identifies variables that are not defined. This may have saved you some time.

Regards,

Regg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor