A little C++ help, please

A little C++ help, please

RogerInHawaii
Collaborator Collaborator
355 Views
1 Reply
Message 1 of 2

A little C++ help, please

RogerInHawaii
Collaborator
Collaborator

I'm trying to write a script to access the various components of my model. I started off with the sample program that traverses the assembly, which works fine, but I'm trying to make some enhancements to it. And I'm confused. I'm sure it's something really simple, something that I'm not understanding about C++ programming, but I could really use some enlightenment. 🙂

I have a simple C++ method that takes a Component as an argument.

void MethodTakingComponentAsArgument(Ptr<Component> AComponent)
{
   if (!AComponent)
   {
      ReportAnError...
   }
   ...
}

In the main method I acquire the RootComponent, exactly as it's done in the original sample program, as follows...

Ptr<Product> product = app->activeProduct();
Ptr<Design> design = product;
Ptr<Component> RootComponent = design->rootComponent();

Within that main method I can successfully access the fields of that RootComponent, such as...

ui->messageBox("The root component name is " + RootComponent->name());

but when I call my MethodTakingComponentAsArgument as ...

MethodTakingComponentAsArgument(RootComponent);

it doesn't get it. What gets passed in is a NULL value.

WHY? What am I doing wrong?

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

BrianEkins
Mentor
Mentor

I'm not sure what the issue is with your program but here's a simple one that does what you're trying to do.

 

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

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

Ptr<Application> _app;
Ptr<UserInterface> _ui;

void MethodTakingComponentAsArgument(Ptr<Component> AComponent);

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

	_ui = _app->userInterface();
	if (!_ui)
		return false;

	_ui->messageBox("Hello script");

	Ptr<Design> des = _app->activeProduct();
	Ptr<Component> root = des->rootComponent();

	MethodTakingComponentAsArgument(root);

	return true;
}

void MethodTakingComponentAsArgument(Ptr<Component> AComponent)
{
	if (!AComponent)
	{
		_ui->messageBox("No component was passed in.");
	}
	else
	{
		_ui->messageBox("Component name: " + AComponent->name());
	}
}

#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
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes