ParentOccurrence seems broken in Inventor Professional R. 2018.3.8

ParentOccurrence seems broken in Inventor Professional R. 2018.3.8

Yadow
Enthusiast Enthusiast
470 Views
3 Replies
Message 1 of 4

ParentOccurrence seems broken in Inventor Professional R. 2018.3.8

Yadow
Enthusiast
Enthusiast

Greetings,

I have an open assembly ("AssemblyTop"). In this assembly I have another assembly ("AssemblyMid"), and within that assembly I have several Occurrences of parts and assemblys ("AssemblyLowest" and "PartLowest").

 

I want to loop and get the ParentOccurrence.name of all of my "Lowest" Components, but I only get an error when I get to the "Debug.print oSubOcc.ParentOccurrence.Name" -part.

Am I missing something or is there something wrong in the API? 

Code posted below.

 

Sub TestSubSubParentName()

Dim oDoc As AssemblyDocument


Set oDoc = ThisApplication.ActiveDocument

Dim oOcc As ComponentOccurrence

 

For Each oOcc In oDoc.ComponentDefinition.Occurrences
     If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
        Dim oSubAsm As AssemblyDocument
        Set oSubAsm = oOcc.Definition.Document


        Dim oSubOcc As ComponentOccurrence


        For Each oSubOcc In oSubAsm.ComponentDefinition.Occurrences
            Debug.Print oSubOcc.ParentOccurrence.Name 'This is where my code breaks for unknown reasons...
        Next
    End If
Next

End Sub

 

Thanks in advance.

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

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @Yadow 

You must stay in the context of the top assembly. When you go in to the document of the subassembly, it's no longer an occurrence in the top assembly but o document object, hence not a parent occurrence.

 

Try this code:

Sub Test()
Dim oDoc As AssemblyDocument

Set oDoc = ThisApplication.ActiveDocument

Dim oOcc As ComponentOccurrence

 

For Each oOcc In oDoc.ComponentDefinition.Occurrences
     If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
        'Dim oSubAsm As AssemblyDocument  -  DON'T DO THIS
        'Set oSubAsm = oOcc.Definition.Document  -  DON'T DO THIS

        Dim oSubOcc As ComponentOccurrence


        For Each oSubOcc In oOcc.SubOccurrences
            Debug.Print oSubOcc.ParentOccurrence.Name
        Next
    End If
Next
End Sub
Message 3 of 4

Yadow
Enthusiast
Enthusiast

Never Mind! I just found out by trying that I needed to use oOcc.SubOccurrences instead of o oSubAsm.Componentdefinition.Document.

Silly me.

0 Likes
Message 4 of 4

Yadow
Enthusiast
Enthusiast

Thanks a lot 🙂
It was quite obvious once I got the mindset straight.