-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicScriptCPP.cpp
More file actions
220 lines (179 loc) · 8.89 KB
/
BasicScriptCPP.cpp
File metadata and controls
220 lines (179 loc) · 8.89 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
#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;
double epsilon = 1e-6;
typedef struct
{
double borderThickness;
double bottomThickness;
double innerWidth;
double innerLength;
double innerHeight;
double filletSize;
} BoxParameters;
Ptr<Component> createBox(Ptr<Design> design, const BoxParameters ¶meters, double margin, const std::string&
bodyName, const std::string& componentName)
{
const std::string inputUnits("mm");
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, std::string("mm"));
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);
// Create a rectangle sketch
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, API is using cm. 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);
// Let's dig a hole in the new created box.
// We need two points to find a face. We want to find top and bottom face as beginning and end here.
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);
// Let's get the new body
Ptr<ExtrudeFeature> baseExtrudeItem = newExtrudeFeature;
Ptr<BRepBody> baseExtrudeBody = baseExtrudeItem->bodies()->item(0);
// Find the faces.
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;
}
extern "C" XI_EXPORT bool run(const char* context)
{
app = Application::get();
if (!app)
return false;
ui = app->userInterface();
if (!ui)
return false;
BoxParameters bottomBoxParameters;
bottomBoxParameters.borderThickness = 4.0;
bottomBoxParameters.bottomThickness = 4.0;
bottomBoxParameters.innerWidth = 190.0;
bottomBoxParameters.innerLength = 390.0;
bottomBoxParameters.innerHeight = 110.0;
bottomBoxParameters.filletSize = 0.5;
BoxParameters topBoxParameters;
topBoxParameters.borderThickness = 4.0;
topBoxParameters.bottomThickness = 4.0;
topBoxParameters.innerWidth = 198.0;
topBoxParameters.innerLength = 398.0;
topBoxParameters.innerHeight = 30.0;
topBoxParameters.filletSize = 0.5;
const double baseMargin = 50.0;
Ptr<Design> design = app->activeProduct();
if (!design)
{
ui->messageBox("The DESIGN workspace must be active when running this command.");
return false;
}
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");
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