Instantiating SubOccurances

Instantiating SubOccurances

Anonymous
Not applicable
417 Views
5 Replies
Message 1 of 6

Instantiating SubOccurances

Anonymous
Not applicable

Hello

 

Perhaps I've been looking at this for too long, but I can't figure out why I can't get a reference to a subOccurance by name.

 

I have been trying to get a reference for the Sub-Assembly named 'Control R7' using the following:

 

            'Set a reference to the active document.
            oInvAssyDoc = oInvApp.ActiveDocument

            'Set a reference to an active assembly component definition.
            oInvAssyCompDef = oInvAssyDoc.ComponentDefinition

            'Set a reference to the occurances in the component definition.
            oInvAssyCompOccs = oInvAssyCompDef.Occurrences

            'Create a reference to the 'Markers' ComponentOccurance.
            Dim location_markers_left_occ As Inventor.ComponentOccurrence = oInvAssyCompOccs.ItemByName("Location Markers Left")

            ' Get a reference to the Location Markers Left Component Occurances.
            Dim lm_left_sub_occs As Inventor.ComponentOccurrences = location_markers_left_occ.SubOccurrences

            ' Get a reference to the occurance named 'Control R7'
            Dim control_left As Inventor.ComponentOccurrence = lm_left_sub_occs.ItemByName("Control R7")

This does not work.

 

However if I loop through the occurances in lm_left_sub_occs I can set control_left as follows:

 

            For Each comp_occ As Inventor.ComponentOccurrence In lm_left_sub_occs

                If comp_occ.Name = ("Control R7") Then
                    Console.WriteLine("Control R7 is here!")
                    control_left = comp_occ
                End If

            Next

 Can anyone please explain why I can't access the occurance by name.

 

Regards

 

Chris

 

0 Likes
418 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable

Chris,

 

It looks like you only left out one small detail, the occurrence name is the document name and also the instance of that document. For example, if you have your subassembly named "Control R7" you could reference it like this:

 

 

If m_inventorApplication.ActiveDocument.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then       

       

Dim aDoc As Inventor.AssemblyDocument = m_inventorApplication.ActiveDocument              

Dim iOcc As ComponentOccurrence = aDoc.ComponentDefinition.Occurrences.ItemByName("Control R7:1") 

'...   


End If 

 

 

As far as I know, you need to do it this way to use ItemByName.

 

Regards, 

Nelson

0 Likes
Message 3 of 6

Anonymous
Not applicable

Thank you for your thoughts.

 

To be clear I have named the Occurance 'Control R7' in the Browser. I know that the name is this because if I iterate through all occurances in the parent assembly looking for 'Control R7' I find it and can create a reference to it. I can't see why I can't simply get the item by name from the parent's occurances.

 

Regards

 

Chris

0 Likes
Message 4 of 6

Anonymous
Not applicable

Ok, I didn't realized you had renamed it. 

 

My next thought would be, if oInvApp.ActiveDocument is the Parent of Control R7 then why can't you just do this:

 

oInvAssyDoc = oInvApp.ActiveDocument

oInvAssyCompDef = oInvAssyDoc.ComponentDefinition

oInvAssyCompOccs = oInvAssyCompDef.Occurrences

Dim control_left As Inventor.ComponentOccurrence = oInvAssyCompOccs.ItemByName("Control R7")

0 Likes
Message 5 of 6

Anonymous
Not applicable

The assembly which I want to get a reference to so that I can set its Visible property, which I have named 'Control R7', is within an assembly named 'Location Markers Left' which is within the top level assembly.

 

I thought I should:

  • Get a reference of the occurances in the top level componentDefinition.
  • Get a reference of the itemByName 'Location Markers Left' in the top level occurances.
  • Get a reference of the subOccurances of 'Location Markers Left'.
  • Get a reference of the itemByName 'Control R7' in the subOccurances.

 

'Set a reference to the active document.
oInvAssyDoc = oInvApp.ActiveDocument

'Set a reference to an active assembly component definition.
oInvAssyCompDef = oInvAssyDoc.ComponentDefinition

'Set a reference to the occurances in the component definition.
oInvAssyCompOccs = oInvAssyCompDef.Occurrences

'Create a reference to the 'Markers' ComponentOccurance.
Dim location_markers_left_occ As Inventor.ComponentOccurrence = oInvAssyCompOccs.ItemByName("Location Markers Left")

' Get a reference to the Location Markers Left Component Occurances.
Dim lm_left_sub_occs As Inventor.ComponentOccurrences = location_markers_left_occ.SubOccurrences

' Get a reference to the occurance named 'Control R7'
Dim control_left As Inventor.ComponentOccurrence = lm_left_sub_occs.ItemByName("Control R7")

 This just gives me an error: System.NotImplementedException: Not implemented (Exception from HRESULT: 0x800040001 (E_NOTIMPL))...

 

Curiously though if I replace the itemByName statement and iterate through the same reference to the subOccurances testing the Name property for 'Control R7' it is found and the reference created!

 

For Each comp_occ As Inventor.ComponentOccurrence In lm_left_sub_occs

  Select Case comp_occ.Name

    ' R7
    Case "Control R7"
    control_left = comp_occ

  End Select

Next

 Is it me or is that odd? I must be missing something obvious.

 

Thanks

0 Likes
Message 6 of 6

Anonymous
Not applicable

Hmmm, it looks like the problem could be in the '.suboccurrence' part. I think that isn't going to return what you want.

I made a small change, see if this works:

 

 

 

 'Set a reference to the active document.           

oInvAssyDoc = oInvApp.ActiveDocument
'Set a reference to an active assembly component definition.           

oInvAssyCompDef = oInvAssyDoc.ComponentDefinition
            'Set a reference to the occurances in the component definition.           

oInvAssyCompOccs = oInvAssyCompDef.Occurrences                        

'Create a reference to the 'Markers' ComponentOccurance.           

Dim location_markers_left_occ As Inventor.ComponentOccurrence = oInvAssyCompOccs.ItemByName("Location Markers Left")                        

' Get a reference to the occurance named 'Control R7'           

Dim control_left As Inventor.ComponentOccurrence = location_markers_left_occ.Definition.Occurrences.ItemByName("Control R7")

0 Likes