Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Get occurrence from selected geometry within component

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
CAD_CAM_MAN
364 Views, 6 Replies

Get occurrence from selected geometry within component

I am trying to develop an add in that allows a user to select a User coordinate system (UCS)  in any part or sub assembly within the active top level assembly and then do some really cool stuff. I am unsure how to get the occurrence from the selected UCS to create the geometry proxy so I can get the transformation within the context of the top level assembly. Can somebody share an example of how to go about doing this?  

 

Below is the just of where I am currently at with the code. This does not work as I expected. The output for the proxy is the same as the UCS. The output for both is correct at the part level the UCS is in not the top level. Output as follows...

 

Proxy ?
X 71.2562842475752
Y -86.2648285528555
Z -12.7341406841421

UCS
X 71.2562842475752
Y -86.2648285528555
Z -12.7341406841421

 

 

It seems there should be a more direct way to get the occurrence than going through the iterations. This method will also be an issue in the case of more than one occurrence of the same component as there is a chance it will not  select the correct occurrence.  

 

	Public Sub CCTExecutionCommand()
		Dim Doc As Document = g_IApp.ActiveDocument
		Dim ADoc As AssemblyDocument = TryCast(Doc, AssemblyDocument)
		If ADoc Is Nothing Then Exit Sub
		Dim SSet As SelectSet = Doc.SelectSet
		Dim TMat As Matrix

		For Each Sel As Object In SSet
			Dim UCS As UserCoordinateSystem = TryCast(Sel, UserCoordinateSystem)
			If UCS IsNot Nothing Then
				Dim MyOC As ComponentOccurrence = Nothing
				For Each O As ComponentOccurrence In ADoc.ComponentDefinition.Occurrences
					Try
						If O.Definition Is UCS.Parent Then
							MyOC = O
							Exit For
						End If
					Catch ex As Exception
					End Try
				Next

				Dim GProx As Object = Nothing
				If MyOC IsNot Nothing Then MyOC.CreateGeometryProxy(UCS, GProx)
				Dim UCSProxy As UserCoordinateSystemProxy = TryCast(GProx, UserCoordinateSystemProxy)
				If UCSProxy IsNot Nothing Then
					TMat = UCSProxy.Transformation
					Debug.Print("")
					Debug.Print("Proxy ?")
					Debug.Print("X " & TMat.Translation.X)
					Debug.Print("Y " & TMat.Translation.Y)
					Debug.Print("Z " & TMat.Translation.Z)
					TMat = UCS.Transformation
					Debug.Print("")
					Debug.Print("UCS")
					Debug.Print("X " & TMat.Translation.X)
					Debug.Print("Y " & TMat.Translation.Y)
					Debug.Print("Z " & TMat.Translation.Z)
					Exit For
				End If
			End If
		Next
	End Sub

 

 

Please advise!!!

 

6 REPLIES 6
Message 2 of 7
WCrihfield
in reply to: CAD_CAM_MAN

Hi @CAD_CAM_MAN.  I see that your code starts out by iterating the contents of the SelectSet, which is supposed to contain UCS type objects, but I have a question about 'how' those UCS objects were initially selected.

  • Did you use the 'Pick' method within some other routine, or did you manually select them with your mouse before the code started?
  • Was the main/top assembly visible on your screen the whole time you did all of those selections (not in 'edit mode' of a component, and not directly while you had some other document visibly open)?

If all of those UCS objects were selected from the context of the main/top assembly, and they were all defined within referenced documents, then they will all be UserCoordinateSystemProxy objects, instead of regular UserCoordinateSystem objects.  And if so, the variable representing them should be declared that way.  This will give you some additional 'tools' to work with, and help you find what you are looking for.  When we have a 'proxy' type object, then we have a way of getting to either the 'original' object (that is not a proxy), and we have a way to get to the 'top level' proxy (if the one you have is not it).

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 7
CAD_CAM_MAN
in reply to: WCrihfield

The UCS was selected with the mouse via the model tree in the top level. The top level is active when selected and when the command is executed. The UCS is within a .ipt under the top level (no sub assembly). I tried simplifying the code (below) and declaring / casting as proxy with the same unexpected results. The cast works so I would think the transformation would be within the context of the top level however the translation values are still correct for the .ipt context. What am I missing?! Any further suggestions?

Public Sub CCTExecutionCommand()
		Dim Doc As Document = g_IApp.ActiveDocument
		Dim ADoc As AssemblyDocument = TryCast(Doc, AssemblyDocument)
		If ADoc Is Nothing Then Exit Sub
		Dim SSet As SelectSet = ADoc.SelectSet
		Dim TMat As Matrix

		For Each Sel As Object In SSet 'Iterate thru the selections.
			Dim UCS As UserCoordinateSystemProxy = TryCast(Sel, UserCoordinateSystemProxy) 'Check if this iteration is a UCS proxy object.
			If UCS IsNot Nothing Then 'Check if the cast worked.
				TMat = UCS.Transformation
				Debug.Print("")
				Debug.Print("UCS")
				Debug.Print("X " & TMat.Translation.X)
				Debug.Print("Y " & TMat.Translation.Y)
				Debug.Print("Z " & TMat.Translation.Z)
				Exit For 'Get out after first UCS proxy.
			End If
		Next

	End Sub

 

Results...

UCS
X 71.2562842475752
Y -86.2648285528555
Z -12.7341406841421

 

CAD_CAM_MAN_2-1709143933161.png

CAD_CAM_MAN_4-1709143992235.png

 

 

 

Message 4 of 7
CAD_CAM_MAN
in reply to: CAD_CAM_MAN

Evidently it is the User Coordinate System Transformation property that is the issue? I Changed the code to use the Origin Point rather than the Transformation Translation property and results are as expected. Would be nice to have the matrix but I suppose I can get what I need by getting the origin point and work planes of the selected UCS and doing some leg work . Thank you WCrihfield for the response!

 

 

 

	Public Sub CCTExecutionCommand()
		Dim Doc As Document = g_IApp.ActiveDocument
		Dim ADoc As AssemblyDocument = TryCast(Doc, AssemblyDocument)
		If ADoc Is Nothing Then Exit Sub
		Dim SSet As SelectSet = ADoc.SelectSet
		Dim TMat As Matrix

		For Each Sel As Object In SSet 'Iterate thru the selections.
			Dim PTP As UserCoordinateSystemProxy = TryCast(Sel, UserCoordinateSystemProxy) 'Check if this iteration is a UCS proxy object.
			If PTP IsNot Nothing Then 'Check if the cast worked.
				Debug.Print("")
				Debug.Print("UCS")
				Debug.Print("X " & PTP.Origin.Point.X)
				Debug.Print("Y " & PTP.Origin.Point.Y)
				Debug.Print("Z " & PTP.Origin.Point.Z)
				Exit For 'Get out after first UCS proxy.
			End If
		Next
	End Sub

 

 

 

 

Message 5 of 7
WCrihfield
in reply to: CAD_CAM_MAN

Hi @CAD_CAM_MAN.  It looks like you marked your original post as the solution.  Did you get it figured out?  After doing some quick testing on my end, I also saw that both the original and the proxy had the same values for the UserCoordinateSystem.Transformation.Translation coordinates.  But when I checked the coordinates of the UserCoordinateSystem.Origin.Point, I got different values between the original and the proxy, so I know I they were different.  When I saw the title of the post, I was thinking I might just need to show how to get the 'top level' proxy of an existing lower level proxy, but that did not seem needed here.  And we basically do not have a way to get the proxy of a regular UserCoordinateSystem object, if we do not have a reference to the owning component, and there are multiple components like that in the assembly.  But when it comes to Matrix objects in the context of components in an assembly that can be in any position and orientation, and we need to inspect and compare them, it starts to get pretty mind bending from a code point of view.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 7
CAD_CAM_MAN
in reply to: WCrihfield

For some reason the post I marked as a solution did not actually post and marked the original as solution?!?! In any case I also noticed the origin point was giving the correct result but the Transformation Translation properties were not. So I can get the correct matrix from the proxy which is ultimately what I was trying to get to proceed with this project.

 

Thank you for the responses !

Public Sub CCTExecutionCommand()
		Dim Doc As Document = g_IApp.ActiveDocument
		Dim ADoc As AssemblyDocument = TryCast(Doc, AssemblyDocument)
		If ADoc Is Nothing Then Exit Sub
		Dim SSet As SelectSet = ADoc.SelectSet
		Dim TMat As Matrix

		For Each Sel As Object In SSet 'Iterate thru the selections.
			Dim UCS As UserCoordinateSystemProxy = TryCast(Sel, UserCoordinateSystemProxy) 'Check if this iteration is a UCS proxy object.
			If UCS IsNot Nothing Then 'Check if the cast worked.
				TMat = MatrixFromUCSProxy(UCS)
				Exit For 'Get out after first UCS proxy.
			End If
		Next
	End Sub

Public Function MatrixFromUCSProxy(UCSP As UserCoordinateSystemProxy) As Matrix
		Dim MyMatrix As Matrix = g_IApp.TransientGeometry.CreateMatrix
		MyMatrix.SetCoordinateSystem(UCSP.Origin.Point, UCSP.XAxis.Line.Direction.AsVector, UCSP.YAxis.Line.Direction.AsVector, UCSP.ZAxis.Line.Direction.AsVector)
		Return MyMatrix
	End Function

Message 7 of 7
CAD_CAM_MAN
in reply to: WCrihfield

For some reason when I click accept solution it deletes the post I submitted?

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report