Combine solid bodies C++

Combine solid bodies C++

galaxyblazer
Enthusiast Enthusiast
803 Views
3 Replies
Message 1 of 4

Combine solid bodies C++

galaxyblazer
Enthusiast
Enthusiast

Hi.
Can somebody provide me a sample code which combine solid bodies in Fusion 360?

0 Likes
Accepted solutions (1)
804 Views
3 Replies
Replies (3)
Message 2 of 4

liujac
Alumni
Alumni
Accepted solution

Hi,

 

I wrote the C++ sample as below. Make sure there are at least two bodies under the root component before you run the script.

 

 

#include <Core/Application/Application.h>
#include <Core/Application/ObjectCollection.h>

#include <Fusion/Fusion/Design.h>
#include <Fusion/Components/Component.h>
#include <Fusion/BRep/BRepBodies.h>
#include <Fusion/BRep/BRepBody.h>
#include <Fusion/Features/Features.h>
#include <Fusion/Features/CombineFeatures.h>
#include <Fusion/Features/CombineFeatureInput.h>

using namespace adsk::core;
using namespace adsk::fusion;

Ptr<UserInterface> ui;

extern "C" XI_EXPORT bool run(const char* context)
{
	Ptr<Application> app = Application::get();
	if (!app)
		return false;

	ui = app->userInterface();
	if (!ui)
		return false;

	Ptr<Design> design = app->activeProduct();
	if (!design)
		return false;

	// Get the root component of the active design
	Ptr<Component> root = design->rootComponent();
	if (!root)
		return false;

	Ptr<BRepBodies> bodies = root->bRepBodies();
	if (!bodies)
		return false;

	Ptr<BRepBody> targetBody = bodies->item(0);
	if (!targetBody)
		return false;

	Ptr<BRepBody> toolBody = bodies->item(1);
	if (!toolBody)
		return false;

	Ptr<ObjectCollection> toolBodies = ObjectCollection::create();
	if (!toolBodies)
		return false;
	toolBodies->add(toolBody);

	Ptr<Features> feats = root->features();
	if (!feats)
		return false;

	Ptr<CombineFeatures> combines = feats->combineFeatures();
	if (!combines)
		return false;

	Ptr<CombineFeatureInput> combineInput = combines->createInput(targetBody, toolBodies);
	if (!combineInput)
		return false;

	Ptr<CombineFeature> combine = combines->add(combineInput);
	return true;
}


#ifdef XI_WIN

#include <windows.h>

BOOL APIENTRY DllMain(HMODULE hmodule, DWORD reason, LPVOID reserved)
{
	switch (reason)
	{
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}

#endif // XI_WIN

 

 

Regards,

Jack

 

Message 3 of 4

galaxyblazer
Enthusiast
Enthusiast

Does Fusion API provides feature to get all selected bodies and then make some operations with that objects (Something like SelectionSet in AutoCad .net api)? I mean, that I want combine more than two objects in one function.

0 Likes
Message 4 of 4

liujac
Alumni
Alumni

Yes, it's able to get all selections through UserInterface.activeSelections. For example:

Ptr<Application> app = Application::get();
	if (!app)
		return false;

	ui = app->userInterface();
	if (!ui)
		return false;

	Ptr<Selections> sels = ui->activeSelections();
	if (!sels)
		return false;

	for (Ptr<Selection> sel : sels)
	{
		Ptr<BRepBody> body = sel->entity();
		if (body)
		{

		}
	}

Regards,

Jack