Visibility control - drawing environment

Visibility control - drawing environment

kresh.bell
Collaborator Collaborator
320 Views
7 Replies
Message 1 of 8

Visibility control - drawing environment

kresh.bell
Collaborator
Collaborator

Hi,

is it possible to create an iLogic that would have visibility control (on/off) in the drawing environment for all parts that have the prefix "-" in the body name. Like this one for example

kreshbell_0-1742805554844.png

 

0 Likes
321 Views
7 Replies
Replies (7)
Message 2 of 8

Ivan_Sinicyn
Advocate
Advocate

Hi @kresh.bell 
Try this

Sub Main()
    ' Check that the active document is a drawing
    If ThisApplication.ActiveDocument.DocumentType <> kDrawingDocumentObject Then
        MessageBox.Show("This code must be executed in a drawing environment.", "Error")
        Exit Sub
    End If
    
    Dim oDrawDoc As DrawingDocument
    oDrawDoc = ThisApplication.ActiveDocument
    
    ' Iterate through all sheets in the drawing
    For Each oSheet As Sheet In oDrawDoc.Sheets
        ' Iterate through all views on the sheet
        For Each oView As DrawingView In oSheet.DrawingViews
            ' Get the model document associated with the view
            Dim oRefDoc As Document
            oRefDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
            
            ' Check document type and call processing
            If oRefDoc.DocumentType = kPartDocumentObject Then
                ProcessPartInView(oView, oRefDoc)
            ElseIf oRefDoc.DocumentType = kAssemblyDocumentObject Then
                ProcessAssemblyInView(oView, oRefDoc)
            End If
        Next
    Next
    
    ' Update the drawing
    oDrawDoc.Update
End Sub

Sub ProcessPartInView(oView As DrawingView, oDoc As PartDocument)
    ' Get the component definition of the part
    Dim oCompDef As PartComponentDefinition
    oCompDef = oDoc.ComponentDefinition
    
    ' Iterate through all solid bodies in the part
    For Each oBody As SurfaceBody In oCompDef.SurfaceBodies
        ' Check if the body name starts with "-"
        If Left(oBody.Name, 1) = "-" Then
            ' Turn off visibility of the body in the drawing view
            oView.SetVisibility(oBody, False)
        End If
    Next
End Sub

Sub ProcessAssemblyInView(oView As DrawingView, oDoc As AssemblyDocument)
    ' Get the component definition of the assembly
    Dim oCompDef As AssemblyComponentDefinition
    oCompDef = oDoc.ComponentDefinition
    
    ' Iterate through all component occurrences in the assembly
    For Each oOcc As ComponentOccurrence In oCompDef.Occurrences
        ' Get the document associated with the occurrence
        Dim oSubDoc As Document
        oSubDoc = oOcc.Definition.Document
        
        ' Process only nested parts
        If oSubDoc.DocumentType = kPartDocumentObject Then
            ProcessPartInView(oView, oSubDoc)
        ElseIf oSubDoc.DocumentType = kAssemblyDocumentObject Then
            ' Recursively process nested assemblies
            ProcessAssemblyInView(oView, oSubDoc)
        End If
    Next
End Sub
INV 2025.3
0 Likes
Message 3 of 8

kresh.bell
Collaborator
Collaborator

Hi,

thanks for your code, but unfortunately appears that error 

kreshbell_0-1742809271652.png

 

0 Likes
Message 4 of 8

Ivan_Sinicyn
Advocate
Advocate

@kresh.bell 
One more attempt🤣

Sub Main()
    ' Check that the active document is a drawing
    If ThisApplication.ActiveDocument.DocumentType <> kDrawingDocumentObject Then
        MessageBox.Show("This code must be executed in a drawing environment.", "Error")
        Exit Sub
    End If
    
    Dim oDrawDoc As DrawingDocument
    oDrawDoc = ThisApplication.ActiveDocument
    
    ' Iterate through all sheets in the drawing
    For Each oSheet As Sheet In oDrawDoc.Sheets
        ' Iterate through all views on the sheet
        For Each oView As DrawingView In oSheet.DrawingViews
            ' Get the model document associated with the view
            Dim oRefDoc As Document
            oRefDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
            
            ' Check if referenced document is valid
            If oRefDoc Is Nothing Then Continue For
            
            ' Check document type and call processing
            If oRefDoc.DocumentType = kPartDocumentObject Then
                ProcessPartInView(oView, oRefDoc)
            ElseIf oRefDoc.DocumentType = kAssemblyDocumentObject Then
                ProcessAssemblyInView(oView, oRefDoc, New Collection)
            End If
        Next
    Next
    
    ' Update the drawing
    oDrawDoc.Update
End Sub

Sub ProcessPartInView(oView As DrawingView, oDoc As PartDocument)
    ' Check if inputs are valid
    If oView Is Nothing Or oDoc Is Nothing Then Exit Sub
    
    ' Get the component definition of the part
    Dim oCompDef As PartComponentDefinition
    oCompDef = oDoc.ComponentDefinition
    
    ' Check if component definition is valid
    If oCompDef Is Nothing Then Exit Sub
    
    ' Iterate through all solid bodies in the part
    For Each oBody As SurfaceBody In oCompDef.SurfaceBodies
        ' Check if body is valid and its name starts with "-"
        If oBody IsNot Nothing AndAlso Left(oBody.Name, 1) = "-" Then
            Try
                ' Turn off visibility of the body in the drawing view
                oView.SetVisibility(oBody, False)
            Catch ex As Exception
                ' Log error silently or handle as needed
            End Try
        End If
    Next
End Sub

Sub ProcessAssemblyInView(oView As DrawingView, oDoc As AssemblyDocument, oParentOccurrences As Collection)
    ' Check if inputs are valid
    If oView Is Nothing Or oDoc Is Nothing Then Exit Sub
    
    ' Get the component definition of the assembly
    Dim oCompDef As AssemblyComponentDefinition
    oCompDef = oDoc.ComponentDefinition
    
    ' Check if component definition is valid
    If oCompDef Is Nothing Then Exit Sub
    
    ' Iterate through all component occurrences in the assembly
    For Each oOcc As ComponentOccurrence In oCompDef.Occurrences
        ' Check if occurrence is valid
        If oOcc Is Nothing Then Continue For
        
        ' Get the document associated with the occurrence
        Dim oSubDoc As Document
        Try
            oSubDoc = oOcc.Definition.Document
        Catch ex As Exception
            Continue For ' Skip if document cannot be accessed
        End Try
        
        ' Check if sub-document is valid
        If oSubDoc Is Nothing Then Continue For
        
        ' Clone the parent occurrences collection and add the current occurrence
        Dim oNewParentOccurrences As New Collection
        For Each item In oParentOccurrences
            oNewParentOccurrences.Add (item)
        Next
        oNewParentOccurrences.Add (oOcc)
        
        ' Process nested parts
        If oSubDoc.DocumentType = kPartDocumentObject Then
            ProcessNestedPartInView(oView, oSubDoc, oNewParentOccurrences)
        ElseIf oSubDoc.DocumentType = kAssemblyDocumentObject Then
            ' Recursively process nested assemblies
            ProcessAssemblyInView(oView, oSubDoc, oNewParentOccurrences)
        End If
    Next
End Sub

Sub ProcessNestedPartInView(oView As DrawingView, oDoc As PartDocument, oParentOccurrences As Collection)
    ' Check if inputs are valid
    If oView Is Nothing Or oDoc Is Nothing Or oParentOccurrences Is Nothing Then Exit Sub
    
    ' Get the component definition of the part
    Dim oCompDef As PartComponentDefinition
    oCompDef = oDoc.ComponentDefinition
    
    ' Check if component definition is valid
    If oCompDef Is Nothing Then Exit Sub
    
    ' Iterate through all solid bodies in the part
    For Each oBody As SurfaceBody In oCompDef.SurfaceBodies
        ' Check if body is valid and its name starts with "-"
        If oBody IsNot Nothing AndAlso Left(oBody.Name, 1) = "-" Then
            Try
                ' Create a proxy for the surface body in the context of the full occurrence path
                Dim oBodyProxy As Object = oBody
                If oParentOccurrences.Count > 0 Then
                    Dim oCurrentOcc As ComponentOccurrence
                    ' Apply proxy creation for each level of nesting in reverse order
                    For i = oParentOccurrences.Count To 1 Step -1
                        oCurrentOcc = oParentOccurrences(i)
                        Dim oTempProxy As Object
                        oCurrentOcc.CreateGeometryProxy(oBodyProxy, oTempProxy)
                        If oTempProxy IsNot Nothing Then
                            oBodyProxy = oTempProxy
                        End If
                    Next
                End If
                
                ' Turn off visibility of the body proxy in the drawing view
                If oBodyProxy IsNot Nothing Then
                    oView.SetVisibility(oBodyProxy, False)
                End If
            Catch ex As Exception
                ' Log error silently or handle as needed
            End Try
        End If
    Next
End Sub


Edited: UPD.

INV 2025.3
0 Likes
Message 5 of 8

kresh.bell
Collaborator
Collaborator

Hi @Ivan_Sinicyn 

much better, but it seems like it doesn't work if it's in a subassembly

kreshbell_0-1742812923651.png

 

but it works if it's a part placed assembly

kreshbell_1-1742812991596.png

 

0 Likes
Message 6 of 8

Ivan_Sinicyn
Advocate
Advocate

@kresh.bell 

I updated the second post with the code 5 minutes ago. Check it again. It's working for me.

INV 2025.3
0 Likes
Message 7 of 8

kresh.bell
Collaborator
Collaborator

unfortunately, dont't works for me for sub sub sub assembly here

kreshbell_1-1742815737022.png

 

0 Likes
Message 8 of 8

Ivan_Sinicyn
Advocate
Advocate

Okay, two problems. 

1. I misunderstood the task at hand.😬

2. I made visibility to be disabled for solids, but not for components containing them. But, I have it turned off at any nested levels and I don't understand why it is not turned off for you.

 

I will later change to disable visibility for parts that contain such solids.

 

Can you make a small assembly with a drawing file where the code doesn't work?

INV 2025.3
0 Likes