mirror of https://gitee.com/cf-fz/WebCAD.git
parent
78a11ff7cd
commit
55061c0faa
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue