Skip to content

Commit

Permalink
✨ feat: refactor shapeConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangechen committed Dec 28, 2024
1 parent 1e9aa51 commit 1cfabe6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/chili-core/src/dataExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class DefaultDataExchange implements IDataExchange {
return;
}
let shapes = shape.value.map((x, i) => {
return new EditableShapeNode(document, `Imported ${i}`, x);
return new EditableShapeNode(document, x.name, x.shape);
});
let nodeList = new FolderNode(document, file.name);
document.addNode(nodeList);
Expand Down
10 changes: 8 additions & 2 deletions packages/chili-core/src/shape/shapeConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
import { Result } from "../foundation";
import { IShape } from "./shape";

export type ShapeInfo = {
name: string;
shape: IShape;
color: string;
};

export interface IShapeConverter {
convertToIGES(...shapes: IShape[]): Result<string>;
convertFromIGES(iges: Uint8Array): Result<IShape[]>;
convertFromIGES(iges: Uint8Array): Result<ShapeInfo[]>;
convertToSTEP(...shapes: IShape[]): Result<string>;
convertFromSTEP(step: Uint8Array): Result<IShape[]>;
convertFromSTEP(step: Uint8Array): Result<ShapeInfo[]>;
convertToBrep(shape: IShape): Result<string>;
convertFromBrep(brep: string): Result<IShape>;
}
48 changes: 29 additions & 19 deletions packages/chili-wasm/src/converter.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
import { IShape, IShapeConverter, Result } from "chili-core";
import { IShape, IShapeConverter, Result, ShapeInfo } from "chili-core";
import { OccShape } from "./shape";
import { OcctHelper } from "./helper";
import { ShapeNode } from "../lib/chili-wasm";

const parseShapeNodeToShapes = (shapes: IShape[], shapeNode: ShapeNode) => {
function shapeNodeToShapeInfo(node: ShapeNode): ShapeInfo {
return {
name: node.name as string,
shape: OcctHelper.wrapShape(node.shape!),
color: node.color as string
};
}

const parseShapeNodeToShapes = (shapes: ShapeInfo[], shapeNode: ShapeNode | undefined) => {
if (!shapeNode) {
return;
}

if (shapeNode.shape) {
shapes.push(shapeNodeToShapeInfo(shapeNode));
}

shapeNode.getChildren().forEach((child) => {
if (child.shape) shapes.push(OcctHelper.wrapShape(child.shape));
parseShapeNodeToShapes(shapes, child);
});

return shapes;
};

Expand All @@ -22,19 +38,15 @@ export class OccShapeConverter implements IShapeConverter {
return Result.ok(wasm.Converter.convertToIges(occShapes));
}

convertFromIGES(iges: Uint8Array): Result<IShape[]> {
let shapes: IShape[] = [];
convertFromIGES(iges: Uint8Array): Result<ShapeInfo[]> {
let shapes: ShapeInfo[] = [];
let node = wasm.Converter.convertFromIges(iges);

if (node) {
if (node.shape) {
shapes.push(OcctHelper.wrapShape(node.shape));
}
parseShapeNodeToShapes(shapes, node);
}
parseShapeNodeToShapes(shapes, node);

return Result.ok(shapes);
}

convertToSTEP(...shapes: IShape[]): Result<string> {
let occShapes = shapes.map((shape) => {
if (shape instanceof OccShape) {
Expand All @@ -44,25 +56,23 @@ export class OccShapeConverter implements IShapeConverter {
});
return Result.ok(wasm.Converter.convertToStep(occShapes));
}
convertFromSTEP(step: Uint8Array): Result<IShape[]> {
let shapes: IShape[] = [];

convertFromSTEP(step: Uint8Array): Result<ShapeInfo[]> {
let shapes: ShapeInfo[] = [];
let node = wasm.Converter.convertFromStep(step);

if (node) {
if (node.shape) {
shapes.push(OcctHelper.wrapShape(node.shape));
}
parseShapeNodeToShapes(shapes, node);
}
parseShapeNodeToShapes(shapes, node);

return Result.ok(shapes);
}

convertToBrep(shape: IShape): Result<string> {
if (shape instanceof OccShape) {
return Result.ok(wasm.Converter.convertToBrep(shape.shape));
}
return Result.err("Shape is not an OccShape");
}

convertFromBrep(brep: string): Result<IShape> {
let shape = wasm.Converter.convertFromBrep(brep);
return Result.ok(OcctHelper.wrapShape(shape));
Expand Down

0 comments on commit 1cfabe6

Please sign in to comment.