Skip to content

Commit

Permalink
✨ feat: add stl importer
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangechen committed Nov 4, 2024
1 parent fa46813 commit 8493170
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/chili-builder/src/ribbon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
},
],
},
Expand Down
1 change: 1 addition & 0 deletions packages/chili-core/src/command/commandKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const COMMAND_KEYS = [
"file.export.iges",
"file.export.stp",
"file.import",
"file.import.stl",
"modify.array",
"modify.break",
"modify.delete",
Expand Down
1 change: 1 addition & 0 deletions packages/chili-core/src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions packages/chili-core/src/i18n/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions packages/chili-core/src/i18n/zh-cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export default {
"command.faceable.isFace": "面",
"command.fuse": "合并",
"command.import": "导入",
"command.import.stl": "导入STL",
"command.line.isConnected": "相连",
"command.line": "直线",
"command.mirror": "镜像",
Expand Down
44 changes: 41 additions & 3 deletions packages/chili/src/commands/importExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import {
AsyncController,
EditableShapeNode,
GeometryNode,
I18n,
IApplication,
ICommand,
Expand All @@ -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;

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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<void> {
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);
}
}

0 comments on commit 8493170

Please sign in to comment.