-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aca1d9c
commit 9d6fc56
Showing
2 changed files
with
51 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |