Skip to content

Commit 4579526

Browse files
committed
🦄 refactor: refactor box\ellipse\pyramid\sphere commands
1 parent 0f7c9d6 commit 4579526

File tree

29 files changed

+1084
-1375
lines changed

29 files changed

+1084
-1375
lines changed

cpp/src/factory.cpp

Lines changed: 113 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <BRepBuilderAPI_MakeEdge.hxx>
33
#include <BRepBuilderAPI_MakeFace.hxx>
44
#include <BRepBuilderAPI_MakePolygon.hxx>
5+
#include <BRepBuilderAPI_MakeSolid.hxx>
56
#include <BRepBuilderAPI_MakeVertex.hxx>
67
#include <BRepFilletAPI_MakeFillet.hxx>
78
#include <BRepFilletAPI_MakeChamfer.hxx>
@@ -28,6 +29,7 @@
2829
#include <BRepPrimAPI_MakeCone.hxx>
2930
#include <BRepPrimAPI_MakeSphere.hxx>
3031
#include <BRepBuilderAPI_GTransform.hxx>
32+
#include <TopoDS.hxx>
3133

3234
using namespace emscripten;
3335

@@ -43,8 +45,21 @@ class ShapeFactory
4345
public:
4446
static ShapeResult box(const Pln &ax3, double x, double y, double z)
4547
{
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, ""};
4863
}
4964

5065
static ShapeResult cone(const Vector3 &normal, const Vector3 &center, double radius, double radiusUp, double height)
@@ -60,10 +75,25 @@ class ShapeFactory
6075
return ShapeResult{sphere, true, ""};
6176
}
6277

63-
static ShapeResult ellipsoid(const Vector3 &normal, const Vector3 &center, const Vector3 &xVec, double xRadius, double yRadius, double zRadius)
78+
static ShapeResult ellipse(const Vector3 &normal, const Vector3 &center, const Vector3 &xvec, double majorRadius, double minorRadius)
6479
{
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+
}
6689

90+
/**
91+
* TODO
92+
*/
93+
static ShapeResult ellipsoid(const Vector3 &normal, const Vector3 &center, const Vector3 &xvec, double xRadius, double yRadius, double zRadius)
94+
{
95+
TopoDS_Shape sphere = BRepPrimAPI_MakeSphere(1).Solid();
96+
6797
gp_GTrsf transform;
6898
transform.SetValue(1, 1, xRadius);
6999
transform.SetValue(2, 2, yRadius);
@@ -79,66 +109,98 @@ class ShapeFactory
79109
return ShapeResult{TopoDS_Shape(), false, ""};
80110
}
81111

82-
static ShapeResult ellipse(const Vector3 &normal, const Vector3 &center, double majorRadius, double minorRadius)
112+
static ShapeResult pyramid(const Pln &ax3, double x, double y, double z)
83113
{
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)
88143
{
89-
return ShapeResult{TopoDS_Shape(), false, "Failed to create ellipse"};
144+
return wire;
90145
}
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, ""};
92153
}
93154

94-
static ShapeResult pyramid(const Vector3 &point, double x, double y, double z)
155+
static ShapeResult pointsToWire(std::vector<gp_Pnt> &points)
95156
{
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+
}
119168

169+
static ShapeResult facesToSolid(const std::vector<TopoDS_Face> &faces)
170+
{
120171
TopoDS_Shell shell;
121172
BRep_Builder shellBuilder;
122173
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+
}
128178

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+
}
133184

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);
135192
}
136193

137194
static ShapeResult cylinder(const Vector3 &normal, const Vector3 &center, double radius, double height)
138195
{
139196
gp_Ax2 ax2(Vector3::toPnt(center), Vector3::toDir(normal));
140197
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, ""};
142204
}
143205

144206
static ShapeResult sweep(const TopoDS_Shape &profile, const TopoDS_Wire &wire)
@@ -174,17 +236,13 @@ class ShapeFactory
174236

175237
static ShapeResult polygon(const Vector3Array &points)
176238
{
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)
180242
{
181-
poly.Add(Vector3::toPnt(p));
243+
pnts.push_back(Vector3::toPnt(p));
182244
}
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);
188246
}
189247

190248
static ShapeResult arc(const Vector3 &normal, const Vector3 &center, const Vector3 &start, double rad)

cpp/src/shared.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ struct Vector3 {
3939
double y;
4040
double z;
4141

42+
static gp_XYZ toXYZ(const Vector3& p) {
43+
return gp_XYZ(p.x, p.y, p.z);
44+
}
45+
4246
static gp_Pnt toPnt(const Vector3& p) {
4347
return gp_Pnt(p.x, p.y, p.z);
4448
}

packages/chili-builder/src/ribbon.ts

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,9 @@ export const DefaultRibbon: RibbonTab[] = [
1111
"create.arc",
1212
"create.rect",
1313
"create.circle",
14-
"create.ellipse",
15-
"create.bezier",
16-
"create.polygon",
17-
"create.box",
18-
"create.pyramid",
19-
"create.cylinder",
20-
"create.cone",
21-
"create.sphere",
22-
"create.ellipsoid",
23-
"create.thickSolid",
14+
["create.ellipse", "create.bezier", "create.polygon"],
15+
["create.box", "create.pyramid", "create.cylinder"],
16+
["create.cone", "create.sphere", "create.thickSolid"],
2417
],
2518
},
2619
{
@@ -29,13 +22,9 @@ export const DefaultRibbon: RibbonTab[] = [
2922
"modify.move",
3023
"modify.rotate",
3124
"modify.mirror",
32-
"create.offset",
3325
"modify.delete",
34-
"modify.break",
35-
"modify.trim",
36-
"modify.fillet",
37-
"modify.chamfer",
38-
"modify.removeFaces",
26+
["create.offset", "modify.break", "modify.trim"],
27+
["modify.fillet", "modify.chamfer", "modify.removeFaces"],
3928
],
4029
},
4130
{
@@ -70,12 +59,50 @@ export const DefaultRibbon: RibbonTab[] = [
7059
tabName: "ribbon.tab.draw",
7160
groups: [
7261
{
73-
groupName: "ribbon.group.draw",
74-
items: ["create.line", "create.rect", "create.circle", "create.box"],
62+
groupName: "ribbon.group.2d",
63+
items: [
64+
"create.line",
65+
"create.rect",
66+
"create.circle",
67+
"create.arc",
68+
"create.ellipse",
69+
"create.polygon",
70+
"create.bezier",
71+
],
7572
},
7673
{
77-
groupName: "ribbon.group.draw",
78-
items: ["test.performace", "create.rect", ["create.circle", "create.box"]],
74+
groupName: "ribbon.group.3d",
75+
items: [
76+
"create.box",
77+
"create.pyramid",
78+
"create.cylinder",
79+
"create.cone",
80+
"create.sphere",
81+
"create.thickSolid",
82+
],
83+
},
84+
],
85+
},
86+
{
87+
tabName: "ribbon.tab.tools",
88+
groups: [
89+
{
90+
groupName: "ribbon.group.modify",
91+
items: [
92+
"modify.break",
93+
"modify.trim",
94+
"modify.fillet",
95+
"modify.chamfer",
96+
"modify.removeFaces",
97+
],
98+
},
99+
{
100+
groupName: "ribbon.group.tools",
101+
items: ["create.section", "modify.split", "convert.toWire", "convert.toFace"],
102+
},
103+
{
104+
groupName: "ribbon.group.other",
105+
items: ["test.performace"],
79106
},
80107
],
81108
},

packages/chili-core/src/i18n/en.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export default {
1818
"body.cylinder": "Cylinder",
1919
"body.cone": "Cone",
2020
"body.sphere": "Sphere",
21-
"body.ellipsoid": "Ellipsoid",
2221
"body.ellipse": "Ellipse",
2322
"body.circle": "Circle",
2423
"body.face": "Face",
@@ -172,16 +171,20 @@ export default {
172171
"properties.multivalue": "Multi Value",
173172
"rect.dx": "Length",
174173
"rect.dy": "Width",
174+
"ribbon.group.2d": "2D",
175+
"ribbon.group.3d": "3D",
175176
"ribbon.group.boolean": "Boolean",
176177
"ribbon.group.converter": "Converter",
177178
"ribbon.group.draw": "Drawing",
178179
"ribbon.group.importExport": "Import/Export",
179180
"ribbon.group.modify": "Modify",
181+
"ribbon.group.other": "Other",
180182
"ribbon.group.selection": "Selection",
181183
"ribbon.group.tools": "Tools",
182184
"ribbon.group.workingPlane": "Working Plane",
183185
"ribbon.tab.draw": "Drawing",
184186
"ribbon.tab.file": "File",
187+
"ribbon.tab.tools": "Tools",
185188
"ribbon.tab.startup": "Startup",
186189
"snap.center": "Center",
187190
"snap.end": "End",
@@ -207,9 +210,9 @@ export default {
207210
"transform.scale": "Scale",
208211
"transform.translation": "Translation",
209212
"vertex.point": "Point",
210-
"workingPlane.dynamic": "Dynamic Plane",
211-
"workingPlane.alignToPlane": "Align to plane",
212-
"workingPlane.set": "Set workplane",
213+
"workingPlane.dynamic": "Dynamic",
214+
"workingPlane.alignToPlane": "Align",
215+
"workingPlane.set": "Set",
213216
"test.performace": "Performace test",
214217
},
215218
} satisfies Locale;

0 commit comments

Comments
 (0)