Custom actions with mouse are widely used in an add-in. Fusion provides MouseEvents that monitors the events. The custom Command object contains the corresponding functions such as Command.mouseClick(), Command.mouseMove() and Command.mouseWheel() etc. By this methods, the add-in will hook the mouse behaviors and grab the data for custom workflow.
The typical steps to use MouseEvent are (take Python as an example)
1. Define the custom class (say MyMouseClickHandler) of the corresponding event which derives from adsk.core.MouseEventHandle
# Event handler for the mouseClick event.
class MyMouseClickHandler(adsk.core.MouseEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
eventArgs = adsk.core.MouseEventArgs.cast(args)
pstn = eventArgs.position
txtBox = eventArgs.firingEvent.sender.commandInputs.itemById('clickResults')
txtBox.text = str(pstn.x) + ', ' + str(pstn.y)
except:
ui = adsk.core.Application.get().userInterface
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
2. In the class of ButtonDefinition.commandCreated, new an object of MyMouseClickHandler, and delegate with Command.mouseClick
class SampleCommandCreatedEventHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
cmd = eventArgs.command
inputs = cmd.commandInputs
inputs.addTextBoxCommandInput('clickResults', 'Click', '', 1, True)
# Connect to the execute event.
onExecute = SampleCommandExecuteHandler()
cmd.execute.add(onExecute)
handlers.append(onExecute)
# Connect to mouse click event.
onMouseClick = MyMouseClickHandler()
cmd.mouseClick.add(onMouseClick)
handlers.append(onMouseClick)
except:
ui = adsk.core.Application.get().userInterface
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Then, in MyMouseClickHandler, you can grab the data of clicking and pass them to the next workflow.
This is a demo project of Python. It delegates all mouse events, and display the data in the dialog. It is written by my colleague Jack Li in engineer team (Thanks Jack).
I converted the code to C++ project.
The steps are similar:
1. Define the custom class of the event (MyMouseClickHandler)
class MyMouseClickHandler : public MouseEventHandler
{
public:
void notify(const Ptr& eventArgs) override
{
Ptr firingEvent = eventArgs->firingEvent();
if (!firingEvent)
return;
Ptr command = firingEvent->sender();
if (!command)
return;
Ptr inputs = command->commandInputs();
if (!inputs)
return;
//get the data of the clicking position
std::string str = "{";
str += std::to_string(eventArgs->position()->x());
str += ",";
str += std::to_string(eventArgs->position()->y());
str += "}";
//display the data in the custom dialog
Ptr txtBox = inputs->itemById("clickResults");
txtBox->text(str);
}
};
2. Delegate an object of MyMouseClickHandler with Command.mouseClick
// CommandCreated event handler.
class OnCommandCreatedEventHandler : public CommandCreatedEventHandler
{
public:
void notify(const Ptr& eventArgs) override
{
if (eventArgs)
{
Ptr cmd = eventArgs->command();
if (cmd)
{
// Connect to the CommandExecuted event.
Ptr onExec = cmd->execute();
if (!onExec)
return;
bool isOk = onExec->add(&onExecuteHander_);
if (!isOk)
return;
// Define the inputs.
Ptr inputs = cmd->commandInputs();
if (!inputs)
return;
//add inputs
inputs->addTextBoxCommandInput("clickResults", "Click", "",1,true);
//add mouse events
// Connect to the MouseEvent.
cmd->mouseClick()->add(&onMouseClickHandler_);
}
}
}
private:
OnExecuteEventHander onExecuteHander_;
MyMouseClickHandler onMouseClickHandler_;
} cmdCreated_;
<
Hope it could help C++ programmer.