- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
@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
Solved! Go to Solution.