Include Sketch from a Part multi-level deep

<?xml encoding=”UTF-8″>By Adam Nagy

We already have a blog post on Include sketches from sub-assemblies in a DrawingView using a sketch proxy, but it does not cover the scenario where the Document the Sketch resides in is multi-level deep in the assembly hierarchy – i.e. it’s a SubOccurrence of an Occurrence.

As I mentioned in this blog post you have to make sure that you are getting objects in the right context. You can either do that by reaching objects only through Occurrences and SubOccurrences collections (never using Definition) or if you have to use Definition then get the object back into the right context using CreateGeometryProxy().
If you do not follow that correctly, like the below code, then your program won’t work:

IncludeSketchFromPart

Whenever you use the property “Definition” you leave the current context and move into a lower one. As you can see the code only brings the oPrtSketch entity into the context of the MidAssembly.iam document. However, the DrawingView is referencing TopAssembly.iam, so that is the context the objects you pass to it should be in.  

The quickest fix to the above code is taking note of the occurrence of MidAssembly.iam inside TopAssembly.iam (oMidAsmOcc) and doing an extra call to CreateGeometryProxy to bring the object into the context of TopAssembly.iam. New code highlighted in red: 

Sub IncludeSketchFromPart()
Dim oDwg As DrawingDocument
Set oDwg = ThisApplication.ActiveDocument
Dim oView As DrawingView
Set oView = oDwg.ActiveSheet.DrawingViews(1)
Dim oTopAsm As AssemblyDocument
Set oTopAsm = oView.ReferencedDocumentDescriptor.ReferencedDocument
'Dim oMidAsmDef As AssemblyComponentDefinition
'Set oMidAsmDef = oAssy.ComponentDefinition.Occurrences.ItemByName("MidAssembly:1").Definition
<span>Dim oMidAsmOcc As ComponentOccurrence
Set oMidAsmOcc = oTopAsm.ComponentDefinition.Occurrences.ItemByName("MidAssembly:1")</span>
Dim oMidAsmDef As AssemblyComponentDefinition
Set oMidAsmDef = oMidAsmOcc.Definition
Dim oPrtOcc As ComponentOccurrence
Set oPrtOcc = oMidAsmDef.Occurrences.ItemByName("PartWithSketch:1")
Dim oSketch As PlanarSketch
Set oSketch = oPrtOcc.Definition.Sketches("PartSketch")
Dim oSketchProxy As PlanarSketchProxy
' This will get it into context of "MidAssembly.iam"
Call oPrtOcc.CreateGeometryProxy(oSketch, oSketchProxy)
<span>' This will get it into context of "TopAssembly.iam"
Call oMidAsmOcc.CreateGeometryProxy(oSketchProxy, oSketchProxy)</span>
Call oView.SetVisibility(oSketchProxy, True)
End Sub

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading