Skip to content

Commit

Permalink
✨ feat: update prism
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangechen committed May 20, 2024
1 parent 9101e7d commit bdf1d2c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
8 changes: 6 additions & 2 deletions packages/chili-geo/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { ICurve, IEdge, IWire, Precision, Result, ShapeType, XYZ } from "chili-core";
import { ICurve, IEdge, IFace, IWire, Precision, Result, ShapeType, XYZ } from "chili-core";

export class GeoUtils {
static nearestPoint(wire: IWire, point: XYZ): { edge: IEdge; point: XYZ } {
Expand Down Expand Up @@ -56,7 +56,11 @@ export class GeoUtils {
return Result.err("Cannot find next edge");
}

static normal(shape: IWire | IEdge): XYZ {
static normal(shape: IFace | IWire | IEdge): XYZ {
if (shape.shapeType === ShapeType.Face) {
return (shape as IFace).normal(0, 0)[1];
}

if (shape.shapeType === ShapeType.Edge) {
let curve = (shape as IEdge).asCurve().unwrap();
return this.curveNormal(curve);
Expand Down
21 changes: 11 additions & 10 deletions packages/chili/src/bodys/prism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ import {
Result,
Serializer,
} from "chili-core";
import { GeoUtils } from "chili-geo";

@Serializer.register("PrismBody", ["document", "face", "length"])
@Serializer.register("PrismBody", ["document", "section", "length"])
export class PrismBody extends ParameterGeometry {
override display: I18nKeys = "body.prism";

private _face: IFace;
private _section: IShape;
@Serializer.serialze()
get face(): IFace {
return this._face;
get section(): IShape {
return this._section;
}
set face(value: IFace) {
this.setPropertyAndUpdate("face", value);
set section(value: IFace) {
this.setPropertyAndUpdate("section", value);
}

private _length: number;
Expand All @@ -34,15 +35,15 @@ export class PrismBody extends ParameterGeometry {
this.setPropertyAndUpdate("length", value);
}

constructor(document: IDocument, face: IFace, length: number) {
constructor(document: IDocument, face: IShape, length: number) {
super(document);
this._face = face;
this._section = face;
this._length = length;
}

protected override generateShape(): Result<IShape> {
let [_, normal] = this.face.normal(0, 0);
let normal = GeoUtils.normal(this.section as any);
let vec = normal.multiply(this.length);
return this.document.application.shapeFactory.prism(this.face, vec);
return this.document.application.shapeFactory.prism(this.section, vec);
}
}
24 changes: 18 additions & 6 deletions packages/chili/src/commands/create/prism.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { GeometryModel, IFace, Precision, ShapeType, command } from "chili-core";
import { GeometryModel, Precision, ShapeType, command } from "chili-core";
import { GeoUtils } from "chili-geo";
import { PrismBody } from "../../bodys";
import { SnapLengthAtAxisData } from "../../snap";
import { IStep, LengthAtAxisStep } from "../../step";
Expand All @@ -16,23 +17,26 @@ let count = 1;
})
export class Prism extends CreateCommand {
protected override create(): GeometryModel {
let shape = this.stepDatas[0].shapes[0].shape as IFace; // todo assert
let [point, normal] = shape.normal(0, 0);
let shape = this.stepDatas[0].shapes[0].shape;
const { point, normal } = this.getAxis();
let dist = this.stepDatas[1].point!.sub(point).dot(normal);
let body = new PrismBody(this.document, shape, dist);
return new GeometryModel(this.document, `prism${count++}`, body);
}

protected override getSteps(): IStep[] {
return [
new SelectShapeStep(ShapeType.Face, "prompt.select.faces", false),
new SelectShapeStep(
ShapeType.Face | ShapeType.Edge | ShapeType.Wire,
"prompt.select.shape",
false,
),
new LengthAtAxisStep("operate.pickNextPoint", this.getLengthStepData),
];
}

private getLengthStepData = (): SnapLengthAtAxisData => {
let shape = this.stepDatas[0].shapes[0].shape as IFace; // todo assert
let [point, normal] = shape.normal(0, 0);
const { point, normal } = this.getAxis();
return {
point,
direction: normal,
Expand All @@ -41,8 +45,16 @@ export class Prism extends CreateCommand {
let dist = p.sub(point).dot(normal);
if (Math.abs(dist) < Precision.Float) return [];
let vec = normal.multiply(dist);
let shape = this.stepDatas[0].shapes[0].shape;
return [this.application.shapeFactory.prism(shape, vec).unwrap().mesh.edges!];
},
};
};

private getAxis() {
let point = this.stepDatas[0].shapes[0].point!;
let shape = this.stepDatas[0].shapes[0].shape;
let normal = GeoUtils.normal(shape as any);
return { point, normal };
}
}
2 changes: 1 addition & 1 deletion packages/chili/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
} from "chili-core";
import { Selection } from "./selection";

const FILE_VERSIOM = "0.1.2";
const FILE_VERSIOM = "0.3.0";

export class Document extends Observable implements IDocument {
readonly visual: IVisual;
Expand Down

0 comments on commit bdf1d2c

Please sign in to comment.