From 541654f571dfa0d861b53e60ddba12a64445e45b Mon Sep 17 00:00:00 2001 From: ChenX Date: Fri, 15 May 2020 15:01:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=91=BD=E4=BB=A4:ExportSTL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Add-on/Exports/ExportSTL.ts | 46 +++++++++++++++++++++++++++++++++ src/Editor/CommandRegister.ts | 2 ++ 2 files changed, 48 insertions(+) create mode 100644 src/Add-on/Exports/ExportSTL.ts diff --git a/src/Add-on/Exports/ExportSTL.ts b/src/Add-on/Exports/ExportSTL.ts new file mode 100644 index 000000000..76c9e85e7 --- /dev/null +++ b/src/Add-on/Exports/ExportSTL.ts @@ -0,0 +1,46 @@ +import { Box3, Group, Object3D } from 'three'; +import { STLExporter } from 'three/examples/jsm/exporters/STLExporter.js'; +import { ColladaExporter } from 'three/examples/jsm/exporters/ColladaExporter'; +import { app } from '../../ApplicationServices/Application'; +import { FileSystem } from '../../Common/FileSystem'; +import { Entity } from '../../DatabaseServices/Entity/Entity'; +import { PromptStatus } from '../../Editor/PromptResult'; +import { MoveMatrix } from '../../Geometry/GeUtils'; +import { RenderType } from '../../GraphicsSystem/RenderType'; +export class Command_ExportSTL +{ + async exec() + { + let ssRes = await app.Editor.GetSelection({ Filter: { filterTypes: [Entity] } }); + if (ssRes.Status !== PromptStatus.OK) return; + let ents = ssRes.SelectSet.SelectEntityList; + let exporter = new STLExporter(); + let ocsInv = ents[0].SpaceOCSInv; + let totalBox = new Box3(); + ents.reduce((box, en) => + { + return box.union(en.GetBoundingBoxInMtx(ocsInv)); + }, totalBox); + + let min = totalBox.min; + let mtx = MoveMatrix(min.negate()).multiply(ocsInv); + + let g = new Group(); + + for (let b of ents) + { + let o: Object3D; + o = b.Clone().ApplyMatrix(mtx).GetDrawObjectFromRenderType(RenderType.Physical); + g.add(o); + } + + g.scale.set(1e-3, 1e-3, 1e-3); + g.updateMatrix(); + g.updateMatrixWorld(); + let result = exporter.parse(g, { binary: true }); + FileSystem.WriteFile("webcad.stl", result); + // let exporter2 = new ColladaExporter(); + // let r2 = exporter2.parse(g, () => { }, {}); + // FileSystem.WriteFile("webcad.dae", r2.data); + } +} diff --git a/src/Editor/CommandRegister.ts b/src/Editor/CommandRegister.ts index 044c42d55..0670d6354 100644 --- a/src/Editor/CommandRegister.ts +++ b/src/Editor/CommandRegister.ts @@ -178,6 +178,7 @@ import { Command_Conver2Polyline } from "../Add-on/Conver2Polyline"; import { Command_Curve2VSBox } from "../Add-on/twoD2threeD/Command_Curve2VSBox"; import { Command_ToggleUI } from "../Add-on/ToggleUI"; import { Command_ExportObj2 } from "../Geometry/ExportObj2"; +import { Command_ExportSTL } from "../Add-on/Exports/ExportSTL"; export function registerCommand() { @@ -486,6 +487,7 @@ export function registerCommand() commandMachine.RegisterCommand("clearrelevance", new DeleteRelevance()); commandMachine.RegisterCommand("exportobj", new Command_ExportObj()); commandMachine.RegisterCommand("exportobj2", new Command_ExportObj2()); + commandMachine.RegisterCommand("ExportSTL", new Command_ExportSTL()); commandMachine.RegisterCommand("updateboardinfos", new UpdateBoardInfos());