By Adam Nagy
The following blog post talks about ComponentOccurrence contexts, which is relevant in this case as well: http://adndevblog.typepad.com/manufacturing/2012/12/componentoccurrence-contexts-and-reference-keys.html
If you want to set the visibility of an object that can only be reached through a ComponentDefinition (PartComponentDefinition/AssemblyComponentDefinition) object and it resides in a sub assembly, then you need to bring it into the context of the main/top assembly using CreateGeometryProxy. This is what the following code does as well. This will make the sketch highlighted in the picture (Sketch 2) invisible.
Sub HideSubAsmSketch() Dim dwg As DrawingDocument Set dwg = ThisApplication.ActiveDocument Dim baseView As DrawingView Set baseView = dwg.Sheets(1).DrawingViews(1) Dim mainAsm As AssemblyDocument Set mainAsm = _ baseView.ReferencedDocumentDescriptor.ReferencedDocument Dim mainDef As AssemblyComponentDefinition Set mainDef = mainAsm.ComponentDefinition Dim subOcc As ComponentOccurrence Set subOcc = mainDef.Occurrences(1) Dim subDef As AssemblyComponentDefinition Set subDef = subOcc.Definition ' This does exactly the same as the below code. This ' way we'll get outside of the context of the main ' assembly and get inside the context of the sub assembly ' Set subDef = GetSubAsmDef() ' Anything we get through subDef will need to be ' brought into the context of mainAsm if ' that's where we want to set properties Dim sk As PlanarSketch Set sk = subDef.Sketches("Sketch 2") ' Let's bring sk into the mainAsm context through the ' occurrence object Dim skProxy As PlanarSketchProxy Call subOcc.CreateGeometryProxy(sk, skProxy) ' Now we can set the visibility for it Call baseView.SetVisibility(skProxy, False) End Sub Function GetSubAsmDef() As AssemblyComponentDefinition Dim asm As AssemblyDocument Set asm = ThisApplication.Documents.Open("SubAsm.iam") Set GetSubAsmDef = asm.ComponentDefinition End Function