Occurrences, Contexts, Definitions, Proxies

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

There is already a blog post on this topic, but it might be worth looking at it from another angle as well. In this case I’ll try to talk about it through geometric information of the model.

Let’s say we have the following document structure: Assembly >> SubAssembly >> Part

Proxies2 

Part is placed inside SubAssembly at X=3, Y=4 position, and SubAssembly is placed inside Assembly at X=1, Y=2 position. Let’s say that the highlighted vertex is the first in the vertex list of the model: prtDef.SurfaceBodies(1).Vertices(1)

Proxies 

The Vertex is the native object residing inside the part document. A VertexProxy is the proxy object that represents the Vertex inside another document. A proxy represents a native object inside another document and provides all the same properties but with values in context of the document that is referencing the native object’s owner document. The vertex of the part is at X=6, Y=5 inside the part. If we get to the same vertex from SubAssembly through Occurrences/SubOccurrences then we’ll see it as a VertexProxy and the information will be provided in context of SubAssembly so the Point information of the vertex will be X=9, Y=9. If we get to the same vertex from the Assembly then the VertexProxy’s Point will provide values in context of Assembly so it will be X=10, Y=11.  

Sub GetVertex()
Dim docs As Documents
Set docs = ThisApplication.Documents
' Assembly
Dim asm As AssemblyDocument
Set asm = docs.Open("C:OccurrencesAssembly.iam")
Dim asmDef As AssemblyComponentDefinition
Set asmDef = asm.ComponentDefinition
' SubAssembly
Dim subAsm As AssemblyDocument
Set subAsm = docs.Open("C:OccurrencesSubAssembly.iam")
Dim subAsmDef As AssemblyComponentDefinition
Set subAsmDef = subAsm.ComponentDefinition
' Part
Dim prt As PartDocument
Set prt = docs.Open("C:OccurrencesPart.ipt")
Dim prtDef As PartComponentDefinition
Set prtDef = prt.ComponentDefinition
' Let's get the vertex in context of the Part
Dim vPart As Vertex
Set vPart = prtDef.SurfaceBodies(1).Vertices(1)
Debug.Print "In context of Part: " + _
"x = " + str(vPart.Point.X) + "; " + _
"y = " + str(vPart.Point.Y)
' Let's get it in context of SubAssembly
' In this case we'll get a VertexProxy
' that represents Vertex of the Part
' Note: we could still declare it as Vertex
Dim vSubAssembly As VertexProxy
Set vSubAssembly = _
subAsmDef.Occurrences(1).SurfaceBodies(1).Vertices(1)
Debug.Print "In context of SubAssembly: " + _
"x = " + str(vSubAssembly.Point.X) + "; " + _
"y = " + str(vSubAssembly.Point.Y)
' We could also break the chain to get to the occurrence's
' definition by using the Definition property
' Some objects can only be reached from the ComponentDefinition
' in which case we need to create a GeometryProxy for it ourselves
' When using the Occurrences/SubOccurrences properties
' then the proxies are available without us having to create them
Debug.Print _
"Note: subAsmDef.Occurrences(1).Definition = prtDef is " + _
str(subAsmDef.Occurrences(1).Definition Is prtDef)
' Let's get it in context of Assembly
' In this case we'll get a VertexProxy
' that represents Vertex of the Part
' Note: we could still declare it as Vertex
Dim vAssembly As VertexProxy
Set vAssembly = asmDef.Occurrences(1).SubOccurrences(1). _
SurfaceBodies(1).Vertices(1)
Debug.Print "In context of Assembly: " + _
"x = " + str(vAssembly.Point.X) + "; " + _
"y = " + str(vAssembly.Point.Y)
Debug.Print _
"Note: asmDef.Occurrences(1).Definition = subAsmDef is " + _
str(asmDef.Occurrences(1).Definition Is subAsmDef)
Debug.Print _
"Note: asmDef.Occurrences(1).SubOccurrences(1).Definition" + _
" = prtDef is " + _
str(asmDef.Occurrences(1).SubOccurrences(1).Definition Is prtDef)
' If you wanted to get to the vertex in context of the Assembly
' through one of the definitions then you just have to close the
' loop of Assembly >> SubAssembly >> Part >> Vertex
' If you got the vertex from Part then you can do it by creating the
' proxy on the Part's occurrence
Call asmDef.Occurrences(1).SubOccurrences(1). _
CreateGeometryProxy(vPart, vAssembly)
Debug.Print "In context of Assembly #1: " + _
"x = " + str(vAssembly.Point.X) + "; " + _
"y = " + str(vAssembly.Point.Y)
' If you got the vertex (VertexProxy) from SubAssembly then you can
' do it by creating proxy on the SubAssembly's occurrence
Call asmDef.Occurrences(1). _
CreateGeometryProxy(vSubAssembly, vAssembly)
Debug.Print "In context of Assembly #2: " + _
"x = " + str(vAssembly.Point.X) + "; " + _
"y = " + str(vAssembly.Point.Y)
End Sub

The above VBA sample code will print this to the Immediate window of the VBA environment:

In context of Part: x =  6; y =  5
In context of SubAssembly: x =  9; y =  9
Note: subAsmDef.Occurrences(1).Definition = prtDef is True
In context of Assembly: x =  10; y =  11
Note: asmDef.Occurrences(1).Definition = subAsmDef is True
Note: asmDef.Occurrences(1).SubOccurrences(1).Definition = prtDef is True
In context of Assembly #1: x =  10; y =  11
In context of Assembly #2: x =  10; y =  11

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading