Export Step File on Save Event

Export Step File on Save Event

ruedi.weber
Contributor Contributor
775 Views
2 Replies
Message 1 of 3

Export Step File on Save Event

ruedi.weber
Contributor
Contributor

Hi, I have an issue with exporting file while saving a design.

I export the design using the following C++ script:

#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;

// Event handler for the documentSaved event.
class MyDocumentSavedEventHandler : public adsk::core::DocumentEventHandler
{
public:
    void notify(const Ptr<DocumentEventArgs>& eventArgs) override
    {
        // get active design
        Ptr<Product> product = app->activeProduct();
        if (!product)
            return;
        
        Ptr<Design> design = product;
        if (!design)
            return;
        
        // get all components in this design
        Ptr<Components> comps = design->allComponents();
        if (!comps)
            return;
        
        // create a single exportManager instance
        Ptr<ExportManager> exportMgr = design->exportManager();
        if (!exportMgr)
            return;
        
        // export the component one by one with a specified format
        size_t count = comps->count();
        for (size_t index = 0; index < count; ++index)
        {
            Ptr<Component> comp = comps->item(index);
            std::string name = comp->name();
            std::string fileName = "/Users/ruediweber/FusionExport/test/" ;
            fileName.append(name);
            
            // export the component with STP format
            Ptr<STEPExportOptions> stepOptions = exportMgr->createSTEPExportOptions(fileName, comp);
            if (!stepOptions) continue;
            exportMgr->execute(stepOptions);
        }
        return;
        
    }
} documentSaved_;

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

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

    Ptr<DocumentEvent> documentSaved = app->documentSaved();
    if (!documentSaved)
        return false;
    documentSaved->add(&documentSaved_);
    
	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

The file contains the actual data from the current version, but the file name becomes the name of the preveous version.

Change the Design:

01ChangePart.png

Bevor saving:

02BevorSaving.png

 

Saved file with new Version v10303AfterSaving.png

 

File named with the previous version.04ExportFiles.png

 

Fiel contains the current content:05FileInViewer.png

 

Is there a way around this behaviour?

On the Step file it would be possible to manipulate the file name. In a F3D file this could be some kind of misleading.

0 Likes
Accepted solutions (1)
776 Views
2 Replies
Replies (2)
Message 2 of 3

goyals
Autodesk
Autodesk
Accepted solution

version number of the document is updated after it is uploaded the the cloud but DocumentSaved event is fire before that that is why component name is still carrying the old version number. One possible workaround might be to get the DataFile object from document and get latest version number and bump its count by 1 to generate the file name.



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

ruedi.weber
Contributor
Contributor

Thanks for your suggestion. This could work for this particular case. On other formats like F3D the version inside the file would still be misleading. I have found a Post on the IdeaStation, thad depends on versioning. I added a suggestion to add an event for version change (https://forums.autodesk.com/t5/fusion-360-ideastation/give-users-control-of-versioning-and-revisions...). Maybe this could be added in the future.

0 Likes