Hi @Koekepeerke Although this task may seem fairly simple when done manually within the model window, it is actually fairly challenging & complicated to do by code, depending on how you plan on specifying the input geometries, and/or how you plan on identifying the target components and the geometry within them. In assemblies, there is no real model geometry such as faces, edges, or vertices, nor the work features or sketches that may be within any of the components. All that stuff is seen as proxy objects within the assembly. Only work features and sketches created directly within the assembly itself will not be proxy objects. So, the face of a top level component will be a FaceProxy within the assembly, and a WorkPlane within a top level component will be a WorkPlaneProxy within the main assembly. However, if a component is down multiple levels deep within sub assemblies, you must start from the component in which the real geometry exists, then work your way back up through each component level to the top assembly, creating/obtaining a Proxy of the child level object within the parent level component. This must be done for each parent component until you done with a top level component, which creates the proxy within the main assembly. Then that proxy object can be used for measurements, constraints, and the like within the main assembly.
The WorkPlane within the Part only exists within the 3D coordinate system of that part, so you can't measure between it and something that only exists within the 3D coordinate system of the main assembly, because they are two different coordinate systems. Both items must exist within the same coordinate system before they can be measured between them.
Based on your description of the situation, and the names used within your code, I created some iLogic code you can play around with. I did not see the name of the 'Part' that was supposed to be within the sub assembly, so I am just specifying the name "Part1" within this code. You will most likely need to change that before running this, or it will throw an error and not work. I also assumed that both of those named work planes exist within both of the parts, and that you wanted to measure between one plane in the on part, and the other plane within the other part, but as they are situated within the main assembly. I included several lines of comments within the code, to help you follow along with what is going on.
Sub Main
If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisDoc.Document
oADef = oADoc.ComponentDefinition
oOccs = oADef.Occurrences
'specify the component representing the first sub-assembly
oSubAsm1 = oOccs.ItemByName("Subassem1")
'specify the component representing the part within that sub-assembly
oSubPrt1 = oSubAsm1.Definition.Occurrences.ItemByName("Part1")
'specify the WorkPlane within that part
Dim oWP1 As WorkPlane = oSubPrt1.Definition.WorkPlanes.Item("endplane_left")
'create a WorkPlaneProxy to represent that WorkPlane within the sub-assembly
Dim oSubAsm1LevelWPProxy As WorkPlaneProxy = Nothing
'set its value using the part component's method
oSubPrt1.CreateGeometryProxy(oWP1, oSubAsm1LevelWPProxy)
'create a WorkPlaneProxy to represent that WorkPlaneProxy within the main assembly
Dim oMainAsmLevelWPProxy1 As WorkPlaneProxy = Nothing
'set its value using the sub-assembly component's method
oSubAsm1.CreateGeometryProxy(oSubAsm1LevelWPProxy, oMainAsmLevelWPProxy1)
'repeat above steps to get other half of geometry to measure between
oSubAsm2 = oOccs.ItemByName("Subassem2")
oSubPrt2 = oSubAsm2.Definition.Occurrences.ItemByName("Part1")
Dim oWP2 As WorkPlane = oSubPrt2.Definition.WorkPlanes.Item("endplane_right")
Dim oSubAsm2LevelWPProxy As WorkPlaneProxy = Nothing
oSubPrt2.CreateGeometryProxy(oWP2, oSubAsm2LevelWPProxy)
Dim oMainAsmLevelWPProxy2 As WorkPlaneProxy = Nothing
oSubPrt1.CreateGeometryProxy(oSubAsm2LevelWPProxy, oMainAsmLevelWPProxy2)
'now we have the two geometries to measure between (WorkPlaneProxys)
'now get the MeasureTools object
oMTools = ThisApplication.MeasureTools
'use the appropriate method to measure
oDist = oMTools.GetMinimumDistance(oMainAsmLevelWPProxy1, oMainAsmLevelWPProxy2)
'result may be in 'database units' (centimeters), instead of 'document units'
'so, prepare to convert the units to document units
UOM = oADoc.UnitsOfMeasure
If UOM.LengthUnits <> UnitsTypeEnum.kDatabaseLengthUnits Then
oDist = UOM.ConvertUnits(oDist, UnitsTypeEnum.kDatabaseLengthUnits, UOM.LengthUnits)
End If
'show results
MsgBox("Distance = " & oDist,,"")
End Sub
Wesley Crihfield

(Not an Autodesk Employee)