Community
Inventor Forum
Welcome to Autodesk’s Inventor Forums. Share your knowledge, ask questions, and explore popular Inventor topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Ilogic Rule to find all Reference parts and turn off visibilty

10 REPLIES 10
SOLVED
Reply
Message 1 of 11
dclunie
5845 Views, 10 Replies

Ilogic Rule to find all Reference parts and turn off visibilty

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

 

10 REPLIES 10
Message 2 of 11
cwhetten
in reply to: dclunie

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.

Message 3 of 11
dclunie
in reply to: cwhetten

Hi  cwhetton      

 

Thanks for your help on this firstly few comments on what we are trying to achieve.

 

  1. We have locked view reps which will need to be updated (so code would need to iterate into all view reps and lock them back down.
  2. We would also need to run this code down to all levels sub-assemblies etc.

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

 

 

Message 4 of 11
cwhetten
in reply to: dclunie

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

Message 5 of 11
mrattray
in reply to: dclunie

Copy the code into a text file first, and make sure it matches what Cameron posted perfectly. Look for any funky characters or line breaks in the wrong spot. There's frequently problems like this when you copy/paste from this board.
Mike (not Matt) Rattray

Message 6 of 11
dclunie
in reply to: cwhetten

HI cwhetton

Got it to work pasted it into word and pasted it from there and it worked.
I use Autodesk Product Design Suite Standard 2013 with sp2 installed.

It errors if master is active at time of running code ,I assume that is because it is a locked viewrep as default.

Thanks for looking at the code to look down all levels as this would be perfect for me , that in conjunction with viewreps nlock and re-lock would be cherry on the cake.

Just one more thing is it possible to return the viewrep to default at end instead of active view rep.

cheers

dc
Message 7 of 11
cwhetten
in reply to: dclunie

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

Message 8 of 11
cwhetten
in reply to: cwhetten

Ok, here is what I have so far:

 

Spoiler
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

 

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:

 

http://forums.autodesk.com/t5/Autodesk-Inventor/Automatically-turning-off-Workfeatures/m-p/3816449#M...

 

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

Message 9 of 11
dclunie
in reply to: cwhetten

Hi Cameron

That code works a treat does exactly what I need, a lot of work in code to what I expected and well explained.

I am trying to learn to code at the moment so I can do all this good stuff.

Many thanks for your help

D Clunie
Message 10 of 11
lesmfunk
in reply to: cwhetten


@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.

Message 11 of 11
S_May
in reply to: lesmfunk

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?

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report