Announcements
Autodesk Community will be read-only between April 26 and April 27 as we complete essential maintenance. We will remove this banner once completed. Thanks for your understanding

Insert part on the point and rotataion

-pezi-
Enthusiast Enthusiast
702 Views
5 Replies
Message 1 of 6

Insert part on the point and rotataion

-pezi-
Enthusiast
Enthusiast

I need to insert part on the point and rotation this part around this point. I created this function, but don't work good. Can somebody help me, please?

void MoveAndRotationOccurrence(Ptr<Point3D>& insertPoint, Ptr<Component>& parentComp, Ptr<DataFile>& dataFile, double degAngle)
{
	double angle = (degAngle * M_PI / 180.0);

	Ptr<Matrix3D> move = Matrix3D::create();
	move->translation(Vector3D::create(insertPoint->x(), insertPoint->y(), insertPoint->z()));
	Ptr<Occurrence> occ = parentComp->occurrences()->addByInsert(dataFile, move, true);

	Ptr<Matrix3D> rotate = Matrix3D::create();
	rotate->setToRotation(angle, Vector3D::create(0, 1, 0), insertPoint);

	occ->transform(rotate);

}
Reply
Reply
0 Likes
Accepted solutions (1)
703 Views
5 Replies
Replies (5)
Message 2 of 6

-pezi-
Enthusiast
Enthusiast
Accepted solution

After many attempts and searching for answers on the Internet. I found a solution. Thanks, Brian Ekins, for publishing MathGeometry.pdf in How to move a component or occurrence? on 01-27-2020.

My solution is:

 

void MoveAndRotationOccurrence(Ptr<Point3D>& insertPoint, Ptr<Component>& parentComp, Ptr<BRepBody>& body, Ptr<DataFile>& dataFile, double degAngle)
{
	Ptr<Matrix3D> insertMatrix = Matrix3D::create();
	Ptr<Matrix3D> oTransMatrix = Matrix3D::create();

	// Function return origin when is inserted part
	Ptr<Point3D> originComponent = GetOriginComponent(body);

	// Convert deg -> radians 
	double angle = (degAngle * M_PI / 180.0);
	
	// Settings rotations
	oTransMatrix->setToRotation(angle, Vector3D::create(0, 1, 0), originComponent);
	insertMatrix->transformBy(oTransMatrix);

	// Important, it doesn't work without this
	oTransMatrix->setToIdentity();

	// Settings movings
	Ptr<Vector3D> vector = Vector3D::create(insertPoint->x(), insertPoint->y(), insertPoint->z());
	oTransMatrix->translation(vector);

	insertMatrix->transformBy(oTransMatrix);

	// Insert a part that is rotated and moved to a insertPoint
	Ptr<Occurrence> occ = parentComp->occurrences()->addByInsert(dataFile, insertMatrix, true);
}

 

Reply
Reply
1 Like
Message 3 of 6

BrianEkins
Mentor
Mentor

I didn't test it, but I think this should have the same result.

 

 

void MoveAndRotationOccurrence(Ptr<Point3D>& insertPoint, Ptr<Component>& parentComp, Ptr<BRepBody>& body, Ptr<DataFile>& dataFile, double degAngle)
{
	Ptr<Matrix3D> insertMatrix = Matrix3D::create();

	// Convert deg -> radians 
	double angle = (degAngle * M_PI / 180.0);
	
	// Settings rotations
	insertMatrix->setToRotation(angle, Vector3D::create(0, 1, 0), Point3D::create(0, 0, 0));

	// Settings movings
	Ptr<Vector3D> vector = Vector3D::create(insertPoint->x(), insertPoint->y(), insertPoint->z());
	insertMatrix->translation(vector);

	// Insert a part that is rotated and moved to a insertPoint
	Ptr<Occurrence> occ = parentComp->occurrences()->addByInsert(dataFile, insertMatrix, true);
}

 

 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Reply
Reply
0 Likes
Message 4 of 6

-pezi-
Enthusiast
Enthusiast

Thanks for reviewing the code, but isn't working well. The inserted part is rotated correctly, but it is moved in the wrong way. My solution was inspired by an example, you published in PDF file MathGeometry, where you had created Matrix for rotation around axis x, rotation around axis y, and move to specific coordinates. Method setToIdentity has been used there. What is the purpose of this method? I suppose, this method should fix the before going matrix parameters and adjustments, before performing the other adjustments of the matrix, shouldn't it?

Reply
Reply
0 Likes
Message 5 of 6

BrianEkins
Mentor
Mentor

Let's break down your code to see what's happening.

 

These two lines create two new Matrix3D objects.  When you create a new matrix it is initialized with an identity matrix.  As far as creating a new occurrence, if you use an identity matrix it will place it at the origin and without any rotation.

	Ptr<Matrix3D> insertMatrix = Matrix3D::create();
	Ptr<Matrix3D> oTransMatrix = Matrix3D::create();

 

This next line is calling a function of yours so I don't know what it's doing and is likely the reason for the difference in behavior.  You're somehow getting a point from a component but I don't know what that point is.

	// Function return origin when is inserted part
	Ptr<Point3D> originComponent = GetOriginComponent(body);

 

This redefines one of the matrices so that it defines a rotation that defined at the point you got from the component and around the Y axis.  It then transforms the other matrix using this one.  Because the other matrix is an identity the matrix, the result after these two lines is that both matrices are exactly the same.  

	// Settings rotations
	oTransMatrix->setToRotation(angle, Vector3D::create(0, 1, 0), originComponent);
	insertMatrix->transformBy(oTransMatrix);

 

The resets this matrix back to an identity matrix.  You would be the same result by creating a new matrix and assigning it that variable.

	oTransMatrix->setToIdentity();

 

The first line is creating a vector and the second line is using this point to set the portion of the matrix that defines the position 

	Ptr<Vector3D> vector = Vector3D::create(insertPoint->x(), insertPoint->y(), insertPoint->z());
	oTransMatrix->translation(vector);

 

This line essentially combines the position just defined with the position that already exists in the insertMatrix.

insertMatrix->transformBy(oTransMatrix);

 

And then you use the matrix to create a new occurrence.  My code should position the occurrence at the location defined by the insertPoint argument and rotated around the Y-axis by the defined angle.

 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Reply
Reply
0 Likes
Message 6 of 6

-pezi-
Enthusiast
Enthusiast

Thank you for the quick answer and the step-by-step description - especially for the clarifying of the setToIdentity() method.
The function GetOriginComponet() serves for finding the origin of the component in which is the part inserted - so I'm creating an assembly. It is possible, that the amount of these assemblies is higher and every one of them can be situated on different coordinates. The file, which is inserted into the assembly, is positioned on the coordinates specified with the matrix. (It is rotated and then shifted to the insertPoint - insertPoint is a point, which is created and defined in a sketch.) The method worldGeometry() allows me to get the coordinates in the world space of the assembly context.
So firstly I am creating two matrixes. The first one sets the rotation parameters, then the rotation is implemented and then I am using this first one matrix for a transformation of the second one. Then I am calling the setToIdentity method for reset of the first one matrix and I am setting the shifting parameters to the matrix (to the first one, which is reset) and I am implementing the shift again, with the same method as by the rotation.

Am I thinking the right way?

I can't find the setToIdentity method in your correction, I have tried the code without it, but it didn't work. When I have added the setToIdentity method to the code, it has worked properly. Do you have any idea why?

Reply
Reply
0 Likes