Hi
I there a way of finding each Bom structured part that is set to reference in an assembly and setting the view of that component to in-visible.
Also needs to iterate into all set View reps that where created and set all views to not show the referenced parts as well.
Any thought or code snippets would be appreciated
Thanks
Dc
Solved! Go to Solution.
Solved by cwhetten. Go to Solution.
Here's a start:
oCompDef = ThisDoc.Document.ComponentDefinition
oAssemblyComponents = oCompDef.Occurrences
oViewRepsCollection = oCompDef.RepresentationsManager.DesignViewRepresentations
oActiveViewRep = oCompDef.RepresentationsManager.ActiveDesignViewRepresentation
Dim oOccurrence As ComponentOccurrence
Dim oViewRep As DesignViewRepresentation
For Each oViewRep In oViewRepsCollection
oViewRep.Activate 'Activate each view rep one at a time
For Each oOccurrence In oAssemblyComponents
If (oOccurrence.BOMStructure = BOMStructureEnum.kReferenceBOMStructure) Then
oOccurrence.Visible = False
Else
oOccurrence.Visible = True
End If
Next
Next
oActiveViewRep.Activate 'Sets the view rep back to the one that was active before the rule was executed
A couple of caveats:
This code only checks the BOM structure and sets the visibility of components in the first level of the assembly. If you have sub-assemblies with reference components, it won't change their visibility. The code could be expanded to do this, but I didn't want to do that yet in case this is all you need.
If any of your view representations are locked, this code will not work at all. If your design has some that are locked, we will have to change the code to have it skip any locked view reps (or unlock them, change them, then re-lock them).
I also made it so that it would switch back to whatever view rep was active before you executed the rule. This isn't necessary, but it may be convenient.
Cameron Whetten
Inventor 2012
Please click "Accept as Solution" if this response answers your question.
Hi cwhetton
Thanks for your help on this firstly few comments on what we are trying to achieve.
When I inserted code into ilgic rule there was 3 errors and would not run attached is screen grab of code errors
Rule Compile Errors in Rule1, in CAM-01-0425-000.iam
Error on Line 3 : End of statement expected.
Error on Line 4 : End of statement expected.
Error on Line 20 : 'oActiveViewRep' is not declared. It may be inaccessible due to its protection level.
I appreciate your help on this
I'm not sure why this isn't working for you. I don't get those errors. Maybe something weird happened when you copied and pasted from the message board? Also, for the record, which version of Inventor are you using?
Getting the code to run through the locked view reps should be pretty easy, but it's going to be a little more involved to get it to iterate through all levels of the assembly. I will have to give this one some more thought.
Cameron Whetten
Inventor 2012
Just letting you know I haven't forgotten about this. I am still trying to figure out how to make it cycle through all subassemblies. It has proven to be quite the challenge for me. I'll keep you posted.
Cameron Whetten
Inventor 2012
Ok, here is what I have so far:
I have also attached the rule as a TXT file, in case you have trouble copying and pasting the code from the message board.
I am not totally satisfied with the code as it is here. The problem is that it is only written if your assembly components are themselves assemblies, down to two levels below the main assembly. If there are any sub-assemblies below two levels, this code won't go through them component-by-component. It would have to be re-written in order to accommodate further levels. It's not automatically scalable, and I don't like that. Unfortunately, I haven't figured out a better way to do it.
My first try was to do something similar to what Curtis Waguespack suggested in this thread:
That approach is to cycle through each component that is referenced by the assembly. If I understand it correctly, that would find each component no matter where it is in the assembly structure.
But, it only works if your view representations are all set up to be associative, all the way down to the deepest component. And it would be extra tricky if each of your subassemblies each had mulitple view reps.
Instead, the approach I chose to use only overrides the visibility at the main assembly level. I think this approach will be appropriate for most situations. HOWEVER--If you do have your view representations set to be associative all the way down, the code I posted will break that (overriding the visibility state will remove any view rep associativity), so don't run it. If this is the case, let me know and I will look into doing it another way.
Cameron Whetten
Inventor 2012
@cwhetten wrote:
Instead, the approach I chose to use only overrides the visibility at the main assembly level. I think this approach will be appropriate for most situations.
So, why is it so hard to automate something that we can do manually?
We often have vehicle frame components that show up in several subassembly levels as reference parts. But if I want to constrain a part to the frame at the top level, I don't know which of the dozen or more instances of the frame to attach it to except by trial and error.
Hello together,
Hello @cwhetten,
Sub Main() oCompDef = ThisDoc.Document.ComponentDefinition oAssyOccurrences = oCompDef.Occurrences oViewRepsCollection = oCompDef.RepresentationsManager.DesignViewRepresentations Dim oOccurrence As ComponentOccurrence Dim oViewRep As DesignViewRepresentation Dim oSubOccurrence1 As ComponentOccurrence 'For first level of sub-occurrences Dim oSubOccurrence2 As ComponentOccurrence 'For second level of sub-occurrences For Each oViewRep In oViewRepsCollection oViewRep.Activate 'Activate each view rep one at a time If oViewRep.Name = "Master" Then 'Do nothing. This makes the code ignore the Master view rep Else If oViewRep.Locked Then 'If the view rep is locked, unlock it, make the changes, then lock it again oViewRep.Locked = False For Each oOccurrence In oAssyOccurrences 'Run the code for top-level occurrences If oOccurrence.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then 'If the occurrence is an assembly, run the code for each of its sub-occurrences If oOccurrence.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then 'If the whole sub-assembly is set to reference, just turn the whole thing off. 'No need to control the visibility of sub-components. oOccurrence.Visible = False Else 'Run the code for all sub-components For Each oSubOccurrence1 In oOccurrence.SubOccurrences 'Run the code for the first level of sub-components If oSubOccurrence1.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then 'If the occurrence is an assembly, run the code for each of its sub-occurrences If oSubOccurrence1.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then 'If the whole sub-assembly is set to reference, just turn the whole thing off. 'No need to control the visibility of sub-components. oSubOccurrence1.Visible = False Else 'Run the code for all sub-components For Each oSubOccurrence2 In oSubOccurrence1.SubOccurrences 'Run the code for the second level of sub-components fVisibilitySwitch(oSubOccurrence2) Next End If Else 'Run the code if the sub-component is a part document fVisibilitySwitch(oSubOccurrence1) End If Next End If Else 'Run the code if the component is a part document fVisibilitySwitch(oOccurrence) End If Next oViewRep.Locked = True Else 'Runs the code on the unlocked view reps For Each oOccurrence In oAssyOccurrences 'Run the code for top-level occurrences If oOccurrence.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then 'If the occurrence is an assembly, run the code for each of its sub-occurrences If oOccurrence.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then 'If the whole sub-assembly is set to reference, just turn the whole thing off. 'No need to control the visibility of sub-components. oOccurrence.Visible = False Else 'Run the code for all sub-components For Each oSubOccurrence1 In oOccurrence.SubOccurrences 'Run the code for the first level of sub-components If oSubOccurrence1.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then 'If the occurrence is an assembly, run the code for each of its sub-occurrences If oSubOccurrence1.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then 'If the whole sub-assembly is set to reference, just turn the whole thing off. 'No need to control the visibility of sub-components. oSubOccurrence1.Visible = False Else 'Run the code for all sub-components For Each oSubOccurrence2 In oSubOccurrence1.SubOccurrences 'Run the code for the second level of sub-components fVisibilitySwitch(oSubOccurrence2) Next End If Else 'Run the code if the sub-component is a part document fVisibilitySwitch(oSubOccurrence1) End If Next End If Else 'Run the code if the component is a part document fVisibilitySwitch(oOccurrence) End If Next End If End If Next oViewRepsCollection.Item("Default").Activate 'Sets the view rep back to the one called "Default" End Sub Sub fVisibilitySwitch(oOccurrence As ComponentOccurrence) 'This sub toggles the component visibility based on if its BOM structure ' is set to reference (visibility = off) or other (visibility = on). If (oOccurrence.BOMStructure = BOMStructureEnum.kReferenceBOMStructure) Then oOccurrence.Visible = False Else If oOccurrence.Visible = False Then 'Do nothing. This makes sure that if the visiblity is already off, ' it will stay off. This prevents the code from undoing any visibility ' changes that have been made to set up custom view representations. 'This method will not automatically turn the visiblity on if the item's ' BOM structure is changed from reference to something else. 'This will have to be done manually. Else oOccurrence.Visible = True End If End Sub
You can also change the code, the components which are referenced on the color red?