×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Insert BOM API

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?

RE: Insert BOM API

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.

RE: Insert BOM API

(OP)
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!

RE: Insert BOM API

(OP)
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

RE: Insert BOM API

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.

RE: Insert BOM API

(OP)
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!

medalmedalmedalafro2afro2afro2
2thumbsup2thumbsup2thumbsup  ?3eyes?

RE: Insert BOM API

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.

RE: Insert BOM API

(OP)
Good 2 know, I'll czech it out.

RE: Insert BOM API

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources