Skip to content

Commit 06eb13e

Browse files
committed
✨ feat: add simplifyShape command
1 parent 98a46e2 commit 06eb13e

File tree

9 files changed

+108
-5
lines changed

9 files changed

+108
-5
lines changed

packages/app/src/commands/modify/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ export * from "./removeFeature";
1313
export * from "./removeSubShapes";
1414
export * from "./rotate";
1515
export * from "./sew";
16+
export * from "./simplify";
1617
export * from "./split";
1718
export * from "./trim";
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Part of the Chili3d Project, under the AGPL-3.0 License.
2+
// See LICENSE file in the project root for full license information.
3+
4+
import {
5+
command,
6+
EditableShapeNode,
7+
property,
8+
SelectShapeStep,
9+
type ShapeNode,
10+
ShapeType,
11+
Transaction,
12+
VisualState,
13+
} from "@chili3d/core";
14+
import { MultistepCommand } from "../multistepCommand";
15+
16+
@command({
17+
key: "modify.simplifyShape",
18+
icon: "icon-simplify",
19+
})
20+
export class SimplifyShapeCommand extends MultistepCommand {
21+
@property("common.removeEdges")
22+
get removeEdges() {
23+
return this.getPrivateValue("removeEdges", true);
24+
}
25+
26+
set removeEdges(value: boolean) {
27+
this.setProperty("removeEdges", value);
28+
}
29+
30+
@property("common.removeFaces")
31+
get removeFaces() {
32+
return this.getPrivateValue("removeFaces", true);
33+
}
34+
35+
set removeFaces(value: boolean) {
36+
this.setProperty("removeFaces", value);
37+
}
38+
39+
protected override executeMainTask() {
40+
Transaction.execute(this.document, `execute ${Object.getPrototypeOf(this).data.name}`, () => {
41+
const node = this.stepDatas[0].shapes[0].owner.node as ShapeNode;
42+
const shape = this.stepDatas[0].shapes[0].shape;
43+
44+
const simplifiedShape = this.document.application.shapeFactory.simplifyShape(
45+
shape,
46+
this.removeEdges,
47+
this.removeFaces,
48+
);
49+
50+
if (!simplifiedShape.isOk) {
51+
throw simplifiedShape.error;
52+
}
53+
54+
const model = new EditableShapeNode(
55+
this.document,
56+
node.name + "_simplified",
57+
simplifiedShape.value,
58+
node.materialId,
59+
);
60+
model.transform = node.transform;
61+
(node.parent ?? this.document.modelManager.rootNode).add(model);
62+
node.parent?.remove(node);
63+
this.document.visual.update();
64+
});
65+
}
66+
67+
protected override getSteps() {
68+
return [
69+
new SelectShapeStep(ShapeType.Shape, "prompt.select.shape", {
70+
shapeFilter: {
71+
allow: (shape) => {
72+
return (
73+
shape.shapeType === ShapeType.Solid ||
74+
shape.shapeType === ShapeType.Compound ||
75+
shape.shapeType === ShapeType.CompoundSolid ||
76+
shape.shapeType === ShapeType.Shell ||
77+
shape.shapeType === ShapeType.Face
78+
);
79+
},
80+
},
81+
selectedState: VisualState.faceTransparent,
82+
}),
83+
];
84+
}
85+
}

packages/builder/src/ribbon.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ export const DefaultRibbon: RibbonTabProfile[] = [
2323
{
2424
groupName: "ribbon.group.modify",
2525
items: [
26-
"modify.move",
27-
"modify.rotate",
28-
["modify.mirror", "modify.array", "modify.trim"],
29-
["modify.split", "modify.break", "modify.sew"],
26+
["modify.move", "modify.rotate", "modify.mirror"],
27+
["modify.array", "modify.trim", "modify.sew"],
28+
["modify.split", "modify.break", "modify.simplifyShape"],
3029
["modify.fillet", "modify.chamfer", "modify.explode"],
3130
["modify.deleteNode", "modify.removeShapes", "modify.removeFeature"],
3231
["modify.brushAdd", "modify.brushRemove", "modify.brushClear"],

packages/core/src/i18n/keys.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export const I18N_KEYS = [
9898
"command.modify.removeShapes",
9999
"command.modify.rotate",
100100
"command.modify.sew",
101+
"command.modify.simplifyShape",
101102
"command.modify.split",
102103
"command.modify.trim",
103104
"command.special.last",
@@ -129,7 +130,10 @@ export const I18N_KEYS = [
129130
"common.numberX",
130131
"common.numberY",
131132
"common.numberZ",
133+
"common.on",
132134
"common.opacity",
135+
"common.removeEdges",
136+
"common.removeFaces",
133137
"common.theme.dark",
134138
"common.theme.light",
135139
"common.theme.system",

packages/core/src/shape/shapeFactory.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,5 @@ export interface IShapeFactory {
5757
removeSubShape(shape: IShape, subShapes: IShape[]): IShape;
5858
replaceSubShape(shape: IShape, subShape: IShape, newSubShape: IShape): IShape;
5959
curveProjection(curve: IEdge | IWire, targetFace: IFace, vec: XYZ): Result<IShape>;
60+
simplifyShape(shape: IShape, removeEdges: boolean, removeFaces: boolean): Result<IShape>;
6061
}

packages/i18n/src/en.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export default {
103103
"command.modify.removeShapes": "Remove Shapes",
104104
"command.modify.rotate": "Rotate",
105105
"command.modify.sew": "Sew",
106+
"command.modify.simplifyShape": "Simplify Shape",
106107
"command.modify.split": "Split",
107108
"command.modify.trim": "Trim",
108109
"command.special.last": "__Last_COMMAND__",
@@ -134,7 +135,10 @@ export default {
134135
"common.numberX": "Number X",
135136
"common.numberY": "Number Y",
136137
"common.numberZ": "Number Z",
138+
"common.on": "On",
137139
"common.opacity": "Opacity",
140+
"common.removeEdges": "Remove Edges",
141+
"common.removeFaces": "Remove Faces",
138142
"common.theme.dark": "Dark",
139143
"common.theme.light": "Light",
140144
"common.theme.system": "System",

packages/i18n/src/zh-cn.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export default {
103103
"command.modify.removeShapes": "删除子元素",
104104
"command.modify.rotate": "旋转",
105105
"command.modify.sew": "缝合",
106+
"command.modify.simplifyShape": "形状简化",
106107
"command.modify.split": "分割",
107108
"command.modify.trim": "修剪",
108109
"command.special.last": "__上个命令__",
@@ -134,7 +135,10 @@ export default {
134135
"common.numberX": "X 数量",
135136
"common.numberY": "Y 数量",
136137
"common.numberZ": "Z 数量",
138+
"common.on": "开启",
137139
"common.opacity": "不透明度",
140+
"common.removeEdges": "移除边",
141+
"common.removeFaces": "移除面",
138142
"common.theme.dark": "深色",
139143
"common.theme.light": "浅色",
140144
"common.theme.system": "系统",

packages/wasm/src/factory.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,4 +345,9 @@ export class ShapeFactory implements IShapeFactory {
345345
),
346346
);
347347
}
348+
simplifyShape(shape: IShape, removeEdges: boolean, removeFaces: boolean): Result<IShape> {
349+
return convertShapeResult(
350+
wasm.ShapeFactory.simplifyShape(ensureOccShape(shape)[0], removeEdges, removeFaces),
351+
);
352+
}
348353
}

public/iconfont.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)