-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathUI.cpp
More file actions
262 lines (218 loc) · 6.48 KB
/
Copy pathUI.cpp
File metadata and controls
262 lines (218 loc) · 6.48 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include <Core/CoreAll.h>
#include <Fusion/FusionAll.h>
#include <CAM/CAMAll.h>
#include "UI.h"
using namespace adsk::core;
using namespace adsk::fusion;
using namespace adsk::cam;
bool isPureNumber(std::string str)
{
for (char c : str)
{
if (c < '0' || c > '9')
return false;
}
return true;
}
class GearParams
{
public:
double module;
int numWheelTeeth;
int numPinionTeeth;
double wheelThickness;
double pinionThickness;
bool isValid()
{
if (module > 0.0 && numWheelTeeth > 2 && numPinionTeeth > 2 && wheelThickness > 0.0 && pinionThickness > 0.0)
return true;
else
return false;
}
bool extractValsFromInput(Ptr<CommandInputs> inputs)
{
Ptr<ValueCommandInput> moduleInput = inputs->itemById("module");
Ptr<StringValueCommandInput> wheelTeethInput = inputs->itemById("numWheelTeeth");
Ptr<StringValueCommandInput> pinionTeethInput = inputs->itemById("numPinionTeeth");
Ptr<ValueCommandInput> wheelThicknessInput = inputs->itemById("wheelThickness");
Ptr<ValueCommandInput> pinionThicknessInput = inputs->itemById("pinionThickness");
if (!moduleInput || !wheelTeethInput || !pinionTeethInput || !wheelThicknessInput || !pinionThicknessInput)
return false;
module = moduleInput->value();
numWheelTeeth = 0;
std::string wheelTeethValue = wheelTeethInput->value();
if (!wheelTeethValue.empty() && isPureNumber(wheelTeethValue))
{
numWheelTeeth = atoi(wheelTeethValue.c_str());
}
numPinionTeeth = 0;
std::string pinionTeethValue = pinionTeethInput->value();
if (!pinionTeethValue.empty() && isPureNumber(pinionTeethValue))
{
numPinionTeeth = atoi(pinionTeethValue.c_str());
}
wheelThickness = wheelThicknessInput->value();
pinionThickness = pinionThicknessInput->value();
return true;
}
};
class OnExecuteEventHander : 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<CommandInputs> inputs = command->commandInputs();
if (!inputs)
return;
GearParams params;
params.extractValsFromInput(inputs);
Ptr<Application> app = Application::get();
if (!app)
return;
Ptr<UserInterface> ui = app->userInterface();
if (!ui)
return;
if (params.isValid())
{
// Make the gear!
}
else
{
ui->messageBox("Inputs are not valid!");
}
}
};
class OnValidateInputsHandler : public ValidateInputsEventHandler
{
public:
void notify(const Ptr<ValidateInputsEventArgs>& eventArgs) override
{
Ptr<Event> firingEvent = eventArgs->firingEvent();
if (!firingEvent)
return;
Ptr<Command> command = firingEvent->sender();
if (!command)
return;
Ptr<CommandInputs> inputs = command->commandInputs();
if (!inputs)
return;
GearParams params;
params.extractValsFromInput(inputs);
if(params.isValid())
eventArgs->areInputsValid(true);
else
eventArgs->areInputsValid(false);
}
};
// Setup the Dialog box to allow user to input params
class OnCommandCreatedEventHandler : public CommandCreatedEventHandler
{
public:
void notify(const Ptr<CommandCreatedEventArgs>& eventArgs) override
{
if (eventArgs)
{
Ptr<Command> cmd = eventArgs->command();
if (cmd)
{
// Connect to the CommandExecuted event.
Ptr<CommandEvent> onExec = cmd->execute();
if (!onExec)
return;
bool isOk = onExec->add(&onExecuteHander_);
if (!isOk)
return;
// Connect to the ValidateInputs event.
Ptr<ValidateInputsEvent> onValidateInputs = cmd->validateInputs();
if (!onValidateInputs)
return;
isOk = onValidateInputs->add(&onValidateInputsHandler_);
if (!isOk)
return;
// Define the inputs.
Ptr<CommandInputs> inputs = cmd->commandInputs();
if (!inputs)
return;
// Module
Ptr<ValueInput> moduleInitialVal = ValueInput::createByReal(1.0);
if (!moduleInitialVal)
return;
inputs->addValueInput("module", "Module", "mm", moduleInitialVal);
// Wheel Teeth
inputs->addStringValueInput("numWheelTeeth", "Number of Wheel Teeth", "24");
// Pinion Teeth
inputs->addStringValueInput("numPinionTeeth", "Number of Pinion Teeth", "8");
// Wheel Thickness
Ptr<ValueInput> wheelThicknessVal = ValueInput::createByReal(1.0);
if (!wheelThicknessVal)
return;
inputs->addValueInput("wheelThickness", "Wheel Thickness", "mm", wheelThicknessVal);
// Pinion Thickness
Ptr<ValueInput> pinionThicknessVal = ValueInput::createByReal(2.0);
if (!pinionThicknessVal)
return;
inputs->addValueInput("pinionThickness", "Pinion Thickness", "mm", pinionThicknessVal);
}
}
}
private:
OnExecuteEventHander onExecuteHander_;
OnValidateInputsHandler onValidateInputsHandler_;
} cmdCreated_;
bool CreateMenus(Ptr<UserInterface> ui)
{
const std::string commandId = "CycloidalGearCommandId";
const std::string commandName = "Create Cycloidal Gear";
const std::string commandDescription = "Create a Cycloidal gear.";
const std::string commandResources = "./resources";
Ptr<Workspaces> workspaces = ui->workspaces();
if (!workspaces)
return false;
Ptr<Workspace> modelingWorkspace = workspaces->itemById("FusionSolidEnvironment");
if (!modelingWorkspace)
return false;
Ptr<ToolbarPanels> toolbarPanels = modelingWorkspace->toolbarPanels();
if (!toolbarPanels)
return false;
Ptr<ToolbarPanel> toolbarPanel = toolbarPanels->itemById("SolidCreatePanel");
if (!toolbarPanel)
return false;
Ptr<ToolbarControls> toolbarControls = toolbarPanel->controls();
if (!toolbarControls)
return false;
Ptr<ToolbarControl> toolbarControl = toolbarControls->itemById(commandId);
if (toolbarControl)
{
ui->messageBox("Cycloidal Gear command is already loaded.");
adsk::terminate();
return true;
}
else
{
Ptr<CommandDefinitions> commandDefinitions = ui->commandDefinitions();
if (!commandDefinitions)
return false;
Ptr<CommandDefinition> commandDefinition = commandDefinitions->itemById(commandId);
if (!commandDefinition)
{
commandDefinition = commandDefinitions->addButtonDefinition(commandId, commandName, commandDescription, commandResources);
if (!commandDefinition)
return false;
}
Ptr<CommandCreatedEvent> commandCreatedEvent = commandDefinition->commandCreated();
if (!commandCreatedEvent)
return false;
commandCreatedEvent->add(&cmdCreated_);
toolbarControl = toolbarControls->addCommand(commandDefinition);
if (!toolbarControl)
return false;
toolbarControl->isVisible(true);
}
return true;
}