Skip to content

Commit

Permalink
✨ feat: add DefaultDataExchange
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangechen committed Nov 8, 2024
1 parent aca1d9c commit 9d6fc56
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 16 deletions.
17 changes: 2 additions & 15 deletions packages/chili-builder/src/appBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { Application, CommandService, EditEventHandler, EditorService, HotkeyService } from "chili";
import {
DefaultDataExchange,
I18n,
IDataExchange,
IDocument,
Expand All @@ -12,7 +13,6 @@ import {
IVisualFactory,
IWindow,
Logger,
VisualNode,
} from "chili-core";
import { IAdditionalModule } from "./additionalModule";

Expand Down Expand Up @@ -103,20 +103,7 @@ export class AppBuilder {
}

initDataExchange(): IDataExchange {
return {
importFormats(): string[] {
return ["chili3d"];
},
exportFormats(): string[] {
return ["chili3d"];
},
import(document: IDocument, files: FileList | File[]): Promise<void> {
return Promise.reject("not implemented");
},
export(type: string, nodes: VisualNode[]): Promise<BlobPart[] | undefined> {
return Promise.reject("not implemented");
},
};
return new DefaultDataExchange();
}

private ensureNecessary() {
Expand Down
50 changes: 49 additions & 1 deletion packages/chili-core/src/dataExchange.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,59 @@
// Copyright 2022-2023 the Chili authors. All rights reserved. AGPL-3.0 license.

import { IDocument } from "./document";
import { VisualNode } from "./model";
import { PubSub } from "./foundation";
import { EditableShapeNode, NodeLinkedList, ShapeNode, VisualNode } from "./model";

export interface IDataExchange {
importFormats(): string[];
exportFormats(): string[];
import(document: IDocument, files: FileList | File[]): Promise<void>;
export(type: string, nodes: VisualNode[]): Promise<BlobPart[] | undefined>;
}

export class DefaultDataExchange implements IDataExchange {
importFormats(): string[] {
return [".step", ".stp", ".iges", ".igs"];
}

exportFormats(): string[] {
return [".step", ".iges"];
}

async import(document: IDocument, files: FileList | File[]): Promise<void> {
for (const file of files) {
let content = new Uint8Array(await file.arrayBuffer());
let factory = document.application.shapeFactory.converter.convertFromIGES;
if (file.name.endsWith(".step") || file.name.endsWith(".stp")) {
factory = document.application.shapeFactory.converter.convertFromSTEP;
}
let shape = factory(content);
if (!shape.isOk) {
PubSub.default.pub("showToast", "toast.read.error");
return;
}
let shapes = shape.value.map((x, i) => {
return new EditableShapeNode(document, `Imported ${i}`, x);
});
let nodeList = new NodeLinkedList(document, file.name);
document.addNode(nodeList);
nodeList.add(...shapes);
document.visual.update();
}
}

async export(type: string, nodes: VisualNode[]): Promise<BlobPart[] | undefined> {
let shapes = nodes.map((x) => (x as ShapeNode).shape.value);
let factory = nodes[0].document.application.shapeFactory.converter.convertToIGES;
if (type === ".step") {
factory = nodes[0].document.application.shapeFactory.converter.convertToSTEP;
}
let shapeString = factory(...shapes);
if (!shapeString.isOk) {
PubSub.default.pub("showToast", "toast.converter.error");
return undefined;
}

return [shapeString.value];
}
}

0 comments on commit 9d6fc56

Please sign in to comment.