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 Transformation from one part to another in C#

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
PanWauu
382 Views, 3 Replies

Get Transformation from one part to another in C#

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);

 

 

 

3 REPLIES 3
Message 2 of 4
PanWauu
in reply to: PanWauu

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

Message 3 of 4
JelteDeJong
in reply to: PanWauu

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

Message 4 of 4
PanWauu
in reply to: JelteDeJong

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"?

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

Post to forums  

Autodesk Design & Make Report