Skip to content

Commit 9c8a3f0

Browse files
committed
✨ feat: add FreeGeometry
1 parent fdd71d8 commit 9c8a3f0

File tree

12 files changed

+99
-107
lines changed

12 files changed

+99
-107
lines changed

packages/chili-core/src/model/geometryEntity.ts

Lines changed: 50 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.
22

33
import { IDocument } from "../document";
4-
import { IEqualityComparer, Result } from "../foundation";
4+
import { IEqualityComparer, PubSub, Result } from "../foundation";
5+
import { I18nKeys } from "../i18n";
56
import { Matrix4 } from "../math";
67
import { Property } from "../property";
78
import { Serializer } from "../serialize";
@@ -19,119 +20,83 @@ export abstract class GeometryEntity extends Entity {
1920
this.setProperty("materialId", value);
2021
}
2122

22-
protected shouldRegenerate: boolean = true;
23-
24-
protected _shape: Result<IShape> = Result.err("Not initialised");
23+
protected _shape: Result<IShape> = Result.err("undefined");
2524
get shape(): Result<IShape> {
26-
if (this.shouldRegenerate) {
27-
this._shape = this.generateShape();
28-
if (this._shape.isOk) {
29-
this._shape.value.matrix = this._matrix;
30-
}
31-
this.shouldRegenerate = false;
32-
}
3325
return this._shape;
3426
}
3527

36-
protected setPropertyAndUpdate<K extends keyof this>(
37-
property: K,
38-
newValue: this[K],
39-
onPropertyChanged?: (property: K, oldValue: this[K]) => void,
40-
equals?: IEqualityComparer<this[K]>,
41-
) {
42-
if (this.setProperty(property, newValue, onPropertyChanged, equals)) {
43-
this.shouldRegenerate = true;
44-
this.emitShapeChanged();
45-
}
46-
}
47-
4828
constructor(document: IDocument, materialId?: string) {
4929
super(document);
5030
this._materialId = materialId ?? document.materials.at(0)!.id;
5131
}
5232

53-
protected emitShapeChanged() {
54-
this.emitPropertyChanged("shape", this._shape);
33+
override onMatrixChanged(newMatrix: Matrix4, oldMatrix: Matrix4): void {
34+
if (this._shape.isOk) this._shape.value.matrix = newMatrix;
5535
}
5636

57-
override onMatrixChanged(newMatrix: Matrix4, oldMatrix: Matrix4): void {
58-
if (this.shape.isOk) {
59-
this.shape.value.matrix = newMatrix;
37+
protected setShape(shape: Result<IShape>, notify: boolean): boolean {
38+
if (shape.isOk && this._shape.isOk && this._shape.value.isEqual(shape.value)) {
39+
return false;
6040
}
61-
}
6241

63-
protected abstract generateShape(): Result<IShape>;
42+
let oldShape = this._shape;
43+
this._shape = shape;
44+
if (this._shape.isOk) {
45+
this._shape.value.matrix = this._matrix;
46+
}
47+
if (notify) this.emitPropertyChanged("shape", oldShape);
48+
return true;
49+
}
6450
}
6551

66-
/*
67-
export abstract class HistoryBody extends Body {
68-
private readonly _features: Feature[] = [];
69-
70-
private onShapeChanged = (entity: Entity) => {
71-
if (entity === this) {
72-
this.drawShape();
73-
} else {
74-
let editor = entity as Feature;
75-
let i = this._features.indexOf(editor);
76-
this.applyFeatures(i);
77-
}
78-
this.redrawModel();
79-
};
52+
@Serializer.register("FreeGeometryEntity", ["document", "shape", "materialId"])
53+
export class FreeGeometry extends GeometryEntity {
54+
override display: I18nKeys = "common.angle";
8055

81-
drawShape() {
82-
this.applyFeatures(0);
56+
@Serializer.serialze()
57+
override get shape(): Result<IShape> {
58+
return this._shape;
8359
}
84-
85-
private applyFeatures(startIndex: number) {
86-
if (startIndex < 0) return;
87-
for (let i = startIndex; i < this._features.length; i++) {
88-
this._shape = this._features[i].shape;
89-
if (!this._shape.isOk) {
90-
return;
91-
}
92-
}
93-
if (this._shape) {
94-
this._shape.matrix = this._matrix;
95-
}
60+
override set shape(value: Result<IShape>) {
61+
this.setShape(value, true);
9662
}
9763

98-
removeFeature(feature: Feature) {
99-
const index = this._features.indexOf(feature, 0);
100-
if (index > -1) {
101-
this._features.splice(index, 1);
102-
feature.removeShapeChanged(this.onShapeChanged);
103-
this._features[index].origin =
104-
index === 0 ? this.body.shape.unwrap() : this._features[index - 1].shape.unwrap(); // todo
105-
this.applyFeatures(index);
106-
this.redrawModel();
107-
}
64+
constructor(document: IDocument, shape: IShape, materialId?: string) {
65+
super(document, materialId);
66+
this._shape = Result.ok(shape);
10867
}
68+
}
10969

110-
addFeature(feature: Feature) {
111-
if (this._features.indexOf(feature) > -1) return;
112-
this._features.push(feature);
113-
feature.onShapeChanged(this.onShapeChanged);
114-
if (this._shape !== undefined) {
115-
feature.origin = this._shape;
116-
this.applyFeatures(this._features.length - 1);
117-
this.redrawModel();
70+
export abstract class ParameterGeometry extends GeometryEntity {
71+
protected shouldRegenerateShape: boolean = true;
72+
override get shape(): Result<IShape> {
73+
if (this.shouldRegenerateShape) {
74+
this.setShape(this.generateShape(), false);
75+
this.shouldRegenerateShape = false;
11876
}
77+
return this._shape;
11978
}
12079

121-
getFeature(index: number) {
122-
if (index < this._features.length) {
123-
return this._features[index];
80+
protected setPropertyAndUpdate<K extends keyof this>(
81+
property: K,
82+
newValue: this[K],
83+
onPropertyChanged?: (property: K, oldValue: this[K]) => void,
84+
equals?: IEqualityComparer<this[K]>,
85+
) {
86+
if (this.setProperty(property, newValue, onPropertyChanged, equals)) {
87+
let shape = this.generateShape();
88+
if (!shape.isOk) {
89+
PubSub.default.pub("showToast", "error.default");
90+
return;
91+
}
92+
this.setShape(shape, true);
12493
}
125-
return undefined;
12694
}
12795

128-
features() {
129-
return [...this._features];
130-
}
96+
protected abstract generateShape(): Result<IShape>;
13197
}
132-
*/
13398

134-
export abstract class FaceableGeometry extends GeometryEntity {
99+
export abstract class FaceableGeometry extends ParameterGeometry {
135100
protected _isFace: boolean = false;
136101
@Serializer.serialze()
137102
@Property.define("command.faceable.isFace")

packages/chili/src/bodys/arcBody.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.
22

3-
import { GeometryEntity, I18nKeys, IDocument, IShape, Property, Result, Serializer, XYZ } from "chili-core";
3+
import {
4+
I18nKeys,
5+
IDocument,
6+
IShape,
7+
ParameterGeometry,
8+
Property,
9+
Result,
10+
Serializer,
11+
XYZ,
12+
} from "chili-core";
413

514
@Serializer.register("ArcBody", ["document", "normal", "center", "start", "angle"])
6-
export class ArcBody extends GeometryEntity {
15+
export class ArcBody extends ParameterGeometry {
716
readonly display: I18nKeys = "body.arc";
817

918
private _center: XYZ;

packages/chili/src/bodys/boolean.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.
22

3-
import { GeometryEntity, I18nKeys, IDocument, IShape, Result, Serializer } from "chili-core";
3+
import { I18nKeys, IDocument, IShape, ParameterGeometry, Result, Serializer } from "chili-core";
44

55
@Serializer.register("BooleanBody", ["document", "booleanShape"])
6-
export class BooleanBody extends GeometryEntity {
6+
export class BooleanBody extends ParameterGeometry {
77
override display: I18nKeys = "body.bolean";
88

99
private _booleanShape: IShape;

packages/chili/src/bodys/box.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.
22

33
import {
4-
GeometryEntity,
54
I18nKeys,
65
IDocument,
76
IShape,
7+
ParameterGeometry,
88
Plane,
99
Property,
1010
Result,
1111
Serializer,
1212
} from "chili-core";
1313

1414
@Serializer.register("BoxBody", ["document", "plane", "dx", "dy", "dz"])
15-
export class BoxBody extends GeometryEntity {
15+
export class BoxBody extends ParameterGeometry {
1616
readonly display: I18nKeys = "body.box";
1717

1818
private _dx: number;

packages/chili/src/bodys/face.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.
22

3-
import { GeometryEntity, I18nKeys, IDocument, IEdge, IShape, IWire, Result, Serializer } from "chili-core";
3+
import {
4+
I18nKeys,
5+
IDocument,
6+
IEdge,
7+
IShape,
8+
IWire,
9+
ParameterGeometry,
10+
Result,
11+
Serializer,
12+
} from "chili-core";
413

514
@Serializer.register("FaceBody", ["document", "shapes"])
6-
export class FaceBody extends GeometryEntity {
15+
export class FaceBody extends ParameterGeometry {
716
override display: I18nKeys = "body.face";
817

918
private _shapes: IEdge[] | IWire;

packages/chili/src/bodys/fuse.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.
22

3-
import { GeometryEntity, I18nKeys, IDocument, IShape, Result, Serializer } from "chili-core";
3+
import { I18nKeys, IDocument, IShape, ParameterGeometry, Result, Serializer } from "chili-core";
44

55
@Serializer.register("FuseBody", ["document", "bottom", "top"])
6-
export class FuseBody extends GeometryEntity {
6+
export class FuseBody extends ParameterGeometry {
77
override display: I18nKeys = "body.fuse";
88

99
private _bottom: IShape;

packages/chili/src/bodys/importer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.
22

3-
import { GeometryEntity, I18nKeys, IDocument, IShape, Result, Serializer } from "chili-core";
3+
import { I18nKeys, IDocument, IShape, ParameterGeometry, Result, Serializer } from "chili-core";
44

55
@Serializer.register("ImportedBody", ["document", "importedShape"])
6-
export class ImportedBody extends GeometryEntity {
6+
export class ImportedBody extends ParameterGeometry {
77
override display: I18nKeys = "body.imported";
88

99
private _importedShape: IShape;

packages/chili/src/bodys/line.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.
22

3-
import { GeometryEntity, I18nKeys, IDocument, IShape, Property, Result, Serializer, XYZ } from "chili-core";
3+
import {
4+
I18nKeys,
5+
IDocument,
6+
IShape,
7+
ParameterGeometry,
8+
Property,
9+
Result,
10+
Serializer,
11+
XYZ,
12+
} from "chili-core";
413

514
@Serializer.register("LineBody", ["document", "start", "end"])
6-
export class LineBody extends GeometryEntity {
15+
export class LineBody extends ParameterGeometry {
716
readonly display: I18nKeys = "body.line";
817

918
private _start: XYZ;

packages/chili/src/bodys/prism.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.
22

33
import {
4-
GeometryEntity,
54
I18nKeys,
65
IDocument,
76
IFace,
87
IShape,
8+
ParameterGeometry,
99
Property,
1010
Result,
1111
Serializer,
1212
} from "chili-core";
1313

1414
@Serializer.register("PrismBody", ["document", "face", "length"])
15-
export class PrismBody extends GeometryEntity {
15+
export class PrismBody extends ParameterGeometry {
1616
override display: I18nKeys = "body.prism";
1717

1818
private _face: IFace;

packages/chili/src/bodys/revolve.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.
22

3-
import { GeometryEntity, I18nKeys, IDocument, IShape, Ray, Result, Serializer } from "chili-core";
3+
import { I18nKeys, IDocument, IShape, ParameterGeometry, Ray, Result, Serializer } from "chili-core";
44

55
@Serializer.register("RevolveBody", ["document", "profile", "axis", "angle"])
6-
export class RevolveBody extends GeometryEntity {
6+
export class RevolveBody extends ParameterGeometry {
77
override display: I18nKeys = "body.revol";
88

99
private _profile: IShape;

0 commit comments

Comments
 (0)