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: 

reading coordinates from a file and displaying CustomGraphicsPointSet

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
SamMTT
698 Views, 8 Replies

reading coordinates from a file and displaying CustomGraphicsPointSet

Hi There,

 

I have successfully drawn a few points using custom graphics in the Fusion API. I would like to draw a point cloud from a .xyz file. Please see my coordinates file and code below.

 

The process gets hung up on line:

Ptr<CustomGraphicsPointSet> points = graphicGroup->addPointSet(ChairPointCloud, indexList, PointCloudCustomGraphicsPointType, "hello");

 

Please let me know what I am doing wrong here.

 

Sincerely, 

Sam

 

1.973145 1.959534 0.909668 
1.973346 1.964355 0.909668 
1.973145 1.964355 0.909034 
1.973145 1.944539 0.918457 
1.975196 1.946777 0.918457 
1.973145 1.946777 0.914897 
1.976019 1.955566 0.918457 
1.973145 1.955566 0.910203 
2.474121 1.964355 0.916655 
// Drawing Custom Graphics Points
// Reading point from a file
// Samuel Melamed, 27.03.2020


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

#include <iostream>
#include <fstream>
#include <string>
#include <vector>


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

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;

	ui->messageBox("Drawing Custom Graphics Points");


	// Create a document, activate design space, and set root component
	Ptr<Documents> docs = app->documents(); // ...
	if (!docs)
		return false;
	ui->messageBox("added docs");

	Ptr<Document> doc = docs->add(DocumentTypes::FusionDesignDocumentType); // ...
	if (!doc)
		return false;
	ui->messageBox("added doc");

	Ptr<Design> design = app->activeProduct(); // ...
	if (!design)
		return false;
	ui->messageBox("added design");

	Ptr<Component> rootComp = design->rootComponent(); // ...
	if (!rootComp)
		return false;
	ui->messageBox("added root component");

	Ptr<CustomGraphicsGroups> graphicGroups = rootComp->customGraphicsGroups();
	if (!graphicGroups)
		return false;
	ui->messageBox("added graphicsGroups");

	Ptr<CustomGraphicsGroup> graphicGroup = graphicGroups->add();
	if (!graphicGroup)
		return false;
	ui->messageBox("added graphicsGroup");

	// Reading from a file and storing in coordinate vector
	// Begin

	std::ifstream myFile ("06976.xyz");
	ui->messageBox("read file ok");

	std::string CoordinatesString; //
	std::vector <double> CoordinateList; //
	ui->messageBox("string and vector ok!");

	while (std::getline(myFile, CoordinatesString))
	{
		//std::cout << CoordinatesString << std::endl;

		int step = 0;

		// extract coordinate numbers from string
		for (int c = 0; c < 2; c++)
		{
			std::string TempString = CoordinatesString.substr(step, 8);
			double TempNumber = std::stod(TempString);
			CoordinateList.push_back(TempNumber);
			step = step + 9;
		}
	}

	myFile.close();
	ui->messageBox("closed file ok!");

	// End

	//std::vector<double> myCoordinates = { 1, 0, 0, 0, 1, 0, 0, 0, 1 };
	Ptr<CustomGraphicsCoordinates> ChairPointCloud = CustomGraphicsCoordinates::create(CoordinateList);
	if (!ChairPointCloud)
		return false;
	ui->messageBox("added Coordinates");

	std::vector<int> indexList;
	
	// Displaying point cloud points
	Ptr<CustomGraphicsPointSet> points = graphicGroup->addPointSet(ChairPointCloud, indexList,  PointCloudCustomGraphicsPointType, "hello");
	if (!points)
		return false;
	ui->messageBox("added pointset");

	return true;

}

extern "C" XI_EXPORT bool stop(const char* context)
{
	if (ui)
	{
		ui->messageBox("Stop addin");
		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

 

8 REPLIES 8
Message 2 of 9
goyals
in reply to: SamMTT

I think you are just reading first two coordinates only in a line of coordinates file. Not sure If that is the reason for the problem you are noticing. for loop should run one more time. 



Shyam Goyal
Sr. Software Dev. Manager
Message 3 of 9
SamMTT
in reply to: goyals

Hello, 

 

Thank you for looking into my code. Yes you were right I did not load the last coordinate. However, even with the for loop fix the problem still persists. I believe the problem is opening the file or at least keeping the file open. I have added an if statement to check this and I discovered the file is not open when run through fusion. I get the message box "Did not load file". Please have a look at that section of code below. 

if (myFile.is_open())
	{
		while (std::getline(myFile, CoordinatesString))
		{
			//std::cout << CoordinatesString << std::endl;

			int step = 0;

			// extract coordinate numbers from string
			for (int c = 0; c <= 2; c++)
			{
				std::string TempString = CoordinatesString.substr(step, 8);
				double TempNumber = std::stod(TempString);
				CoordinateList.push_back(TempNumber);
				step = step + 9;
			}
		}
		myFile.close();
		ui->messageBox("File Closed.");
	}
	else
	{
		ui->messageBox("Did not load file.");
		return false;
	}

I have the file saved in my .dll path. Please see .zip folder below.

 

Does anyone know how to load files properly through the API interface? Or why my file is not opening?

 

Looking forward to your answer. 

 

Sincerely, 
Sam

Message 4 of 9
goyals
in reply to: SamMTT

I was able to get it working with the code you put it out in your original query with the fix in for loop I mentioned in my reply. Please take a look at the attached image showing the result I got using 06976.xyz present in the zip file you attached.



Shyam Goyal
Sr. Software Dev. Manager
Message 5 of 9
goyals
in reply to: goyals

Forgot to attach the image in my earlier reply.



Shyam Goyal
Sr. Software Dev. Manager
Message 6 of 9
SamMTT
in reply to: goyals

Oh that looks great! It's exactly what I wanted as an output. 

 

For some reason it is still not working for me!!! 

 

This is my work flow:

1. I launch Scripts and Add ins from fusion. 

2. I pick my Add in, PointCloud, and click Edit.

3. This launches Visual Studio 2019.

4. I build my code through VS 2019

5. I click run in Fusion Scripts and Add ins

6. My ui messages display until this line: ui->messageBox("closed file ok!");

7. The next message I get is: ui->messageBox("Stop addin");

 

So I believe that either my CoordinateList vector is not populating propoerly from the external .xyz file or there is something wrong with this function:

 

Ptr<CustomGraphicsCoordinates> ChairPointCloud = CustomGraphicsCoordinates::create(CoordinateList);
if (!ChairPointCloud)
{
return false;
}

 

Another clue: when I build the code this is what I get as an output: 

 

1>------ Build started: Project: PointCloud, Configuration: Debug x64 ------
1>PointCloud.cpp
1>PointCloud.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/INCREMENTAL:NO' specification
1>   Creating library C:\Users\SAMME\AppData\Roaming\Autodesk\Autodesk Fusion 360\API\AddIns\PointCloud\Debug\PointCloud.lib and object C:\Users\SAMME\AppData\Roaming\Autodesk\Autodesk Fusion 360\API\AddIns\PointCloud\Debug\PointCloud.exp
1>PointCloud.vcxproj -> C:\Users\SAMME\AppData\Roaming\Autodesk\Autodesk Fusion 360\API\AddIns\PointCloud\Debug\PointCloud.dll
1>C:\Users\SAMME\AppData\Roaming\Autodesk\Autodesk Fusion 360\API\AddIns\PointCloud\Debug\PointCloud.dll
1>1 File(s) copied
1>Done building project "PointCloud.vcxproj".
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

 

 

Looking forward to your answer. 

Message 7 of 9
goyals
in reply to: SamMTT

One possible reason is that it is not able to find .xyz file on your machine. Can you try by keeping .xyz file at some specific locations and give its full path hardcoded at line no. 67 in your code like this?

 

 std::ifstream myFile ("/Users/goyals/Desktop/06976.xyz");

 



Shyam Goyal
Sr. Software Dev. Manager
Message 8 of 9
SamMTT
in reply to: goyals

Thank you @goyals yes it finally worked!! I am so happy. 

Message 9 of 9
goyals
in reply to: SamMTT

Glad to hear that. I am also happy to help you. Thanks.



Shyam Goyal
Sr. Software Dev. Manager

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