/
RenameThisDimension.cpp
190 lines (142 loc) · 4.44 KB
/
RenameThisDimension.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <sstream>
#pragma warning(push, 0)
#include <Core/CoreAll.h>
#include <Fusion/FusionAll.h>
#include <expected/tl/expected.hpp>
#pragma warning(pop)
using namespace adsk::core;
using namespace adsk::fusion;
Ptr<Application> app;
Ptr<UserInterface> ui;
const std::string panelId = "SketchPanel";
const std::string myControlId = "RenameDimension";
class Err {
public:
Err(std::string message) : message(message) {}
std::string message;
};
Ptr<SketchDimension> getSelectedDimension(Ptr<Selections> selections) {
if (!selections) return nullptr;
if (selections->count() != 1) return nullptr;
auto sel = selections->item(0);
if (!sel) return nullptr;
auto ent = sel->entity();
if (!ent) return nullptr;
// this will be null if the cast is invalid because of the fancy Ptr type
return ent->cast<SketchDimension>();
}
tl::expected<int, Err> handleRename() {
auto selections = ui->activeSelections();
auto dim = getSelectedDimension(selections);
if (!dim) return tl::make_unexpected(Err("Given dimension is null"));
auto param = dim->parameter();
if (!param) return tl::make_unexpected(Err("Given dimension has a null parameter"));
bool cancelled;
auto newName = ui->inputBox("New dimension name?", cancelled, "", param->name());
if (!cancelled) param->name(newName);
return 1;
}
class RenameMenuEventHandler : public MarkingMenuEventHandler {
void notify(const Ptr<MarkingMenuEventArgs>& args) {
auto menu = args->linearMarkingMenu();
if (!menu) return;
auto controls = menu->controls();
if (!getSelectedDimension(ui->activeSelections())) {
auto myControl = controls->itemById(myControlId);
if (!myControl) return;
myControl->deleteMe();
}
auto cmdDefs = ui->commandDefinitions();
if (!cmdDefs) return;
auto myCmd = cmdDefs->itemById(myControlId);
if (!myCmd) return;
auto myControl = controls->addCommand(myCmd);
}
};
RenameMenuEventHandler onMenu;
class CommandExecutedHandler : public CommandEventHandler {
public:
void notify(const Ptr<CommandEventArgs>& eventArgs) override {
Ptr<Event> firingEvent = eventArgs->firingEvent();
if (!firingEvent) return;
Ptr<Command> command = firingEvent->sender();
if (!command) return;
Ptr<CommandDefinition> cmdDef = command->parentCommandDefinition();
if (!cmdDef) return;
if (cmdDef->id() != myControlId) return;
auto res = handleRename();
if (!res.has_value()) ui->messageBox(res.error().message);
}
};
class CommandCreatedHandler : public CommandCreatedEventHandler {
public:
void notify(const Ptr<CommandCreatedEventArgs>& eventArgs) {
if (!eventArgs) return;
Ptr<Command> cmd = eventArgs->command();
if (!cmd) return;
Ptr<CommandEvent> exec = cmd->execute();
if (!exec) return;
exec->add(&onCommandExecuted_);
}
private:
CommandExecutedHandler onCommandExecuted_;
};
CommandCreatedHandler onCommandCreated;
tl::expected<Ptr<CommandDefinition>, Err> getOrCreateCommandDef() {
auto cmdDefs = ui->commandDefinitions();
if (!cmdDefs) {
return tl::make_unexpected(Err("Could not retrieve command definitions"));
}
auto btn = cmdDefs->itemById(myControlId);
if (!btn) {
btn = cmdDefs->addButtonDefinition(myControlId, "Rename Dimension", "");
if (!btn) {
return tl::make_unexpected(Err("Could not make button"));
}
}
auto creationEvent = btn->commandCreated();
if (!creationEvent) return tl::make_unexpected(Err("Failed to get creation event"));
creationEvent->add(&onCommandCreated);
return btn;
}
extern "C" XI_EXPORT bool run(const char*)
{
app = Application::get();
if (!app) return false;
ui = app->userInterface();
if (!ui) return false;
auto cmdDef = getOrCreateCommandDef();
if (!cmdDef.has_value()) {
ui->messageBox(cmdDef.error().message);
return false;
}
ui->markingMenuDisplaying()->add(&onMenu);
return true;
}
extern "C" XI_EXPORT bool stop(const char*)
{
if (ui)
{
ui->markingMenuDisplaying()->remove(&onMenu);
auto cmdDefs = ui->commandDefinitions();
auto myCmd = cmdDefs->itemById(myControlId);
if (myCmd) myCmd->deleteMe();
ui = nullptr;
}
return true;
}
#ifdef XI_WIN
#include <windows.h>
BOOL APIENTRY DllMain(HMODULE, DWORD reason, LPVOID)
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
#endif // XI_WIN