Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

CommandDefinitionクラスのexecute()について

obatake5TC3R
Advocate

CommandDefinitionクラスのexecute()について

obatake5TC3R
Advocate
Advocate

@Community managers,

This post is in Japanese but please leave it in this forum and help them to collect ideas and replies from other community members globally. Thanks.

 

 

CommandDefinitionクラスのexecute()メソッドを使ってSketchActivateコマンドを実行するコードを作成してテストしました。

run()の内部では問題なく動作するのですが、ダイアログBOXを生成してclass OnExecuteEventHandler : public adsk::core::CommandEventHandlerの内部ではSketchActivateコマンドは正常に動作してくれません。

どうしたらいいでしょうか?

 

class OnExecuteEventHandler : public adsk::core::CommandEventHandler --->実行ハンドラ
{
public:
void notify(const Ptr<CommandEventArgs>& eventArgs) override
{
Ptr<Command> command = eventArgs->command();
if (!command)
return;
Ptr<CommandInputs> inputs = command->commandInputs();
// Get the root component of the active design
Ptr<Design> design = app->activeProduct();
if (!design)
return;
Ptr<Component> rootComp = design->rootComponent();
if (!rootComp)
return;
// Create an extrusion input
Ptr<Features> feats = rootComp->features();
if (!feats)
return;
// ターゲットボディをゲット
Ptr<SelectionCommandInput> selectionInput = inputs->itemById("TargetSketchInput");
if (!selectionInput)
return;
if (!selectionInput->selectionCount())
return;
Ptr<Selection> selection = selectionInput->selection(0);
if (!selection)
return;
Ptr<Sketch> target = selection->entity(); // セレクトされたスケッチをGET
if (!target)
return;
ui->activeSelections()->clear();
ui->activeSelections()->add(target);
Ptr<CommandDefinition> skCmd = ui->commandDefinitions()->itemById("SketchActivate");
skCmd->execute();---->失敗する
}
} onExecuteEventHander;

// CommandDestroyed event handler
class OnDestroyEventHandler : public adsk::core::CommandEventHandler
{
public:
void notify(const Ptr<CommandEventArgs>& eventArgs) override
{
adsk::terminate();
}
} onDestroyEventHandler;

// 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)
{
Ptr<CommandEvent> onDestroy = command->destroy();
if (!onDestroy)
return;
bool isOk = onDestroy->add(&onDestroyEventHandler);
if (!isOk)
return;
// Connect to the execute event.
Ptr<CommandEvent> onExecute = command->execute();
if (!onExecute)
return;
isOk = onExecute->add(&onExecuteEventHander);
if (!isOk)
return;
// Get the CommandInputs collection associated with the command.
Ptr<CommandInputs> inputs = command->commandInputs();
if (!inputs)
return;

Ptr<SelectionCommandInput> selectionSketchInput = inputs->addSelectionInput("TargetSketchInput", u8"ターゲットスケッチ", u8"ターゲットスケッチ選択");
if (!selectionSketchInput)
return;
selectionSketchInput->clearSelection();
selectionSketchInput->isFullWidth(false);
selectionSketchInput->addSelectionFilter("Sketches");
selectionSketchInput->setSelectionLimits(1, 1);
}
}
}
} commandCreatedEventHandler;

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

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

Ptr<Product> product = app->activeProduct();
if (!product)
return false;
Ptr<Design> design = product;
if (!design)
return false;
// Create the command definition.
Ptr<CommandDefinitions> commandDefinitions = ui->commandDefinitions();
if (!commandDefinitions)
return nullptr;

// Get the existing command definition or create it if it doesn't already exist.
Ptr<CommandDefinition> cmdDef = commandDefinitions->itemById("SketchActiveSample");
if (!cmdDef)
{
cmdDef = commandDefinitions->addButtonDefinition("SketchActiveSample", "Sketch Activate Sample","Sample to demonstrate the sketch activate command execute.");
}

// Connect to the command created event.
Ptr<CommandCreatedEvent> createdEvent = cmdDef->commandCreated();
if (!createdEvent)
return false;
createdEvent->add(&commandCreatedEventHandler);

// Execute the command definition.
cmdDef->execute();

// Prevent this module from being terminated when the script returns, because we are waiting for event handlers to fire.
adsk::autoTerminate(false);

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
Reply
Accepted solutions (1)
368 Views
1 Reply
Reply (1)

kandennti
Mentor
Mentor
Accepted solution

@obatake5TC3R さん こんにちは

 

自分の認識(pythonで試す限り)では、CommandDefinition.executeメソッドは、

どの位置で実行してもスクリプト/アドインの終了直前(又は終了直後)にまとめて

処理されます。その為、途中での利用が出来ません。(=役に立ちません)

 

その為、TextCommandsを利用する方法をこのフォーラムに書き残しました。

(以下、全てpythonでの利用を前提で記載しています)

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/use-textcommands/m-p/9645688#M10756 

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/use-textcommands2/m-p/9937161#M12118 

 

日本語で良いのであれば、ほぼ同等の内容をブログに書き残しています。

https://kantoku.hatenablog.com/entry/2020/05/30/145007 

https://kantoku.hatenablog.com/entry/2020/12/01/125806 

 

記載していない事が1つあります。

日本語環境で行っている事が原因なのではないかと思いますが、

executeTextCommandメソッドのパラメータの文字列はUnicodeの文字列でないと

実行されないものがありました。

 

 

TextCommandsは、APIだけでは出来ない処理が出来る可能性が有ります。

但し、Autodeskさんからの動作保証はありません。必要となる部分だけでの

利用に留めておいた方が良さそうです。

0 Likes