From 849317060d340e2f07b636031032f087cabb7b57 Mon Sep 17 00:00:00 2001 From: xiange Date: Mon, 4 Nov 2024 20:30:30 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20add=20stl=20importer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/chili-builder/src/ribbon.ts | 2 +- .../chili-core/src/command/commandKeys.ts | 1 + packages/chili-core/src/i18n/en.ts | 1 + packages/chili-core/src/i18n/keys.ts | 1 + packages/chili-core/src/i18n/zh-cn.ts | 1 + packages/chili/src/commands/importExport.ts | 44 +++++++++++++++++-- 6 files changed, 46 insertions(+), 4 deletions(-) diff --git a/packages/chili-builder/src/ribbon.ts b/packages/chili-builder/src/ribbon.ts index 9d4bda76..ade80e0b 100644 --- a/packages/chili-builder/src/ribbon.ts +++ b/packages/chili-builder/src/ribbon.ts @@ -53,7 +53,7 @@ export const DefaultRibbon: RibbonTab[] = [ }, { groupName: "ribbon.group.importExport", - items: ["file.import", "file.export.iges", "file.export.stp"], + items: ["file.import", "file.import.stl", "file.export.iges", "file.export.stp"], }, ], }, diff --git a/packages/chili-core/src/command/commandKeys.ts b/packages/chili-core/src/command/commandKeys.ts index fea1dddd..7e25d291 100644 --- a/packages/chili-core/src/command/commandKeys.ts +++ b/packages/chili-core/src/command/commandKeys.ts @@ -31,6 +31,7 @@ const COMMAND_KEYS = [ "file.export.iges", "file.export.stp", "file.import", + "file.import.stl", "modify.array", "modify.break", "modify.delete", diff --git a/packages/chili-core/src/i18n/en.ts b/packages/chili-core/src/i18n/en.ts index d4bf29a5..d0e9feed 100644 --- a/packages/chili-core/src/i18n/en.ts +++ b/packages/chili-core/src/i18n/en.ts @@ -51,6 +51,7 @@ export default { "command.faceable.isFace": "Face", "command.fuse": "Fuse", "command.import": "Import", + "command.import.stl": "Import STL", "command.line.isConnected": "Connected", "command.line": "Line", "command.mirror": "Mirror", diff --git a/packages/chili-core/src/i18n/keys.ts b/packages/chili-core/src/i18n/keys.ts index f0639b76..b8048d4b 100644 --- a/packages/chili-core/src/i18n/keys.ts +++ b/packages/chili-core/src/i18n/keys.ts @@ -43,6 +43,7 @@ const I18N_KEYS = [ "command.document.saveToFile", "command.export.iges", "command.export.step", + "command.import.stl", "command.faceable.isFace", "command.fuse", "command.import", diff --git a/packages/chili-core/src/i18n/zh-cn.ts b/packages/chili-core/src/i18n/zh-cn.ts index eb664f67..57a572f3 100644 --- a/packages/chili-core/src/i18n/zh-cn.ts +++ b/packages/chili-core/src/i18n/zh-cn.ts @@ -51,6 +51,7 @@ export default { "command.faceable.isFace": "面", "command.fuse": "合并", "command.import": "导入", + "command.import.stl": "导入STL", "command.line.isConnected": "相连", "command.line": "直线", "command.mirror": "镜像", diff --git a/packages/chili/src/commands/importExport.ts b/packages/chili/src/commands/importExport.ts index 1f900cd3..af44815d 100644 --- a/packages/chili/src/commands/importExport.ts +++ b/packages/chili/src/commands/importExport.ts @@ -3,7 +3,6 @@ import { AsyncController, EditableShapeNode, - GeometryNode, I18n, IApplication, ICommand, @@ -21,8 +20,9 @@ import { download, readFilesAsync, } from "chili-core"; +import { TypedArray } from "three"; import { STLLoader } from "three/examples/jsm/loaders/STLLoader"; -import { SelectNodeStep } from "../step"; +import { SelectShapeNodeStep } from "../step"; let count = 1; @@ -134,7 +134,7 @@ abstract class Export implements ICommand { .filter((x) => x instanceof VisualNode); if (models?.length === 0) { let controller = new AsyncController(); - let step = new SelectNodeStep("prompt.select.models", true); + let step = new SelectShapeNodeStep("prompt.select.models", true); let data = await step.execute(application.activeView?.document!, controller); if (!data?.nodes) { PubSub.default.pub("showToast", "prompt.select.noModelSelected"); @@ -167,3 +167,41 @@ export class ExportStep extends Export { return "step"; } } +@command({ + name: "file.import.stl", + display: "command.import.stl", + icon: "icon-export", +}) +export class ImportStl implements ICommand { + async execute(application: IApplication): Promise { + let document = application.activeView?.document; + if (!document) return; + let data = await readFilesAsync(".stl", false); + if (!data.isOk || data.value.length === 0) { + return; + } + + let arrayBuffer = await data.value.item(0)?.arrayBuffer(); + const loader = new STLLoader(); + let geometry = loader.parse(arrayBuffer!); + // geometry.computeVertexNormals(); + + console.log(geometry); + + let mesh = new Mesh(); + mesh.position = this.getDatas(geometry.attributes["position"].array)!; + mesh.normal = this.getDatas(geometry.attributes["normal"]?.array); + mesh.index = this.getDatas(geometry.index?.array!); + mesh.uv = this.getDatas(geometry.attributes["uv"]?.array); + mesh.meshType = "surface"; + console.log(mesh); + + let meshNode = new MeshNode(document, mesh, data.value.item(0)!.name); + document.addNode(meshNode); + } + + private getDatas(array: TypedArray | undefined): number[] | undefined { + if (!array) return undefined; + return Array.from(array); + } +}