dataFile and addByInsert do Not Work

dataFile and addByInsert do Not Work

tperam
Advocate Advocate
1,226 Views
9 Replies
Message 1 of 10

dataFile and addByInsert do Not Work

tperam
Advocate
Advocate

hi,

   dataFile and addByInsert do Not Work.

 

I don't know the exact syntax to declare the datafile.

 

My test code is given below.

 

Please correct my code.

 

Thurai

 

/////////////////////////////////////////////////////////////

 

 

  

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

 

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)
{


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

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

 

Ptr<Documents> docs = app->documents();
if (!docs)
return false;

 

// Create a document.
Ptr<Document> doc = docs->add(DocumentTypes::FusionDesignDocumentType);
if (!doc)
return false;

 

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

 

Ptr<Component> rootComp = design->rootComponent();
if (!rootComp)
return false;

 

Ptr<Sketches> sketches = rootComp->sketches();
if (!sketches)
return false;

 

Ptr<ConstructionPlane> xyPlane;

xyPlane = rootComp->xYConstructionPlane();
if (!xyPlane)
return false;

 

Ptr<Point3D> insertPoint = Point3D::create(0.0, 0.0, 0.0);


Ptr<DataFile> dataFile;

ui->messageBox(" Before dataFile ");

 

// Set the value of the property, where value_var is a string.
bool returnValue = dataFile->name("E:\\Backups\\Fusion 360\\FeatureExtrude.f3d");
if(!returnValue)
return false;

 

ui->messageBox(" After dataFile ");


Ptr<Matrix3D> insertMatrix = Matrix3D::create();

 

Ptr<Vector3D> vector = Vector3D::create(insertPoint->x(), insertPoint->y(), insertPoint->z());

 

insertMatrix->translation(vector);

 

Ptr<Occurrence> occ = rootComp->occurrences()->addByInsert(dataFile, insertMatrix, true);

 

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

0 Likes
1,227 Views
9 Replies
Replies (9)
Message 2 of 10

goyals
Autodesk
Autodesk

I think you are trying to insert a design from your local disk. To do that you need to use importManager class. Please take a look at this sample https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-3f24e9e8-422d-11e5-937b-f8b156d7cd97

 

Once the document is opened in Fusion, you can get the data file using this property, https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-99CAED33-B90B-4548-823B-81048BC5BB0E, available on document class. 



Shyam Goyal
Sr. Software Dev. Manager
0 Likes
Message 3 of 10

tperam
Advocate
Advocate

Hi Goyals,


thanks for your repsonse.

 

Your 2nd link https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-99CAED33-B90B-4548-823B-81048BC5BB0E does not work.

 

I added the following into my code:

It gives me a Memory Error 107 at the dataFile line.

 

Pls can you modify my  C++ code so that it works.

 

Thanks,
Thurai

 

/////////////////////////////////////////////

// Get import manager
Ptr<ImportManager> importManager = app->importManager();
if (!importManager)
return false;

 

// Get archive import options
const std::string archiveFileName = "E:\\Backups\\Fusion 360\\FeatureExtrude.f3d";
Ptr<FusionArchiveImportOptions> archiveOptions = importManager->createFusionArchiveImportOptions(archiveFileName);

// Import archive file to root component
importManager->importToTarget(archiveOptions, rootComp);


Ptr<Point3D> insertPoint = Point3D::create(0.0, 0.0, 0.0);


Ptr<DataFile> dataFile;

ui->messageBox(" Before dataFile ");

// Set the value of the property, where value_var is a string.
bool returnValue = dataFile->name(archiveFileName);
if(!returnValue)
return false;

ui->messageBox(" After dataFile ");

 

//////////////////////////////////////////////////////////

0 Likes
Message 4 of 10

goyals
Autodesk
Autodesk

Not sure why the link did not work for you. I just tried again and it seems working for me. May be you add below two lines in your code to get the data file for the imported document. I hope it helps. Thanks.

 

Ptr<Document> document  = importManager->importToNewDocument(archiveOptions);

Ptr<DataFile> datFile = document->dataFile()



Shyam Goyal
Sr. Software Dev. Manager
0 Likes
Message 5 of 10

tperam
Advocate
Advocate

Hi Goyals,

                 thanks for your response.

 

I added the following code given below. It went through the code without any error message.

 

But it did not go through the dataFile step

 

Pls Correct it and Test it at your end.

 

Thurai

/////////////////////////////////////////////////////

 

// Get import manager
Ptr<ImportManager> importManager = app->importManager();
if (!importManager)
return false;

// Get archive import options
const std::string archiveFileName = "E:\\Backups\\Fusion 360\\FeatureExtrude.f3d";
Ptr<FusionArchiveImportOptions> archiveOptions = importManager->createFusionArchiveImportOptions(archiveFileName);

// Import archive file to root component
// importManager->importToTarget(archiveOptions, rootComp);

ui->messageBox(" Before document ");

Ptr<Document> document = importManager->importToNewDocument(archiveOptions);
if(!document)
return false;

 

// Ptr<Document> document = importManager->importToTarget2(archiveOptions, rootComp);

ui->messageBox(" Before dataFile ");

 

Ptr<DataFile> dataFile = document->dataFile();
if(!dataFile)
return false;

 

ui->messageBox(" After dataFile ");


Ptr<Point3D> insertPoint = Point3D::create(10.0, 20.0, 30.0);

 

Ptr<Matrix3D> insertMatrix = Matrix3D::create();
if(!insertMatrix)
return false;


ui->messageBox(" After insertMatrix create ");

Ptr<Vector3D> vector = Vector3D::create(insertPoint->x(), insertPoint->y(), insertPoint->z());
if(!vector)
return false;

 

ui->messageBox(" After vector");

bool retval = insertMatrix->translation(vector);
if(!retval)
return false;


ui->messageBox(" After translation ");

Ptr<Occurrence> occ = rootComp->occurrences()->addByInsert(dataFile, insertMatrix, true);
if(!occ)
return false;


ui->messageBox(" After addByInsert() ");

return true;

///////////////////////////////////////////

 

 

0 Likes
Message 6 of 10

tperam
Advocate
Advocate

The two lines of code:
const std::string archiveFileName = "E:\\Backups\\Fusion 360\\FeatureExtrude.f3d";
Ptr<FusionArchiveImportOptions> archiveOptions = importManager->createFusionArchiveImportOptions(archiveFileName);

 

is opening the file and inserting the Component at (0,0,0), before reaching the lines of code:

Ptr<Document> document = importManager->importToNewDocument(archiveOptions);
if(!document)
return false;

 

Ptr<DataFile> dataFile = document->dataFile();
if(!dataFile)
return false;

 

So my issues is to insert the component at the selected(x, y, z) location using addByInsert(dataFile, insertMatrix, true)..

 

The complete code is given below.

///////////////////////////////////////////////////////////

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

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)
{


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

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

Ptr<Documents> docs = app->documents();
if (!docs)
return false;

// Create a document.
Ptr<Document> doc = docs->add(DocumentTypes::FusionDesignDocumentType);
if (!doc)
return false;


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

Ptr<Component> rootComp = design->rootComponent();
if (!rootComp)
return false;


Ptr<Sketches> sketches = rootComp->sketches();
if (!sketches)
return false;


Ptr<ConstructionPlane> xyPlane;

xyPlane = rootComp->xYConstructionPlane();
if (!xyPlane)
return false;

// Get import manager
Ptr<ImportManager> importManager = app->importManager();
if (!importManager)
return false;

// Get archive import options
const std::string archiveFileName = "E:\\Backups\\Fusion 360\\FeatureExtrude.f3d";
Ptr<FusionArchiveImportOptions> archiveOptions = importManager->createFusionArchiveImportOptions(archiveFileName);

ui->messageBox(" Before document ");

Ptr<Document> document = importManager->importToNewDocument(archiveOptions);
if(!document)
return false;

ui->messageBox(" Before dataFile ");

Ptr<DataFile> dataFile = document->dataFile();
if(!dataFile)
return false;

ui->messageBox(" After dataFile ");


Ptr<Point3D> insertPoint = Point3D::create(10.0, 20.0, 30.0);

Ptr<Matrix3D> insertMatrix = Matrix3D::create();
if(!insertMatrix)
return false;


ui->messageBox(" After insertMatrix create ");

Ptr<Vector3D> vector = Vector3D::create(insertPoint->x(), insertPoint->y(), insertPoint->z());
if(!vector)
return false;

ui->messageBox(" After vector");

bool retval = insertMatrix->translation(vector);
if(!retval)
return false;


ui->messageBox(" After translation ");

Ptr<Occurrence> occ = rootComp->occurrences()->addByInsert(dataFile, insertMatrix, true);
if(!occ)
return false;


ui->messageBox(" After addByInsert() ");

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


/////////////////////////////////////////////////////////

0 Likes
Message 7 of 10

tperam
Advocate
Advocate

 

 

My issues is still  to make it go through the statement:

 

Ptr<DataFile> dataFile = document->dataFile();
if(!dataFile)
return false;

 

and insert the component at the selected insertPoint (10, 20, 30) location using

 

addByInsert(dataFile, insertMatrix, true);

 

Now the program Stops at the statement: 

 

Ptr<DataFile> dataFile = document->dataFile();

 

Thurai

0 Likes
Message 8 of 10

goyals
Autodesk
Autodesk

@tperam ,

DataFile is associated with a document saved on cloud. In your case the file is imported from the disk and not uploaded to cloud yet hence dataFile is coming as null. You can try to save the document on cloud first before getting the dataFile object or you can upload it to the cloud through API.



Shyam Goyal
Sr. Software Dev. Manager
0 Likes
Message 9 of 10

goyals
Autodesk
Autodesk

Here is a sample script showing how to save a file to cloud. I assumed document is already opened in Fusion and is currently active document.

 

app = adsk.core.Application.get()

data = app.data

project = app.activeProject

folder = project.rootFolder

doc.saveAs("MyFile",folder,"Description","Tag")

 



Shyam Goyal
Sr. Software Dev. Manager
Message 10 of 10

tperam
Advocate
Advocate

Hi Goyals,

                 Thanks for your response.

 

I have saved the File "FeatureExtude v1.f3d" to the Cloud in "My First Project"

 

How do I refer to this  in my C++ code?

 

Can you modify my code below to reflect this file in the Cloud.

 

Thanks Goyals

Thurai

 

//////////////////////////////////////////////////////////////////

// Get import manager
Ptr<ImportManager> importManager = app->importManager();
if (!importManager)
return false;

// Get archive import options
const std::string archiveFileName = "My First Project\\FeatureExtrude v1.f3d";
Ptr<FusionArchiveImportOptions> archiveOptions = importManager->createFusionArchiveImportOptions(archiveFileName);

 

ui->messageBox(" Before document ");

Ptr<Document> document = importManager->importToNewDocument(archiveOptions);
if(!document)

return false;

 

ui->messageBox(" Before dataFile ");

Ptr<DataFile> dataFile = document->dataFile();
if(!dataFile)
return false;

 

ui->messageBox(" After dataFile ");


Ptr<Point3D> insertPoint = Point3D::create(10.0, 20.0, 30.0);

 

Ptr<Matrix3D> insertMatrix = Matrix3D::create();
if(!insertMatrix)
return false;


ui->messageBox(" After insertMatrix create ");

 

//////////////////////////////////////////////////////////////////

 

 

0 Likes