Get Transformation from one part to another in C#

Get Transformation from one part to another in C#

PanWauu
Contributor Contributor
594 Views
3 Replies
Message 1 of 4

Get Transformation from one part to another in C#

PanWauu
Contributor
Contributor

My goal is to calculate the transformation between two occurences in order to later insert both parts again with the same transformation in between.

 

Somehow, I cant get it to work. In the result the parts are not placed correctly and also when trying to check the relation by eye they seem wrong. I am thinking that I got the inverse wrong in the calculation of the transform. Below I try to describe my approach.

Thanks for any help or suggestions!! 🤞

 

In the first step I am calculating the Transformation in between. You can see my thinking in the comments in the code:

 

 

public static Inventor.Matrix GetMatrixBetweenParts(ComponentOccurrence occurrenceOne, ComponentOccurrence occurrenceTwo)
{
   // Notation:
   //   - Transformation T_a_b transforms from system a to system b
   //   - o denotes origin
   //   - 1 denotes the system of part one (with smaller documentID)
   //   - 2 denotes the system of part two (with larger documentID)
   // Calculation:
   //   - Goal: Transformation T_1_2 from system 1 to system 2: T_1_2
   //   - Known: Transformation from origin to system 1 and 2: T_o_1, T_o_2
   //   - T_1_2 = T_1_o * T_o_2 = inv(T_o_1) * T_o_2

   Inventor.Matrix partTwoTransformation = occurrenceTwo.Transformation.Copy();
   Inventor.Matrix partOneTransformation = occurrenceOne.Transformation.Copy();
   Inventor.Matrix result = partOneTransformation.Copy();
   result.Invert();
   result.TransformBy(partTwoTransformation);
   return result;
}

 

 

 

I then save the internal data cells using:

 

 

 

double[] matrixCells = new double[16];
relativePosition.GetMatrixData(ref matrixCells);
_relativePositionCells = matrixCells;

 

 

 

 

Later I use this code to insert the parts again:

 

 

Inventor.Matrix matrixPartOne = _invAPI.InvApp.TransientGeometry.CreateMatrix();
matrixPartOne.SetTranslation(_invAPI.InvApp.TransientGeometry.CreateVector(0,yAxisCounter,0));
Inventor.Matrix matrixPartTwo = matrixPartOne.Copy();
Inventor.Matrix relativePositionMatrix = _invAPI.InvApp.TransientGeometry.CreateMatrix();
relativePositionMatrix.PutMatrixData(position._relativePositionCells);
matrixPartTwo.TransformBy(relativePositionMatrix);

ComponentOccurrence partOne = _assDoc.ComponentDefinition.Occurrences.Add(filePathOne, matrixPartOne);
ComponentOccurrence partTwo = _assDoc.ComponentDefinition.Occurrences.Add(filePathTwo, matrixPartTwo);

 

 

 

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

PanWauu
Contributor
Contributor
Accepted solution

Nevermind, I found the error. I used TransformBy although I needed PostMultiplyBy.

0 Likes
Message 3 of 4

JelteDeJong
Mentor
Mentor

Was just posting this. just too late, I suppose...

public void Main()
{
    AssemblyDocument doc = (AssemblyDocument)ThisDoc.Document;
    var occs = doc.ComponentDefinition.Occurrences;

    var relativePositionMatrix = GetMatrixBetweenParts(occs[1], occs[2]);

    var occPath1 = occs[1].ReferencedFileDescriptor.FullFileName;
    var occPath2 = occs[2].ReferencedFileDescriptor.FullFileName;

    var moveVector = ThisApplication.TransientGeometry.CreateVector(10,10,10);
    var newPlaceMatrix = ThisApplication.TransientGeometry.CreateMatrix();
    newPlaceMatrix.SetTranslation(moveVector);

    Add(doc, occPath1, occPath2, newPlaceMatrix, relativePositionMatrix);
}

public void Add(AssemblyDocument doc, string path1, string path2, Matrix position, Matrix relativePositionMatrix)
{
    var occs = doc.ComponentDefinition.Occurrences;


    occs.Add(path1, position);
    position.PostMultiplyBy(relativePositionMatrix);
    occs.Add(path2, position);

}

public static Inventor.Matrix GetMatrixBetweenParts(ComponentOccurrence occurrenceOne, ComponentOccurrence occurrenceTwo)
{
    Inventor.Matrix partTwoTransformation = occurrenceTwo.Transformation.Copy();
    Inventor.Matrix partOneTransformation = occurrenceOne.Transformation.Copy();
    Inventor.Matrix result = partOneTransformation.Copy();
    result.Invert();
    result.TransformBy(partTwoTransformation);
    return result;
}

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 4 of 4

PanWauu
Contributor
Contributor
Thanks for your help anyways

Should have put it in minimal reproducible form like you did before posting it here. This maybe would have made you a couple of seconds faster 😉

Can you explain to me why the "TransformBy" method exists if there is already "PostMultiplyBy" and "PreMultipleBy"?
0 Likes