Create an array list of Part Number iProperties from reference model

Create an array list of Part Number iProperties from reference model

mikejones
Collaborator Collaborator
1,364 Views
21 Replies
Message 1 of 22

Create an array list of Part Number iProperties from reference model

mikejones
Collaborator
Collaborator

Hi all.

 

I want to create an array that will store the iProperty Partnumber of each component in a reference model. The reference model is defined as the model used in a drawing view and the rule will be run from the drawing document.

Any help will be appreciated on this one.

 

Mike

Autodesk Certified Professional
0 Likes
Accepted solutions (1)
1,365 Views
21 Replies
Replies (21)
Message 21 of 22

WCrihfield
Mentor
Mentor

@mikejones That sounds like a reasonable/logical assumption.  I took a closer look at both the BOMStructure filtering and that recursive Sub routine's code, looking for something too.  I came to a similar conclusion too.  Try replacing that recursive Sub routine with this slightly altered version code.

	Sub RecursivelyProcessComponents(oComps As ComponentOccurrences)
		If IsNothing(oComps) OrElse oComps.Count = 0 Then Exit Sub
		For Each oComp As ComponentOccurrence In oComps
			Dim ProcessFurther As Boolean = ProcessComponent(oComp) 'run our other routine
			If ProcessFurther = True Then
				If oComp.SubOccurrences.Count > 0 Then
					RecursivelyProcessComponents(oComp.SubOccurrences)
				End If
			End If
		Next
	End Sub

Clearly 'Exit Sub' was too harsh there.  'Continue For' might have been a better choice, but this should have a similar effect, and is easy on the eyes.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 22 of 22

mikejones
Collaborator
Collaborator

@WCrihfield that together with an additional little tweak appears so far to have done the trick.

I added in an extra If Then statement within the Function Statement that only adds the Part Number iProperty if the oComp.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject therefore not adding any Sub Assembly files to the list. I'll give it a good tryout tomorrow but hopefully, that's sorted it.

 

Thanks again 

 

Mike

 

Class ThisRule
	Dim oDocs As List(Of Document) 'shared with all routines
	Dim oPNList As List(Of String) 'shared with all routines
	Sub Main
		If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
			MsgBox("A Drawing document must be active for this code to work. Exiting.", vbCritical, "")
			Exit Sub
		End If
		Dim oDDoc As DrawingDocument = ThisDoc.Document
		Dim oViews As DrawingViews = oDDoc.ActiveSheet.DrawingViews
		If oViews.Count = 0 Then Exit Sub 'may want a feedback message or Log entry here.
		Dim oBV As DrawingView
		For Each oView As DrawingView In oViews
			If oView.ViewType = DrawingViewTypeEnum.kStandardDrawingViewType Then
				oBV = oView : Exit For
			End If
		Next 'oView
		If IsNothing(oBV) Then Exit Sub 'may want a feedback message or Log entry here.
		Dim oADoc As AssemblyDocument = oBV.ReferencedDocumentDescriptor.ReferencedDocument
		Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
		oDocs = New List(Of Document) 'to initiate it
		oPNList = New List(Of String) 'to initiate it
		RecursivelyProcessComponents(oOccs)
		
		oMyPN = InputListBox("List Count " & oPNList.Count, oPNList, "", "Part Numbers")
	End Sub

Sub RecursivelyProcessComponents(oComps As ComponentOccurrences)
		If IsNothing(oComps) OrElse oComps.Count = 0 Then Exit Sub
		For Each oComp As ComponentOccurrence In oComps
			Dim ProcessFurther As Boolean = ProcessComponent(oComp) 'run our other routine
			If ProcessFurther = True Then
				If oComp.SubOccurrences.Count > 0 Then
					RecursivelyProcessComponents(oComp.SubOccurrences)
				End If
			End If
		Next
	End Sub
	
	Function ProcessComponent(oComp As ComponentOccurrence) As Boolean
		If IsNothing(oComp) OrElse oComp.Suppressed Then Return False
		'filter by BOMStructure values
		'Note:  BOMStructure of component can be different than its referenced document
		If oComp.BOMStructure <> BOMStructureEnum.kNormalBOMStructure And _
			oComp.BOMStructure <> BOMStructureEnum.kInseparableBOMStructure Then
			Return False
		End If
		If oComp.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			If oComp.Definition.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then Return False
		ElseIf oComp.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then
			If oComp.Definition.BOMStructure <> BOMStructureEnum.kNormalBOMStructure Then Return False
		End If
		Dim oDoc As Document = oComp.Definition.Document
		If oDocs.Contains(oDoc) Then Return False 'do not process the same document again
		Dim SOP = StandardObjectFactory.Create(oDoc) 'Inventor 2021 or later only
		
		If oComp.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then
		Dim oPN As String = SOP.iProperties.Value("Project", "Part Number")
		oPNList.Add(oPN)
		oDocs.Add(oDoc)
		End If
	
		Return True 'return True, so it will process any sub occurrances of this component
	End Function
End Class

 

Autodesk Certified Professional
0 Likes