Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangechen committed Sep 17, 2023
2 parents 01786c6 + c332f74 commit 4ce3ac6
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 22 deletions.
23 changes: 21 additions & 2 deletions packages/chili-core/src/math/ray.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { Plane, XYZ } from "chili-core";
import { ClassMap, ISerialize, Serialized, Serializer } from "../serialize";
import { Plane } from "./plane";
import { XYZ } from "./xyz";

export class Ray {
@ClassMap.key("Ray")
export class Ray implements ISerialize {
/**
* unit vector
*/
Expand All @@ -19,6 +22,22 @@ export class Ray {
this.direction = n;
}

serialize(): Serialized {
return {
classKey: "Ray",
constructorParameters: {
location: this.location.serialize(),
direction: this.direction.serialize(),
},
properties: {},
};
}

@Serializer.deserializer()
static from({ direction, location }: { direction: XYZ; location: XYZ }) {
return new Ray(location, direction);
}

intersect(right: Ray): XYZ | undefined {
if (this.direction.isParallelTo(right.direction)) return undefined;
let result = this.nearestTo(right);
Expand Down
1 change: 1 addition & 0 deletions packages/chili-core/src/serialize/classKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type ClassKey =
| "Plane"
| "PolygonBody"
| "NodeLinkedList"
| "Ray"
| "RectBody"
| "WireBody"
| "Shape"
Expand Down
4 changes: 2 additions & 2 deletions packages/chili-geo/src/shapeFactory.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 { IEdge, IFace, ILine, IShape, ISolid, IVertex, IWire, Plane, Result, XYZ } from "chili-core";
import { IEdge, IFace, ILine, IShape, ISolid, IVertex, IWire, Plane, Ray, Result, XYZ } from "chili-core";

export interface IShapeFactory {
point(point: XYZ): Result<IVertex>;
Expand All @@ -13,5 +13,5 @@ export interface IShapeFactory {
prism(shape: IShape, vec: XYZ): Result<IShape>;
fuse(bottom: IShape, top: IShape): Result<IShape>;
sweep(profile: IShape, path: IWire): Result<IShape>;
revolve(profile: IShape, axis: ILine, angle: number): Result<IShape>;
revolve(profile: IShape, axis: Ray, angle: number): Result<IShape>;
}
6 changes: 6 additions & 0 deletions packages/chili-occ/src/occConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ export class OccShapeConverter implements IShapeConverter {
return this.convertFrom("step", data);
}

/**
* 如果原 IWire 仅由一个 IEdge 组成,则从 IWre 反序列化时,会变为 IEdge。暂时认为不是 bug!!!
* @param format
* @param data
* @returns
*/
private convertFrom(format: "step" | "iges", data: string): Result<IShape> {
const fileName = `blob.${format}`;
let reader = format === "step" ? new occ.STEPControl_Reader_1() : new occ.IGESControl_Reader_1();
Expand Down
9 changes: 5 additions & 4 deletions packages/chili-occ/src/shapeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
IWire,
MathUtils,
Plane,
Ray,
Result,
XYZ,
} from "chili-core";
Expand All @@ -25,18 +26,18 @@ export class ShapeFactory implements IShapeFactory {
}

sweep(profile: IShape, path: IWire): Result<IShape> {
let twire = (path as OccWire).shape;
let spine = (path as OccWire).shape;
let tprofile = (profile as OccShape).shape;
let builder = new occ.BRepOffsetAPI_MakePipe_1(twire, tprofile);
let builder = new occ.BRepOffsetAPI_MakePipe_1(spine, tprofile);
if (builder.IsDone()) {
return Result.success(OccHelps.getShape(builder.Shape()));
}
return Result.error("Failed to create a shape from a profile and a path");
}

revolve(profile: IShape, axis: ILine, angle: number): Result<IShape> {
revolve(profile: IShape, axis: Ray, angle: number): Result<IShape> {
let tprofile = (profile as OccShape).shape;
let ax1 = new occ.gp_Ax1_2(OccHelps.toPnt(axis.start), OccHelps.toDir(axis.direction));
let ax1 = new occ.gp_Ax1_2(OccHelps.toPnt(axis.location), OccHelps.toDir(axis.direction));
let builder = new occ.BRepPrimAPI_MakeRevol_1(tprofile, ax1, MathUtils.degToRad(angle), false);
if (builder.IsDone()) {
return Result.success(OccHelps.getShape(builder.Shape()));
Expand Down
10 changes: 5 additions & 5 deletions packages/chili/src/bodys/revolve.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 { Body, ClassMap, I18nKeys, IDocument, ILine, IShape, Result, Serializer } from "chili-core";
import { Body, ClassMap, I18nKeys, IDocument, ILine, IShape, Ray, Result, Serializer } from "chili-core";

@ClassMap.key("RevolveBody")
export class RevolveBody extends Body {
Expand All @@ -15,12 +15,12 @@ export class RevolveBody extends Body {
this.setPropertyAndUpdate("profile", value);
}

private _axis: ILine;
private _axis: Ray;
@Serializer.property("constructor")
get axis() {
return this._axis;
}
set axis(value: ILine) {
set axis(value: Ray) {
this.setPropertyAndUpdate("axis", value);
}

Expand All @@ -33,7 +33,7 @@ export class RevolveBody extends Body {
this.setPropertyAndUpdate("angle", value);
}

constructor(document: IDocument, profile: IShape, axis: ILine, angle: number) {
constructor(document: IDocument, profile: IShape, axis: Ray, angle: number) {
super(document);
this._profile = profile;
this._axis = axis;
Expand All @@ -49,7 +49,7 @@ export class RevolveBody extends Body {
}: {
document: IDocument;
profile: IShape;
axis: ILine;
axis: Ray;
angle: number;
}) {
return new RevolveBody(document, profile, axis, angle);
Expand Down
20 changes: 17 additions & 3 deletions packages/chili/src/bodys/sweep.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { Body, ClassMap, I18nKeys, IDocument, IShape, IWire, Result, Serializer } from "chili-core";
import {
Body,
ClassMap,
I18nKeys,
IDocument,
IEdge,
IShape,
IWire,
Result,
Serializer,
ShapeType,
} from "chili-core";

@ClassMap.key("SweepBody")
export class SweepBody extends Body {
Expand All @@ -24,10 +35,13 @@ export class SweepBody extends Body {
this.setPropertyAndUpdate("path", value);
}

constructor(document: IDocument, profile: IShape, path: IWire) {
constructor(document: IDocument, profile: IShape, path: IWire | IEdge) {
super(document);
this._profile = profile;
this._path = path;
this._path =
path.shapeType === ShapeType.Wire
? (path as IWire)
: document.application.shapeFactory.wire(path as unknown as IEdge).getValue()!;
}

@Serializer.deserializer()
Expand Down
15 changes: 13 additions & 2 deletions packages/chili/src/commands/create/revolve.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { GeometryModel, ICurve, IEdge, ILine, IShape, IShapeFilter, ShapeType, command } from "chili-core";
import {
GeometryModel,
ICurve,
IEdge,
ILine,
IShape,
IShapeFilter,
Ray,
ShapeType,
command,
} from "chili-core";
import { RevolveBody } from "../../bodys";
import { IStep } from "../../step";
import { SelectStep } from "../../step/selectStep";
Expand All @@ -26,7 +36,8 @@ export class Revolve extends CreateCommand {
protected override create(): GeometryModel {
let shape = this.stepDatas[0].shapes[0].shape; // todo assert
let edge = (this.stepDatas[1].shapes[0].shape as IEdge).asCurve().getValue() as ILine;
let body = new RevolveBody(this.document, shape, edge, this._angle);
let axis = new Ray(edge.start, edge.direction);
let body = new RevolveBody(this.document, shape, axis, this._angle);
return new GeometryModel(this.document, `Revolve ${count++}`, body);
}

Expand Down
7 changes: 3 additions & 4 deletions packages/chili/src/commands/create/sweep.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 { GeometryModel, IEdge, ShapeType, command } from "chili-core";
import { GeometryModel, IEdge, IWire, ShapeType, command } from "chili-core";
import { SweepBody } from "../../bodys";
import { IStep } from "../../step";
import { SelectStep } from "../../step/selectStep";
Expand All @@ -16,9 +16,8 @@ let count = 1;
export class Sweep extends CreateCommand {
protected override create(): GeometryModel {
let shape = this.stepDatas[0].shapes[0].shape;
let edge = this.stepDatas[1].shapes[0].shape as IEdge;
let wire = this.application.shapeFactory.wire(edge).getValue()!;
let body = new SweepBody(this.document, shape, wire);
let path = this.stepDatas[1].shapes[0].shape;
let body = new SweepBody(this.document, shape, path as IWire);
return new GeometryModel(this.document, `Sweep ${count++}`, body);
}

Expand Down

0 comments on commit 4ce3ac6

Please sign in to comment.