2
2
#include < BRepBuilderAPI_MakeEdge.hxx>
3
3
#include < BRepBuilderAPI_MakeFace.hxx>
4
4
#include < BRepBuilderAPI_MakePolygon.hxx>
5
+ #include < BRepBuilderAPI_MakeSolid.hxx>
5
6
#include < BRepBuilderAPI_MakeVertex.hxx>
6
7
#include < BRepFilletAPI_MakeFillet.hxx>
7
8
#include < BRepFilletAPI_MakeChamfer.hxx>
28
29
#include < BRepPrimAPI_MakeCone.hxx>
29
30
#include < BRepPrimAPI_MakeSphere.hxx>
30
31
#include < BRepBuilderAPI_GTransform.hxx>
32
+ #include < TopoDS.hxx>
31
33
32
34
using namespace emscripten ;
33
35
@@ -43,8 +45,21 @@ class ShapeFactory
43
45
public:
44
46
static ShapeResult box (const Pln &ax3, double x, double y, double z)
45
47
{
46
- TopoDS_Shape box = BRepPrimAPI_MakeBox (Vector3::toPnt (ax3.location ), x, y, z).Shape ();
47
- return ShapeResult{box, true , " " };
48
+ gp_Pln pln = Pln::toPln (ax3);
49
+ BRepBuilderAPI_MakeFace makeFace (pln, 0 , x, 0 , y);
50
+ if (!makeFace.IsDone ())
51
+ {
52
+ return ShapeResult{TopoDS_Shape (), false , " Failed to create box" };
53
+ }
54
+
55
+ gp_Vec vec (pln.Axis ().Direction ());
56
+ vec.Multiply (z);
57
+ BRepPrimAPI_MakePrism box (makeFace.Face (), vec);
58
+ if (!box.IsDone ())
59
+ {
60
+ return ShapeResult{TopoDS_Shape (), false , " Failed to create box" };
61
+ }
62
+ return ShapeResult{box.Shape (), true , " " };
48
63
}
49
64
50
65
static ShapeResult cone (const Vector3 &normal, const Vector3 ¢er, double radius, double radiusUp, double height)
@@ -60,10 +75,25 @@ class ShapeFactory
60
75
return ShapeResult{sphere, true , " " };
61
76
}
62
77
63
- static ShapeResult ellipsoid (const Vector3 &normal, const Vector3 ¢er, const Vector3 &xVec , double xRadius , double yRadius, double zRadius )
78
+ static ShapeResult ellipse (const Vector3 &normal, const Vector3 ¢er, const Vector3 &xvec , double majorRadius , double minorRadius )
64
79
{
65
- TopoDS_Shape sphere = BRepPrimAPI_MakeSphere (1 ).Shape ();
80
+ gp_Ax2 ax2 (Vector3::toPnt (center), Vector3::toDir (normal), Vector3::toDir (xvec));
81
+ gp_Elips ellipse (ax2, majorRadius, minorRadius);
82
+ BRepBuilderAPI_MakeEdge edge (ellipse);
83
+ if (!edge.IsDone ())
84
+ {
85
+ return ShapeResult{TopoDS_Shape (), false , " Failed to create ellipse" };
86
+ }
87
+ return ShapeResult{edge.Edge (), true , " " };
88
+ }
66
89
90
+ /* *
91
+ * TODO
92
+ */
93
+ static ShapeResult ellipsoid (const Vector3 &normal, const Vector3 ¢er, const Vector3 &xvec, double xRadius, double yRadius, double zRadius)
94
+ {
95
+ TopoDS_Shape sphere = BRepPrimAPI_MakeSphere (1 ).Solid ();
96
+
67
97
gp_GTrsf transform;
68
98
transform.SetValue (1 , 1 , xRadius);
69
99
transform.SetValue (2 , 2 , yRadius);
@@ -79,66 +109,98 @@ class ShapeFactory
79
109
return ShapeResult{TopoDS_Shape (), false , " " };
80
110
}
81
111
82
- static ShapeResult ellipse (const Vector3 &normal, const Vector3 ¢er , double majorRadius , double minorRadius )
112
+ static ShapeResult pyramid (const Pln &ax3, double x , double y , double z )
83
113
{
84
- gp_Ax2 ax2 (Vector3::toPnt (center), Vector3::toDir (normal));
85
- gp_Elips ellipse (ax2, majorRadius, minorRadius);
86
- BRepBuilderAPI_MakeEdge edge (ellipse);
87
- if (!edge.IsDone ())
114
+ if (abs (x) <= Precision::Confusion () || abs (y) <= Precision::Confusion () || abs (z) <= Precision::Confusion ()) {
115
+ return ShapeResult{TopoDS_Shape (), false , " Invalid dimensions" };
116
+ }
117
+
118
+ gp_Pln pln = Pln::toPln (ax3);
119
+ auto xvec = gp_Vec (pln.XAxis ().Direction ()).Multiplied (x);
120
+ auto yvec = gp_Vec (pln.YAxis ().Direction ()).Multiplied (y);
121
+ auto zvec = gp_Vec (pln.Axis ().Direction ()).Multiplied (z);
122
+ auto p1 = pln.Location ();
123
+ auto p2 = p1.Translated (xvec);
124
+ auto p3 = p1.Translated (xvec).Translated (yvec);
125
+ auto p4 = p1.Translated (yvec);
126
+ auto top = pln.Location ().Translated ((xvec + yvec) * 0.5 + zvec);
127
+
128
+ std::vector<TopoDS_Face> faces = {
129
+ TopoDS::Face (pointsToFace ({ p1, p2, p3, p4, p1 }).shape ),
130
+ TopoDS::Face (pointsToFace ({ p1, p2, top, p1 }).shape ),
131
+ TopoDS::Face (pointsToFace ({ p2, p3, top, p2 }).shape ),
132
+ TopoDS::Face (pointsToFace ({ p3, p4, top, p3 }).shape ),
133
+ TopoDS::Face (pointsToFace ({ p4, p1, top, p4 }).shape )
134
+ };
135
+
136
+ return facesToSolid (faces);
137
+ }
138
+
139
+ static ShapeResult pointsToFace (std::vector<gp_Pnt> &&points)
140
+ {
141
+ auto wire = pointsToWire (points);
142
+ if (!wire.isOk )
88
143
{
89
- return ShapeResult{ TopoDS_Shape (), false , " Failed to create ellipse " } ;
144
+ return wire ;
90
145
}
91
- return ShapeResult{edge.Edge (), true , " " };
146
+
147
+ BRepBuilderAPI_MakeFace face (TopoDS::Wire (wire.shape ));
148
+ if (!face.IsDone ())
149
+ {
150
+ return ShapeResult{TopoDS_Shape (), false , " Failed to create face" };
151
+ }
152
+ return ShapeResult{face.Face (), true , " " };
92
153
}
93
154
94
- static ShapeResult pyramid ( const Vector3 &point, double x, double y, double z)
155
+ static ShapeResult pointsToWire (std::vector<gp_Pnt> &points)
95
156
{
96
- Standard_Real baseSizeX = x;
97
- Standard_Real baseSizeY = y;
98
- Standard_Real height = z;
99
- gp_Pnt baseCenter = gp_Pnt (point.x , point.y , point.z );
100
-
101
- gp_Pnt p1 (baseCenter.X (), baseCenter.Y (), baseCenter.Z ());
102
- gp_Pnt p2 (baseCenter.X () + baseSizeX, baseCenter.Y (), baseCenter.Z ());
103
- gp_Pnt p3 (baseCenter.X () + baseSizeX, baseCenter.Y () + baseSizeY, baseCenter.Z ());
104
- gp_Pnt p4 (baseCenter.X (), baseCenter.Y () + baseSizeY, baseCenter.Z ());
105
- gp_Pnt apex (baseCenter.X () + baseSizeX / 2 , baseCenter.Y () + baseSizeY / 2 , baseCenter.Z () + height);
106
-
107
- TopoDS_Wire baseWire = BRepBuilderAPI_MakeWire (
108
- BRepBuilderAPI_MakeEdge (p1, p2),
109
- BRepBuilderAPI_MakeEdge (p2, p3),
110
- BRepBuilderAPI_MakeEdge (p3, p4),
111
- BRepBuilderAPI_MakeEdge (p4, p1))
112
- .Wire ();
113
- TopoDS_Face baseFace = BRepBuilderAPI_MakeFace (baseWire).Face ();
114
-
115
- TopoDS_Face face1 = BRepBuilderAPI_MakeFace (BRepBuilderAPI_MakeWire (BRepBuilderAPI_MakeEdge (p1, p2), BRepBuilderAPI_MakeEdge (p2, apex), BRepBuilderAPI_MakeEdge (apex, p1))).Face ();
116
- TopoDS_Face face2 = BRepBuilderAPI_MakeFace (BRepBuilderAPI_MakeWire (BRepBuilderAPI_MakeEdge (p2, p3), BRepBuilderAPI_MakeEdge (p3, apex), BRepBuilderAPI_MakeEdge (apex, p2))).Face ();
117
- TopoDS_Face face3 = BRepBuilderAPI_MakeFace (BRepBuilderAPI_MakeWire (BRepBuilderAPI_MakeEdge (p3, p4), BRepBuilderAPI_MakeEdge (p4, apex), BRepBuilderAPI_MakeEdge (apex, p3))).Face ();
118
- TopoDS_Face face4 = BRepBuilderAPI_MakeFace (BRepBuilderAPI_MakeWire (BRepBuilderAPI_MakeEdge (p4, p1), BRepBuilderAPI_MakeEdge (p1, apex), BRepBuilderAPI_MakeEdge (apex, p4))).Face ();
157
+ BRepBuilderAPI_MakePolygon poly;
158
+ for (auto &p : points)
159
+ {
160
+ poly.Add (p);
161
+ }
162
+ if (!poly.IsDone ())
163
+ {
164
+ return ShapeResult{TopoDS_Shape (), false , " Failed to create polygon" };
165
+ }
166
+ return ShapeResult{poly.Wire (), true , " " };
167
+ }
119
168
169
+ static ShapeResult facesToSolid (const std::vector<TopoDS_Face> &faces)
170
+ {
120
171
TopoDS_Shell shell;
121
172
BRep_Builder shellBuilder;
122
173
shellBuilder.MakeShell (shell);
123
- shellBuilder.Add (shell, baseFace);
124
- shellBuilder.Add (shell, face1);
125
- shellBuilder.Add (shell, face2);
126
- shellBuilder.Add (shell, face3);
127
- shellBuilder.Add (shell, face4);
174
+ for (const auto &face : faces)
175
+ {
176
+ shellBuilder.Add (shell, face);
177
+ }
128
178
129
- TopoDS_Solid pyramid;
130
- BRep_Builder solidBuilder;
131
- solidBuilder.MakeSolid (pyramid);
132
- solidBuilder.Add (pyramid, shell);
179
+ BRepBuilderAPI_MakeSolid solidBuilder (shell);
180
+ if (!solidBuilder.IsDone ())
181
+ {
182
+ return ShapeResult{TopoDS_Shape (), false , " Failed to create solid" };
183
+ }
133
184
134
- return ShapeResult{pyramid, true , " " };
185
+ return ShapeResult{solidBuilder.Solid (), true , " " };
186
+ }
187
+
188
+ static ShapeResult facesToSolid (const FaceArray &faces)
189
+ {
190
+ auto facesVec = vecFromJSArray<TopoDS_Face>(faces);
191
+ return facesToSolid (facesVec);
135
192
}
136
193
137
194
static ShapeResult cylinder (const Vector3 &normal, const Vector3 ¢er, double radius, double height)
138
195
{
139
196
gp_Ax2 ax2 (Vector3::toPnt (center), Vector3::toDir (normal));
140
197
BRepPrimAPI_MakeCylinder cylinder (ax2, radius, height);
141
- return ShapeResult{cylinder.Shape (), true , " " };
198
+ cylinder.Build ();
199
+ if (!cylinder.IsDone ())
200
+ {
201
+ return ShapeResult{TopoDS_Shape (), false , " Failed to create cylinder" };
202
+ }
203
+ return ShapeResult{cylinder.Solid (), true , " " };
142
204
}
143
205
144
206
static ShapeResult sweep (const TopoDS_Shape &profile, const TopoDS_Wire &wire)
@@ -174,17 +236,13 @@ class ShapeFactory
174
236
175
237
static ShapeResult polygon (const Vector3Array &points)
176
238
{
177
- std::vector<Vector3> pts = vecFromJSArray<Vector3>(points);
178
- BRepBuilderAPI_MakePolygon poly ;
179
- for (auto &p : pts )
239
+ std::vector<Vector3> vector3s = vecFromJSArray<Vector3>(points);
240
+ std::vector<gp_Pnt> pnts ;
241
+ for (auto &p : vector3s )
180
242
{
181
- poly. Add (Vector3::toPnt (p));
243
+ pnts. push_back (Vector3::toPnt (p));
182
244
}
183
- if (!poly.IsDone ())
184
- {
185
- return ShapeResult{TopoDS_Shape (), false , " Failed to create polygon" };
186
- }
187
- return ShapeResult{poly.Wire (), true , " " };
245
+ return pointsToWire (pnts);
188
246
}
189
247
190
248
static ShapeResult arc (const Vector3 &normal, const Vector3 ¢er, const Vector3 &start, double rad)
0 commit comments