Public Sub SetChildComponentCustomProperty(ByVal propertyName As String, ByVal ParentPropertySet As String, ByVal ParentPropetyName As String, ByVal ForceOverwrite As Boolean, ByVal parentDoc As Inventor.Document, Optional ByRef comparer As IComparer = Nothing)
For Each occ As ComponentOccurrence In parentDoc.ComponentDefinition.Occurrences
If occ.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject OrElse occ.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then
Dim childDoc As Inventor.Document = occ.Definition.Document
Dim childPartNo As String = childDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
If IsEngineeredPart(childPartNo) = True Then 'dont process children of assemblies that are not engineered files (no library, or components)
If childDoc IsNot parentDoc Then
If comparer IsNot Nothing Then
If comparer.Compare(childDoc, parentDoc) = 1 Then
WriteCustomProperty(propertyName, parentDoc.PropertySets.Item(ParentPropertySet).Item(ParentPropetyName).Value, childDoc, ForceOverwrite)
End If
Else
WriteCustomProperty(propertyName, parentDoc.PropertySets.Item(ParentPropertySet).Item(ParentPropetyName).Value, childDoc, ForceOverwrite)
End If
If occ.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
SetChildComponentCustomProperty(propertyName, ParentPropertySet, ParentPropetyName, ForceOverwrite, childDoc, comparer)
End If
End If
End If
End If
Next
End Sub
This routine is the completion of my walking down your similar road. We call it "Next Assembly" in our industry. I needed 3 sub routines to make this work.
1. IsEngineeredPart - simply weeds out part numbers that do not conform to our document standard. This prevents the program from diving into vendor assemblies we don't care to see a list of part1, part2 from the vendor.
2. Comparer - I created a custom comparer to determine if a part is to be written with the property or not. In your scenario, you may not need a comparer. We checked for needing to bump the next assembly value to a different assembly (because it was drawn first), or discard writing because it was not an Engineered Part, and different minor logic decisions. The comparer only needs to answer 0 or 1 for the program to write or not write. I used a comparer because it can be called on to sort the list, or just answer a single question.
3. WriteCustomProperty - this is how we choose to capture the value within each file's custom iProperty. You would want a different approach, such as creating your own class (more later).
Otherwise here is the basics of this self recurring crawler:
Assembly (top at top of tree), has children assemblies and parts (as component occurrences). I loop through all the occurrences.
Then I ask if each occurrence is a part or assembly.
If part, then I do my thing (capture the parent assembly number) then quit.
If assembly, I do my thing, and then run the same routine on that lower level assembly.
You can create a simple class to capture the information you want and hold it in memory until you write it to excel.
Public Class BOMExcelItem 'I made that up
Public Property Document as Inventor.Document 'keep track of the actual data to be written to a single row
Public Property PartNumber as string 'quick reference to the above document's part number
Public Property ParentPartNumber as string 'reference to the parent document's part number
Public Property ParentDocument as Inventor.Document 'keep track of the parent document
Public Property ItemNo as string 'this is the BOM item number (1.1.1.1.1) you can later sort your excel file by this to reorganize the tree.
'So instead of WriteCustomProperty, you could create a new instance of this 'helper class' and put the known objects into it.
Public Sub New(Child, Parent, ItemNo)
Document = Child
ParentDocument = Parent
'get part numbers and such
End Class
Then add them to a list of your own
dim BOMList as list(of BOMExcelItem)
'crawl data
'found data - dim bei as new bomexcelitem(child,parent,item)
bomlist.add(bei)
That is likely the overall path you will want to take. I'm trying not to write out all the details, because it is your baby to raise up. Happy parenting.
Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/