Inventor 2022, BOM Export with Design State Issues

ChristiPedersen
Advocate
Advocate

Inventor 2022, BOM Export with Design State Issues

ChristiPedersen
Advocate
Advocate

We switched from 2020 to 2022.  Most of our custom programs are working fine except an older one that has not been rewritten.  When exporting to Excel, I believe it is having an issue with the design states.  Only assemblies with multiple design states does it have this issue.

Having trouble finding documentation that tells me where I can set what design state I want the BOM export for.

 

Set oBom = ThisAssyDoc.ComponentDefinition.BOM
oBom.ImportBOMCustomization (BOM_XML_ENGINEERING)
If oBom.StructuredViewDelimiter <> "," Then
oBom.StructuredViewDelimiter = ","
End If

On Error Resume Next
oBom.StructuredViewFirstLevelOnly = False

oBom.StructuredViewEnabled = True
Set OStructuredBomView = oBom.BOMViews.Item("Structured")

oBom.BOMViews.Item("Structured").Sort "Item", True
'Export the BOM view to an Excel file
OStructuredBomView.Export MANUAL_EXPORT_LOC & "BOM-StructuredAllLevels.xls", kMicrosoftExcelFormat

0 Likes
Reply
Accepted solutions (1)
488 Views
5 Replies
Replies (5)

WCrihfield
Mentor
Mentor

Hi @ChristiPedersen.  That code looks like part of a VBA macro to me, due to the use of the 'Set' keyword.  Under the AssemblyComponentDefinition are a few properties you will need to work with much more often from now on.  One of those properties is 'ModelStates', which is where you will find all of the new ModelState objects under, as well as some other related properties & methods.  Another few useful properties just under the ComponentDefinition are the 'IsModelStateFactory' & 'IsModelStateMember'.  There will not be a ModelState factory, unless the there is more than one ModelState in the file.  Then when there is more than one ModelState, the 'factory' version will be just one of the multiple possible Documents now existing within that one file on disk, and it will be the one that represents the ModelState that is currently 'active'.  The factory is usually the only document version that can be edited (Read/Write), while other versions (members) will be ReadOnly.  A ModelState member is any other ModelState that is not currently the active one, and the document(s) representing those member ModelStates will also usually be ReadOnly.  So, if you need to make changes to a Document in a file that represents a specific ModelState, and that ModelState is not currently the active one, you will usually need to activate the right ModelState before you can make changes to it.  You can usually check the Document.FullDocumentName property of a Document to check which ModelState it is associated with (if any), because it will contain the name of the ModelState within "<" & ">" brackets, right after the FullFileName portion.  The PartDocument object & AssemblyDocument object will now also have a property called 'ModelStateName', which is a ReadOnly property that will contain the name of the document's currently active ModelState.  If that is not the one you want, then go get the ModelState you want, and activate it, then make sure your document variable is pointing to the correct ModelState after that point. 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

ChristiPedersen
Advocate
Advocate

Thank you.  That is all very good information.  What I am struggling with is, 1 assembly with a model state but set on master will not export the BOM, but another assembly with no model states does fine.
When I Set ThisAssyDoc = ODrawDoc.AllReferencedDocuments(Number), I am assuming I need to be checking if there is a model state before that and specify in that line what model state I want when I set it?
The code is not opening the assembly but setting a reference to it so I assumed I wouldn't need to open it and active that model state.  In reality the model state I want already happened to be the current one.
Just can't see how to tell the "set" what model state.
Hope that makes sense.
This code doesn't need to modify the model, just want to have the BOM export.
These ones now fail.  Ones without model states do not fail.

Thank you for any help you can provide.

0 Likes

WCrihfield
Mentor
Mentor
Accepted solution

Hi @ChristiPedersen.  You can take a look at this similar example VBA macro code for some additional information and pointers.  I left a lot of comments in there, and left some lines of code commented out for now too.  The file references are just generic ones for the example too.

 

Sub ExportBOM()
    If ThisApplication.ActiveDocumentType <> kAssemblyDocumentObject Then Exit Sub
    Dim oADoc As AssemblyDocument
    Set oADoc = ThisApplication.ActiveDocument
    
    Dim bMSFactoryExists As Boolean
    bMSFactoryExists = (oADoc.ComponentDefinition.ModelStates.Count > 1)
    'If False, there is no ModelState 'factory' and no document reference confusion
    'if True, you will need to make sure you are working with the factory version document
    
    'to ensure you are working with the ModelState factory document version:
    If bMSFactoryExists And oADoc.ComponentDefinition.IsModelStateMember Then
        Set oADoc = oADoc.ComponentDefinition.FactoryDocument
    End If
    
    On Error Resume Next
     'you can use "On Error GoTo 0" to resume normal error handling (that is a zero at the end)
     'that does not take you to the first line of the code, just resets the error handling
    
    'the first ModelState in the ModelStates collection (Item(1)) will be the original 'Master or Primary' ModelState
    'you can activate a specific one, then use the document it represents to work with going forward if you want
'    Dim oMSs As ModelStates
'    Set oMSs = oADoc.ComponentDefinition.ModelStates
'    oMSs.Item(1).Activate
'    Set oADoc = oMSs.Item(1).FactoryDocument
    'then make sure the MemberEditScope is set to the active member (instead of all members) like this
'    If oMSs.MemberEditScope <> kEditActiveMember Then
'        oMSs.MemberEditScope = kEditActiveMember
'    End If
    
    Dim sBOMCustomizationFile As String
    sBOMCustomizationFile = "C:\Temp\MyBOMCustomization.xml"
    Dim sExportedFileName As String
    sExportedFileName = Strings.Replace(oADoc.FullFileName, ".iam", ".xlsx")
    Dim oBOM As Inventor.BOM
    Set oBOM = oADoc.ComponentDefinition.BOM
    Call oBOM.ImportBOMCustomization(sBOMCustomizationFile)
    oBOM.StructuredViewEnabled = True
    If oBOM.StructuredViewDelimiter <> "," Then oBOM.StructuredViewDelimiter = ","
    oBOM.StructuredViewFirstLevelOnly = False
    Dim oStView As BOMView
    Set oStView = oBOM.BOMViews.Item("Structured")
    
    'you can check this, but can not change its value, because it is ReadOnly
    Dim sBOMViewMemberName As String
    sBOMViewMemberName = oStView.ModelStateMemberName 'ReadOnly
    
    Call oStView.Sort("Item", True)
    Call oStView.Export(sExportedFileName, kMicrosoftExcelFormat)
End Sub

 

I don't use VBA for hardly anything anymore, in favor of iLogic, because Inventor has been steering away from it for years now too, due to security issues. (Link1, Link2)

 

Edit:  I posted this before I saw your last response.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

WCrihfield
Mentor
Mentor

If you are getting a reference to this assembly from within an open DrawingDocument, then the AssemblyDocument should already be at least 'initialized' (almost open - partially loaded into memory), and any calls to it will go ahead and fully open it.  I am not 100% sure, but I suspect that attempting to change some of the BOM settings may be seen as an attempt to 'edit' the document, and may 'dirty' the document.  If the assembly document is representing a ModelState member, it may then be ReadOnly until that member ModelState is activated, or until you switch your document reference to the factory document reference.  But switching to the factory document reference, if it is different than the one you already have a reference to, may get you the BOM of that different version of the assembly.  These ModelStates really added a whole extra layer of complication into the mix. 😅

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

ChristiPedersen
Advocate
Advocate

Thank you.  This code example was exactly what I needed.

0 Likes