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

yuhanzhang,

 

My code is basically the same as yours except that I also check the DerivedAssemblyComponents.Count. However, as soon as the SetVisibility method is called on an occurrence of a derived component that is in a sub-assembly, the following error is thrown. 
"Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))"

 

 

Have you tried your code out to see if it works in that situation?

 

Darren

 

 

    Private Function HideAllDerivedPartsInSelectedView(ByRef SelectedView As Inventor.DrawingView, ByVal ShowParts As Boolean, ByRef SubAssemblyDoc As Inventor.AssemblyDocument) As Boolean
        Dim AssemblyInView As Inventor.AssemblyDocument = Nothing
        Dim AssemblyComponentDefObject As Inventor.AssemblyComponentDefinition = Nothing
        Dim ComponentDefObject As Inventor.PartComponentDefinition = Nothing
        Dim DocumentInView As Inventor.Document = Nothing
        Dim OccurrenceCollection As Inventor.ComponentOccurrences = Nothing
        Dim OccurrenceObject As Inventor.ComponentOccurrence = Nothing
        Dim PartDocumentObject As Inventor.PartDocument = Nothing
        Dim ReferenceComponentCollection As Inventor.ReferenceComponents = Nothing
        Dim ReferencedDocObject As Inventor._Document = Nothing
        Dim ReturnBoolean As Boolean = False

        Try
            If SubAssemblyDoc Is Nothing Then 'This is the first time the routine has been called
                DocumentInView = SelectedView.ReferencedDocumentDescriptor.ReferencedDocument

                If Not TypeOf DocumentInView Is Inventor.AssemblyDocument Then
                    MsgBox("The Document In The View Needs To Be An Assembly!", MsgBoxStyle.Information, "WRONG DOCUMENT TYPE!!!")
                    Exit Try
                End If

                AssemblyInView = CType(DocumentInView, Inventor.AssemblyDocument)
            Else
                AssemblyInView = SubAssemblyDoc
            End If

            AssemblyComponentDefObject = AssemblyInView.ComponentDefinition
            OccurrenceCollection = AssemblyComponentDefObject.Occurrences

            For Each OccurrenceObject In OccurrenceCollection
                If OccurrenceObject.ReferencedDocumentDescriptor Is Nothing Then Continue For

                ReferencedDocObject = OccurrenceObject.ReferencedDocumentDescriptor.ReferencedDocument

                If TypeOf ReferencedDocObject Is Inventor.PartDocument Then
                    PartDocumentObject = CType(ReferencedDocObject, Inventor.PartDocument)
                    ComponentDefObject = PartDocumentObject.ComponentDefinition
                    ReferenceComponentCollection = ComponentDefObject.ReferenceComponents

                    If ReferenceComponentCollection.DerivedPartComponents.Count = 0 And ReferenceComponentCollection.DerivedAssemblyComponents.Count = 0 Then Continue For
                    'show or hide the occurrence of the derived part or assembly
                    SelectedView.SetVisibility(OccurrenceObject, ShowParts)
                ElseIf TypeOf ReferencedDocObject Is Inventor.AssemblyDocument Then
                    'recursively go through the sub assembly and look for derived parts
                    HideAllDerivedPartsInSelectedView(SelectedView, ShowParts, CType(ReferencedDocObject, Inventor.AssemblyDocument))
                End If
            Next

            ReturnBoolean = True
        Catch ex As Exception
            MsgBox(ex.Message)
            ReturnBoolean = False
        Finally
            ReleaseCOMObject(AssemblyInView)
            ReleaseCOMObject(AssemblyComponentDefObject)
            ReleaseCOMObject(DocumentInView)
            ReleaseCOMObject(OccurrenceCollection)
            ReleaseCOMObject(OccurrenceObject)
            ReleaseCOMObject(PartDocumentObject)
            ReleaseCOMObject(ReferencedDocObject)
            GC.WaitForPendingFinalizers()
            GC.Collect()
        End Try

        Return ReturnBoolean
    End Function