Model state BOM row error

Model state BOM row error

wouter
Advocate Advocate
1,435 Views
8 Replies
Message 1 of 9

Model state BOM row error

wouter
Advocate
Advocate

Hi all,

 

I have a rule that checks some iproperties of all parts in the bill of materials and it used to work just fine.

Now i encountered an error that occurs when the assembly is in any other model state then master.

 

The error occurs on this piece of code:

Dim oCompDef As ComponentDefinition
oCompDef = oBOMRow.ComponentDefinitions.Item(1)

 I've read that there are more issues with the model state and i'm wondering if this is one of them or that the code needs to be rewritten compared to the old LOD system.

 

Any help is appreciated.

 

Kind regards,

Wouter

0 Likes
Accepted solutions (1)
1,436 Views
8 Replies
Replies (8)
Message 2 of 9

WCrihfield
Mentor
Mentor

Hi @wouter.  We can't really debug your code if we can't see it.  If there is nothing proprietary or personal within your code, then can you post the full code here so we can better understand what all is going on?  When ModelStates get involved, it does tend to complicate some things a bit, and it can be a bit challenging to understand what's going on at first.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 9

wouter
Advocate
Advocate

Hi @WCrihfield , this is pretty much the whole rule.

The part that errors is straigt copy paste of someone else code that i also just tested.

That one also gives an error for the same line of code when in a model state.

 

Hope this helps.

Dim oAssyDoc As AssemblyDocument
	oAssyDoc = ThisApplication.ActiveDocument
	Dim oAssyDef As AssemblyComponentDefinition
	oAssyDef = oAssyDoc.ComponentDefinition

Dim openDoc As Inventor.Document
Dim docFile As Inventor.Document

openDoc = ThisApplication.ActiveDocument

	'set  a reference to the BOM
	Dim oBOM As BOM
	oBOM = oAssyDef.BOM

	'set a reference to the "Parts Only" BOMView
	Dim oBOMView As BOMView
	oBOM.PartsOnlyViewEnabled = True
	oBOMView = oBOM.BOMViews.Item("Parts Only")
	Dim oBOMRow As BOMRow

	For Each docFile In openDoc.AllReferencedDocuments
'MsgBox(docFile.FullFileName)


	
If docFile.DocumentType = kPartDocumentObject Or docFile.DocumentType = kAssemblyDocumentObject Then
	
	If docFile.FullFileName.Contains("Content") Or docFile.FullFileName.Contains("Standard parts") Then Continue For
		
Totalname = docFile.FullFileName

Dim Bestandsnaam As String
Dim BestandsnaamMetExtentie As String

Bestandsnaam = IO.Path.GetFileNameWithoutExtension(Totalname) 
BestandsnaamMetExtentie = IO.Path.GetFileName(Totalname)
		 
	StockNumber = iProperties.Value(BestandsnaamMetExtentie, "Project", "Stock Number")
	PartNumber = iProperties.Value(BestandsnaamMetExtentie, "Project", "Part Number")
	RevisionNumber = iProperties.Value(BestandsnaamMetExtentie, "Project", "Revision Number")
	
	Dim StockNumberLength As Integer
	StockNumberLength = StockNumber.length
	
	If StockNumberLength <> 14 Then
		MsgBox("Controleer op stocknumber en gelinkte bestanden" & vbCrLf & vbCrLf & Totalname)
		Exit sub
	End If
	
	Stockdeel1 = (Left(StockNumber, (InStr(StockNumber, "-")) -1))
	
	Stockdeel2 = (Right(StockNumber, (3)))

	For Each oBOMRow In oBOMView.BOMRows


Dim oCompDef As ComponentDefinition
oCompDef = oBOMRow.ComponentDefinitions.Item(1)
Dim oBOMRowDoc As Document
oBOMRowDoc = oCompDef.Document
Dim oFileName As String
oFileName = oBOMRowDoc.FullFileName
If oFileName.Contains("Content") Or oFileName.Contains("Standard parts") Then Continue For


	StockNumber2 = oBOMRow.ComponentDefinitions(1).Document.PropertySets("Design Tracking Properties")("Stock Number").Value
	PartNumber2 = oBOMRow.ComponentDefinitions(1).Document.PropertySets("Design Tracking Properties")("Part Number").Value

'MsgBox(oFileName)

If StockNumber2 = "" Then
	MsgBox("Controleer dit onderdeel" & vbCrLf & "PartNumber: " & PartNumber2)
	Exit sub
End If

If StockNumber2 = StockNumber And
	StockNumber = "" Then
	
	

MessageBox.Show("LET OP!" & vbCrLf & "Sommige bestanden hebben geen Stocknumber" & PartNumber2 & vbCrLf &
	"Optie 1: Laat de Titelblok rule lopen en probeer deze regel opnieuw" & vbCrLf &
	"Optie 2, stap 1: Controleer de Bill of Materials op lege vakken bij het stocknumber (parts only tabblad)" & vbCrLf &
	"Optie 2, stap 2: Als het een content center part is moet deze in de bibliotheek worden aangepast, meld dit bij Wouter!" & vbCrLf , "Lege waarde Stock Number")

Exit Sub

	End If

	If StockNumber2 = StockNumber And
	PartNumber2 <> PartNumber Then
	
	question1 = MessageBox.Show("LET OP!" & vbCrLf & "Tekeningnummer " & StockNumber2 & " dubbel gebruikt" & PartNumber & PartNumber2 & vbCrLf &
	"Optie 1: Laat de Titelblok rule lopen en probeer deze regel opnieuw" & vbCrLf &
	"Optie 2, stap 1: Verander het tekeningnummer van 1 van de parts met nummer " & StockNumber2 & vbCrLf &
	"Optie 2, stap 2: Laat de Titelblok rule lopen" & vbCrLf & " " & vbCrLf & "WIL JE DE RULE BEEINDIGEN ?!", "Dubbele waarde Stock Number", MessageBoxButtons.YesNo, MessageBoxIcon.Error)
	If question1 = vbYes Then	
		Exit Sub
	Else If question1 = vbNo Then
		MessageBox.Show("Rule loopt gewoon door", "LET OP !!",MessageBoxButtons.OK, MessageBoxIcon.Warning)
	End If 


	End If
	
	If StockNumber2 = "" Then
		
	Else
		
	Stockdeel1_1 = (Left(StockNumber2, (InStr(StockNumber2, "-")) -1))
	Stockdeel2_1 = (Right(StockNumber2, (3)))
	

	If Stockdeel1_1 = Stockdeel1 And Stockdeel2_1 = Stockdeel2 And
	PartNumber2 <> PartNumber Then
	
	question2 = MessageBox.Show("LET OP!" & vbCrLf & "Tekeningnummer dubbel gebruikt met een ander revisie/versie nummer" & vbCrLf &
	Stockdeel1 & " " & Stockdeel2 & " " & PartNumber & vbCrLf & 
	Stockdeel1_1 & " " & Stockdeel2_1 & " " & PartNumber2 & vbCrLf &
	"Stap 1: Verander het tekeningnummer van het nieuwste part" & vbCrLf &
	"Stap 2: Laat de Titelblok rule lopen" & vbCrLf & " " & vbCrLf & "WIL JE DE RULE BEEINDIGEN ?!", "Dubbele waarde Stock Number", MessageBoxButtons.YesNo, MessageBoxIcon.Error)
	If question2 = vbYes Then	
		Exit Sub
	Else If question2 = vbNo Then
		MessageBox.Show("Rule loopt gewoon door", "LET OP !!",MessageBoxButtons.OK, MessageBoxIcon.Warning)
	End If 
	End If
	End If
	
	Next

End If

Next




'Updaten mass van assembly (door het accessen van de mass wordt hij ook geupdate)
mass = iProperties.Mass


'BOM Publisher
oDoc = ThisDoc.ModelDocument
If oDoc.DocumentType = kPartDocumentObject Then
MessageBox.Show("You need to be in an Assembly to Export a BOM", "Databar: iLogic - BOM Publisher")
Return
End If
oDoc = ThisApplication.ActiveDocument
'Dim oBOM As BOM
oBOM = oDoc.ComponentDefinition.BOM
'Options.Value("Author") = iProperties.Value("Summary", "Author")
'==========================================================================================

'STRUCTURED BoM ===========================================================================
oBOM.ImportBOMCustomization("T:\01 ALGEMEEN\Inventor 2018\Templates\Bill of Materials.xml")
Try
' the structured view to 'all levels'
oBOM.StructuredViewFirstLevelOnly = False
Catch
	MsgBox("Handmatig aanpassen van de kolommen en opslaan BOM")
End Try
' Make sure that the structured view is enabled.
oBOM.StructuredViewEnabled = True
Dim oStructuredBOMView As BOMView
oStructuredBOMView = oBOM.BOMViews.Item("Structured")
' Export the BOM view to an Excel file

oStructuredBOMView.Export(ThisDoc.Path + "\" + iProperties.Value("Project", "Stock Number") + ".xlsx", kMicrosoftExcelFormat, "BOM")

'==========================================================================================

'PARTS ONLY================================================================================
oBOM.PartsOnlyViewEnabled = True
'==========================================================================================

'TECUMA BOM================================================================================
filename=iProperties.Value("Project", "Stock Number")

 

0 Likes
Message 4 of 9

WCrihfield
Mentor
Mentor

Wow, that was longer and more complex that I thought it was going to be.  So basically all I've done so far is just slightly condensed it and structured it logically with tabs, so it's a bit easier to look at and follow.

I attached the resulting code in a simple text file, just because it is still so long.

What version of Inventor are you using?

If you are using Inventor 2022: 1) Are there ModelStates involved?  2) Is the main 'active' assembly set to a ModelState other than "Master" when you start/run the code?

Are there any 'Virtual' components?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 9

wouter
Advocate
Advocate

What version of Inventor are you using?

2022.2

If you are using Inventor 2022: 1) Are there ModelStates involved?  2)

Yes

Is the main 'active' assembly set to a ModelState other than "Master" when you start/run the code?

In this case yes

Are there any 'Virtual' components?

No

 

The rule works fine when model state is set on master but in this case i have multiple configuration of a product using LOD which are now Modelstates (i did update this as well).

 

I've also tested a simple piece of code to see if the specific function works there but it also gives an error. (when not in master model state)

 

Hope this helps

0 Likes
Message 6 of 9

WCrihfield
Mentor
Mentor
Accepted solution

OK.  This morning I revisited this, and combed through the code a bit closer, and I think I may have been able to help a bit more this time.  The main change is right near the start of the code, where I make sure we are dealing with the 'Factory Document', even if it is currently set to another model state (other than "Master"), without actually changing the current ModelState.  I also further condensed a couple things, and added a couple variable type definitions in there for the variables that store the values of iProperties.  Because the value returned from an iProperty is a generic Object, and it is usually beneficial to pre-define the variable's type, if you know what type it will be.  It is usually also good practice to check if an iProperty returned a value or if it was empty, if that might possibly cause problems down the line.  There are also a few places within your code where you are using the key phrase 'Exit Sub', that you may want to change.  In those locations, I might suggest something like 'Continue For' (skips to next item in loop), or 'Exit For' (exits the loop), instead of exiting the whole rule.

Anyways, attached is the text file of the resulting iLogic rule code of that suggested edit that you can look over.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 9

wouter
Advocate
Advocate

This worked like a charm! Thank you very much. I guess FactoryDocument is new for inventor 2022? Otherwise it's new me. I'm still working on 2 things now.

 

1. The bom shows components with a quantity of 0. Why anyone would want this is beyond me. 

But i'm still reading into this.

https://forums.autodesk.com/t5/inventor-forum/hide-quot-0-quot-quantity-items-in-parts-list/td-p/102...

 

2. When i export the bom via ilogic it generates a quantity column for each model state but i only want the current model state. When i export it manually, it only shows 1 column for quantity. Which is what i want via ilogic.

 

Any tips are welcome.

Message 8 of 9

Maxim-CADman77
Advisor
Advisor

@WCrihfield 

I'm getting Error "Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))" in Line 12 when try to run your version of rule on "iLogic" Model State of PlaySetGA.iam (the model is available here).

Line12-Error.png

What I'm missing?

Please vote for Inventor-Idea Text Search within Option Names

0 Likes
Message 9 of 9

WCrihfield
Mentor
Mentor

Hi @Maxim-CADman77.  I was not able to download or open the Inventor assembly you mentioned, because we have a company security policy to not download ZIP,  RAR (and similar file type) files from sources like social media or forums.  But after looking at Line 12 of that code, I can see that is where it is attempting to enable the Parts Only BOMView, and I can tell that would be the first 'change' being attempted to the Document.  Since it is encountering an error with that simple line, I can only conclude that it is trying to make a change to a ModelState 'member' document, instead of the ModelState 'factory' document, because ModelState member's are generally ReadOnly.  Today, I would have simply replaced those first 7 lines of code there with these two lines:

 

If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then Exit Sub
Dim oAssyDoc As AssemblyDocument = ThisDoc.FactoryDocument

 

...and I would have fully declared both the oAssyDef and oBOM variables, but at that time I believe I was trying to preserve as much of the poster's original code as possible.  You may have also noticed the 4 places where I am suggesting that the phrase 'Exit Sub' should probably be replaced with 'Continue For' (skip to next) or 'Exit For' (exit the loop).  Plus, at that moment in time, I likely did not yet have the level of understanding about ModelStates that I have today, which is probably why I made the mistake of checking if the assembly was currently set to the 'Master' ModelState.  That would not have mattered.  The factory document is simply the one associated with the active ModelState.  If the file does not have at least 2 ModelStates, then there will not be a factory document, just the one main document for the file.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes