|
| 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 | +} |
0 commit comments