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);
Solved! Go to Solution.
Solved by PanWauu. Go to Solution.
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.
Blog: hjalte.nl - github.com
Can't find what you're looking for? Ask the community or share your knowledge.