diff --git a/src/Add-on/Command_ExportGLTF.ts b/src/Add-on/Command_ExportGLTF.ts new file mode 100644 index 000000000..d6a54f08c --- /dev/null +++ b/src/Add-on/Command_ExportGLTF.ts @@ -0,0 +1,77 @@ + +import { Box3, Group, Object3D, Vector3 } from "three"; +import { GLTFExporter, GLTFExporterOptions } from "three/examples/jsm/exporters/GLTFExporter"; +import { app } from "../ApplicationServices/Application"; +import { FS } from "../Common/FileSystem"; +import { Hole } from "../DatabaseServices/3DSolid/Hole"; +import { Dimension } from "../DatabaseServices/Dimension/Dimension"; +import { Curve } from "../DatabaseServices/Entity/Curve"; +import { Entity } from "../DatabaseServices/Entity/Entity"; +import { Command } from "../Editor/CommandMachine"; +import { PromptStatus } from "../Editor/PromptResult"; +import { VisualSpaceBox } from "../Editor/VisualSpaceBox"; +import { MoveMatrix } from "../Geometry/GeUtils"; +import { RenderType } from "../GraphicsSystem/RenderType"; + +export async function ExportGLTF(ens: Entity[]) +{ + let ocsInv = ens[0].SpaceOCSInv; + let totalBox = new Box3(); + ens.reduce((box, en) => box.union(en.GetBoundingBoxInMtx(ocsInv)), totalBox); + + let min = totalBox.getCenter(new Vector3); + min.z = totalBox.min.z; + let mtx = MoveMatrix(min.negate()).multiply(ocsInv); + + let g = new Group(); + + for (let b of ens) + { + let o: Object3D; + o = b.Clone().ApplyMatrix(mtx).GetDrawObjectFromRenderType(RenderType.Physical); + g.add(o); + } + + g.scale.set(0.1, 0.1, 0.1); + g.updateMatrix(); + g.updateMatrixWorld(); + var exporter = new GLTFExporter(); + const options: GLTFExporterOptions = { + trs: false,//导出位置、旋转和比例,而不是每个节点的矩阵。默认值为 false + onlyVisible: true,//仅导出可见对象。默认为 true。 + binary: false,//以二进制 (.glb) 格式导出,返回 ArrayBuffer。默认值为 false。 + // maxTextureSize: params.maxTextureSize //将图像的最大尺寸(宽度和高度)限制为给定值。默认值为无穷大。 + //animations- Array< AnimationClip >。要包含在导出中的动画列表。 + //includeCustomExtensions- bool。导出对象属性上定义的自定义 glTF 扩展。默认值为 false。userData.gltfExtensions + }; + + var result = exporter.parse(g, (gltf) => + { + if (options.binary || gltf instanceof Uint8Array) + FS.WriteFile(`webcad.glb`, gltf as Uint8Array); + else + FS.WriteFile(`webcad.gltf`, JSON.stringify(gltf)); + }, options); +} + +export class Command_ExportGLTF implements Command +{ + async exec() + { + let ssRes = await app.Editor.GetSelection({ + Msg: "请选择要导出GLTF的实体(导出单位为厘米):", + UseSelect: true, Filter: { + filterFunction: (obj, ent) => + { + if (ent instanceof Dimension || ent instanceof Curve || ent instanceof VisualSpaceBox || ent instanceof Hole || ent instanceof Text) + return false; + return true; + } + } + }); + + if (ssRes.Status !== PromptStatus.OK) return; + + ExportGLTF(ssRes.SelectSet.SelectEntityList); + } +} diff --git a/src/Common/CommandNames.ts b/src/Common/CommandNames.ts index ca03a6eed..e4c04e615 100644 --- a/src/Common/CommandNames.ts +++ b/src/Common/CommandNames.ts @@ -247,6 +247,7 @@ export enum CommandNames ExportobjMtl = "EXPORTOBJMTL", ExportObj2 = "EXPORTOBJ2", ExportSTL = "EXPORTSTL", + ExportGLTF = "EXPORTGLTF", UpdateBoardInfos = "UPDATEBOARDINFOS", ToggleUI = "TOGGLEUI", BoardReplaceTempate = "BOARDREPLACETEMPLATE", diff --git a/src/Editor/CommandRegister.ts b/src/Editor/CommandRegister.ts index 056408a11..7d1951dac 100644 --- a/src/Editor/CommandRegister.ts +++ b/src/Editor/CommandRegister.ts @@ -221,6 +221,7 @@ import { Command_ClearCDBrHoleModeling } from "../Add-on/ClearCDBrHoleModeling"; import { Cmd_LockMaterial, Cmd_UnLockMaterial } from "../Add-on/Cmd_LockMaterial"; import { Command_RemovePolylineRepeatPos } from "../Add-on/Cmd_RemovePolylineRepeatPos"; import { SideModelFeedingCommand } from "../Add-on/CommandSideModelFeediing"; +import { Command_ExportGLTF } from "../Add-on/Command_ExportGLTF"; import { Command_Modeling } from "../Add-on/Command_Modeling"; import { Command_PickUp2DModelCsgs } from "../Add-on/Command_PickUp2DModelCsgs"; import { Command_TemplateGroup } from "../Add-on/Command_TemplateGroup"; @@ -805,6 +806,7 @@ export function registerCommand() commandMachine.RegisterCommand(CommandNames.ExportobjMtl, new Command_ExportObjAndMtl()); commandMachine.RegisterCommand(CommandNames.ExportObj, new Command_ExportObj()); commandMachine.RegisterCommand(CommandNames.ExportSTL, new Command_ExportSTL()); + commandMachine.RegisterCommand(CommandNames.ExportGLTF, new Command_ExportGLTF()); commandMachine.RegisterCommand(CommandNames.UpdateBoardInfos, new UpdateBoardInfos());