Get UCS workpoint co-ordinates

Get UCS workpoint co-ordinates

Saikiran_arakeri
Enthusiast Enthusiast
1,194 Views
13 Replies
Message 1 of 14

Get UCS workpoint co-ordinates

Saikiran_arakeri
Enthusiast
Enthusiast

Hey guys I am trying to extract these co-ordinates of the workpoint.

 

Selection 1 was the co-ordinate system you see on the right side marked in black associated with the part, selection 2 was the assembly co-ordinate system. I am trying to extract the X,Y & Z values highlighted in red. @WCrihfield @Andrii_Humeniuk @A.Acheson @JelteDeJong 

 

sarakeriEP9MJ_0-1686941310138.png

 

0 Likes
Accepted solutions (2)
1,195 Views
13 Replies
Replies (13)
Message 2 of 14

WCrihfield
Mentor
Mentor

Running short on time, but here is a good link to look into.  It explains how the ThisApplication.MeasureTools.GetMinimumDistance method works.  It can be much more complex than most folks normally make use of.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 14

Saikiran_arakeri
Enthusiast
Enthusiast

Hey @WCrihfield that's okay. It can wait till Monday, let's see if we can find out a solution.

0 Likes
Message 4 of 14

JelteDeJong
Mentor
Mentor

Using the measureTools might be the best option. But there is also the option to calculate the distance yourself. Here is my two-penny worth.

It was not clear to me if you wanted to measure from an occurrence UCS to some custom UCS or between 2 UCSs. Anyway here 2 examples

occurrence -> UCS

 

Dim occ As ComponentOccurrence = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "Select Occurrence")
Dim ucs2 As UserCoordinateSystem = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kUserCoordinateSystemFilter, "Select UCS 2")

Dim position1 = occ.Transformation.Translation  
Dim position2 = ucs2.Definition.Transformation.Translation

Dim diffX = (position2.X - position1.X) ^ 2
Dim diffY = (position2.Y - position1.Y) ^ 2
Dim diffZ = (position2.Z - position1.Z) ^ 2

Dim distance = Math.Sqrt(diffX + diffY + diffZ)

MsgBox(distance & " Cm")

 

 UCS -> UCS

 

Dim ucs1 As UserCoordinateSystem = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kUserCoordinateSystemFilter, "Select UCS 1")
Dim ucs2 As UserCoordinateSystem = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kUserCoordinateSystemFilter, "Select UCS 2")

Dim position1 = ucs1.Definition.Transformation.Translation
Dim position2 = ucs2.Definition.Transformation.Translation

Dim diffX = (position2.X - position1.X) ^ 2
Dim diffY = (position2.Y - position1.Y) ^ 2
Dim diffZ = (position2.Z - position1.Z) ^ 2

Dim distance = Math.Sqrt(diffX + diffY + diffZ)

MsgBox(distance & " Cm")

 

 

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 5 of 14

Saikiran_arakeri
Enthusiast
Enthusiast

So, UCS1 is in the assembly component definition and UCS2 is in the component occurrence definition. When I used the code @JelteDeJong gave, I am getting the difference as 0,0,0. I think this is because UCS1 is created at 0,0,0 in the assembly and UCS2 is created at 0,0,0 in the ComponentOccurrence. Correct me if I am wrong.

I am trying to hence get the workpoint X,Y & Z values like shown in the picture so that I can get precise co-ordinates.

 

 

0 Likes
Message 6 of 14

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @Saikiran_arakeri 

Try this example. This code lets you pick the two coordinatesystems you want and then we use the transient geometry of the centerpoints of these coordinate systems to measure the distance and the differences in x, y, z.

 

I'm also using units of measure to convert to the units your'e using in Inventor.

 

Dim ucs1 As UserCoordinateSystem = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kUserCoordinateSystemFilter, "Select UCS 1")
Dim ucs2 As UserCoordinateSystem = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kUserCoordinateSystemFilter, "Select UCS 2")

Dim centerPoint1 As Point = ucs1.Origin.Point
Dim centerPoint2 As Point = ucs2.Origin.Point

Dim UoM As UnitsOfMeasure = ThisDoc.Document.UnitsOfMeasure

Dim retString As String = "Distance: " & UoM.ConvertUnits(centerPoint1.DistanceTo(centerPoint2), UnitsTypeEnum.kDatabaseLengthUnits, _
UnitsTypeEnum.kDefaultDisplayLengthUnits) & UoM.GetStringFromType(UnitsTypeEnum.kDefaultDisplayLengthUnits) & vbCrLf

Dim deltaX As Double = centerPoint1.X - centerPoint2.X
Dim deltaY As Double = centerPoint1.Y - centerPoint2.Y
Dim deltaZ As Double = centerPoint1.Z - centerPoint2.Z

retString += vbCrLf & "Delta X: " & UoM.ConvertUnits(deltaX, UnitsTypeEnum.kDatabaseLengthUnits, _
UnitsTypeEnum.kDefaultDisplayLengthUnits) & UoM.GetStringFromType(UnitsTypeEnum.kDefaultDisplayLengthUnits)
retString += vbCrLf & "Delta Y: " & UoM.ConvertUnits(deltaY, UnitsTypeEnum.kDatabaseLengthUnits, _
UnitsTypeEnum.kDefaultDisplayLengthUnits) & UoM.GetStringFromType(UnitsTypeEnum.kDefaultDisplayLengthUnits)
retString += vbCrLf & "Delta Z: " & UoM.ConvertUnits(deltaZ, UnitsTypeEnum.kDatabaseLengthUnits, _
UnitsTypeEnum.kDefaultDisplayLengthUnits) & UoM.GetStringFromType(UnitsTypeEnum.kDefaultDisplayLengthUnits)

MsgBox(retString)
0 Likes
Message 7 of 14

WCrihfield
Mentor
Mentor

If the position of the first UCS will always be the same as the main assembly's own origin, and already aligned with the main assembly's origin work features, that would certainly simplify the task a bit...eliminating the need for the first Pick function, and eliminating the need for the 'difference' type math.  And if the second UCS will always be within a component, you would just need to get the top level proxy of that second UCS (which will be in the context of the main assembly), then get its origin coordinates,  which will already be relative to the main assembly's origin.  And if always using the Pick function to get that second UCS, you will already have the top level proxy of it.  The only thing left to do is maybe convert the units, if needed, which Jhoel showed how to do in his example.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 14

Saikiran_arakeri
Enthusiast
Enthusiast

Thanks @JhoelForshav , this worked perfectly fine.

0 Likes
Message 9 of 14

Saikiran_arakeri
Enthusiast
Enthusiast

Hey @WCrihfield @JhoelForshav 

 

sarakeriEP9MJ_0-1687257603961.png

I am trying to get UCS2 without using the Select function, how do I do that?

0 Likes
Message 10 of 14

WCrihfield
Mentor
Mentor
Accepted solution

Example:

Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
'get UCS in main assembly named "Global"
Dim oGlobalUCS As UserCoordinateSystem = oADef.UserCoordinateSystems.Item("Global")
'get component named "Part2"
Dim oP2 As ComponentOccurrence = oADef.Occurrences.ItemByName("Part2")
'get UCS in Part2, named "UCS2"
Dim oP2_UCS As UserCoordinateSystem = oP2.Definition.UserCoordinateSystems.Item("UCS2")
'get the proxy of that UCS, so it is in the context of the main assembly
Dim oP2_UCSProxy As UserCoordinateSystemProxy = Nothing
oP2.CreateGeometryProxy(oP2_UCS, oP2_UCSProxy)

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 11 of 14

Saikiran_arakeri
Enthusiast
Enthusiast

Hey @WCrihfield Line 8, the property Usercoordinatesystems isn't available in Occurrence.Definition

0 Likes
Message 12 of 14

Saikiran_arakeri
Enthusiast
Enthusiast

Instead of trying to find Usercoordinatesystems in Occurrence.Definiton, I type casted the Occurrence into Partdocument, went for the Part document Component Definition and accessed the Usercoordinatesystems there and created a proxy. It worked perfectly fine @WCrihfield Thank you

0 Likes
Message 13 of 14

Saikiran_arakeri
Enthusiast
Enthusiast

Thanks @JhoelForshav @WCrihfield @JelteDeJong, I have understood so much doing this project with your constant help and guidance.

0 Likes
Message 14 of 14

WCrihfield
Mentor
Mentor

That is true from an Intellisense (the pop-up suggestions system) point of view, because a generic ComponentDefinition object does not have that property, but the more specific types of a ComponentDefinition, such as a PartComponentDefinition or AssemblyComponentDefinition do have that property, so it should still work, even if Intellisense does not show it as being available.  If it would make more sense, the code could be broken down into more lines of code, where we create a variable of the more specific type, then set its value using the ComponentOccurrence.Definition.  Then move forward using that more specific type of variable, that way the Intellisense will show it as available.

Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
'get UCS in main assembly named "Global"
Dim oGlobalUCS As UserCoordinateSystem = oADef.UserCoordinateSystems.Item("Global")
'get component named "Part2"
Dim oP2 As ComponentOccurrence = oADef.Occurrences.ItemByName("Part2")
Dim oP2Def As PartComponentDefinition = oP2.Definition
'get UCS in Part2, named "UCS2"
Dim oP2_UCS As UserCoordinateSystem = oP2Def.UserCoordinateSystems.Item("UCS2")
'get the proxy of that UCS, so it is in the context of the main assembly
Dim oP2_UCSProxy As UserCoordinateSystemProxy = Nothing
oP2.CreateGeometryProxy(oP2_UCS, oP2_UCSProxy)

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes