Skip to content

Commit

Permalink
✨ feat: Disassemble the imported model.
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangechen committed Dec 23, 2023
1 parent ff881d8 commit c063e5f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
4 changes: 2 additions & 2 deletions packages/chili-core/src/geometry/shapeConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { IShape } from "./shape";

export interface IShapeConverter {
convertToIGES(...shapes: IShape[]): Result<string>;
convertFromIGES(iges: string): Result<IShape>;
convertFromIGES(iges: string): Result<IShape[]>;
convertToSTEP(...shapes: IShape[]): Result<string>;
convertFromSTEP(step: string): Result<IShape>;
convertFromSTEP(step: string): Result<IShape[]>;
convertToBrep(shape: IShape): Result<string>;
convertFromBrep(brep: string): Result<IShape>;
}
15 changes: 11 additions & 4 deletions packages/chili-occ/src/occConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class OccShapeConverter implements IShapeConverter {
return success ? Result.success(file) : Result.error("export IGES error");
}

convertFromIGES(data: string): Result<IShape> {
convertFromIGES(data: string) {
return this.convertFrom("iges", data);
}

Expand Down Expand Up @@ -72,7 +72,7 @@ export class OccShapeConverter implements IShapeConverter {
}
}

convertFromSTEP(data: string): Result<IShape> {
convertFromSTEP(data: string) {
return this.convertFrom("step", data);
}

Expand All @@ -82,7 +82,7 @@ export class OccShapeConverter implements IShapeConverter {
* @param data
* @returns
*/
private convertFrom(format: "step" | "iges", data: string): Result<IShape> {
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();
occ.FS.createDataFile("/", fileName, data, true, true, true);
Expand All @@ -91,7 +91,14 @@ export class OccShapeConverter implements IShapeConverter {
if (readResult === occ.IFSelect_ReturnStatus.IFSelect_RetDone) {
const progress = new occ.Message_ProgressRange_1();
reader.TransferRoots(progress);
return Result.success(OccHelps.getShape(reader.OneShape()));
let shapes: IShape[] = [];
for (let i = 1; i <= reader.NbShapes(); i++) {
let shape = reader.Shape(i);
if (shape instanceof occ.TopoDS_Shape) {
shapes.push(OccHelps.getShape(shape));
}
}
return Result.success(shapes);
} else {
return Result.error(`Cannot load ${format}`);
}
Expand Down
38 changes: 26 additions & 12 deletions packages/chili/src/commands/importExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import {
GeometryModel,
IApplication,
ICommand,
IDocument,
IModel,
INode,
IShape,
NodeLinkedList,
PubSub,
Result,
Transaction,
command,
download,
readFileAsync,
Expand All @@ -29,30 +32,41 @@ export class Import implements ICommand {
let document = application.activeDocument;
if (!document) return;
let shape = await this.readShape(application);
if (!shape.success) {
Transaction.excute(document, "import model", () => {
this.addImportedShape(document!, shape);
});
}

private addImportedShape = (document: IDocument, shape: [string | undefined, Result<IShape[]>]) => {
if (!shape[1].success) {
PubSub.default.pub("showToast", "toast.read.error");
return;
}
let body = new ImportedBody(document, shape.value);
let model = new GeometryModel(document, `Imported ${count++}`, body);
document.addNode(model);
let shapes = shape[1].value.map((x) => {
let body = new ImportedBody(document!, x);
return new GeometryModel(document!, `Imported ${count++}`, body);
});
let nodeList = new NodeLinkedList(document, shape[0]!);
document.addNode(nodeList);
nodeList.add(...shapes);
document.visual.viewer.update();
}
};

private async readShape(application: IApplication) {
private async readShape(application: IApplication): Promise<[string | undefined, Result<IShape[]>]> {
let data = await readFileAsync(".iges, .igs, .step, .stp", false);
if (!data.success || data.value.length === 0) {
return Result.error("toast.read.error");
return [undefined, Result.error("toast.read.error")];
}
let shape: Result<IShape>;
if (data.value[0].fileName.endsWith(".igs") || data.value[0].fileName.endsWith(".iges")) {
let shape: Result<IShape[]>;
let name = data.value[0].fileName;
if (name.endsWith(".igs") || name.endsWith(".iges")) {
shape = application.shapeFactory.converter.convertFromIGES(data.value[0].data);
} else if (data.value[0].fileName.endsWith(".stp") || data.value[0].fileName.endsWith(".step")) {
} else if (name.endsWith(".stp") || name.endsWith(".step")) {
shape = application.shapeFactory.converter.convertFromSTEP(data.value[0].data);
} else {
throw new Error(`不支持的文件:${data.value[0].fileName}`);
throw new Error(`不支持的文件:${name}`);
}
return shape;
return [name, shape];
}
}

Expand Down

0 comments on commit c063e5f

Please sign in to comment.