From e068f46fde7c5251681e3cf2b569bb3ea4cba02f Mon Sep 17 00:00:00 2001 From: ChenX Date: Wed, 4 Sep 2019 16:21:22 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=B7=E5=AE=B6=E4=B9=90=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=AF=BC=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Add-on/KJLImport.ts | 145 ++++++++++++++++++++++++++++++++++ src/Editor/CommandRegister.ts | 5 +- 2 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 src/Add-on/KJLImport.ts diff --git a/src/Add-on/KJLImport.ts b/src/Add-on/KJLImport.ts new file mode 100644 index 000000000..c6ec9b246 --- /dev/null +++ b/src/Add-on/KJLImport.ts @@ -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); +} diff --git a/src/Editor/CommandRegister.ts b/src/Editor/CommandRegister.ts index 2b87c5851..1b51d0ef0 100644 --- a/src/Editor/CommandRegister.ts +++ b/src/Editor/CommandRegister.ts @@ -81,6 +81,7 @@ import { Command_HideSelected, Command_HideUnselected, Command_ShowAll } from ". import { Command_Insert } from "../Add-on/Insert"; import { Command_INsTest } from "../Add-on/instest"; import { Command_Join } from "../Add-on/Join"; +import { Command_KJLImport } from "../Add-on/KJLImport"; import { DrawLattice } from "../Add-on/LatticeDrawer/DrawLatticeDrawer"; import { Command_Length } from "../Add-on/Length"; import { Command_Lisp } from "../Add-on/Lisp"; @@ -118,6 +119,7 @@ import { Command_TestBox } from "../Add-on/testEntity/TestBox"; import { TestCollision } from "../Add-on/testEntity/testCollision"; import { TestTargeOnCurve } from "../Add-on/testEntity/TestCurve"; import { TestFillet } from "../Add-on/testEntity/TestFilletCode"; +import { Command_DeleteTemplate } from "../Add-on/testEntity/TestTemplateDelete"; import { TestFb } from "../Add-on/TestFb"; import { Command_TestPointPickParse } from "../Add-on/TestPointPickParse"; import { Command_Trim } from "../Add-on/Trim"; @@ -129,10 +131,11 @@ import { CommandServer } from "../DatabaseServices/CommandServer"; import { AutoTempateSizeAction } from "../DatabaseServices/Template/TemplateTest"; import { ICommand } from "../UI/Components/CommandPanel/CommandList"; import { commandMachine } from "./CommandMachine"; -import { Command_DeleteTemplate } from "../Add-on/testEntity/TestTemplateDelete"; export function registerCommand() { + commandMachine.RegisterCommand("kjl", new Command_KJLImport()); + commandMachine.RegisterCommand("group", new Command_Group()); commandMachine.RegisterCommand("dxf", new Command_DXFImport());