-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicAddinCPP.cpp
More file actions
483 lines (385 loc) · 18.2 KB
/
BasicAddinCPP.cpp
File metadata and controls
483 lines (385 loc) · 18.2 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#include <Core/CoreAll.h>
#include <Fusion/FusionAll.h>
#include <Cam/CamAll.h>
using namespace adsk::core;
using namespace adsk::fusion;
using namespace adsk::cam;
Ptr<Application> app;
Ptr<UserInterface> ui;
class CommandCreatedHandler;
double epsilon = 1e-6;
const std::string workspaceId("FusionSolidEnvironment");
const std::string toolbarId("ToolsTab");
const std::string toolbarName("BoxTools");
const std::string myPanelId("BoxCreator_panel");
const std::string myPanelName("BoxCreator");
const std::string myPanelAfter("");
const std::string cmdId("ACME_BoxCreator_cmdBoxDialog");
const std::string cmdName("Creating a box");
const std::string cmdDescription("A Fusion Add-in Command for create a box");
const std::string iconFolder("./resources");
static std::vector<Ptr<EventHandler>> eventHandlers;
CommandCreatedHandler* pCommandCreatedHandler = nullptr;
Ptr<CommandDefinition> pCommandDefininition = nullptr;
Ptr<ToolbarTab> pToolbarTab = nullptr;
Ptr<ToolbarPanel> pToolbarPanel = nullptr;
Ptr<CommandControl> pToolbarControl = nullptr;
typedef struct
{
double borderThickness;
double bottomThickness;
double innerWidth;
double innerLength;
double innerHeight;
double filletSize;
std::string inputUnits;
} BoxParameters;
Ptr<Component> createBox(Ptr<Design> design, const BoxParameters& parameters, double margin, const std::string&
bodyName, const std::string& componentName)
{
const std::string inputUnits = parameters.inputUnits;
double borderThickness = parameters.borderThickness;
double bottomThickness = parameters.bottomThickness;
double innerWidth = parameters.innerWidth;
double innerLength = parameters.innerLength;
double innerHeight = parameters.innerHeight;
double filletSize = parameters.filletSize;
// We may take different input units than default units in Fusion. We'll need to convert them first.
Ptr<UnitsManager> unitsManager = design->fusionUnitsManager();
const std::string defaultUnits = unitsManager->internalUnits();
borderThickness = unitsManager->convert(borderThickness, inputUnits, defaultUnits);
bottomThickness = unitsManager->convert(bottomThickness, inputUnits, defaultUnits);
innerWidth = unitsManager->convert(innerWidth, inputUnits, defaultUnits);
innerLength = unitsManager->convert(innerLength, inputUnits, defaultUnits);
innerHeight = unitsManager->convert(innerHeight, inputUnits, defaultUnits);
// I'll keep fillet using mm since 0.5mm is hard to read when it converts to inches.
filletSize = unitsManager->convert(filletSize, inputUnits, defaultUnits);
margin = unitsManager->convert(margin, inputUnits, defaultUnits);
// Get root component
Ptr<Component> rootComponent = design->rootComponent();
// Get references to the sketches and plane
Ptr<Sketches> sketches = rootComponent->sketches();
Ptr<ConstructionPlane> xzPlane = rootComponent->xZConstructionPlane();
// Create a new sketch and get lines reference
Ptr<Sketch> sketchOuterBase = sketches->add(xzPlane);
double baseWidthTotal = borderThickness * 2.0 + innerWidth;
double baseLengthTotal = borderThickness * 2.0 + innerLength;
Ptr<Point3D> pointOne = Point3D::create(margin - baseWidthTotal / 2.0, -baseLengthTotal / 2.0, 0);
Ptr<Point3D> pointTwo = Point3D::create(margin + baseWidthTotal / 2.0, baseLengthTotal / 2.0, 0);
Ptr<SketchLines> rectangles = sketchOuterBase->sketchCurves()->sketchLines();
rectangles->addTwoPointRectangle(pointOne, pointTwo);
// Create extrusion input
Ptr<Profile> baseSketchProfile = sketchOuterBase->profiles()->item(0);
Ptr<ExtrudeFeatures> extrudes = rootComponent->features()->extrudeFeatures();
Ptr<ExtrudeFeatureInput> baseExtrudeNewBody = extrudes->createInput(baseSketchProfile, FeatureOperations::NewBodyFeatureOperation);
// Define that the extent is a distance of bottom thickness + inner height.
// Our input unit is mm, we'll need to convert it into cm.
Ptr<ValueInput> baseExtrudeDistance = ValueInput::createByReal(bottomThickness + innerHeight);
Ptr<DistanceExtentDefinition> baseObjectExtendDefinition = DistanceExtentDefinition::create(baseExtrudeDistance);
baseExtrudeNewBody->setOneSideExtent(baseObjectExtendDefinition, ExtentDirections::PositiveExtentDirection);
baseExtrudeNewBody->isSolid(true);
Ptr<ExtrudeFeature> newExtrudeFeature = extrudes->add(baseExtrudeNewBody);
// We'll need to find the top and bottom sides of the box, we'll check the diagonal points with the faces.
Ptr<Point3D> baseTopPoint1 = Point3D::create(margin - baseWidthTotal / 2.0, bottomThickness + innerHeight, -baseLengthTotal / 2.0);
Ptr<Point3D> baseTopPoint2 = Point3D::create(margin + baseWidthTotal / 2.0, bottomThickness + innerHeight, baseLengthTotal / 2.0);
Ptr<Point3D> baseBottomPoint1 = Point3D::create(margin - baseWidthTotal / 2.0, 0.0, -baseLengthTotal / 2.0);
Ptr<Point3D> baseBottomPoint2 = Point3D::create(margin + baseWidthTotal / 2.0, 0.0, baseLengthTotal / 2.0);
Ptr<ExtrudeFeature> baseExtrudeItem = newExtrudeFeature;
Ptr<BRepBody> baseExtrudeBody = baseExtrudeItem->bodies()->item(0);
Ptr<BRepFace> bottomFace = nullptr;
Ptr<Sketch> sketchInnerBase = nullptr;
Ptr<BRepFaces> faces = baseExtrudeBody->faces();
for (unsigned i = 0; i < faces->count(); ++i)
{
Ptr<BRepFace> face = faces->item(i);
bool topPoint1Flag = face->isPointOnFace(baseTopPoint1, epsilon);
bool topPoint2Flag = face->isPointOnFace(baseTopPoint2, epsilon);
bool bottomPoint1Flag = face->isPointOnFace(baseBottomPoint1, epsilon);
bool bottomPoint2Flag = face->isPointOnFace(baseBottomPoint2, epsilon);
if (topPoint1Flag && topPoint2Flag)
{
sketchInnerBase = sketches->add(face);
pointOne = Point3D::create(-innerLength / 2.0, margin - innerWidth / 2.0, 0);
pointTwo = Point3D::create(innerLength / 2.0, margin + innerWidth / 2.0, 0);
rectangles = sketchInnerBase->sketchCurves()->sketchLines();
rectangles->addTwoPointRectangle(pointOne, pointTwo);
}
if (bottomPoint1Flag && bottomPoint2Flag)
{
bottomFace = face;
}
}
// Have we found top face and bottom face?
if (bottomFace != nullptr && sketchInnerBase != nullptr)
{
// There are two profiles
// The first one is the border between the new sketch and first sketch
// The second one is the sketch we've just drawn.
// We'll need the second one here.
Ptr<Profile> baseInnerProfile = sketchInnerBase->profiles()->item(1);
Ptr<ToEntityExtentDefinition> bottomObjectExtendDefinition = ToEntityExtentDefinition::create(bottomFace, false, ValueInput::createByReal(-bottomThickness));
Ptr<ExtrudeFeatureInput> baseExtrudeCut = extrudes->createInput(baseInnerProfile, FeatureOperations::CutFeatureOperation);
baseExtrudeCut->setOneSideExtent(bottomObjectExtendDefinition, ExtentDirections::NegativeExtentDirection);
baseExtrudeCut->isSolid(true);
extrudes->add(baseExtrudeCut);
}
// Let's add fillet feature
Ptr<FilletFeatures> filletFeatures = rootComponent->features()->filletFeatures();
Ptr<FilletFeatureInput> filletInput = filletFeatures->createInput();
Ptr<ObjectCollection> filletEdges = ObjectCollection::create();
for (unsigned i = 0; i < baseExtrudeBody->edges()->count(); ++i)
{
Ptr<BRepEdge> edge = baseExtrudeBody->edges()->item(i);
filletEdges->add(edge);
}
char buffer[128];
snprintf(buffer, sizeof(buffer), "%.2f mm", filletSize);
Ptr<ValueInput> filletSizeValueInput = ValueInput::createByString(buffer);
filletInput->edgeSetInputs()->addConstantRadiusEdgeSet(filletEdges, filletSizeValueInput, false);
filletFeatures->add(filletInput);
baseExtrudeBody->name(bodyName);
Ptr<BRepBody> newBody = baseExtrudeBody->createComponent();
Ptr<Component> component = newBody->parentComponent();
component->name(componentName);
return component;
}
class CommandExecutedHandler : public adsk::core::CommandEventHandler
{
public:
void notify(const Ptr<CommandEventArgs>& eventArgs) override
{
Ptr<Event> firingEvent = eventArgs->firingEvent();
if (!firingEvent)
return;
Ptr<CommandInputs> pCommandInputs = eventArgs->command()->commandInputs();
// Verify the validity of the input values.This controls if the OK button is enabled or not.
Ptr<ValueCommandInput> pBorderThickness = pCommandInputs->itemById("border_thickness");
Ptr<ValueCommandInput> pBottomThickness = pCommandInputs->itemById("bottom_thickness");
Ptr<ValueCommandInput> pInnerWidth = pCommandInputs->itemById("inner_width");
Ptr<ValueCommandInput> pInnerLength = pCommandInputs->itemById("inner_length");
Ptr<ValueCommandInput> pBottomInnerHeight = pCommandInputs->itemById("bottom_inner_height");
Ptr<ValueCommandInput> pLidInnerHeight = pCommandInputs->itemById("lid_inner_height");
Ptr<ValueCommandInput> pFillet = pCommandInputs->itemById("fillet");
Ptr<Design> design = app->activeProduct();
if (!design)
{
ui->messageBox("The DESIGN workspace must be active when running this command.");
return;
}
double baseMargin = 5.0;
BoxParameters bottomBoxParameters;
bottomBoxParameters.borderThickness = pBorderThickness->value();
bottomBoxParameters.bottomThickness = pBottomThickness->value();
bottomBoxParameters.innerWidth = pInnerWidth->value();
bottomBoxParameters.innerLength = pInnerLength->value();
bottomBoxParameters.innerHeight = pBottomInnerHeight->value();
bottomBoxParameters.filletSize = pFillet->value();
bottomBoxParameters.inputUnits = std::string("cm");
double topEnlargeSize = bottomBoxParameters.borderThickness * 2.0;
BoxParameters topBoxParameters;
topBoxParameters.borderThickness = pBorderThickness->value();
topBoxParameters.bottomThickness = pBottomThickness->value();
topBoxParameters.innerWidth = pInnerWidth->value() + topEnlargeSize;
topBoxParameters.innerLength = pInnerLength->value() + topEnlargeSize;
topBoxParameters.innerHeight = pLidInnerHeight->value();
topBoxParameters.filletSize = pFillet->value();
topBoxParameters.inputUnits = std::string("cm");
createBox(design, bottomBoxParameters, (-bottomBoxParameters.innerWidth - bottomBoxParameters.borderThickness) / 2.0 - baseMargin, "bottom_body", "box");
createBox(design, topBoxParameters, baseMargin + (topBoxParameters.borderThickness + topBoxParameters.innerWidth) / 2.0, "top_body", "lid");
}
};
class ValidateInputHandler : public adsk::core::ValidateInputsEventHandler
{
public:
void notify(const Ptr<ValidateInputsEventArgs>& eventArgs) override
{
Ptr<CommandInputs> pCommandInputs = eventArgs->inputs();
// Verify the validity of the input values.This controls if the OK button is enabled or not.
Ptr<ValueCommandInput> pBorderThickness = pCommandInputs->itemById("border_thickness");
Ptr<ValueCommandInput> pBottomThickness = pCommandInputs->itemById("bottom_thickness");
Ptr<ValueCommandInput> pInnerWidth = pCommandInputs->itemById("inner_width");
Ptr<ValueCommandInput> pInnerLength = pCommandInputs->itemById("inner_length");
Ptr<ValueCommandInput> pBottomInnerHeight = pCommandInputs->itemById("bottom_inner_height");
Ptr<ValueCommandInput> pLidInnerHeight = pCommandInputs->itemById("lid_inner_height");
Ptr<ValueCommandInput> pFillet = pCommandInputs->itemById("fillet");
eventArgs->areInputsValid(true);
if (pBorderThickness->value() <= 0.0)
{
eventArgs->areInputsValid(false);
return;
}
if (pBottomThickness->value() <= 0.0)
{
eventArgs->areInputsValid(false);
return;
}
if (pInnerWidth->value() <= 0.0)
{
eventArgs->areInputsValid(false);
return;
}
if (pInnerLength->value() <= 0.0)
{
eventArgs->areInputsValid(false);
return;
}
if (pBottomInnerHeight->value() <= 0.0)
{
eventArgs->areInputsValid(false);
return;
}
if (pLidInnerHeight->value() <= 0.0)
{
eventArgs->areInputsValid(false);
return;
}
if (pFillet->value() <= 0.0)
{
eventArgs->areInputsValid(false);
}
}
};
class CommandCreatedHandler : public adsk::core::CommandCreatedEventHandler
{
public:
~CommandCreatedHandler() override
{
if (pCommand != nullptr)
{
if (pCommandExecuteHandler != nullptr)
{
Ptr<CommandEvent> executeEvent = pCommand->execute();
executeEvent->remove(pCommandExecuteHandler);
delete pCommandExecuteHandler;
pCommandExecuteHandler = nullptr;
}
if (pValidateInputHandler != nullptr)
{
Ptr<ValidateInputsEvent> validInputEvent = pCommand->validateInputs();
validInputEvent->remove(pValidateInputHandler);
delete pValidateInputHandler;
pValidateInputHandler = nullptr;
}
pCommand = nullptr;
}
}
CommandCreatedHandler() : CommandCreatedEventHandler()
{
pValidateInputHandler = nullptr;
pCommandExecuteHandler = nullptr;
pCommand = nullptr;
}
void notify(const Ptr<CommandCreatedEventArgs>& eventArgs) override
{
Ptr<UnitsManager> unitsManager = app->activeProduct()->unitsManager();
std::string defaultLengthUnits = unitsManager->defaultLengthUnits();
pCommand = eventArgs->command();
Ptr<CommandInputs> inputs = pCommand->commandInputs();
Ptr<CommandEvent> executeEvent = pCommand->execute();
Ptr<ValidateInputsEvent> validInputEvent = pCommand->validateInputs();
// We'll use createByReal here, its units is cm.
Ptr<ValueInput> defaultBorderThickness = ValueInput::createByReal(0.4);
inputs->addValueInput("border_thickness", "Border Thickness", defaultLengthUnits, defaultBorderThickness);
Ptr<ValueInput> defaultBottomThickness = ValueInput::createByReal(0.4);
inputs->addValueInput("bottom_thickness", "Bottom Thickness", defaultLengthUnits, defaultBottomThickness);
Ptr<ValueInput> defaultInnerWidth = ValueInput::createByReal(19.0);
inputs->addValueInput("inner_width", "Inner Width", defaultLengthUnits, defaultInnerWidth);
Ptr<ValueInput> defaultInnerLength= ValueInput::createByReal(39.0);
inputs->addValueInput("inner_length", "Inner Length", defaultLengthUnits, defaultInnerLength);
Ptr<ValueInput> defaultBottomInnerHeight = ValueInput::createByReal(11.0);
inputs->addValueInput("bottom_inner_height", "Bottom Box Inner Height", defaultLengthUnits, defaultBottomInnerHeight);
Ptr<ValueInput> defaultLidInnerHeight = ValueInput::createByReal(3.0);
inputs->addValueInput("lid_inner_height", "Lid Box Inner Height", defaultLengthUnits, defaultLidInnerHeight);
Ptr<ValueInput> defaultFilletSize = ValueInput::createByReal(0.05);
inputs->addValueInput("fillet", "Fillet Size", defaultLengthUnits, defaultFilletSize);
pCommandExecuteHandler = new CommandExecutedHandler();
pValidateInputHandler = new ValidateInputHandler();
executeEvent->add(pCommandExecuteHandler);
validInputEvent->add(pValidateInputHandler);
}
private:
ValidateInputHandler* pValidateInputHandler;
CommandExecutedHandler* pCommandExecuteHandler;
Ptr<Command> pCommand;
};
extern "C" XI_EXPORT bool run(const char* context)
{
app = Application::get();
if (!app)
return false;
ui = app->userInterface();
if (!ui)
return false;
Ptr<Workspaces> workspaces = ui->workspaces();
Ptr<Workspace> modelingWorkspace = workspaces->itemById("FusionSolidEnvironment");
if (!modelingWorkspace)
return false;
Ptr<ToolbarTabs> toolbarTabs = modelingWorkspace->toolbarTabs();
if (!toolbarTabs)
return false;
Ptr<ToolbarTab> toolbarTab = toolbarTabs->itemById(toolbarId);
if (!toolbarTab)
{
toolbarTab = toolbarTabs->add(toolbarId, toolbarName);
pToolbarTab = toolbarTab;
}
Ptr<ToolbarPanels> toolbarPanels = modelingWorkspace->toolbarPanels();
if (!toolbarPanels)
return false;
Ptr<ToolbarPanel> toolbarPanel = toolbarPanels->itemById(myPanelId);
if (!toolbarPanel)
{
toolbarPanel = toolbarPanels->add(myPanelId, myPanelName, myPanelAfter, false);
pToolbarPanel = toolbarPanel;
}
pCommandDefininition = ui->commandDefinitions()->addButtonDefinition(cmdId, cmdName, cmdDescription, iconFolder);
Ptr<CommandCreatedEvent> cmdCreatedEvent = pCommandDefininition->commandCreated();
pCommandCreatedHandler = new CommandCreatedHandler();
cmdCreatedEvent->add(pCommandCreatedHandler);
pToolbarControl = toolbarPanel->controls()->addCommand(pCommandDefininition);
pToolbarControl->isPromoted(true);
return true;
}
extern "C" XI_EXPORT bool stop(const char* context)
{
if (ui)
{
//ui->messageBox("Stop addin");
ui = nullptr;
}
if (pToolbarControl != nullptr)
{
pToolbarControl->deleteMe();
}
if (pToolbarPanel != nullptr)
{
pToolbarPanel->deleteMe();
}
if (pToolbarTab != nullptr)
{
pToolbarTab->deleteMe();
}
if (pCommandCreatedHandler != nullptr)
{
Ptr<CommandCreatedEvent> cmdCreatedEvent = pCommandDefininition->commandCreated();
cmdCreatedEvent->remove(pCommandCreatedHandler);
delete pCommandCreatedHandler;
pCommandDefininition->deleteMe();
}
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