Showing Only Sheetmetal and Weldment Parts

Showing Only Sheetmetal and Weldment Parts

izaakHNEB8
Participant Participant
224 Views
2 Replies
Message 1 of 3

Showing Only Sheetmetal and Weldment Parts

izaakHNEB8
Participant
Participant

I have this code which so far it will only show the sheet metal parts and weldments within the top level on the assembly. 

Dim oAssyDoc As AssemblyDocument
Dim oComp As ComponentOccurrence
Dim oPartDoc As PartDocument
Dim oSheetMetalDef As SheetMetalComponentDefinition
Dim oWeldmentDef As WeldmentComponentDefinition

oAssyDoc = ThisApplication.ActiveDocument

' Iterate through each component in the assembly
For Each oComp In oAssyDoc.ComponentDefinition.Occurrences
    ' Skip suppressed components
    If oComp.Suppressed = False Then
        ' Check if the component is a part document
        If TypeOf oComp.Definition.Document Is PartDocument Then
            ' Try to cast the document to PartDocument
            oPartDoc = oComp.Definition.Document

            ' Check if the part is a sheet metal part
            If Not oPartDoc.ComponentDefinition Is Nothing Then
                If TypeOf oPartDoc.ComponentDefinition Is SheetMetalComponentDefinition Then
                    ' Show sheet metal part
                    oComp.Visible = True
                Else
                    ' Hide non-sheet metal parts
                    oComp.Visible = False
                End If
            End If
        ElseIf TypeOf oComp.Definition Is WeldmentComponentDefinition Then
            ' Show weldments, even if it's an assembly
            oComp.Visible = True
        ElseIf TypeOf oComp.Definition Is AssemblyComponentDefinition Then
            ' Hide other assemblies
            oComp.Visible = False
        End If
    End If
Next

I want to include within the code that any sub assemblies that contain sheet metal components or weldments will only show there as well. I would prefer that the documents are not opened and the visibility is only at the top level. Like at the active edit level verse opening a document and making the changes. 

The goal for this is I'm trying to nest sheet metal and weldments, but I don't want to have an assembly with alot of parts that can't be nested.

Any insight would be appreciated.

0 Likes
Accepted solutions (1)
225 Views
2 Replies
Replies (2)
Message 2 of 3

JelteDeJong
Mentor
Mentor

You could try this. (I can't test it at the moment.) The GUID is taken from this site.

    Public Sub Main()

        Dim doc As AssemblyDocument = ThisApplication.ActiveDocument

        For Each subOcc As ComponentOccurrence In doc.ComponentDefinition.Occurrences
            SetVisability(subOcc)
        Next

    End Sub

    Private Sub SetVisability(occ As ComponentOccurrence)

        Dim refDoc As Document = occ.Definition.Document
        Dim subType = refDoc.SubType

        If (subType = "{4D29B490-49B2-11D0-93C3-7E0706000000}" Or ' <= Sheetmetal part
            subType = "{28EC8354-9024-440F-A8A2-0E0E55D635B0}") Then ' <= Weld assembly
            occ.Visible = True
        Else
            occ.Visible = False
        End If

        If (refDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject) Then
            For Each subOcc As ComponentOccurrence In occ.SubOccurrences
                SetVisability(subOcc)
            Next
        End If

    End Sub

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 3 of 3

izaakHNEB8
Participant
Participant
Accepted solution

Thank you for the quick reply. The code you send got me going back in the right direction. I did have to change some of the GUID values and to include some code about cases of suppressed parts. 

    Public Sub Main()

        Dim doc As AssemblyDocument = ThisApplication.ActiveDocument

        For Each subOcc As ComponentOccurrence In doc.ComponentDefinition.Occurrences
            SetVisability(subOcc)
        Next

    End Sub

    Private Sub SetVisability(occ As ComponentOccurrence)

		If occ.Suppressed = False Then
        Dim refDoc As Document = occ.Definition.Document
        Dim subType = refDoc.SubType
	        If (subType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Or ' <= Sheetmetal part
	            subType = "{28EC8354-9024-440F-A8A2-0E0E55D635B0}") Then ' <= Weld assembly
	            occ.Visible = True
	        Else
	            occ.Visible = False
	        End If
	
	        If (subType = "{E60F81E1-49B3-11D0-93C3-7E0706000000}") Then
	            For Each subOcc As ComponentOccurrence In occ.SubOccurrences
	                SetVisability(subOcc)
	            Next
	        End If
		End If
    End Sub

 

0 Likes