Hello, I'm working on some code to count all the subcomponents in an assembly. I am mapping that value to the iProperty Authority. However, I want to skip files that have their BOM structures set to reference and I also want to skip subcomponents that have their parent sub assembly set to reference. Currently, the code is just skipping reference subcomponents, but will still write to the subcomponents of a reference sub assembly. Any help would be much appreciated. Thanks!
Dim openDoc As Document
openDoc = ThisDoc.Document
Dim docFile As Document
If openDoc.DocumentType = 12291 Then
For Each docFile In openDoc.AllReferencedDocuments
Dim FNamePos As Long
Dim docFName As String
FNamePos = InStrRev(docFile.FullFileName, "\", -1)
docFName = Mid(docFile.FullFileName, FNamePos + 1, Len(docFile.FullFileName) -FNamePos)
'This works on documents if reference, but still looks at subcomponents of a reference assembly
If docFile.ComponentDefinition.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then
Else
If docFile.IsModifiable = True Then
Dim assemblyDoc As AssemblyDocument
Dim assemblyDef As AssemblyComponentDefinition
Dim partQty As ComponentOccurrencesEnumerator
assemblyDoc = openDoc
assemblyDef = assemblyDoc.ComponentDefinition
partQty = assemblyDef.Occurrences.AllReferencedOccurrences(docFile)
If IsNumeric(partQty.Count) = True Then
If IsNumeric(iProperties.Value(docFName, "Project", "Authority")) = False Then
iProperties.Value(docFName, "Project", "Authority") = 0
End If
If partQty.Count <> iProperties.Value(docFName, "Project", "Authority") Then
iProperties.Value(docFName, "Project", "Authority") = partQty.Count
End If
End If
End If
End If
Next
End If
Solved! Go to Solution.
Solved by Salsa7. Go to Solution.
Solved by JelteDeJong. Go to Solution.
Hi @Salsa7,
If I understand correcly, you could instead use the assembly occurrence collection. This will loop through each top node in the browser tree and will not go through each sub assembly parts etc.. So if the main assembly in the assembly does not meet the right criteria, the code won't go deeper in this said assembly. Here is an exemple :
Dim Doc As Inventor.AssemblyDocument = ThisApplication.ActiveDocument Dim AssyCompDef As Inventor.AssemblyComponentDefinition = Doc.ComponentDefinition For Each Occ As Inventor.ComponentOccurrence In AssyCompDef.Occurrences Dim OccDoc As Inventor.Document = Occ.ReferencedDocumentDescriptor.ReferencedDocument Next
This would replace this line :
For Each docFile In openDoc.AllReferencedDocuments
Kind regards,
FINET L.
If this post solved your question, please kindly mark it as "Solution"
If this post helped out in any way to solve your question, please drop a "Like"Hey @FINET_Laurent I think you maybe on the right track by using the assembly occurrence. But, I get this error when following your instructions: Object reference not set to an instance of an object.
I think you need to go recursively through all documents. Something like this:
Sub Main()
Dim doc As AssemblyDocument = ThisDoc.Document
Dim mainOccs As ComponentOccurrences = doc.ComponentDefinition.Occurrences
SetAuthority(doc, mainOccs)
End Sub
Public Sub SetAuthority(doc As Document, mainOccs As ComponentOccurrences)
For Each refDoc As Document In doc.ReferencedDocuments
If (refDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kReferenceBOMStructure) Then Continue For
If (Not refDoc.IsModifiable) Then Continue For
Dim partQty As String = mainOccs.AllReferencedOccurrences(refDoc).Count().ToString()
Dim prop As [Property] = refDoc.PropertySets.Item("Design Tracking Properties").Item("Authority")
If (partQty <> prop.Value) Then
prop.Value = partQty
End If
SetAuthority(refDoc, mainOccs)
Next
End Sub
Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Blog: hjalte.nl - github.com
@JelteDeJong I left yours as a solution as it does traverse the assembly, skip references, and is written simpler. But, the way you and I are referencing the BOMStructure it looks directly into a part or assembly's individual document settings. So, if within it's document settings it is set to Normal, but in the assembly it is set to reference then it would not skip it. I made some changes the day before and forgot to update the post. The below code works as I had envisioned and doesn't have that issue.
Sub Main
' Get the active assembly.
Dim oAssyDoc As AssemblyDocument
oAssyDoc = ThisApplication.ActiveDocument
' Call the function
Call PartCount(oAssyDoc.ComponentDefinition.Occurrences)
End Sub
Private Sub PartCount(oOccs As ComponentOccurrences)
' Iterate through all of the occurrence in this collection
Dim oOcc As ComponentOccurrence
Dim openDoc As Document
openDoc = ThisDoc.Document
If openDoc.DocumentType = 12291 Then
For Each oOcc In oOccs
'This method for looking at BOMStructure replaced the below method
If oOcc.BOMStructure = kReferenceBOMStructure Then
Continue For
End If
Dim OccDoc As Inventor.Document = oOcc.ReferencedDocumentDescriptor.ReferencedDocument
Dim FNamePos As Long
Dim docFName As String
FNamePos = InStrRev(OccDoc.FullFileName, "\", -1)
docFName = Mid(OccDoc.FullFileName, FNamePos + 1, Len(OccDoc.FullFileName) -FNamePos)
'This works on documents if reference, but still looks at subcomponents of a reference assembly
'This method ended up not working as envisioned. This was looking directly into the the file's BOMStucture in the document settings. Regardless of how it was toggled in assembly.
'If OccDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then
'Else
If OccDoc.IsModifiable = True Then
Dim assemblyDoc As AssemblyDocument
Dim assemblyDef As AssemblyComponentDefinition
Dim partQty As ComponentOccurrencesEnumerator
assemblyDoc = openDoc
assemblyDef = assemblyDoc.ComponentDefinition
partQty = assemblyDef.Occurrences.AllReferencedOccurrences(OccDoc)
If IsNumeric(partQty.Count) = True Then
If IsNumeric(iProperties.Value(docFName, "Project", "Authority")) = False Then
iProperties.Value(docFName, "Project", "Authority") = 0
End If
If partQty.Count <> iProperties.Value(docFName, "Project", "Authority") Then
iProperties.Value(docFName, "Project", "Authority") = partQty.Count
End If
End If
End If
'Recursively call sub
If oOcc.DefinitionDocumentType = kAssemblyDocumentObject And oOcc.BOMStructure <> kReferenceBOMStructure Then
Call PartCount(oOcc.SubOccurrences)
End If
Next
End If
End Sub
Can't find what you're looking for? Ask the community or share your knowledge.