From c063e5fa16579273618989128a6b4bef2f1e8276 Mon Sep 17 00:00:00 2001 From: xiange Date: Sat, 23 Dec 2023 22:20:08 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20Disassemble=20the=20importe?= =?UTF-8?q?d=20model.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chili-core/src/geometry/shapeConverter.ts | 4 +- packages/chili-occ/src/occConverter.ts | 15 ++++++-- packages/chili/src/commands/importExport.ts | 38 +++++++++++++------ 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/packages/chili-core/src/geometry/shapeConverter.ts b/packages/chili-core/src/geometry/shapeConverter.ts index 918b5d86..98ca3f3c 100644 --- a/packages/chili-core/src/geometry/shapeConverter.ts +++ b/packages/chili-core/src/geometry/shapeConverter.ts @@ -5,9 +5,9 @@ import { IShape } from "./shape"; export interface IShapeConverter { convertToIGES(...shapes: IShape[]): Result; - convertFromIGES(iges: string): Result; + convertFromIGES(iges: string): Result; convertToSTEP(...shapes: IShape[]): Result; - convertFromSTEP(step: string): Result; + convertFromSTEP(step: string): Result; convertToBrep(shape: IShape): Result; convertFromBrep(brep: string): Result; } diff --git a/packages/chili-occ/src/occConverter.ts b/packages/chili-occ/src/occConverter.ts index 05bd2a38..afb0edc6 100644 --- a/packages/chili-occ/src/occConverter.ts +++ b/packages/chili-occ/src/occConverter.ts @@ -44,7 +44,7 @@ export class OccShapeConverter implements IShapeConverter { return success ? Result.success(file) : Result.error("export IGES error"); } - convertFromIGES(data: string): Result { + convertFromIGES(data: string) { return this.convertFrom("iges", data); } @@ -72,7 +72,7 @@ export class OccShapeConverter implements IShapeConverter { } } - convertFromSTEP(data: string): Result { + convertFromSTEP(data: string) { return this.convertFrom("step", data); } @@ -82,7 +82,7 @@ export class OccShapeConverter implements IShapeConverter { * @param data * @returns */ - private convertFrom(format: "step" | "iges", data: string): Result { + private convertFrom(format: "step" | "iges", data: string): Result { 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); @@ -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}`); } diff --git a/packages/chili/src/commands/importExport.ts b/packages/chili/src/commands/importExport.ts index 53d28a1e..2d2f4459 100644 --- a/packages/chili/src/commands/importExport.ts +++ b/packages/chili/src/commands/importExport.ts @@ -5,11 +5,14 @@ import { GeometryModel, IApplication, ICommand, + IDocument, IModel, INode, IShape, + NodeLinkedList, PubSub, Result, + Transaction, command, download, readFileAsync, @@ -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]) => { + 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]> { 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; - if (data.value[0].fileName.endsWith(".igs") || data.value[0].fileName.endsWith(".iges")) { + let shape: Result; + 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]; } }