Measure distance between workplanes of parts inside an assembly

Measure distance between workplanes of parts inside an assembly

Koekepeerke
Advocate Advocate
528 Views
3 Replies
Message 1 of 4

Measure distance between workplanes of parts inside an assembly

Koekepeerke
Advocate
Advocate

Hello everyone,

 

For a module i'm building i need to measure the distance between 2 workplanes.

However, de workplanes are inside a part that is inside a subassembly that is inside the main assembly.

the subassembly has a unique name, but the part inside the subassem exists in multiple subassems.

How do i declare the workplane of the part of the specific subassem from the main assem.

I know about: 

Measure.MinimumDistance(plane1, plane2)

but i cant get the reference to the planes right. i tried several things but i cant seem to figure it out (i'm new to API). 

 

Dim oDoc As AssemblyDocument
oADoc = ThisApplication.ActiveDocument

Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition

Dim occ1 As ComponentOccurrence 
occ1 = oADef.Occurrences.ItemByName("Subassem1")

Dim occ2 As ComponentOccurrence
occ2 = oADef.Occurrences.ItemByName("Subassem2")

Dim plane1 As WorkPlane = occ1.Definition.workplanes.ItemByName("endplane_left")
Dim plane2 As WorkPlane = occ2.Definition.workplanes.ItemByName("endplane_right")

distance = Measure.MinimumDistance(plane1, plane2)
MessageBox.Show(distance & "cm")
messageBox.Show(distance/10 & "mm")

 Thanks for your time!

0 Likes
Accepted solutions (1)
529 Views
3 Replies
Replies (3)
Message 2 of 4

WCrihfield
Mentor
Mentor
Accepted solution

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

EESignature

(Not an Autodesk Employee)

Message 3 of 4

Koekepeerke
Advocate
Advocate

Thanks a lot @WCrihfield everything seems to work! thanks for the solution itself but also for the detailed explanation you gave with it, it really helps me understand ilogic and the inventor API better. Also the unit conversion function is very usefull to know about. I knew about the database units of centimeters so i just did times 10 for millimeters (in my example i accidentally divided by 10 😅) .

 

regards,

Jeremy

0 Likes
Message 4 of 4

JoeyHe
Explorer
Explorer

JoeyHe_0-1741763527621.png

I ran this rule ,but the code marked red above occurs error. Please confirm that this rule works correctly on your computer?

 

0 Likes