Created
April 24, 2018 18:09
-
-
Save martinhansdk/e44003a816405506ffa50061bcacf6aa to your computer and use it in GitHub Desktop.
Testcase of issue with the Fusion 360 API that shows that setting an attribute on the design causes a selection input to loose it's selection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Core/CoreAll.h> | |
#include <Fusion/FusionAll.h> | |
#include <string> | |
using namespace adsk::core; | |
using namespace adsk::fusion; | |
using namespace std; | |
Ptr<Application> app; | |
Ptr<UserInterface> ui; | |
const char* BUTTON_NAME = "MinimalTestcaseButton"; | |
const char* PANEL_TO_USE = "SolidScriptsAddinsPanel"; | |
const char* COMMAND_ID = "MinimalTestcaseCmdId"; | |
const char* FACES_INPUT = "facesSelection"; | |
const char* ATTRIBUTE_GROUP = "MH-MinimalTestcase"; | |
const char* ATTRIBUTE_TOLERANCE = "Tolerance"; | |
// CommandExecuted event handler. | |
class OnExecuteEventHander : public adsk::core::CommandEventHandler | |
{ | |
public: | |
void notify(const Ptr<CommandEventArgs>& eventArgs) override | |
{ | |
Ptr<Command> cmd = eventArgs->command(); | |
if (cmd) { | |
Ptr<Design> design = app->activeProduct(); | |
if (!design) { | |
ui->messageBox("No active Fusion design", "No Design"); | |
return; | |
} | |
Ptr<CommandInputs> inputs = cmd->commandInputs(); | |
Ptr<SelectionCommandInput> selectionInput = inputs->itemById(FACES_INPUT); | |
size_t count1 = selectionInput->selectionCount(); | |
design->attributes()->add(ATTRIBUTE_GROUP, ATTRIBUTE_TOLERANCE, "value"); | |
size_t count2 = selectionInput->selectionCount(); | |
ui->messageBox(string("valid: ") + (selectionInput->isValid() ? "yes" : "no") | |
+ " count1: " + to_string(count1) | |
+ " count2: " + to_string(count2)); | |
} | |
} | |
}; | |
// CommandCreated event handler. | |
class CommandCreatedEventHandler : public adsk::core::CommandCreatedEventHandler | |
{ | |
public: | |
void notify(const Ptr<CommandCreatedEventArgs>& eventArgs) override | |
{ | |
if (eventArgs) | |
{ | |
// Get the command that was created. | |
Ptr<Command> command = eventArgs->command(); | |
if (command) | |
{ | |
// Connect to the execute event. | |
Ptr<CommandEvent> onExecute = command->execute(); | |
if (!onExecute) | |
return; | |
bool isOk = onExecute->add(&onExecuteHandler); | |
if (!isOk) | |
return; | |
// Get the CommandInputs collection associated with the command. | |
Ptr<CommandInputs> inputs = command->commandInputs(); | |
if (!inputs) | |
return; | |
Ptr<SelectionCommandInput> facesSelectionInput = inputs->addSelectionInput(FACES_INPUT, "Faces to export", "Faces to export"); | |
if (!facesSelectionInput) | |
return; | |
} | |
} | |
} | |
private: | |
OnExecuteEventHander onExecuteHandler; | |
} _cmdCreatedHandler; | |
extern "C" XI_EXPORT bool run(const char* context) | |
{ | |
app = Application::get(); | |
if (!app) | |
return false; | |
ui = app->userInterface(); | |
if (!ui) | |
return false; | |
// Create a button command definition. | |
Ptr<CommandDefinitions> cmdDefs = ui->commandDefinitions(); | |
Ptr<CommandDefinition> cmdDef = cmdDefs->addButtonDefinition(COMMAND_ID, | |
"Export faces to DXF", | |
""); | |
Ptr<ToolbarPanel> addinsPanel = ui->allToolbarPanels()->itemById(PANEL_TO_USE); | |
Ptr<ToolbarControl> cntrl = addinsPanel->controls()->itemById(COMMAND_ID); | |
if (!cntrl) { | |
addinsPanel->controls()->addCommand(cmdDef); | |
} | |
// Connect to the Command Created event. | |
Ptr<CommandCreatedEvent> commandCreatedEvent = cmdDef->commandCreated(); | |
commandCreatedEvent->add(&_cmdCreatedHandler); | |
return true; | |
} | |
extern "C" XI_EXPORT bool stop(const char* context) | |
{ | |
if (ui) | |
{ | |
// Create the command definition. | |
Ptr<CommandDefinitions> commandDefinitions = ui->commandDefinitions(); | |
if (!commandDefinitions) { | |
ui = nullptr; | |
return false; | |
} | |
// Clean up the UI. | |
Ptr<CommandDefinition> cmdDef = commandDefinitions->itemById(COMMAND_ID); | |
if (cmdDef) { | |
cmdDef->deleteMe(); | |
} | |
else | |
ui->messageBox("unable to find cmdDef to delete it!"); | |
Ptr<ToolbarPanel> addinsPanel = ui->allToolbarPanels()->itemById(PANEL_TO_USE); | |
Ptr<ToolbarControl> cntrl = addinsPanel->controls()->itemById(COMMAND_ID); | |
if (cntrl) { | |
cntrl->deleteMe(); | |
} | |
else | |
ui->messageBox("unable to find cntrl to delete it!"); | |
ui = nullptr; | |
return 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment