If I am explaining this correctly the issue here is the selecting of the occurrence is a ComponentOccurrenceProxy in the context of the assembly rather than a ComponentOccurrence in the sub assembly. So simply making it a native object will edit the part in the context of the sub assembly.
Sub Main()
' Doc Parameters
If ThisApplication Is Nothing Then Exit Sub
Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
asmCompDef_ = ThisDoc.Document.ComponentDefinition
Dim compOcc As ComponentOccurrence
While True
'Add object to work with the occurrence in the context of its parent and not as a proxy of the main assembly
compOcc = ThisServer.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "Select Component for hatch toggle").NativeObject
If compOcc Is Nothing
ThisServer.CommandManager.StopActiveCommand
Exit While
Else If compOcc.DefinitionDocumentType <> DocumentTypeEnum.kPartDocumentObject
MsgBox("Selected Occurrence is not a part.")
Else
setIso(compOcc)
End If
If compOcc Is Nothing OrElse Not compOcc.Name.Toupper = "CHBR" Then Exit While
End While
''Update the top assembly to update the graphics otherwise exitng the sub assembly.
oAsmDoc.Update
End Sub
Sub setIso(occ As ComponentOccurrence)
For Each sBody In occ.SurfaceBodies
If sBody.Name.Tolower = "hatch"
sBody.Visible = Not sBody.Visible
End If
Next sBody
End Sub This topic came up recently in a post and although I read it too late to help figure this one out (hence the below code I constructed) it makes explaining it much easier.
https://forums.autodesk.com/t5/inventor-ilogic-api-vba-forum/difference-between-componentoccurrence-...
Here is also another method to pass the selected occurrence to the sub assembly loop. The passing is done by filtering through the occurrence name. if you tried to make the two objects equal you will get an error message about ComponentOccurrenceProxy not equal to the ComponentOccurrence
Sub Main()
' set a reference to the assembly document.
Dim oAsmDoc As AssemblyDocument
oAsmDoc = ThisApplication.ActiveDocument
'set a reference to the assembly component definintion.
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = oAsmDoc.ComponentDefinition
'define the Component Occurrence collection
Dim oCompOcc As Inventor.ComponentOccurrence
'set top level view rep to Default
oAsmCompDef.RepresentationsManager.DesignViewRepresentations.Item("Edit").activate
For Each oCompOcc In oAsmCompDef.Occurrences
If oCompOcc.Name = "Sub:1" Then
oCompOcc.SetDesignViewRepresentation("Edit", , True) 'True sets the view rep to be associative
End If
Next
'modify actual subassembly document
If oCompOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
'set a reference to the subassembly component definintion
compOccDef = oCompOcc.Definition
'Select Part
PickPart(oAsmDoc, compOccDef)
End If
End Sub
Sub PickPart(oAsmDoc,compOccDef)
If ThisApplication Is Nothing Then Exit Sub
asmCompDef_ = ThisDoc.Document.ComponentDefinition
Dim compOcc As ComponentOccurrence
While True
compOcc = ThisServer.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "Select Component for hatch toggle")
If compOcc Is Nothing
ThisServer.CommandManager.StopActiveCommand
Exit While
Else If compOcc.DefinitionDocumentType <> DocumentTypeEnum.kPartDocumentObject
MsgBox("Selected Occurrence is not a part.")
Else
'[Option: Work backwards from selected item if needed
'set a reference to the subassembly component definintion
'compOccDef = compOcc.ParentOccurrence.Definition
'For Each PickOcc In compOccDef.Occurrences
']
'Loop throught the subassembly occurrences
Dim oSubCompOcc As Inventor.ComponentOccurrence
For Each oSubCompOcc In compOccDef.Occurrences
'Check If picked occurrence (proxy) matches subassembly occurrence
If compOcc.Name = oSubCompOcc.Name Then
setIso(oSubCompOcc)
End If
Next
End If
If compOcc Is Nothing OrElse Not compOcc.Name.ToUpper = "CHBR" Then Exit While
End While
'Update the top assembly to update the graphics otherwise exitng the sub assembly.
oAsmDoc.Update
End Sub
Sub setIso(occ As ComponentOccurrence)
For Each sBody In occ.SurfaceBodies
If sBody.Name.Tolower = "hatch"
sBody.Visible = Not sBody.Visible
End If
Next sBody
End Sub
If this solved a problem, please click (accept) as solution.
Or if this helped you, please, click (like)
Regards
Alan