mirror of https://gitee.com/cf-fz/WebCAD.git
parent
46a3856c1b
commit
e068f46fde
@ -0,0 +1,145 @@
|
|||||||
|
import { Command } from "../Editor/CommandMachine";
|
||||||
|
import { Vec2 } from "../Geometry/CheckIntersect";
|
||||||
|
import { Vec3 } from "../Geometry/IVec3";
|
||||||
|
import { HotCMD } from "../Hot/HotCommand";
|
||||||
|
import { AsVector2 } from "../Geometry/GeUtils";
|
||||||
|
import { FixIndex } from "../Common/Utils";
|
||||||
|
import { Matrix4, Euler, Vector3 } from "three";
|
||||||
|
import { Board } from "../DatabaseServices/Entity/Board";
|
||||||
|
import { Polyline, PolylineProps } from "../DatabaseServices/Entity/Polyline";
|
||||||
|
import { app } from "../ApplicationServices/Application";
|
||||||
|
import { FileSystem } from "../Common/FileSystem";
|
||||||
|
|
||||||
|
/** 模型类型 */
|
||||||
|
enum KJL_ModelType
|
||||||
|
{
|
||||||
|
ParamModel = 1,//参数化模型
|
||||||
|
Max3DModel = 2, //3DMax模型
|
||||||
|
BaseModel = 3, //元件
|
||||||
|
VirtualModel = 4,//虚拟模型
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 参数模型 */
|
||||||
|
interface KJL_ParamModel
|
||||||
|
{
|
||||||
|
modelTypeId: KJL_ModelType;
|
||||||
|
modelName: string;
|
||||||
|
parameters: [];
|
||||||
|
|
||||||
|
position: Vec3;
|
||||||
|
center: Vec3;
|
||||||
|
rotate: Vec3;
|
||||||
|
|
||||||
|
absPosition: Vec3;
|
||||||
|
absRotation: Vec3;
|
||||||
|
|
||||||
|
subModels: KJL_ParamModel[];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 曲线类型 */
|
||||||
|
enum KJL_LineType
|
||||||
|
{
|
||||||
|
Line = 0,
|
||||||
|
Arc = 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 板件数据(元件,可能) */
|
||||||
|
interface KJL_ParamModel_Board extends KJL_ParamModel
|
||||||
|
{
|
||||||
|
thickness: number;
|
||||||
|
points: Vec2[];
|
||||||
|
productionOldPath: {
|
||||||
|
points: Vec2[];
|
||||||
|
lines: {
|
||||||
|
type: KJL_LineType;
|
||||||
|
clockwise: boolean;
|
||||||
|
radius: number;
|
||||||
|
}[];
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
interface KJL_JsonFile
|
||||||
|
{
|
||||||
|
paramModel: KJL_ParamModel[];
|
||||||
|
}
|
||||||
|
|
||||||
|
@HotCMD
|
||||||
|
export class Command_KJLImport implements Command
|
||||||
|
{
|
||||||
|
async exec()
|
||||||
|
{
|
||||||
|
let flist = await FileSystem.ChooseFile(".json", false);
|
||||||
|
if (flist.length > 0)
|
||||||
|
{
|
||||||
|
let f = flist.item(0);
|
||||||
|
let fstr = await FileSystem.ReadFileAsText(f);
|
||||||
|
let json = JSON.parse(fstr) as KJL_JsonFile;
|
||||||
|
json.paramModel.forEach(ParseModel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function ParseModel(model: KJL_ParamModel)
|
||||||
|
{
|
||||||
|
if (model.modelTypeId === KJL_ModelType.BaseModel)
|
||||||
|
{
|
||||||
|
let bmodel = model as KJL_ParamModel_Board;
|
||||||
|
if (!bmodel.productionOldPath)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let pts = bmodel.productionOldPath.points.map(AsVector2);
|
||||||
|
let radiuss = bmodel.productionOldPath.lines.map(d =>
|
||||||
|
{
|
||||||
|
if (d.type === KJL_LineType.Line)
|
||||||
|
return 0;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
let radius = d.radius;
|
||||||
|
if (d.clockwise)
|
||||||
|
radius = -radius;
|
||||||
|
|
||||||
|
return radius;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let buls: number[] = [];
|
||||||
|
|
||||||
|
for (let i = 0; i < radiuss.length; i++)
|
||||||
|
{
|
||||||
|
let radius = radiuss[i];
|
||||||
|
if (radius === 0)
|
||||||
|
buls.push(0);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
let nextIndex = FixIndex(i + 1, radiuss.length);
|
||||||
|
let dist = pts[i].distanceTo(pts[nextIndex]);
|
||||||
|
let bul = Math.tan(0.5 * Math.asin(dist / Math.abs(radius))) * Math.sign(radius);
|
||||||
|
buls.push(bul);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let lined: PolylineProps[] = [];
|
||||||
|
for (let i = 0; i < pts.length; i++)
|
||||||
|
lined.push({ pt: pts[i], bul: buls[i] });
|
||||||
|
let pl = new Polyline(lined);
|
||||||
|
pl.CloseMark = true;
|
||||||
|
|
||||||
|
let br = new Board();
|
||||||
|
|
||||||
|
br.Name = bmodel.modelName;
|
||||||
|
br.Thickness = bmodel.thickness;
|
||||||
|
br.ContourCurve = pl;
|
||||||
|
|
||||||
|
br.Position = new Vector3(0, 0, bmodel.thickness * -0.5);
|
||||||
|
|
||||||
|
let mtx = new Matrix4().makeRotationFromEuler(new Euler(bmodel.absRotation.x, bmodel.absRotation.y, bmodel.absRotation.z, "ZYX"))
|
||||||
|
.setPosition(bmodel.absPosition.x, bmodel.absPosition.y, bmodel.absPosition.z);
|
||||||
|
br.ApplyMatrix(mtx);
|
||||||
|
|
||||||
|
app.Database.ModelSpace.Append(br);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (model.subModels)
|
||||||
|
model.subModels.forEach(ParseModel);
|
||||||
|
}
|
Loading…
Reference in new issue