I have a pattern of reference components, but the quantity of the pattern can change based on certain criteria. When the quantity increases or decreases, I need each pattern element to have its BOM structure set to reference.
The problem is, even if the original pattern element is set to reference, each subsequent element comes in as normal. This causes the BOM quantities for that component to be reported incorrectly in higher assemblies.
Is there some iLogic or VBA code that will automatically set each component in a pattern to BOM structure "Reference", even if the pattern quantity changes?
There was just recently a thread wehere someone wanted to use view reps to control their partslist. In that thread should be some code that iterates through each occurence in the assembly, checks it's visibility, and sets it's BOM structure accordingly. That code could be modifies to work for your problem. Try doing a search for it and see what you can do with it.
Ok, I see that thread. I was thinking along those lines, too. I was hoping there would be a similar way to step through just the pattern elements and change them, instead of stepping through the entire assembly.
This is the code I was refering to, courtesy of Curtis. (tee-hee)
' set a reference to the assembly component definintion. ' This assumes an assembly document is open. Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition 'Iterate through all of the occurrences Dim oOccurrence As ComponentOccurrence For Each oOccurrence In oAsmCompDef.Occurrences 'check for and skip virtual components If Not TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then 'set BOM as default if the component is visible If Component.Visible(oOccurrence.Name) = True Then Component.InventorComponent(oOccurrence.Name).BOMStructure = _ BOMStructureEnum.kDefaultBOMStructure 'set BOM as reference if the component is not visible ElseIf Component.Visible(oOccurrence.Name) = False Then Component.InventorComponent(oOccurrence.Name).BOMStructure = _ BOMStructureEnum.kReferenceBOMStructure End If Else End If Next
I think we could tweak this by checking the occurence name instead of the visibility status. Something like:
' set a reference to the assembly component definintion. ' This assumes an assembly document is open. Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition 'Iterate through all of the occurrences Dim oOccurrence As ComponentOccurrence For Each oOccurrence In oAsmCompDef.Occurrences 'check for and skip virtual components If Not TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then 'set BOM as default if the component matches our name check If Left(oOccurrence.Name,Len(oOccurrence.Name)-2) = "MyComponent" Then Component.InventorComponent(oOccurrence.Name).BOMStructure = _ BOMStructureEnum.kReferenceBOMStructure End If End If Next
Replace MyComponent with the name of your patterned component sans the ":x". I haven't tested this, because I'm lazy like that. But go ahead and give it a go and tell me if it works for you.
Hi cwhetten,
I'm juggling too much right now to look closely at this, but here's a snippet I had on hand that looks at the assembly patterns, and then looks at the first occurrence within each pattern and then renames the pattern to the same name as that component.
This is a "quick and dirty" rule that I use from time to time for some special circumstances so I don't expect it to be of use as is, but it might help as an example of looking at the pattern components.
I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com
Dim oDoc As AssemblyDocument oDoc = ThisApplication.ActiveDocument Dim oPattern As OccurrencePattern Dim oElement As OccurrencePatternElement Dim oComp As ComponentOccurrence Dim i as Long i =0 For Each oPattern In oDoc.ComponentDefinition.OccurrencePatterns i=i+1 oPattern = oDoc.ComponentDefinition.OccurrencePatterns.Item(i) 'MessageBox.Show(oPattern.name, "Pattern") oElement = oPattern.OccurrencePatternElements.Item(i) 'MessageBox.Show(oElement.name, "Element") oComp = oElement.Occurrences.item(1) 'MessageBox.Show(oComp.name, "Component") oPattern.name = oComp.name & " Pattern" Next oPattern
Thanks, Curtis. This looks promising. I will see if I can get it to work with what you've provided.
Hi There
Great to copy your code to ilogic and it works.
I seach one time in Google and found your code. Pasted it to iLogic and it worked.
Now I want iLogic to also go through all sub ****.
Do you have a idear how to do that ?
very best regards Peter Valbjorn, Alfa Laval Copenhagen, Denmark
Here's a snippet to iterate through sub assemblies:
Sub Main Dim oDoc As AssemblyDocument oDoc = ThisApplication.ActiveDocument Dim oAllOccur As ObjectCollection oAllOccur = ThisApplication.TransientObjects.CreateObjectCollection Dim oOcc As ComponentOccurrence For Each oOcc In oDoc.ComponentDefinition.Occurrences ' Check if it's child occurrence (leaf node) If oOcc.SubOccurrences.Count = 0 Then 'MessageBox.Show ("its a part") oAllOccur.Add(oOcc) Else 'MessageBox.Show ("its an assembly") oAllOccur.Add(oOcc) Call processAllSubOcc(oOcc,oAllOccur) End If Next 'MessageBox.Show(oAllOccur.Count) For Each oOcc In oAllOccur 'MessageBox.Show(oOcc.Name) On Error Resume Next Dim oSketch As Sketch oSketch = oOcc.Definition.Sketches.Item("Sketch_TC") oSketch.Visible = False On Error Goto 0 Next End Sub Private Sub processAllSubOcc(ByVal oOcc As ComponentOccurrence, oAllOccur As objectcollection) Dim oDoc As AssemblyDocument oDoc = ThisApplication.ActiveDocument Dim oSubCompOcc As ComponentOccurrence For Each oSubCompOcc In oOcc.SubOccurrences ' Check if it's child occurrence (leaf node) If oSubCompOcc.SubOccurrences.Count = 0 Then 'MessageBox.Show ("its a part") oAllOccur.Add(oSubCompOcc) Else 'MessageBox.Show ("its an assembly") oAllOccur.Add(oSubCompOcc) Call processAllSubOcc(oSubCompOcc,oAllOccur) End If Next End Sub
Hello All,
The code that Mike posted in courtesy of Curtis is exactly what I am in need of but for some reason I get the following method error.
To me the code makes sense as it is & I just can't figure out what it is asking for, what needs to be bracketed??
Your feedback on this would be much appreciated.
Kind Regards
Tim
Hi tim.mulder,
My guess is that the errors are due to copy/paste formatting issues. Attached is the same rule as a *.txt file, with no formatting.
If you copy/paste from the *.txt file and are still having issues, you might try to modify lines 14 and 18 as such:
change from:
Component.InventorComponent(oOccurrence.Name).BOMStructure = _ BOMStructureEnum.kDefaultBOMStructure
to:
Component.InventorComponent(oOccurrence.Name).BOMStructure = BOMStructureEnum.kDefaultBOMStructure
The underscore preceded by a space simply allows the code line to be "wrapped" to the next line.
How to: Break and Combine Statements in Code (Visual Basic)
http://msdn.microsoft.com/en-us/library/ba9sxbw4.aspx
I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com
Hello Curtis,
I got this below error when I update the main assembly. Please refer the screenshots.
Code applied on the sub assembly (090104.iam). Works fine but I got the error message only in main assembly.
I missed something could you please check the code.
opara = S1:09
'Visiblity off for the component pattern
If opara = 12 Then
Component.Visible("Komponentenanordnung *** only if S1:09 = 16 ***") = False
Else
Component.Visible("Komponentenanordnung *** only if S1:09 = 16 ***") = True
End If
' set a reference to the assembly component definintion.
' This assumes an assembly document is open.
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition
'Iterate through all of the occurrences
Dim oOccurrence As ComponentOccurrence
For Each oOccurrence In oAsmCompDef.Occurrences
'check for and skip virtual components
If Not TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then
'set BOM as default if the component is visible
If Component.Visible(oOccurrence.Name) = True Then
Component.InventorComponent(oOccurrence.Name).BOMStructure = BOMStructureEnum.kDefaultBOMStructure
'set BOM as reference if the component is not visible
ElseIf Component.Visible(oOccurrence.Name) = False Then
Component.InventorComponent(oOccurrence.Name).BOMStructure = BOMStructureEnum.kReferenceBOMStructure
End If
Else
End If
Next
With Best regards,
Veerappa
Hi Veerappa,,
I tested this block of code, and it ran without issue. I would check your files and make sure that the browser names match what is expected.
I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com
opara = 12 'Visiblity off for the component pattern If opara = 12 Then Component.Visible("Komponentenanordnung *** only if S1:09 = 16 ***") = False Else Component.Visible("Komponentenanordnung *** only if S1:09 = 16 ***") = True End If ' set a reference to the assembly component definintion. ' This assumes an assembly document is open. Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition 'Iterate through all of the occurrences Dim oOccurrence As ComponentOccurrence For Each oOccurrence In oAsmCompDef.Occurrences 'check for and skip virtual components If Not TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then 'set BOM as default if the component is visible If Component.Visible(oOccurrence.Name) = True Then Component.InventorComponent(oOccurrence.Name).BOMStructure = BOMStructureEnum.kDefaultBOMStructure 'set BOM as reference if the component is not visible ElseIf Component.Visible(oOccurrence.Name) = False Then Component.InventorComponent(oOccurrence.Name).BOMStructure = BOMStructureEnum.kReferenceBOMStructure End If Else End If Next
I did not test
Hello Curtis,
I have attached herewith with files. Please have a look and help me!!!!!
Last few days I am struggling with this.
Best regards,
Veerappa