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: 

Removing a Feature (body) in C++

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
rusty.bird
261 Views, 8 Replies

Removing a Feature (body) in C++

Hello, I am having some trouble removing a body in the root component of a part.  I currently have two bodies in the root component and would like to remove the second one.  Here is a snippet of the code I have.

 

 

 

// Get the body  
	Ptr<BRepBodies> bodiesinrootComp = rootComp->bRepBodies();
	if (!bodiesinrootComp)
		return false;

// Get the second body to delete
	Ptr<Features> delete1 = bodiesinrootComp->item(1);
	if (!delete1)
		return false;

	//Get the remove features
	Ptr<RemoveFeatures> removebody1 = delete1->removeFeatures();
	if (!removebody1)
		return false;

	//Create remove feature
	Ptr<RemoveFeature> removebody11 = removebody1->add(delete1);
	if (!removebody11)
		return false;

 

 

  

8 REPLIES 8
Message 2 of 9
j.han97
in reply to: rusty.bird

No experience on C++ but maybe try getting the removeFeatures from rootComp instead of body

Message 3 of 9
rusty.bird
in reply to: j.han97

Do you have an example and what you mean/to try?  I believe I am following the right syntax from Features.removeFeatures Property    

Message 4 of 9

Hi @rusty.bird ,

 

You just need to change line 12 of your code like so:

 

Ptr<RemoveFeatures> removebody1 = rootComp->features()->removeFeatures();

 

 

Hope this help you.

 

Regards,
Jorge

Message 5 of 9
rusty.bird
in reply to: rusty.bird

I have changed to the above but it is still not removing the feature in the browser.  Here is my full code and a snapshot of the browser of the body I want to remove (body2).  I also have capture design history turned on 

#include <Core/CoreAll.h>
#include <Fusion/FusionAll.h>
#include <CAM/CAMAll.h>

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

Ptr<Application> app;
Ptr<UserInterface> ui;

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

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

	// Get active design
	Ptr<Product> product = app->activeProduct();
	if (!product)
		return false;

	Ptr<Design> design = product;
	if (!design)
		return false;

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

	// Get the body  
	Ptr<BRepBodies> bodiesinrootComp = rootComp->bRepBodies();
	if (!bodiesinrootComp)
		return false;

	// Get the second body to delete
	Ptr<Features> delete1 = bodiesinrootComp->item(1);
	if (!delete1)
		return false;

	//Get the remove features
	Ptr<RemoveFeatures> removebody1 = rootComp->features()->removeFeatures();
	if (!removebody1)
		return false;

	//Create remove feature
	Ptr<RemoveFeature> removebody11 = removebody1->add(delete1);
	if (!removebody11)
		return false;

	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

  

rustybird_0-1663069296784.png

 

Message 6 of 9
j.han97
in reply to: rusty.bird

Again I am not familiar with C++ and its debugging methods. But perhaps you could make use of the logging function (Fusion 360 Help | Application.log Method | Autodesk) to identify which exact step went wrong?

Message 7 of 9
rusty.bird
in reply to: j.han97

Thanks a logging function would help since I am not getting any info.  How do I use this function in C++.  I checked your link but it is a very vague on the explanation and usage.

Message 8 of 9
rusty.bird
in reply to: rusty.bird

I managed to find another error method that worked to help narrow down where the error is:

if (!feature)
	{
		std::string error;
		app->getLastError(&error);
		app->userInterface()->messageBox(error);
	}

 

I have learned that the error is on this section of code below.  It is not passing anything into "delete1" after running this section.  Although I am still working on the fix.  

// Get the second body to delete
	Ptr<Features> delete1 = bodiesinrootComp->item(1);
	if (!delete1)
		return false;

 

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

I managed to get it working now.  The Code is below.  I had to edit the "features" line and also the "removeFeatures" line and made a separate line to get the body.

#include <Core/CoreAll.h>
#include <Fusion/FusionAll.h>
#include <CAM/CAMAll.h>

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

Ptr<Application> app;
Ptr<UserInterface> ui;

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

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

	// Get active design
	Ptr<Product> product = app->activeProduct();
	if (!product)
		return false;

	Ptr<Design> design = product;
	if (!design)
		return false;

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

	// Get the bodies  
	Ptr<BRepBodies> bodiesinrootComp = rootComp->bRepBodies();
	if (!bodiesinrootComp)
		return false;

	// Get the body
	Ptr<BRepBody> secondbody = bodiesinrootComp->item(1);
	if (!secondbody)
		return false;

	// Get features
	Ptr<Features> delete1 = rootComp->features();
	if (!delete1)
		return false;

	//Get the remove features
	Ptr<RemoveFeatures> removebody1 = delete1->removeFeatures();
	if (!removebody1)
		return false;

	//Create remove feature
	Ptr<RemoveFeature> removebody11 = removebody1->add(secondbody);
	if (!removebody11)
		return false;

	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

 

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