How to use commandmanager on all non referenced parts (BOM structure) of an assembly?

How to use commandmanager on all non referenced parts (BOM structure) of an assembly?

Jakob_Hamburg
Advocate Advocate
592 Views
7 Replies
Message 1 of 8

How to use commandmanager on all non referenced parts (BOM structure) of an assembly?

Jakob_Hamburg
Advocate
Advocate

Hello,

I am struggling how to use commandmanager on all non referenced parts (BOM structure) of an assembly.

I want to open all parts of an assembly and do an "operation" with commandmanager, but only on those parts that are not set to reference (in the assembly BOM structure)  and also their iproperty "part number" should start with "AAA".

 

The "operation" with commandmanager is to activate a third party add-inn on the specific part, but that just as an information why I want to open the parts.

 

My problem is checking the "If" condition about the BOM structure. (in line 11). I am doing it wrong. I am obviously checking the  "BOM structure" of the part itself and not which "BOM structure" (default, not set to reference) the part has in the assembly.

 

Can anybody give me an advice how to improve my code?

 

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	For Each oRefDoc As Document In oADoc.AllReferencedDocuments
		Dim partnumber As String = oRefDoc.PropertySets(3)("Part Number").Value
		
		If Left(partnumber, 3) = "AAA" And 
		oRefDoc.ComponentDefinition.Bomstructure = BOMStructureEnum.kDefaultBOMStructure Then
		
		oPartPath = oRefDoc.FullFileName
		' Open the target part
	ThisApplication.Documents.Open(oPartPath)
	
	oRefDoc.Activate
		
		
			Call ThisApplication.CommandManager.ControlDefinitions.Item("3rd_party_add-inn").Execute	 
			oRefDoc.Close
		
		End If
	Next
End Sub

 

0 Likes
Accepted solutions (1)
593 Views
7 Replies
Replies (7)
Message 2 of 8

Andrii_Humeniuk
Advisor
Advisor

Hi @Jakob_Hamburg . Please try this code:

Sub Main
	Dim oApp As Inventor.Application = ThisApplication
	Dim oDoc As Document = ThisDoc.Document
	If oDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = oDoc
	For Each oRefDoc As Document In oADoc.AllReferencedDocuments
		If Not TypeOf oRefDoc Is PartDocument Then Continue For
		Dim oPartDoc As PartDocument = oRefDoc
		Dim partnumber As String = oPartDoc.PropertySets(3)("Part Number").Value
		
		If Left(partnumber, 3) = "AAA" AndAlso 
			oPartDoc.ComponentDefinition.BOMStructure <> BOMStructureEnum.kReferenceBOMStructure Then
		
		oPartDoc = oApp.Documents.Open(oPartDoc.FullFileName)
		
		oPartDoc.Activate()	
		
		Call oApp.CommandManager.ControlDefinitions.Item("3rd_party_add-inn").Execute()
		oPartDoc.Save()
		oPartDoc.Close()
		
		End If
	Next
End Sub

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 3 of 8

Jakob_Hamburg
Advocate
Advocate

Thank you Andrii,

 

but the behaviour stays the same.

The code is unfortunately still checking the  "BOM structure" of the part itself and not which "BOM structure" (default, not set to reference) the part has in the assembly.

 

0 Likes
Message 4 of 8

Andrii_Humeniuk
Advisor
Advisor

Not sure I understand how a part can have two different BOM structures. Here is the code that reads the BOM structure of the part in the "Model Data" BOM. If this is not what you expect, please display your question with screenshots. Thank you.

Sub Main
	Dim oApp As Inventor.Application = ThisApplication
	Dim oDoc As Document = ThisDoc.Document
	If oDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = oDoc
	Dim oBOM As BOMView = oADoc.ComponentDefinition.BOM.BOMViews(1)
	Call SearchReference(oApp, oBOM.BOMRows)
End Sub

Private Function SearchReference(oApp As Inventor.Application, ByVal oRows As BOMRowsEnumerator)
	For i As Integer = 1 To oRows.Count
		Dim oCompDef As ComponentDefinition = oRows(i).ComponentDefinitions(1)
		If Not TypeOf oCompDef.Document Is PartDocument Then Continue For
		Dim oCompDoc As PartDocument = oCompDef.Document
		If oRows(i).BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then
			Dim partnumber As String = oCompDoc.PropertySets(3)("Part Number").Value
			If Not Left(partnumber, 3) = "AAA" Then Continue For
			oPartDoc = oApp.Documents.Open(oCompDoc.FullFileName)
			oPartDoc.Activate()
			Call oApp.CommandManager.ControlDefinitions.Item("3rd_party_add-inn").Execute()
			oPartDoc.Save()
			oPartDoc.Close()
		End If
		If oRows(i).ChildRows IsNot Nothing Then Call SearchReference(oApp, oRows(i).ChildRows)
	Next i
End Function

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 5 of 8

Jakob_Hamburg
Advocate
Advocate

Unfortunately the code did not help.

I will try to explain "how a part can have two different BOM structures".

 

A part (.ipt) can have a certain "Bom Structure" set, which is then the parts default BOM structure.

Jakob_Hamburg_0-1692345172145.png

 

 

In the assembly level that part can be either in it's default BOM structure or set to "reference":

Jakob_Hamburg_1-1692344778745.png

(The reason is that a part can be in different assemblies and in one assembly it can be set to reference but in another assembly it has its default BOM structure (for example "normal")

 

0 Likes
Message 6 of 8

Jakob_Hamburg
Advocate
Advocate

.

0 Likes
Message 7 of 8

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

I finally understood you. I had no idea that it was possible to do that. Here is the code that should help you, pay attention to line 17.

 

 

Sub Main
	Dim oApp As Inventor.Application = ThisApplication
	Dim oDoc As Document = ThisDoc.Document
	If oDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = oDoc
	Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
	Dim oBOM As BOMView = oADoc.ComponentDefinition.BOM.BOMViews(1)
	Call SearchReference(oApp, oOccs)
End Sub

Private Function SearchReference(oApp As Inventor.Application, ByVal oOccs As ComponentOccurrences)
	For Each oOcc As ComponentOccurrence In oOccs
		If oOcc.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then
			If oOcc.BOMStructure <> BOMStructureEnum.kReferenceBOMStructure Then Continue For
			Dim oCompDoc As PartDocument = oOcc.Definition.Document
			Dim partnumber As String = oCompDoc.PropertySets(3)("Part Number").Value
			If Not Left(partnumber, 3) = "AAA" Then Continue For
			oPartDoc = oApp.Documents.Open(oCompDoc.FullFileName)
			oPartDoc.Activate()
			Call oApp.CommandManager.ControlDefinitions.Item("3rd_party_add-inn").Execute()
			oPartDoc.Save()
			oPartDoc.Close()
		Else If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			Dim oSubOccs As ComponentOccurrencesEnumerator			
			Try : oSubOccs = oOcc.SubOccurrences : Catch : Continue For : End Try
			Call SearchReference(oApp, oSubOccs)
		End If
	Next
End Function

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 8 of 8

Jakob_Hamburg
Advocate
Advocate

Thank you very much for your help!

Now it is working.

 

 

First the code worked on the "set to reference" parts, but I want it to work on the "default BOM structure" parts, so I added a "not" in  line  17 :

If not oOcc.BOMStructure <> BOMStructureEnum.kReferenceBOMStructure Then Continue For

But that is just tiny adjustment.

Another information also for other users:

A referenced document of an assembly can appear as many "Occurences" .

(Like a table has 4 legs)

The code will open all occurrences, which might be in other situations a problem.

In my case it is not an issue, because that 3rd party add-inn which I start with the commandmanager also changes the Part number...so in the next iteration the occurrence will be skipped because of the part number anyway.

 

0 Likes