Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
nstevelmans
in reply to: Shag_Bore

Hi, try this code it has a recursive function so it will find every Occurrence in the assembly tree.

 

Sub main
	 Dim CATEGORYVALUE As String = "ex.WELDMENT"
        ' Get the active assembly.
        Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
        Dim oAssyCompdef As ComponentDefinition = oAsmDoc.ComponentDefinition
        Dim ViewRep As DesignViewRepresentation = SetDesignview(oAssyCompdef, CATEGORYVALUE)

        ' Call the function that does the recursion.
        TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1, CATEGORYVALUE)

        ViewRep.Locked = True
	
End Sub


Private Sub TraverseAssembly(Occurrences As ComponentOccurrences, Level As Integer, PropValue As String)
        ' Iterate through all of the occurrence in this collection.  This
        ' represents the occurrences at the top level of an assembly.
        Dim oOcc As ComponentOccurrence
        Dim value As String = Nothing
        For Each oOcc In Occurrences
            ' Print the name of the current occurrence.
            Dim oCompdef As ComponentDefinition = oOcc.Definition
            Dim oDoc As Document = oCompdef.Document
            Dim oCustomprop As Inventor.Property = Getproperty(oDoc, "CATEGORY")
            oOcc.Visible = False
            If oCustomprop IsNot Nothing Then
                If oCustomprop.Value = PropValue Then
                    oOcc.Visible = True
                End If
            End If
            ' Check to see if this occurrence represents a subassembly
            ' and recursively call this function to traverse through it.
            If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
                TraverseAssembly(oOcc.SubOccurrences, Level + 1, PropValue)
            End If
        Next
    End Sub

Private Function Getproperty(ByVal oDoc As Document, ByVal Prop As String)
        Dim result As Inventor.Property = Nothing
        Try
            Dim oPropertyset As PropertySet = oDoc.PropertySets("Inventor User Defined Properties")
            Dim oProperty As Inventor.Property = Nothing
            Try
                oProperty = oPropertyset.Item(Prop)
            Catch ex As Exception
            End Try
            result = oProperty
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical, "Getproperty")
        End Try
        Return result
    End Function
	
	 Private Function SetDesignview(ByVal oAssyCompDef As AssemblyComponentDefinition, ViewRepName As String) As DesignViewRepresentation
        Dim result As DesignViewRepresentation = Nothing
        Dim ViewRep As DesignViewRepresentation = Nothing
        Dim ViewReps As DesignViewRepresentations = oAssyCompDef.RepresentationsManager.DesignViewRepresentations
        Try
            Dim ViewRepExist As Boolean = False
            For Each ViewRep In ViewReps
                If ViewRep.Name = ViewRepName Then
                    ViewRep.Activate()
                    ViewRepExist = True
                    Exit For
                End If
            Next
            If ViewRepExist = False Then
                ViewRep = oAssyCompDef.RepresentationsManager.DesignViewRepresentations.Add(ViewRepName)
                ViewRep.Activate()
            End If
        Catch ex As Exception
        End Try
        result = ViewRep
        Return result
    End Function