Ilogic Count all Normal components in Assembly

Ilogic Count all Normal components in Assembly

j.romo
Advocate Advocate
4,867 Views
13 Replies
Message 1 of 14

Ilogic Count all Normal components in Assembly

j.romo
Advocate
Advocate

I couldnt find anything like this in the forum.

I need to know how many total Normal parts are in the master assembly, including sub assemblie components, to give me a total number of NORMAL parts, wich we manufacture in our shop, so we can calculate the manufacturing progress vs the total number of parts it can be a custom parameter that can be filled in the master assembly.

Thanks in advance

PD I dont even know how to start this. Man Sad

Accepted solutions (2)
4,868 Views
13 Replies
Replies (13)
Message 2 of 14

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

@j.room,

 

Hoping that below iLogic code would help to get number of parts in Assembly(Includes all sub assembly).

 

 

Sub Main() 
    ' Get the active assembly. 
    Dim oAsmDoc As AssemblyDocument 
    oAsmDoc = ThisApplication.ActiveDocument 

    ' Get the assembly component definition. 
    Dim oAsmDef As AssemblyComponentDefinition 
    oAsmDef = oAsmDoc.ComponentDefinition 

    ' Get all of the leaf occurrences of the assembly. 
    Dim oLeafOccs As ComponentOccurrencesEnumerator 
    oLeafOccs = oAsmDef.Occurrences.AllLeafOccurrences 

    ' Iterate through the occurrences and print the name. 
	Dim cnt As Integer 
	cnt = 0
	
    Dim oOcc As ComponentOccurrence 
    For Each oOcc In oLeafOccs 
        If oOcc.BomStructure = BOMStructureEnum.kNormalBOMStructure Then
			cnt = cnt + 1
			MessageBox.Show(oOcc.Name) 
		End If
			 
    Next 
	MessageBox.Show(cnt)
End Sub

For more details on accessing assembly components, refer the below blog article link.

 

 

http://modthemachine.typepad.com/my_weblog/2009/03/accessing-assembly-components.html

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 3 of 14

AlexFielder
Advisor
Advisor
Accepted solution

I have this old rule (linked in case I change/update it for any future reader) which does this as well as adding it to a custom iProperty:

(My rule doesn't filter by "Normal BOM state" however but this could be accomplished by combining with @chandra.shekar.g's post!)

Imports Inventor
Imports System.IO
Public Sub Main
If TypeOf ThisDoc.Document Is PartDocument Then
PartFeatureCount(ThisDoc)
Else
RunFeatureCount(ThisDoc.Document)
End If
MessageBox.Show("Done!")
End Sub

Public Sub RunFeatureCount(ByVal oDoc as Inventor.Document)
	Dim oAssy As inventor.AssemblyDocument
	Dim oSubDoc as Inventor.Document
	If oDoc.DocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
		oAssy = CType(oDoc,AssemblyDocument)
		AssemblyFeatureCount(oAssy)
		For Each oComp In oAssy.ReferencedDocuments
			'oSubDoc = CType(oComp.Definition.Document,Document)
			'MessageBox.Show(oSubDoc.File.FullFileName)
			If oComp.DocumentType = Inventor.DocumentTypeEnum.kAssemblyDocumentObject Then
				'run FeatureCount and then call RunFeatureCount to recurse the assembly structure
				'iLogicVb.RunExternalRule("FEATURECOUNT")
				FeatureCount(oComp)
				RunFeatureCount(oComp)
			Else
				'run FeatureCount
				'iLogicVb.RunExternalRule("FEATURECOUNT")
				FeatureCount(oComp)
			End If
		Next
	ElseIf oDoc.DocumentType = Inventor.DocumentTypeEnum.kPartDocumentObject Then
		FeatureCount(oDoc)
	End If
End Sub

Sub FeatureCount(ByVal oDoc as Inventor.Document)
	
	Try
	Dim SaveRequired As Boolean = False
	
	'uncomment for debugging purposes!
	'MessageBox.Show(DocName)
		If TypeOf oDoc Is PartDocument Then
			If Not oDoc.File.FullFileName.Contains("Content") And Not oDoc.File.fullfilename.contains("FACILITY") And Not oDoc.File.fullfilename.contains("Imported Components") Then 'skip CC and FACILITY files
				Dim PartDocName As String = System.IO.Path.GetFileNameWithoutExtension(oDoc.DisplayName) & ":1"
				Dim oFeats as PartFeatures = oDoc.ComponentDefinition.Features
				Dim oParams as Parameters = oDoc.ComponentDefinition.Parameters
				Try
					If Not iProperties.Value(PartDocName,"Custom", "FEATURECOUNT") = oFeats.Count Then 'or update it
						iProperties.Value(PartDocName,"Custom", "FEATURECOUNT") = oFeats.Count
						SaveRequired = True
					End If
					'MessageBox.Show("Feature Count for this part is: " & oFeats.Count, "FEATURECOUNT")
					If Not iProperties.Value(PartDocName,"Custom","PARAMETERCOUNT") = oParams.Count Then 'or update it
						iProperties.Value(PartDocName,"Custom","PARAMETERCOUNT") = oParams.Count
						SaveRequired = True
					End If
					'MessageBox.Show("Parameter Count for " & oDoc.File.fullfilename &" is: " & oParams.Count, "PARAMETERCOUNT")
					If SaveRequired Then
						'oDoc.Save 'try to save the file.
					End If
				Catch
					iProperties.Value(PartDocName,"Custom", "FEATURECOUNT") = oFeats.Count
					iProperties.Value(PartDocName,"Custom","PARAMETERCOUNT") = oParams.Count
					'oDoc.Save 'try to save the file.
					Exit Sub
				End Try
			End If
		ElseIf TypeOf oDoc Is AssemblyDocument Then
			If Not oDoc.File.FullFileName.Contains("Content") And Not oDoc.File.fullfilename.contains("FACILITY") And Not oDoc.File.fullfilename.contains("Imported Components") Then
				Dim DocName As String = System.IO.Path.GetFileNameWithoutExtension(oDoc.DisplayName) & ":1"
				Dim oFeats as Features = oDoc.ComponentDefinition.Features
				Dim Occs as ComponentOccurrences = oDoc.ComponentDefinition.Occurrences			
				Dim oParams as Parameters = oDoc.ComponentDefinition.Parameters
				'Dim oConstraints as Constraints = oDoc.ComponentDefinition.Constraints
				Try
					If Not iProperties.Value(DocName,"Custom", "FEATURECOUNT") = oFeats.Count Then
						iProperties.Value(DocName,"Custom", "FEATURECOUNT") = oFeats.Count
						SaveRequired = True
					End If
					'MessageBox.Show("Feature Count for this assembly is: " & oFeats.Count, "FEATURECOUNT")
					If Not iProperties.Value(DocName, "Custom", "OCCURRENCECOUNT") = Occs.Count Then
						iProperties.Value(DocName, "Custom", "OCCURRENCECOUNT") = Occs.Count
						SaveRequired = True
					End If
					'MessageBox.Show("Occurrence Count for " & oDoc.File.fullfilename & " is: " & Occs.Count, "OCCURRENCECOUNT")
					If Not iProperties.Value(DocName, "Custom","PARAMETERCOUNT") = oParams.Count Then
						iProperties.Value(DocName, "Custom", "PARAMETERCOUNT") = oParams.Count
						SaveRequired = True
					End If
					'MessageBox.Show("Parameter Count for this part is: " & oDoc.ComponentDefinition.Constraints.Count, "PARAMETERCOUNT")
					If Not iProperties.Value(DocName, "Custom","CONSTRAINTCOUNT") = oDoc.ComponentDefinition.Constraints.Count Then
						iProperties.Value(DocName,"Custom", "CONSTRAINTCOUNT") = oDoc.ComponentDefinition.Constraints.Count
						SaveRequired = True
					End If
					'MessageBox.Show("Constraint Count for Assembly " & DocName & " is: " & oDoc.ComponentDefinition.Constraints.Count, "CONSTRAINTCOUNT")
					If SaveRequired Then
						'oDoc.Save 'try to save the file.
					End If
				Catch
					'creates any missing iProperties.
					iProperties.Value(DocName,"Custom", "FEATURECOUNT") = oFeats.Count
					iProperties.Value(DocName, "Custom", "OCCURRENCECOUNT") = Occs.Count
					iProperties.Value(DocName, "Custom", "PARAMETERCOUNT") = oParams.Count
					iProperties.Value(DocName,"Custom", "CONSTRAINTCOUNT") = oDoc.ComponentDefinition.Constraints.Count
					'oDoc.Save 'saves the assembly
					Exit Sub
				End Try
			End If
		End If
	Catch ex As Exception
		'MessageBox.Show("The error is: " & ex.Message & vbCrLf & ex.StackTrace,oDoc.DisplayName)
	End Try
	
End Sub
	Dim SaveRequired As Boolean = False
Sub PartFeatureCount(ByVal oDoc)
	Try
		If Not oDoc.File.FullFileName.Contains("Content") And Not oDoc.File.fullfilename.contains("FACILITY") And Not oDoc.File.fullfilename.contains("Imported Components") Then
			Dim oFeats as PartFeatures = oDoc.Document.ComponentDefinition.Features
			Dim oParams as Parameters = oDoc.Document.ComponentDefinition.Parameters
			Try
				'may need to save the file when we're done, hence the boolean check
				If Not iProperties.Value("Custom", "FEATURECOUNT") = oFeats.Count Then
					iProperties.Value("Custom", "FEATURECOUNT") = oFeats.Count
					SaveRequired = True
				End If
				'MessageBox.Show("Feature Count for this part is: " & oFeats.Count, "FEATURECOUNT")
		
				If Not iProperties.Value("Custom","PARAMETERCOUNT") = oParams.Count Then
					iProperties.Value("Custom","PARAMETERCOUNT") = oParams.Count
					SaveRequired = True
				End If
				'MessageBox.Show("Parameter Count for " & oDoc.Document.File.fullfilename &" is: " & oParams.Count, "PARAMETERCOUNT")
				If SaveRequired Then
					'oDoc.Save 'try to save the file.
				End If
			Catch
				'definitely need to save the file!
				iProperties.Value("Custom", "FEATURECOUNT") = oFeats.Count
				iProperties.Value("Custom","PARAMETERCOUNT") = oParams.Count
				'oDoc.Save 'try to save the file.
				'oDoc.Close 'try to close the file - on a vaulted file this will fire the check-in dialogue.
			End Try
		End If
	Catch ex As Exception
		'MessageBox.Show("The error is: " & ex.Message & vbCrLf & ex.StackTrace)
	End Try
	
End Sub

Sub AssemblyFeatureCount(ByVal oDoc)
	Try
		Dim SaveRequired As Boolean = False
		'Dim DocName As String = oDoc.DisplayName
		If Not oDoc.File.FullFileName.Contains("Content") And Not oDoc.File.fullfilename.contains("FACILITY") And Not oDoc.File.fullfilename.contains("Imported Components") Then
			Dim oFeats as Features = oDoc.ComponentDefinition.Features
			Dim Occs as ComponentOccurrences = oDoc.ComponentDefinition.Occurrences			
			Dim oParams as Parameters = oDoc.ComponentDefinition.Parameters
			'Dim oConstraints as Constraints = oDoc.ComponentDefinition.Constraints
			Try
				If Not iProperties.Value("Custom", "FEATURECOUNT") = oFeats.Count Then
					iProperties.Value("Custom", "FEATURECOUNT") = oFeats.Count
					SaveRequired = True
				End If
				'MessageBox.Show("Feature Count for this assembly is: " & oFeats.Count, "FEATURECOUNT")
				If Not iProperties.Value("Custom", "OCCURRENCECOUNT") = Occs.Count Then
					iProperties.Value("Custom", "OCCURRENCECOUNT") = Occs.Count
					SaveRequired = True
				End If
				'MessageBox.Show("Occurrence Count for " & oDoc.File.fullfilename & " is: " & Occs.Count, "OCCURRENCECOUNT")
				If Not iProperties.Value("Custom","PARAMETERCOUNT") = oParams.Count Then
					iProperties.Value("Custom", "PARAMETERCOUNT") = oParams.Count
					SaveRequired = True
				End If
				'MessageBox.Show("Parameter Count for this part is: " & oDoc.ComponentDefinition.Constraints.Count, "PARAMETERCOUNT")
				If Not iProperties.Value("Custom","CONSTRAINTCOUNT") = oDoc.ComponentDefinition.Constraints.Count Then
					iProperties.Value("Custom", "CONSTRAINTCOUNT") = oDoc.ComponentDefinition.Constraints.Count
					SaveRequired = True
				End If
				'MessageBox.Show("Constraint Count for Assembly " & DocName & " is: " & oDoc.ComponentDefinition.Constraints.Count, "CONSTRAINTCOUNT")
				If SaveRequired Then
					'oDoc.Save 'try to save the file.
				End If
			Catch
				'creates any missing iProperties.
				iProperties.Value("Custom", "FEATURECOUNT") = oFeats.Count
				iProperties.Value("Custom", "OCCURRENCECOUNT") = Occs.Count
				iProperties.Value("Custom", "PARAMETERCOUNT") = oParams.Count
				iProperties.Value("Custom", "CONSTRAINTCOUNT") = oDoc.ComponentDefinition.Constraints.Count
				'oDoc.Save 'saves the assembly
				Exit Sub
			End Try
		End If
	Catch ex As Exception
		'MessageBox.Show("The error is: " & ex.Message & vbCrLf & ex.StackTrace)
	End Try
End Sub

🙂

Message 4 of 14

j.romo
Advocate
Advocate

Thanks Guys thats exactly what i needed....

Kudos for both

0 Likes
Message 5 of 14

RoyWickrama_RWEI
Advisor
Advisor

Hi Chandra.Shekar;

 

In the process of scanning all occurrences,  I am currently interested in retrieving the parent child relationship of all the documents is an assembly. For example, it will have a name Parent1_p2300 for part p2300  (at 1st time occurence detected). If p2300 exists as a chiled in another assembly then variable Parent2_p2300. Parent3_p2300, Parent4_p2300 etcwill be created as required.

 

Could you provide me with a clue to retrieve this information.

I will provide you with a sample code to better illustrate my need.

 

Autoesk Inventor Professional 2019

Vault Professional 2019

0 Likes
Message 6 of 14

chandra.shekar.g
Autodesk Support
Autodesk Support

@RoyWickrama_RWEI,

 

Can you please provide sample assembly to illustrate parent - child relationship? please make sure that files are non confidential.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 7 of 14

RoyWickrama_RWEI
Advisor
Advisor

Thanks for the reply. I am not able to get around with my need. Finally, posted in the forum help.

 

https://forums.autodesk.com/t5/forums/postpage/choose-node/true/interaction-style/forum?interaction_...

 

Please try to help.

Thanks.

0 Likes
Message 8 of 14

RoyWickrama_RWEI
Advisor
Advisor

Hi Chandra;

 

Can we assign this quantity to the custom property, QTYT (for quantity Total)

 

Thanks.

 

0 Likes
Message 9 of 14

chandra.shekar.g
Autodesk Support
Autodesk Support

@RoyWickrama_RWEI,

 

Yes, you can assign total quantity to any custom property.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 10 of 14

RoyWickrama_RWEI
Advisor
Advisor

Could you provide me with the lines of code, please.

0 Likes
Message 11 of 14

chandra.shekar.g
Autodesk Support
Autodesk Support

@RoyWickrama_RWEI,

 

Try below iLogic code.

Sub Main() 
    ' Get the active assembly. 
    Dim oAsmDoc As AssemblyDocument 
    oAsmDoc = ThisApplication.ActiveDocument 

    ' Get the assembly component definition. 
    Dim oAsmDef As AssemblyComponentDefinition 
    oAsmDef = oAsmDoc.ComponentDefinition 

    ' Get all of the leaf occurrences of the assembly. 
    Dim oLeafOccs As ComponentOccurrencesEnumerator 
    oLeafOccs = oAsmDef.Occurrences.AllLeafOccurrences 

    ' Iterate through the occurrences and print the name. 
	Dim cnt As Integer 
	cnt = 0
	
    Dim oOcc As ComponentOccurrence 
    For Each oOcc In oLeafOccs 
        If oOcc.BOMStructure = BOMStructureEnum.kNormalBOMStructure Then
			cnt = cnt + 1
			MessageBox.Show(oOcc.Name) 
		End If
			 
    Next 
	MessageBox.Show(cnt)
	
	Dim propertyName As String = "TotalQuantity"
	Dim propertyValue As String = cnt.ToString 

	customPropertySet = ThisDoc.Document.PropertySets.Item("Inventor User Defined Properties")
	Try
		prop = customPropertySet.Item(propertyName)
	Catch
	' Assume error means not found
		customPropertySet.Add("", propertyName)
	End Try

	iProperties.Value("Custom", propertyName) = propertyValue
End Sub

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 12 of 14

RoyWickrama_RWEI
Advisor
Advisor

Thanks a lot.

Great help.

0 Likes
Message 13 of 14

RoyWickrama_RWEI
Advisor
Advisor

Hi Chandra;

It does not go the occurrences: creates TotalQuantity in the main assembly.

Could you take a look at. Thanks.

 

0 Likes
Message 14 of 14

chandra.shekar.g
Autodesk Support
Autodesk Support

@RoyWickrama_RWEI,

 

Try below changes.

For Each oOcc In oAsmdef.Ocuurences
        If oOcc.BOMStructure = BOMStructureEnum.kNormalBOMStructure Then
			cnt = cnt + 1
			MessageBox.Show(oOcc.Name) 
		End If
			 
    Next 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network