Moving an Occurrence from two known points in C++

Moving an Occurrence from two known points in C++

alex_winter
Advocate Advocate
273 Views
1 Reply
Message 1 of 2

Moving an Occurrence from two known points in C++

alex_winter
Advocate
Advocate

Hello I have a script where I am moving an occurrence from one point to another.  One point is the center origin of a selected occurrence and the other is a drawn circle that has been previously positioned.  The Sketch of the circle is drawn in the rootComp. I am trying to move the occurrence to the center of this circle. 

 

Currently it is moving the component to the World Origin.  Can anybody help me see what I am doing wrong.

 

 

    // Have an occurrence selected.
	Ptr<Selection> occSel = ui->selectEntity("Select an occurrence.", "Occurrences");
	Ptr<Occurrence> occ = occSel->entity();

	//Get Position Of Occurrence
	Ptr<Point3D> occPos = occ->transform2()->translation()->asPoint();

	//Get Center Point Of Circle
	Ptr<Point3D> centerPoint = circle1->centerSketchPoint()->worldGeometry();


	Ptr<Matrix3D> trans = adsk::core::Matrix3D::create();
	trans->translation() = occPos->vectorTo(centerPoint);

	occ->transform2(trans);

 

  

0 Likes
274 Views
1 Reply
Reply (1)
Message 2 of 2

BrianEkins
Mentor
Mentor

I think the problem is that you're losing the existing transform of the occurrence. You're applying a completely new transform, which only defines a translation from the model origin.

 

To move the occurrence from where it currently is to the center of the circle, I think the following will work. I didn't test this and wrote it here in the forum, so there could easily be some dumb mistakes, but I think it demonstrates the idea.

    // Have an occurrence selected.
	Ptr<Selection> occSel = ui->selectEntity("Select an occurrence.", "Occurrences");
	Ptr<Occurrence> occ = occSel->entity();

	//Get the current transform of the occurrence.
	Ptr<Matrix3D> occTrans = occ->transform2();

	//Get Center Point Of Circle
	Ptr<Point3D> centerPoint = circle1->centerSketchPoint()->worldGeometry();

        // Create a vector that defines the translation.
        Ptr<Point3D> currentPnt = occTrans->translation()->asPoint();
	Ptr<Vector3D> moveVec = current->vectorTo(centerPointPnt);
        Ptr<Matrix3D> trans = adsk.core.Matrix3D.create();
        trans.translation(moveVec);

        // Apply the translation to the matrix.
        occTrans.transformBy(trans);

        // Transform the occurrence.
	occ->transform2(trans);

  

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