Fusion Add-In with MFC

It is possible to use MFC functionality inside your Fusion add-in on Windows.
First I tried going down the path of creating a C++ add-in inside the “Scripts and Add-InsFusion dialog and then add MFC support to the created Visual Studio C++ project. However, that proved quite difficult and I just gave up and went the other way: created an MFC DLL project and turned it into a Fusion add-in. That was much simpler. 

1) Create an MFC DLL project

MfcCreate1

MfcCreate2

2) Add a Dialog resource to it

Open CustomMfcUi.rc, right-click and choose “Add Resource…

MfcResource

… then select “Dialog” and click “New

MfcResourceNew

3) Double-click the “OK” button of the newly added Dialog so that the “MFC Add Class Wizard” dialog will pop up: 

MfcWizard

Fill it in and click “Finish

4) Rename IDD_DIALOG1 to IDD_MYDIALOG just to keep things tidy

You can use the “Find and Replace” dialog of Visual Studio for that:

MfcFind1

There should be 4 instances replaced at the end:

MfcFind2

5) Add a function to show our dialog

a) Inside CustomMfcUi.cpp add this code:

void showDialog()
{  
  // Always call this macro first thing in any function that
  // will use MFC functionality
  AFX_MANAGE_STATE(AfxGetStaticModuleState());

  CMyDialog myDialog;
  myDialog.DoModal();
};

b) Inside CustomMfcUi.h add this code at the top:

#include “MyDialog.h”

void showDialog();

6) Add Fusion functionality 

Add a new C++ file to the project called FusionCode.cpp 

FusionCode

Fill it with this code:

#include “stdafx.h”

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

// XI_WIN is a macro added by Fusion headers
// to show if the project is a Windows project
#ifdef XI_WIN
  #include “CustomMfcUi.h”
#endif

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;

  showDialog();

  return true;
}

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

  return true;
}

7) Make the project compile as 64 bit since Fusion 360 is 64 bit too

Inside the Configuration Manager” dialog, under “Active solution platform” select “<New…>

Config1

… and add the “x64” platform

Config2

8) Add references to Fusion headers and libraries so that our project will compile

a) Inside the project settings, under “C/C++” >> “Additional Include Directories” add “$(APPDATA)/Autodesk/Autodesk Fusion 360/API/CPP/include” 

MfcProject1

b) Under “Linker” >> “Additional Library Directories” add “$(APPDATA)/Autodesk/Autodesk Fusion 360/API/CPP/lib

MfcProject2

c) Under “Linker” >> “Additional Dependencies” add “core.lib;fusion.lib;

MfcProject3

9) Let Fusion find our project

a) Under “$(APPDATA)AutodeskAutodesk Fusion 360APIAddIns” create a folder named the same as our dll “CustomMfcUi” 

Add to that folder a manifest file with the same name, i.e. “CustomMfcUi.manifest” and add the following content to it in any text editor:

{
"autodeskProduct":"Fusion360",
"type":"addin",
"author":"",
"description":{"":""},
"supportedOS":"windows",
"editEnabled":false,
"id":"38EE9339-591A-4F72-AFFF-7B20111CC10C",
"version":"1.0.0",
"runOnStartup":false
}

The “id” part in the above text needs to be unique. You could use e.g. Visual Studio‘s “Create GUID” dialog for that. Just remove the curly braces from the generated string

Guid

I’m setting the “runOnStartup” option above to false because it would not be a good idea to pop up a modal dialog while Fusion is starting up. That would halt the startup process.

b) Make our project output the created dll into the Add-In folder we just created. Inside the project settings under “General” >> “Output Directory” add the path of our folder “$(APPDATA)AutodeskAutodesk Fusion 360APIAddInsCustomMfcUi”   

Output

10) Test it

Compile the project and inside the “Scripts and Add-Ins” dialog of Fusion, select our add-in and click “Run

MfcTest

Our dialog should pop up

MfcTest2

The sample project can be found here:
https://github.com/AutodeskFusion360/NativeUI

-Adam 


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading