iLogic for hide/show every possible object

iLogic for hide/show every possible object

b_kurt_ch
Enthusiast Enthusiast
133 Views
3 Replies
Message 1 of 4

iLogic for hide/show every possible object

b_kurt_ch
Enthusiast
Enthusiast

Hello

Since Autodesk doesn't natively provide this functionality, I'd like to implement my own tool/iLogic to toggle the visibility of selected objects throughout the software.
All features for which Inventor natively provides visibility functionality must be grouped into one code to manage them via a shortcut.
Regardless of the user's selection (and multi selection), the code should toggle the visibility of these objects:

 

.ipt
– Sketches, 3D sketches
– Work features (axes, planes, points, etc.)
– Origin features
- ...

.iam
– Components
– Subcomponents
– All features from the .ipt
- ...

.idw
– Components
– Individual lines/splines
- All features from .ipt
- ...

 

Are there already existing tool I did not found?

Or any hint how to start this more complex project?

 

Thanks in advance.

0 Likes
134 Views
3 Replies
Replies (3)
Message 2 of 4

b_kurt_ch
Enthusiast
Enthusiast

Oh wow, this was impressive.
More as a joke, I asked ChatGPT the same.

 

Following cool, working code as an answer:

Sub Main()
' iLogic: Sichtbarkeit umschalten je nach Dokumenttyp und Auswahl

Dim oDoc As Document = ThisApplication.ActiveDocument
Dim oSelSet As SelectSet = oDoc.SelectSet

If oSelSet.Count = 0 Then
    MsgBox("Bitte mindestens ein Objekt auswählen.", vbExclamation)
    Return
End If

Select Case oDoc.DocumentType

    '------------------------------
    ' PART-Dokument (.ipt)
    '------------------------------
    Case DocumentTypeEnum.kPartDocumentObject
        For Each oObj In oSelSet
            Call ToggleVisibility(oObj)
        Next

    '------------------------------
    ' ASSEMBLY-Dokument (.iam)
    '------------------------------
    Case DocumentTypeEnum.kAssemblyDocumentObject
        For Each oObj In oSelSet
            Call ToggleVisibility(oObj)
        Next

    '------------------------------
    ' DRAWING-Dokument (.idw)
    '------------------------------
    Case DocumentTypeEnum.kDrawingDocumentObject
        For Each oObj In oSelSet
            Call ToggleVisibility(oObj)
        Next

    Case Else
        MsgBox("Dokumenttyp wird nicht unterstützt.", vbExclamation)
End Select

End Sub

'--------------------------------------
' Hilfsfunktion: Sichtbarkeit umschalten
'--------------------------------------
Sub ToggleVisibility(oObj As Object)
    Try
        ' Falls das Objekt die Property "Visible" hat
        Dim t As Type = oObj.GetType()
        Dim p = t.GetProperty("Visible")
        If Not p Is Nothing Then
            Dim current As Boolean = CBool(p.GetValue(oObj, Nothing))
            p.SetValue(oObj, Not current, Nothing)
            Exit Sub
        End If

        ' Spezielle Fälle behandeln:
        If TypeOf oObj Is ComponentOccurrence Then
            oObj.Visible = Not oObj.Visible
        ElseIf TypeOf oObj Is Sketch Then
            oObj.Visible = Not oObj.Visible
        ElseIf TypeOf oObj Is WorkAxis Then
            oObj.Visible = Not oObj.Visible
        ElseIf TypeOf oObj Is WorkPlane Then
            oObj.Visible = Not oObj.Visible
        ElseIf TypeOf oObj Is WorkPoint Then
            oObj.Visible = Not oObj.Visible
        ElseIf TypeOf oObj Is DrawingCurveSegment Then
            oObj.Visible = Not oObj.Visible
		ElseIf TypeOf oObj Is DrawingCurve Then
            Dim oCurve As DrawingCurve = oObj
            Dim oOcc As ComponentOccurrence = oCurve.ModelGeometry.ContainingOccurrence
            If Not oOcc Is Nothing Then
                Dim oView As DrawingView = oCurve.Parent
                oView.SetVisibility(oOcc, Not oView.GetVisibility(oOcc))
            End If
        Else
            ' Optional: Debug-Ausgabe für nicht unterstützte Typen
            ' MsgBox("Kein Sichtbarkeits-Flag für: " & oObj.ToString)
        End If

    Catch ex As Exception
        MsgBox("Fehler bei: " & oObj.ToString & vbCrLf & ex.Message)
    End Try
End Sub

 

Components of a drawing view won't work. 

AS well as sketches/features of components of an assembly. But as a start, this is a really cool and smart code.

Any idea how to improve?

0 Likes
Message 3 of 4

b_kurt_ch
Enthusiast
Enthusiast

Here an updated version. Works fine except the components in drawing view.

 

Sub Main()

' iLogic: Sichtbarkeit umschalten je nach Dokumenttyp und Auswahl

Dim oDoc As Document = ThisApplication.ActiveDocument
Dim oSelSet As SelectSet = oDoc.SelectSet

If oSelSet.Count = 0 Then
    MsgBox("Selektiere zuerst etwas (Bauteil, Ebene, Skizze...)", vbExclamation)
    Return
End If

' Ursprüngliche Auswahl sichern
Dim savedSelection As New List(Of Object)
For Each oObj In oSelSet
    savedSelection.Add(oObj)
Next

For Each oObj In oSelSet
     Call ToggleVisibility(oObj)
Next

' Auswahl wiederherstellen
oSelSet.Clear()
For Each oObj In savedSelection
    Try
        oSelSet.Select(oObj)
    Catch
        ' Falls Objekt nicht mehr auswählbar ist (z.B. ausgeblendet)
    End Try
Next


End Sub

'--------------------------------------
' Hilfsfunktion: Sichtbarkeit umschalten
'--------------------------------------
Sub ToggleVisibility(oObj As Object)
    Try
        ' Falls das Objekt die Property "Visible" hat
        Dim t As Type = oObj.GetType()
        Dim p = t.GetProperty("Visible")
        If Not p Is Nothing Then
            Dim current As Boolean = CBool(p.GetValue(oObj, Nothing))
            p.SetValue(oObj, Not current, Nothing)
            Exit Sub
        End If
		
        ' Spezielle Fälle behandeln:
        If TypeOf oObj Is ComponentOccurrence Then
            oObj.Visible = Not oObj.Visible

        ' Skizzen im Part
        ElseIf TypeOf oObj Is PlanarSketch Then
            oObj.Visible = Not oObj.Visible
        ElseIf TypeOf oObj Is Sketch3D Then
            oObj.Visible = Not oObj.Visible


        ' Skizzen in der Baugruppe (Proxy)
        ElseIf TypeOf oObj Is PlanarSketchProxy Then
		    Dim oProxy As PlanarSketchProxy = oObj
		    Dim oNative As PlanarSketch = oProxy.NativeObject
		    oNative.Visible = Not oNative.Visible
		    
		    ' Occurrence im Assembly holen und sicherstellen, dass Skizzen angezeigt werden
		    Dim occ As ComponentOccurrence = oProxy.ContainingOccurrence
		    If Not occ Is Nothing Then
		        If oNative.Visible Then
		            occ.Visible = True ' Stellt sicher, dass Komponente sichtbar ist
		            occ.Parent.ShowComponentSketches = True ' Skizzenanzeige aktivieren
		        End If
		    End If
        ElseIf TypeOf oObj Is Sketch3DProxy Then
		    Dim oProxy As Sketch3DProxy = oObj
		    Dim oNative As Sketch3D = oProxy.NativeObject
		    oNative.Visible = Not oNative.Visible
		
		    Dim occ As ComponentOccurrence = oProxy.ContainingOccurrence
		    If Not occ Is Nothing Then
		        If oNative.Visible Then
		            occ.Visible = True
		            occ.Parent.ShowComponentSketches = True
		        End If
		    End If


		' Arbeitsebenen
		ElseIf TypeOf oObj Is WorkPlane Then
		    oObj.Visible = Not oObj.Visible
		ElseIf TypeOf oObj Is WorkPlaneProxy Then
		    Dim oProxy As WorkPlaneProxy = oObj
		    Dim oNative As WorkPlane = oProxy.NativeObject
		    oNative.Visible = Not oNative.Visible
		    ' Sicherstellen, dass Arbeitsebenen in der Baugruppe angezeigt werden
		    Dim occ As ComponentOccurrence = oProxy.ContainingOccurrence
		    If Not occ Is Nothing AndAlso oNative.Visible Then
		        occ.Visible = True
		        occ.Parent.ShowComponentWorkPlanes = True
		    End If
		
		' Arbeitsachsen
		ElseIf TypeOf oObj Is WorkAxis Then
		    oObj.Visible = Not oObj.Visible
		ElseIf TypeOf oObj Is WorkAxisProxy Then
		    Dim oProxy As WorkAxisProxy = oObj
		    Dim oNative As WorkAxis = oProxy.NativeObject
		    oNative.Visible = Not oNative.Visible
		    Dim occ As ComponentOccurrence = oProxy.ContainingOccurrence
		    If Not occ Is Nothing AndAlso oNative.Visible Then
		        occ.Visible = True
		        occ.Parent.ShowComponentWorkAxes = True
		    End If
		
		' Arbeitspunkte
		ElseIf TypeOf oObj Is WorkPoint Then
		    oObj.Visible = Not oObj.Visible
		ElseIf TypeOf oObj Is WorkPointProxy Then
		    Dim oProxy As WorkPointProxy = oObj
		    Dim oNative As WorkPoint = oProxy.NativeObject
		    oNative.Visible = Not oNative.Visible
		    Dim occ As ComponentOccurrence = oProxy.ContainingOccurrence
		    If Not occ Is Nothing AndAlso oNative.Visible Then
		        occ.Visible = True
		        occ.Parent.ShowComponentWorkPoints = True
		    End If

		' Komponentenmuster Rund
		ElseIf TypeOf oObj Is CircularOccurrencePattern Then
   			 oObj.Visible = Not oObj.Visible

		' Komponentenmuster Rechteckig
		ElseIf TypeOf oObj Is RectangularOccurrencePattern Then
		    oObj.Visible = Not oObj.Visible
		
		' Flächenkörper
		ElseIf TypeOf oObj Is SurfaceBody Then
		    oObj.Visible = Not oObj.Visible

        ' Zeichnungselemente
        ElseIf TypeOf oObj Is DrawingCurveSegment Then
            oObj.Visible = Not oObj.Visible

        Else
             'Optional: Debug-Ausgabe für unbekannte Typen
			 MsgBox("Kein Sichtbarkeits-Flag für: " & TypeName(oObj) & vbCrLf & "Voller Name: " & oObj.GetType().FullName)
		End If

    Catch ex As Exception
        MsgBox("Fehler bei: " & oObj.ToString & vbCrLf & TypeName(oObj) & vbCrLf & ex.Message)
    End Try
End Sub

 

0 Likes
Message 4 of 4

b_kurt_ch
Enthusiast
Enthusiast

Any advice how to solve the remaining problem with components in drawing views?

0 Likes