How to get UCS difference between two parts? Hard to explain...

How to get UCS difference between two parts? Hard to explain...

viktorkulik
Contributor Contributor
1,947 Views
14 Replies
Message 1 of 15

How to get UCS difference between two parts? Hard to explain...

viktorkulik
Contributor
Contributor

Here's the problem:

 

I have a part1 and part2 in assembly, i want to get origin + vector data from part2 in relationship to part1. If part1 moves, I want to update part 2 transformation to match their previous location (in relationship to each other).

So far I tried this:

 

Call p1t.GetCoordinateSystem(origin1, xAxis1, yAxis1, zAxis1)
Call p2t.GetCoordinateSystem(origin2, xAxis2, yAxis2, zAxis2)

 

This gives me the location data I want, but only in relationship to the Assembly file UCS. Getting the difference of the Origin seems somewhat straight forward, but getting the vector difference is making my brain hurt. Is there an easy way to do this? (programmatically, without using constraints)

 

 

0 Likes
Accepted solutions (1)
1,948 Views
14 Replies
Replies (14)
Message 2 of 15

FINET_Laurent
Advisor
Advisor

To make it clear, if part 2 move, you want the part 1 to do the same movement?

 

have a nice day,

 

FINET L.

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

0 Likes
Message 3 of 15

Michael.Navara
Advisor
Advisor

For manipulating with occurrences use its property Transformation.

If you want to synchronize transformations of two occurrences it is very simple (SyncGCS)

 

Dim oAsm As AssemblyDocument = ThisDoc.Document

Dim occ1 As ComponentOccurrence = oAsm.ComponentDefinition.Occurrences(1)
Dim occ2 As ComponentOccurrence = oAsm.ComponentDefinition.Occurrences(2)

occ2.Transformation = occ1.Transformation

 

 

If you want to synchronize position using UCS, you can use following code where calculation with matrices is used. (SyncUCS)

 

Sub Main
	Dim oAsm As AssemblyDocument = ThisDoc.Document

	Dim occ1 As ComponentOccurrence = oAsm.ComponentDefinition.Occurrences(1)
	Dim occ2 As ComponentOccurrence = oAsm.ComponentDefinition.Occurrences(2)

	Dim T1 As Matrix = GetUcsTransformationMatrix(occ1)
	Dim T2 As Matrix = GetUcsTransformationMatrix(occ2)

	Dim T As Matrix = occ1.Transformation.Copy
	T.PostMultiplyBy(T1)

	T2.Invert()
	T.PostMultiplyBy(T2)

	occ2.Transformation = T
End Sub

Function GetUcsTransformationMatrix(occ As ComponentOccurrence) As Matrix
	Dim compDef As PartComponentDefinition = occ.Definition
	Dim transformationMatrix As Matrix = compDef.UserCoordinateSystems(1).Transformation.Copy
	Return transformationMatrix
End Function

 

Full iLogic sample is in attachment.

 

0 Likes
Message 4 of 15

viktorkulik
Contributor
Contributor

That is correct.

Message 5 of 15

viktorkulik
Contributor
Contributor

Thank you, a couple of questions, in the first example you're simply copying the matrix which will move both parts to the same origin and orientation. What I am after is something like this:

2 parts are placed into assembly, the user spaces them apart a certain distance and rotates them in some way. Then I take a snapshot of both of the locations using by saving the matrix of each.

After this a user can move part1 anywhere  and rotate in any way, the second part will need to be able to sync up to the same location as it was in reference to part1.

 

The second example doesn't appear to be what I am after either, but i'm digging into it for another reason.

 

0 Likes
Message 6 of 15

FINET_Laurent
Advisor
Advisor

You could get the position of both part,

 

Call p1t.GetCoordinateSystem(origin1, xAxis1, yAxis1, zAxis1)
Call p2t.GetCoordinateSystem(origin2, xAxis2, yAxis2, zAxis2)

 

Substract the coordinates from both parts to get the relative position of  the parts.

 

Then define the new part1 coordinates as:

 

xAxis = ( xAxis 2 - xRelativePosistion)
...

 

let's say part1 is on x = 150 and part 2 on x = 500

Then relative x position = 500 - 150 = 350

 

so xAxis1 = xAxis2 - 350

 

..........hope i made myself clear lmao..

 

Have a nice day.

 

 

 

 

 

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

0 Likes
Message 7 of 15

viktorkulik
Contributor
Contributor

Thanks, yea i'm the one that's having a hard time making myself clear. But to answer your question, that is what I'm attempting, currently I'm just doing that by subtracting each cell of the matrix, to determine the relative location + orientation. Are you saying you can actually subtract Point from Point? or just manually subtract?

 

Doing this through the matrix.cell seems pretty quick so I may just keep to that, but if there's a method that already does this, i'm all ears.

0 Likes
Message 8 of 15

FINET_Laurent
Advisor
Advisor

I'be been digging a bit but i can't seem to find a way.. 😞

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

0 Likes
Message 9 of 15

viktorkulik
Contributor
Contributor

So i need some help understanding why SubtractVector and AddVector don't return a perfect numbers, the angles are like 90.00000000022575364, which then causes an error when I try to SetCoordinateSystem. Is there a way to round the results? Here's my basic demo script, button1 records the locations of both objects, and button2 updates the location of part2 based on a new location of part1. Everything works great while i move the part around, but as soon as I free-rotate a bit, it fails.

 

 

Dim origin1 As Point
Dim origin2 As Point
Dim xAxis1 As Vector
Dim yAxis1 As Vector
Dim zAxis1 As Vector
Dim xAxis2 As Vector
Dim yAxis2 As Vector
Dim zAxis2 As Vector
Private Sub CommandButton1_Click()


Dim oAsm As AssemblyDocument
Set oAsm = ThisApplication.ActiveDocument


Dim occ1 As ComponentOccurrence
Dim occ2 As ComponentOccurrence

Set occ1 = oAsm.ComponentDefinition.Occurrences(1)
Set occ2 = oAsm.ComponentDefinition.Occurrences(2)
Dim oTransform1 As Matrix
Set oTransform1 = occ1.Transformation

Dim oTransform2 As Matrix
Set oTransform2 = occ2.Transformation


Call oTransform1.GetCoordinateSystem(origin1, xAxis1, yAxis1, zAxis1)
Call oTransform2.GetCoordinateSystem(origin2, xAxis2, yAxis2, zAxis2)


End Sub



Private Sub CommandButton2_Click()
Dim oAsm As AssemblyDocument
Set oAsm = ThisApplication.ActiveDocument

Dim occ1 As ComponentOccurrence
Dim occ2 As ComponentOccurrence

Set occ1 = oAsm.ComponentDefinition.Occurrences(1)
Set occ2 = oAsm.ComponentDefinition.Occurrences(2)

Dim oTransform1 As Matrix
Set oTransform1 = occ1.Transformation

Dim oTransform2 As Matrix
Set oTransform2 = occ2.Transformation

Dim origin3 As Point
Dim xAxis3 As Vector
Dim yAxis3 As Vector
Dim zAxis3 As Vector

Call oTransform1.GetCoordinateSystem(origin3, xAxis3, yAxis3, zAxis3)

Dim xa As Vector
Dim ya As Vector
Dim za As Vector
Dim o As Point

Call xAxis2.SubtractVector(xAxis1)
Call yAxis2.SubtractVector(yAxis1)
Call zAxis2.SubtractVector(zAxis1)

Call xAxis2.AddVector(xAxis3)
Call yAxis2.AddVector(yAxis3)
Call zAxis2.AddVector(zAxis3)

Set o = AddPoint(SubtractPoint(origin2, origin1), origin3)

Call oTransform2.SetCoordinateSystem(origin2, xAxis2, yAxis2, zAxis2)

Dim a As Double

a = xAxis2.AngleTo(yAxis2)
a = xAxis2.AngleTo(zAxis2)
a = yAxis2.AngleTo(zAxis2)

occ2.Transformation = oTransform2


End Sub

Function SubtractPoint(p1 As Point, p2 As Point) As Point
Dim p As Point
Set p = ThisApplication.TransientGeometry.CreatePoint(p1.x - p2.x, p1.Y - p2.Y, p1.Z - p2.Z)
Set SubtractPoint = p
End Function
Function AddPoint(p1 As Point, p2 As Point) As Point
Dim p As Point
Set p = ThisApplication.TransientGeometry.CreatePoint(p1.x + p2.x, p1.Y + p2.Y, p1.Z + p2.Z)
Set AddPoint = p
End Function
0 Likes
Message 10 of 15

viktorkulik
Contributor
Contributor

Actually the variation on the angle between the X & Y & Z vectors ranges between 89 to 91 degrees, causing the SetCoordinateSystem to fail.

0 Likes
Message 11 of 15

viktorkulik
Contributor
Contributor

With updating the matrix manually it works but not all the time, its really odd, small rotation changes get applied but then all of a sudden it will fail. I even tried adding the Round function to see if it would work better but not really

 

Private Sub CommandButton3_Click()
    Dim oAsm As AssemblyDocument
    Set oAsm = ThisApplication.ActiveDocument

    Set occ1 = oAsm.ComponentDefinition.Occurrences(1)
    Set occ2 = oAsm.ComponentDefinition.Occurrences(2)
    Set m1 = occ1.Transformation
    Set m2 = occ2.Transformation
    
    
End Sub

Private Sub CommandButton4_Click()
    Dim oAsm As AssemblyDocument
    Set oAsm = ThisApplication.ActiveDocument
    
    Set occ1 = oAsm.ComponentDefinition.Occurrences(1)
    Set occ2 = oAsm.ComponentDefinition.Occurrences(2)
    Set m3 = occ1.Transformation
    Dim dl As Integer
    dl = 20
    m2.Cell(1, 1) = Round(m2.Cell(1, 1), dl) - Round(m1.Cell(1, 1), dl) + Round(m3.Cell(1, 1), dl)
    m2.Cell(1, 2) = Round(m2.Cell(1, 2), dl) - Round(m1.Cell(1, 2), dl) + Round(m3.Cell(1, 2), dl)
    m2.Cell(1, 3) = Round(m2.Cell(1, 3), dl) - Round(m1.Cell(1, 3), dl) + Round(m3.Cell(1, 3), dl)
    m2.Cell(2, 1) = Round(m2.Cell(2, 1), dl) - Round(m1.Cell(2, 1), dl) + Round(m3.Cell(2, 1), dl)
    m2.Cell(2, 2) = Round(m2.Cell(2, 2), dl) - Round(m1.Cell(2, 2), dl) + Round(m3.Cell(2, 2), dl)
    m2.Cell(2, 3) = Round(m2.Cell(2, 3), dl) - Round(m1.Cell(2, 3), dl) + Round(m3.Cell(2, 3), dl)
    m2.Cell(3, 1) = Round(m2.Cell(3, 1), dl) - Round(m1.Cell(3, 1), dl) + Round(m3.Cell(3, 1), dl)
    m2.Cell(3, 2) = Round(m2.Cell(3, 2), dl) - Round(m1.Cell(3, 2), dl) + Round(m3.Cell(3, 2), dl)
    m2.Cell(3, 3) = Round(m2.Cell(3, 3), dl) - Round(m1.Cell(3, 3), dl) + Round(m3.Cell(3, 3), dl)
    occ2.Transformation = m2
End Sub
0 Likes
Message 12 of 15

Michael.Navara
Advisor
Advisor
Accepted solution

You don't need to calculate coordinate system manually. You can store transformation as object directly. I use iLogic SharedVariable for this.

Store relative position between two occurrences

 

 

Dim oAsm As AssemblyDocument = ThisDoc.Document

Dim occ1 As ComponentOccurrence = oAsm.ComponentDefinition.Occurrences(1)
Dim occ2 As ComponentOccurrence = oAsm.ComponentDefinition.Occurrences(2)

Dim T1 As Matrix = occ1.Transformation
Dim T2 As Matrix = occ2.Transformation

'Transformation between two Occurrences
Dim T As Matrix = T1.Copy()
T.Invert
T.PostMultiplyBy(T2)

'Store result for future use
SharedVariable("RelativePosition") = T

 

 

 

Restore relative position

 

 

Dim oAsm As AssemblyDocument = ThisDoc.Document

Dim occ1 As ComponentOccurrence = oAsm.ComponentDefinition.Occurrences(1)
Dim occ2 As ComponentOccurrence = oAsm.ComponentDefinition.Occurrences(2)

'Restore relative position from StoreRelativePosition rule
Dim T As Matrix = SharedVariable("RelativePosition")

'Calculate new T2
Dim T2 As Matrix = occ1.Transformation.Copy
T2.PostMultiplyBy(T)

'Apply
occ2.Transformation = T2

 

 

 

 

 

Message 13 of 15

Michael.Navara
Advisor
Advisor

You can't calculate final transformation matrix as add and subtract cell values. Standard transformation matrix consists of submatrix of direction cosines, translation vector and constant values.

| direction : X |

| _cosines_ : Y |

| __here__  : Z |

|  0  0  0  : 1 |

 

When you combine two or more transformation matrices, you must always use MULTIPLICATION of matrices instead of ADD or SUBTRACT

 

Beware when you multiply matrices T1 x T2 is NOT the same as T2 x T1

See some resources of matrix calculations. For example here: https://en.wikipedia.org/wiki/Matrix_(mathematics)

 

0 Likes
Message 14 of 15

viktorkulik
Contributor
Contributor

Thanks, the invert & postmultiply was exactly what I needed, appreciate your help! The main reason why i was not saving the matrix was because i need to be able to store this information outside of inventor, so i was just testing how that would work.

 

Thanks.

0 Likes
Message 15 of 15

viktorkulik
Contributor
Contributor

That makes sense, thanks Michael.

0 Likes