Insert stl file using C++ script.

Insert stl file using C++ script.

galaxyblazer
Enthusiast Enthusiast
832 Views
3 Replies
Message 1 of 4

Insert stl file using C++ script.

galaxyblazer
Enthusiast
Enthusiast

There are many samples how to insert .stl or .obj file using Python or JavaScript script. But there are no any sample for C++.
I can't call addByInsert method from :
Ptr<Component> rootComp = design->rootComponent();
auto occ = rootComp->occurrences();
what next should be?
what I doing wrong ?

0 Likes
833 Views
3 Replies
Replies (3)
Message 2 of 4

ekinsb
Alumni
Alumni

Here's the full code for a script that displays a dialog to get the filename of an stl or obj file and then inserts it into the active design.  To simplify the sample, there's almost no error handling.

 

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

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

	Ptr<Design> des = app->activeProduct();

	Ptr<FileDialog> fileDialog = ui->createFileDialog();
	fileDialog->filter("Mesh Files (*.STL;*.OBJ);;All files (*.*)");
	if (fileDialog->showOpen() == adsk::core::DialogResults::DialogOK)
	{
		std::string filename = fileDialog->filename();

		Ptr<Component> root = des->rootComponent();
		Ptr<BaseFeature> base = root->features()->baseFeatures()->add();
		base->startEdit();
		Ptr<MeshBody> body = root->meshBodies()->add(filename, adsk::fusion::MeshUnits::CentimeterMeshUnit, base);
		base->finishEdit();
	}
	else
	{
		ui->messageBox("Canceled file dialog.");
	}

	return true;
}

Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 3 of 4

galaxyblazer
Enthusiast
Enthusiast

Could you please send includes and using namespaces for this sample .

0 Likes
Message 4 of 4

ekinsb
Alumni
Alumni

Here's the complete source.

#include <Core/Application/Application.h>
#include <Core/UserInterface/UserInterface.h>
#include <Core/Application/Product.h>
#include <Fusion/Fusion/Design.h>
#include <Core/UserInterface/FileDialog.h>
#include <Fusion/Components/Component.h>
#include <Fusion/Features/BaseFeature.h>
#include <Fusion/Features/BaseFeatures.h>
#include <Fusion/Features/Features.h>
#include <Fusion/MeshBody/MeshBodies.h>
#include <Fusion/MeshBody/MeshBody.h>
#include <Fusion/MeshBody/MeshBodyList.h>

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

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;

	Ptr<Design> des = app->activeProduct();

	Ptr<FileDialog> fileDialog = ui->createFileDialog();
	fileDialog->filter("Mesh Files (*.STL;*.OBJ);;All files (*.*)");
	if (fileDialog->showOpen() == adsk::core::DialogResults::DialogOK)
	{
		std::string filename = fileDialog->filename();

		Ptr<Component> root = des->rootComponent();
		Ptr<BaseFeature> base = root->features()->baseFeatures()->add();
		base->startEdit();
		Ptr<MeshBody> body = root->meshBodies()->add(filename, adsk::fusion::MeshUnits::CentimeterMeshUnit, base);
		base->finishEdit();
	}
	else
	{
		ui->messageBox("Canceled file dialog.");
	}

	return true;
}

extern "C" XI_EXPORT bool stop(const char* context)
{
	if (ui)
	{
		ui->messageBox("in stop");
		ui = nullptr;
	}

	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

Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog