Turning off Visibility of a part in a sub assembly from a top level Ilogic rule

Turning off Visibility of a part in a sub assembly from a top level Ilogic rule

JamesMeBoi
Enthusiast Enthusiast
1,880 Views
12 Replies
Message 1 of 13

Turning off Visibility of a part in a sub assembly from a top level Ilogic rule

JamesMeBoi
Enthusiast
Enthusiast

Looking to figure out how to turn off the visibility of a part inside a subassembly of the top level I'm currently in.  I have a Case Select that the code will go in so if 1 of the 4 options are selected 1 of 4 parts will turn off visibility. Thanks!

Accepted solutions (2)
1,881 Views
12 Replies
Replies (12)
Message 2 of 13

WCrihfield
Mentor
Mentor
Accepted solution

I'm thinking you could probably use the same code from the other post here:

https://forums.autodesk.com/t5/inventor-ilogic-api-vba-forum/deleting-parts-in-a-subassembly-from-a-... 

But just change:

oSubOcc.Delete

to

oSubOcc.Visible = False

 However, keep in mind the current design view representation settings and/or ModelState settings.

See if that will work for you.

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡 or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 13

JamesMeBoi
Enthusiast
Enthusiast

So yes that did work, but not in the way I necessarily wanted, it made the part invisible in the subassembly file but not in the top level. I would like to make the part invisible in the top level but still visible in the subassembly file as this part is used throughout a multitude of files.

Message 4 of 13

JamesMeBoi
Enthusiast
Enthusiast
Okay I was able to make it work with another method, it was actually right in front of me the whole time, thank you for the quick response!
Message 5 of 13

WCrihfield
Mentor
Mentor

Glad to hear I was able to help you out.  Yes, that code was pretty basic.  There a several ways the code could be laid out differently to suit your custom needs.  A better process might have been to make sure there was an 'unlocked' view representation available in both the main assembly and in the sub assembly, and if they did not exist yet, create them.  Then in the sub assembly, record which view rep is currently active, if it's not the one we want, then activate the one we want, then set the component's visibility to off, then switch back to the original view rep, then update and save the sub assembly.  Then back in the main assembly, activate the unlocked view rep we want to use there (if not already active), then set the view rep of the sub assembly (as a component) to the one we used within it, then update/save the main assembly.  The view reps can complicate these types of situations, especially if trying to maintain 'associative' settings of those view reps.  Those components can also be suppressed or have their BOMStructure property set to Reference, so they won't get listed in certain BOM outputs, and those settings could also be stored in the ModelStates.

If you are in need of a more robust solution, or one that loops through multiple layers of sub assemblies and components, that can be done too, but of course it adds to the complexity.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 13

JamesMeBoi
Enthusiast
Enthusiast

Do you have any Idea how to ground a sub assembly that is already active in a top level? I am planning on grounding it after I perform an Constraints.AddMate in my rule. I want to ground it when it ends up moving to.

Message 7 of 13

WCrihfield
Mentor
Mentor
Accepted solution

Yes.  That part is much simpler.  The ComponentOccurrence object has a Property called "Grounded" that is Read/Write and has a Boolean type value.  So a line like this should do the trick:

 

If Not oSubAsmOcc.Grounded Then
	oSubAsmOcc.Grounded = True
End If

or

Component.InventorComponent("SubAsmName:1").Grounded = True

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 13

JamesMeBoi
Enthusiast
Enthusiast
Thank you again!
Message 9 of 13

danielius_samsonas
Explorer
Explorer

Hi James, could you please share the method you used to make it work? I’m also trying to change the visibility of a part in the top-level assembly without modifying it in the subassembly.

 

0 Likes
Message 10 of 13

WCrihfield
Mentor
Mentor

Hi @danielius_samsonas.  Just to clarify, you have an occurrence of a single part type component within the top level of your assembly, but also have an occurrence of that same part component down within a sub assembly, and you want to turn off the visibility of the occurrence in the top level of the assembly, without turning off the visibility of the occurrence down within the sub assembly, correct?  Well, that should be the simplest scenario to control, or make happen.  How do you want/need to be able to specify which component that is?  I assume that you do not want to manually select that top level component with your mouse, because then this ability is right in the right-click menu from there, and a code based rule would not be necessary.  So, do you want to specify it by the name you see in the model browser tree (including the usual ":1" Index number, which would be needed), or do you want to find it by its Part Number value?  Are there multiple instances/occurrences of that same part in the top level of the assembly, or just the one?  If specifying which one to get by Part Number or File Name, and not by its unique model browser tree name, then it may find multiple instances/occurrences if there are multiple in the top level of the assembly.

 

Essentially it is always the DesignViewRepresentations (DVRs) that record whether or not components have their 'visibility' turned on or off, as well as their appearance/color.  When we insert a component occurrence (ComponentOccurrence) into an assembly, that occurrence has its own independent settings for things like which DesignViewRepresentation (DVR) it is set to (ComponentOccurrence.SetDesignViewRepresentation), and which ModelState it is set to (ComponentOccurrence.ActiveModelState).  When we set an occurrence to a DVR, we can make that setting 'associative', which means, if its appearance/color changes in the part file, then it will automatically update to match that in the assembly.  However, if we just go changing how a component looks in a parent level assembly without keeping that DVR setting in mind, it will 'break' that DVR association functionality.

So, we would only really need to change the value of one property:  ComponentOccurrence.Visible , for that one occurrence in the context of the main assembly, and that should not really have any effect on any other occurrences, because that visibility change is being recorded by the currently active DVR of the main assembly.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 11 of 13

danielius_samsonas
Explorer
Explorer

Hi Wesley @WCrihfield ,

Thank you for your explanation.

I want to use DVRs in my top-level assembly to store assembly steps. I can’t use Model States for this purpose, as they are already being used for something else.

To make it clear for assembly workers which components are added in the next stage (the next DVR), I want to make all parts that were visible in the previous DVR transparent (not hidden), while keeping all newly added parts opaque.

This approach works well until I try dividing subassemblies into separate DVRs from the top-level assembly.

Here’s an example:

  • I have two occurrences of the subassembly in top-level assy:
    • SubassyA:1 contains Part001:1 and Part002:1.
    • SubassyA:2 contains the same parts, Part001:1 and Part002:1.

For the first step of the top-level assembly process (DVR1), I want to make only Part001:1 in SubassyA:1 and Part002:1 in SubassyA:2 visible. I can manually select only these parts directly from the top-level assembly using my cursor when "Select Part Priority" is active instead of "Select Component Priority." 

In the second assembly stage (DVR2), the rest of the parts in those subassemblies are set to visible.

After setting up these DVRs, my intended workflow is:

  1. Activate DVR1 and run my iLogic script to record which exact occurrences are visible (this part works fine).
  2. Activate DVR2 (this also works fine).
  3. Set all occurrences recorded in the first step to transparent directly in the top-level assembly (as if manually selecting those parts directly from top-level assemby using "Select Part Priority").

I’m struggling to implement the last step. Do you have any advice on how to achieve this? Thank you in advance!

0 Likes
Message 12 of 13

WCrihfield
Mentor
Mentor

Hi @danielius_samsonas.  Transparency status is also recorded and controlled by the DVR's, just like visibility and appearances.  So, this sounds like it will require some 'preparation' steps.  The best way to enable that level of control and functionality, keeping with your example situation, would be to open that sub assembly directly, then create at least 6 custom DVR's directly within that sub assembly.

  1. DVR1 = Both components Visible, neither is transparent
  2. DVR2 = first component Visible, second not visible
  3. DVR3 = second component Visible, first one not visible
  4. DVR4 = first component visible, and transparent, second component not visible
  5. DVR5 = second component visible and transparent, first component not visible
  6. DVR6 = both components visible and both transparent

Then save that sub assembly file.

Then, back in your 'main' assembly, you can set each instance of the sub assembly to one of those DVRs that exist within it, and can record that component's DVR setting status in the currently active DVR of the main assembly, so that the main assembly's DVR remembers what DVR that occurrence is set to.  No ModelStates need to be involved in that process, because ModelStates primarily effect 'suppression' (also parameter values & iProperty values, among others).  All that could likely be done entirely by code also, but would likely be much more complex code, due to how many sub assemblies, and how many different specific names that would be involved.  But that's a bit difficult to judge from my outside point of view, not being familiar with your exact file set.  I generally create all the DVR's I will need as I am originally creating/designing our individual model files though, instead of trying to do so from the perspective of a higher level assembly at some later point in time, by code, which makes processes like that a lot easier to manage.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 13 of 13

danielius_samsonas
Explorer
Explorer

 

Thank you for your help and response! After all the efforts, I was able to get it working exactly as I originally intended—directly from the top-level assembly.

The main key was to store the entire hierarchical path to the occurrence and then use that same path later when modifying its transparency. This ensured precise targeting of individual parts, even within complex subassembly structures.

Below is a sample iLogic code that sets any selected (or previously stored list of parts in my case) to transparent mode.

 
I hope this solution helps others facing a similar challenge! Let me know if further clarification is needed. 😊
 
Sub Main()
    ' Get the active document (assumes an assembly is open)
    Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument

    ' Get the current selection set
    Dim oSelSet As Inventor.SelectSet = oDoc.SelectSet

    ' Check if any component is selected
    If oSelSet.Count = 0 Then
        MessageBox.Show("No component selected. Please select a component.", "Error")
        Return
    End If

    ' Initialize a variable for the selected component occurrence
    Dim oSelectedOccurrence As ComponentOccurrence = Nothing

    ' Loop through the selection set to find a ComponentOccurrence
    For Each oSelObj In oSelSet
        If TypeOf oSelObj Is ComponentOccurrence Then
            oSelectedOccurrence = oSelObj
            Exit For
        End If
    Next

    ' If no valid component is selected, show an error
    If oSelectedOccurrence Is Nothing Then
        MessageBox.Show("The selected item is not a valid component occurrence.", "Error")
        Return
    End If

    ' Prepare to store hierarchy information
    Dim hierarchyInfo As New System.Text.StringBuilder()
    Dim fullHierarchyID As New System.Text.StringBuilder()

    ' Process the selected component and its parent hierarchy
    Dim currentOccurrence As ComponentOccurrence = oSelectedOccurrence
    Dim level As Integer = 0

    Do While Not currentOccurrence Is Nothing
        ' Indent based on the hierarchy level
        Dim indentation As String = New String(" "c, level * 4) ' 4 spaces per level

        ' Get the occurrence ID
        Dim occurrenceID As String = currentOccurrence.Name ' Occurrence ID

        ' Add the occurrence ID to the hierarchy info
        hierarchyInfo.Insert(0, indentation & occurrenceID & vbNewLine)

        ' Build the full hierarchy path (using ~ as a separator for easier processing)
        fullHierarchyID.Insert(0, currentOccurrence.Name & "~")

        ' Move to the parent occurrence
        currentOccurrence = currentOccurrence.ParentOccurrence
        level += 1
    Loop

    ' Remove the trailing ~ from the full hierarchy ID
    If fullHierarchyID.Length > 0 Then
        fullHierarchyID.Length -= 1
    End If

    ' Set the selected part to transparent
    Try
        oSelectedOccurrence.Transparent = True
        MessageBox.Show("Selected part has been set to transparent." & vbNewLine & 
                        "Hierarchy:" & vbNewLine & hierarchyInfo.ToString() & vbNewLine & 
                        "Full Occurrence ID Path: " & fullHierarchyID.ToString(), "Transparency Set")
    Catch ex As Exception
        MessageBox.Show("Failed to set transparency: " & ex.Message, "Error")
    End Try
End Sub
 
0 Likes