Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Rotating a body around axis in C++

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
rusty.bird
326 Views, 9 Replies

Rotating a body around axis in C++

Hello I am having some trouble rotating a body around the part axis.  I have two bodies in my rootComp and I want to rotate the second body.  I have tryed for a few hours now and I am unable to get this to work.  Does anybody know what I am missing: Here is the code:

// Create a collection of entities for move
	Ptr<BRepBodies> brepBodies = rootComp->bRepBodies();
	if (!brepBodies)
		return false;
	Ptr<BRepBody> brepBody = brepBodies->item(1);
	if (!brepBody)
		return false;
	Ptr<ObjectCollection> entities1 = adsk::core::ObjectCollection::create();
	if (!entities1)
		return false;
	entities1->add(brepBody);

// Create a transform to do move
	Ptr<Matrix3D> transform = adsk::core::Matrix3D::create();

	// Convert deg -> radians 
	double angle1 = (90 * M_PI / 180.0);

	transform->setToRotation(angle1, rootComp->xConstructionAxis()->geometry(), rootComp->originConstructionPoint()->geometry());

// Create a move feature
	Ptr<MoveFeatures> moveFeats = features->moveFeatures();
	if (!moveFeats)
		return false;
	Ptr<MoveFeatureInput> moveFeatureInput = moveFeats->createInput(entities1, transform);
	if (!moveFeatureInput)
		return false;
	Ptr<MoveFeature> moveFeature = moveFeats->add(moveFeatureInput);
	if (!moveFeature)
		return false;

 

9 REPLIES 9
Message 2 of 10

Hi @rusty.bird 

It seems like the issue is with the 

transform->setToRotation(angle1, rootComp->xConstructionAxis()->geometry(), rootComp->originConstructionPoint()->geometry());

 

Here the second argument should be a Vector3D but currently its infiniteLine3D which the reason its not working 

I made a slight change to the code 

	Ptr<Vector3D> vec = adsk::core::Vector3D::create(1,0,0);

	transform->setToRotation(angle1, vec, rootComp->originConstructionPoint()->geometry());

This should solve the problem

 


Boopathi Sivakumar
Senior Technology Consultant

Message 3 of 10

I have tryed your fix but it is not showing the body rotated on the screen.  

Message 4 of 10

Hi @rusty.bird 

I just gave an example, if you could give me a hint on exactly where your rotation point needs to be in then it should work as well


Boopathi Sivakumar
Senior Technology Consultant

Message 5 of 10

I have a cylinder Body with a flat on it.  The origin is the center of the cylinder.  I would like to rotate it around the X axis by a certain amount of degrees say 90deg.   

Message 6 of 10
john.kirchner
in reply to: rusty.bird

Ran your C++ code with @boopathi.sivakumar 's suggestions below:

 

	// Create a collection of entities for move
	Ptr<BRepBodies> brepBodies = rootComp->bRepBodies();
	if (!brepBodies)
		return false;
	Ptr<BRepBody> brepBody = brepBodies->item(0);
	if (!brepBody)
		return false;
	Ptr<ObjectCollection> entities1 = adsk::core::ObjectCollection::create();
	if (!entities1)
		return false;
	entities1->add(brepBody);

	// Create a transform to do move
	Ptr<Matrix3D> transform = adsk::core::Matrix3D::create();

	// Convert deg -> radians 
	double angle1 = (90 * M_PI / 180.0);

	transform->setToRotation(angle1, adsk::core::Vector3D::create(1,0,0), rootComp->originConstructionPoint()->geometry());

	// Create a move feature
	Ptr<MoveFeatures> moveFeats = features->moveFeatures();
	if (!moveFeats)
		return false;
	Ptr<MoveFeatureInput> moveFeatureInput = moveFeats->createInput(entities1, transform);
	if (!moveFeatureInput)
		return false;
	Ptr<MoveFeature> moveFeature = moveFeats->add(moveFeatureInput);
	if (!moveFeature)
		return false;

 

 

Results were successful here, with the cylinder rotated now

 

rotate_screenshot.PNG

 

 

Maybe double check bRepBody index to make sure it's the right one and double check M_PI for you is correct, I used this for my test run

 

#define M_PI 3.1415926535897931

 

 

Another thing to check with C++ is to make sure your code is built/up to date, maybe add a 

 

ui->messageBox("Test");

 

to see if the new changes are being used

Otherwise, is it possible to upload your f3d file if the above is all correct on your end? Maybe something weird going on behind the scenes



Message 7 of 10
rusty.bird
in reply to: john.kirchner

Sorry Yes @boopathi.sivakumar fix was working.  I had some code above I was testing I forgot to change.  Thanks  

 

Although I am having another issue now with making a simple sketch after that Move feature. It doesn't show up in the UI for some reason.  I have just used that message box Test line to help debug it, the messageBox "Test" shows up before the MoveFeature() but "Test1" doesn't show up.

ui->messageBox("Test");
	// Create move feature
	Ptr<Features> features = rootComp->features();
	if (!features)
		return false;
	Ptr<MoveFeatures> moveFeats = features->moveFeatures();
	if (!moveFeats)
		return false;
	Ptr<MoveFeatureInput> moveFeatureInput = moveFeats->createInput(coll1, transform);
	if (!moveFeatureInput)
		return false;
	Ptr<MoveFeature> moveFeature = moveFeats->add(moveFeatureInput);
	if (!moveFeature)
		return false;
	ui->messageBox("Test1");

 

Message 8 of 10
john.kirchner
in reply to: rusty.bird

moveFeature most likely is invalid so it's returning on that line 14. Double check coll1 and transform, maybe there's a typo/capitalization mismatch somewhere. Maybe coll1 is empty

Might be worthwhile to check out the C++ Specific Issues page or visit it again to go over some helpful troubleshooting tips + using the debugger



Message 9 of 10
rusty.bird
in reply to: john.kirchner

I don't see any typos and it does move the body at the end of the code correctly.

 

I have tested the Move Feature Code Here Fusion 360 Help | Move Feature API Sample and added that same MessageBox at the end and its not showing up there eather.  That is weird.  

Message 10 of 10
rusty.bird
in reply to: rusty.bird

Removing these lines at the end seams to fix the problem 

if(!moveFeature)
		return false;

 

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report