IS there a way to make a rule, which export SubAssembly component QTY and create Custom iProperty for each?
For example in Assembly01, there are 6 Parts: 4 x Part A and 2 x Part B.
The rule should make an Custom iProperty for each Part A and Part B with Custom Property named "iQTY" = Qty in assembly.
Part A would have Custom iProperty "iQTY = 4"
Part B would have Custom iProperty "iQTY = 2"
Also if there is Subassebly in main assembly, it might also create a iQTY for it too.
Solved! Go to Solution.
Solved by vkulikajevas. Go to Solution.
Hi, try the next rule driven from the master assembly.
Sub Main doc = ThisDoc.Document Dim oAssyDef As AssemblyComponentDefinition = doc.ComponentDefinition Dim oBOM As BOM = oAssyDef.BOM oBOM.StructuredViewFirstLevelOnly = False oBOM.StructuredViewEnabled = True oBOM.PartsOnlyViewEnabled = True Dim oBOMViewPO As BOMView = oBOM.BOMViews.Item(3) ' Parts Only Dim oBOMRowPO As BOMRow For Each oBOMRowPO In oBOMViewPO.BOMRows On Error Resume Next 'Set a reference to the primary ComponentDefinition of the row Dim oCompDef As ComponentDefinition oCompDef = oBOMRowPO.ComponentDefinitions.Item(1) Dim CompFileNameOnly As String CompFileNameOnly = oCompDef.Document.Displayname 'MessageBox.Show(CompFileNameOnly) Dim Qty As String Qty = oBOMRowPO.TotalQuantity iProperties.Value(CompFileNameOnly, "Custom", "iQTY") = Qty Next Dim oBOMViewStruc As BOMView = oBOM.BOMViews.Item(2) 'Structured Dim oBOMRowStruc As BOMRow Dim arrSubAssemblyList As New ArrayList Call QueryBOMRowProperties(oBOMViewStruc.BOMRows, arrSubAssemblyList, 1) End Sub Private Sub QueryBOMRowProperties(oBOMRows As BOMRowsEnumerator, arrSubAssembly As ArrayList, oParentQty As Integer) Dim i As Long For i = 1 To oBOMRows.Count Dim oBOMRowStruc As BOMRow = oBOMRows.Item(i) Dim oCompDef As ComponentDefinition = oBOMRowStruc.ComponentDefinitions.Item(1) Dim oQty As Integer If TypeOf oCompDef Is AssemblyComponentDefinition And oCompDef.BOMStructure = BOMStructureEnum.kNormalBOMStructure Then Dim CompFileNameOnly As String CompFileNameOnly = oCompDef.Document.Displayname 'MessageBox.Show(CompFileNameOnly) oQty = oBOMRowStruc.ItemQuantity * oParentQty Dim additionalQty As Integer If arrSubAssembly.Count <> 0 Then Dim counter As Integer = 0 For Each CompData As String In arrSubAssembly Dim commaindex As Integer = CompData.IndexOf(":") Dim CompName As String = CompData.Substring(0,commaindex) If CompName = CompFileNameOnly Then additionalQty = CompData.Substring(commaindex+1) Else counter += 1 End If Next If additionalQty = 0 Then arrSubAssembly.Add(CompFileNameOnly & ":" & oQty) Else arrSubAssembly(counter) = CompFileNameOnly & ":" & oQty + additionalQty End If Else arrSubAssembly.Add(CompFileNameOnly & ":" & oQty) End If iProperties.Value(CompFileNameOnly, "Custom", "iQTY") = oQty + additionalQty 'Recursively iterate child rows if present. If Not oBOMRowStruc.ChildRows Is Nothing Then Call QueryBOMRowProperties(oBOMRowStruc.ChildRows, arrSubAssembly, oQty) End If End If Next End Sub
I hope this helps with your problem. Grettings!
Sergio Daniel Suarez
Mechanical Designer
| Upwork Profile | LinkedIn
Thank you for quick response, Sergio D. Suarez!
I tried using this code on master assembly.
No part received the parameter iQTY.
I am not sure where is the problem. I didn't get any error messages as well.
Maybe code could somehow link BOM parts list with the iQTY?
Because it is possible to view all part quantities there.
Maybe the code could link the part number and QTY and create the iQTY for each part?
Try the following rule. Sometimes the inventor's language can give some problems to access the BOM.
Sub Main Dim doc As AssemblyDocument= ThisDoc.Document Dim oAssyDef As AssemblyComponentDefinition = doc.ComponentDefinition Dim oBOM As BOM = oAssyDef.BOM oBOM.StructuredViewFirstLevelOnly = False oBOM.StructuredViewEnabled = True oBOM.PartsOnlyViewEnabled = True Dim oValueList As New ArrayList For Each oBOMView As BOMView In oBOM.BOMViews oValueList.Add(oBOMView.Name) Next Dim oBOMViewPO As BOMView = oBOM.BOMViews.Item(oValueList(2)) ' Parts Only For Each oBOMRowPO As BOMRow In oBOMViewPO.BOMRows On Error Resume Next 'Set a reference to the primary ComponentDefinition of the row Dim oCompDef As ComponentDefinition oCompDef = oBOMRowPO.ComponentDefinitions.Item(1) Dim CompFileNameOnly As String CompFileNameOnly = oCompDef.Document.Displayname 'MessageBox.Show(CompFileNameOnly) Dim Qty As String Qty = oBOMRowPO.TotalQuantity iProperties.Value(CompFileNameOnly, "Custom", "iQTY") = Qty Next Dim oBOMViewStruc As BOMView = oBOM.BOMViews.Item(oValueList(1)) 'Structured Dim oBOMRowStruc As BOMRow Dim arrSubAssemblyList As New ArrayList Call QueryBOMRowProperties(oBOMViewStruc.BOMRows, arrSubAssemblyList, 1) End Sub Private Sub QueryBOMRowProperties(oBOMRows As BOMRowsEnumerator, arrSubAssembly As ArrayList, oParentQty As Integer) Dim i As Long For i = 1 To oBOMRows.Count Dim oBOMRowStruc As BOMRow = oBOMRows.Item(i) Dim oCompDef As ComponentDefinition = oBOMRowStruc.ComponentDefinitions.Item(1) Dim oQty As Integer If TypeOf oCompDef Is AssemblyComponentDefinition And oCompDef.BOMStructure = BOMStructureEnum.kNormalBOMStructure Then Dim CompFileNameOnly As String CompFileNameOnly = oCompDef.Document.Displayname 'MessageBox.Show(CompFileNameOnly) oQty = oBOMRowStruc.ItemQuantity * oParentQty Dim additionalQty As Integer If arrSubAssembly.Count <> 0 Then Dim counter As Integer = 0 For Each CompData As String In arrSubAssembly Dim commaindex As Integer = CompData.IndexOf(":") Dim CompName As String = CompData.Substring(0,commaindex) If CompName = CompFileNameOnly Then additionalQty = CompData.Substring(commaindex+1) Else counter += 1 End If Next If additionalQty = 0 Then arrSubAssembly.Add(CompFileNameOnly & ":" & oQty) Else arrSubAssembly(counter) = CompFileNameOnly & ":" & oQty + additionalQty End If Else arrSubAssembly.Add(CompFileNameOnly & ":" & oQty) End If iProperties.Value(CompFileNameOnly, "Custom", "iQTY") = oQty + additionalQty 'Recursively iterate child rows if present. If Not oBOMRowStruc.ChildRows Is Nothing Then Call QueryBOMRowProperties(oBOMRowStruc.ChildRows, arrSubAssembly, oQty) End If End If Next End Sub
I hope this solves the problem. Regards
Sergio Daniel Suarez
Mechanical Designer
| Upwork Profile | LinkedIn
Hello, Sergio D Suarez, unfortunately the second code does not work too.
Maybe the code could create excel file and associate the number with parts?
Copy iQTY to each part and then delete created excel file"?
I think then it should run more smoothly
Managed to find and edit this code to work as needed! Maybe someone will also find this useful!
Sub Main If ThisDoc.Document.DocumentType <> kAssemblyDocumentObject Then MsgBox("Rule only valid in assembly files!"): Exit Sub If ThisDoc.Document.FullFileName = "" Then MsgBox("GA Not Saved yet!" & vbLf & vbLf & "Aborting!"): Exit Sub Dim oGADoc As AssemblyDocument oGADoc = ThisDoc.Document BatchQTY = InputBox("ENTER TOTAL QTY REQUIRED OF GA", "GA QTY", "") iProperties.Value("Custom", "iQTY") = BatchQTY Dim oBOM As BOM oBOM = oGADoc.ComponentDefinition.BOM With oBOM .PartsOnlyViewEnabled = True .StructuredViewEnabled = True .StructuredViewFirstLevelOnly = False End With Call PurgeBatchQTY(oGADoc) Dim oBOMViewPO As BOMView = oBOM.BOMViews.Item("Parts Only") Call SetBatchPartQTYs(oBOMViewPO, BatchQTY) Dim oBOMViewStruc As BOMView = oBOM.BOMViews.Item("Structured") Call SetBatchAssemblyQTYs(oBOMViewStruc.BOMRows, BatchQTY) End Sub Sub PurgeBatchQTY(oDoc As Document) For Each oSubDoc in oDoc.AllReferencedDocuments If oSubDoc.IsModifiable = True Then iProperties.Value(System.IO.Path.GetFileName(oSubDoc.FullFileName), "Custom", "iQTY") = 0 End If Next End Sub Sub SetBatchPartQTYs(ByVal oBOMViewPO As BOMView, ByVal BatchQTY As Integer) For Each oBOMRowPO In oBOMViewPO.BOMRows oCompDef = oBOMRowPO.ComponentDefinitions.Item(1) oRowDoc = oCompDef.Document Dim Qty As Integer = oBOMRowPO.TotalQuantity If oRowDoc.IsModifiable = True Then iProperties.Value(System.IO.Path.GetFileName(oRowDoc.FullFileName), "Custom", "iQTY") = Qty * BatchQTY End If Next End Sub Private Sub SetBatchAssemblyQTYs(oBOMRows As BOMRowsEnumerator, oParentQty As Integer) Dim oQty As Integer For Each oBOMRowStruct In oBOMRows oCompDef = oBOMRowStruct.ComponentDefinitions.Item(1) oRowDoc = oCompDef.Document 'Single line Check + Continues: If Not (TypeOf oCompDef Is AssemblyComponentDefinition And oCompDef.BOMStructure = BOMStructureEnum.kNormalBOMStructure) Then Continue For If oRowDoc.IsModifiable = False Then Continue For 'Resume functionality oQty = oBOMRowStruct.ItemQuantity * oParentQty iProperties.Value(System.IO.Path.GetFileName(oRowDoc.FullFileName), "Custom", "iQTY") = oQty + iProperties.Value(System.IO.Path.GetFileName(oRowDoc.FullFileName), "Custom", "iQTY") If Not (oBOMRowStruct.ChildRows Is Nothing) Then Call SetBatchAssemblyQTYs(oBOMRowStruct.ChildRows, oQty) End If Next End Sub
Can't find what you're looking for? Ask the community or share your knowledge.