酷家乐数据导入

pull/478/MERGE
ChenX 5 years ago
parent b8f6d74266
commit 1294f6b2de

@ -1,14 +1,17 @@
import { Euler, Matrix4, Vector2, Vector3 } from "three";
import { app } from "../ApplicationServices/Application";
import { EBoardKeyList } from "../Common/BoardKeyList";
import { FileSystem } from "../Common/FileSystem";
import { FixIndex, ToFixed } from "../Common/Utils";
import { Board, BoardType } from "../DatabaseServices/Entity/Board";
import { ExtrudeSolid } from "../DatabaseServices/Entity/Extrude";
import { Polyline, PolylineProps } from "../DatabaseServices/Entity/Polyline";
import { TemplateRecord } from "../DatabaseServices/Template/TemplateRecord";
import { Command } from "../Editor/CommandMachine"; import { Command } from "../Editor/CommandMachine";
import { Vec2 } from "../Geometry/CheckIntersect"; import { Vec2 } from "../Geometry/CheckIntersect";
import { AsVector2, equaln } from "../Geometry/GeUtils";
import { Vec3 } from "../Geometry/IVec3"; import { Vec3 } from "../Geometry/IVec3";
import { HotCMD } from "../Hot/HotCommand"; 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 enum KJL_ModelType
@ -24,6 +27,8 @@ interface KJL_ParamModel
{ {
modelTypeId: KJL_ModelType; modelTypeId: KJL_ModelType;
modelName: string; modelName: string;
roomId: string;
modelBrandGoodName: string;
parameters: []; parameters: [];
position: Vec3; position: Vec3;
@ -64,9 +69,30 @@ interface KJL_ParamModel_Board extends KJL_ParamModel
}; };
} }
interface KJL_DesignData
{
roomDataList: {
roomId: string;
name: string;
}[];
}
interface KJL_JsonFile interface KJL_JsonFile
{ {
paramModel: KJL_ParamModel[]; paramModel: KJL_ParamModel[];
designData: KJL_DesignData;
}
class FuzzPoint
{
private map: { [key: string]: Vec2 } = {};
GetVector(v: Vec2)
{
let key = `${ToFixed(v.x, 2)},${ToFixed(v.y, 2)}`;
if (key in this.map) return this.map[key];
else this.map[key] = v;
return v;
}
} }
@HotCMD @HotCMD
@ -79,21 +105,147 @@ export class Command_KJLImport implements Command
{ {
let f = flist.item(0); let f = flist.item(0);
let fstr = await FileSystem.ReadFileAsText(f); let fstr = await FileSystem.ReadFileAsText(f);
let json = JSON.parse(fstr) as KJL_JsonFile; let fileData = JSON.parse(fstr) as KJL_JsonFile;
json.paramModel.forEach(ParseModel); let roomMap = ParseRoomNameMap(fileData.designData);
for (let m of fileData.paramModel)
{
let roomName = roomMap.get(m.roomId);
let gName = m.modelBrandGoodName;
ParseModel(m, roomName, gName);
}
} }
} }
} }
function ParseModel(model: KJL_ParamModel) function ParseRoomNameMap(d: KJL_DesignData): Map<string, string>
{ {
let map = new Map<string, string>();
for (let r of d.roomDataList)
{
map.set(r.roomId, r.name);
}
return map;
}
function ParseModel(model: KJL_ParamModel, roomName: string, gName: string, parentMatrix?: Matrix4): Board | TemplateRecord
{
let mtx = new Matrix4().makeRotationFromEuler(new Euler(model.rotate.x, model.rotate.y, model.rotate.z, "ZYX"))
.setPosition(model.position.x, model.position.y, model.position.z);
if (model.modelTypeId === KJL_ModelType.BaseModel) if (model.modelTypeId === KJL_ModelType.BaseModel)
{ {
let bmodel = model as KJL_ParamModel_Board; let bmodel = model as KJL_ParamModel_Board;
if (!bmodel.paramPlankPath) if (!bmodel.paramPlankPath)
return; return;
let pts = bmodel.paramPlankPath.path.resultPoints.map(AsVector2);
let radiuss = bmodel.paramPlankPath.path.resultLines.map(d => let pls = ParsePathOutlineAndHole(bmodel.paramPlankPath.path);
let br = new Board();
br.Name = model.modelName;
br.BoardProcessOption[EBoardKeyList.RoomName] = roomName;
br.BoardProcessOption[EBoardKeyList.CabinetName] = gName;
br.Thickness = bmodel.thickness;
br.ContourCurve = pls[0];
for (let i = 1; i < pls.length; i++)
{
let ext = new ExtrudeSolid();
ext.ContourCurve = pls[i];
ext.Thickness = bmodel.thickness;
br.AppendGroove(ext);
}
br.GrooveCheckAll([]);
let n = new Vector3().setFromMatrixColumn(mtx, 2).toArray();
for (let i = 0; i < n.length; i++)
{
if (!equaln(n[i], 0, 1e-3))
{
switch (i)
{
case 0:
br.BoardType = BoardType.Vertical;
br.ColorIndex = 11;
break;
case 1:
br.BoardType = BoardType.Behind;
br.ColorIndex = 3;
break;
case 2:
br.BoardType = BoardType.Layer;
br.ColorIndex = 2;
break;
}
break;
}
}
if (parentMatrix)
mtx.premultiply(parentMatrix);
br.OCS = new Matrix4().setPosition(0, 0, bmodel.thickness * -0.5).premultiply(mtx);
app.Database.ModelSpace.Append(br);
return br;
}
else
{
if (parentMatrix)
mtx.premultiply(parentMatrix);
}
if (model.subModels)
{
let brs: Board[] = [];
let templates: TemplateRecord[] = [];
for (let m of model.subModels)
{
let obj = ParseModel(m, roomName, gName, mtx);
if (obj)
{
if (obj instanceof Board)
{
if (model.modelBrandGoodName !== gName)
obj.Name = model.modelBrandGoodName;
brs.push(obj);
}
else if (obj instanceof TemplateRecord)
{
templates.push(obj);
}
}
}
if (brs.length > 0)
{
let template = new TemplateRecord().InitBaseParams();
template.Name = model.modelName;
app.Database.TemplateTable.Add(template);
template.Objects.push(...brs.map(br => br.Id));
return template;
}
else if (templates.length > 0)
{
let template = new TemplateRecord().InitBaseParams();
template.Name = model.modelName;
app.Database.TemplateTable.Add(template);
template.Children.push(...templates.map(t => t.Id));
return template;
}
}
}
/**
* Path
* @param path
* @returns
*/
function ParsePathOutlineAndHole(path: KJL_Path): Polyline[]
{
let fuzzP = new FuzzPoint();
let ptsAll = path.resultPoints.map(p => fuzzP.GetVector(p));
let radiusAll = path.resultLines.map(d =>
{ {
if (d.type === KJL_LineType.Line) if (d.type === KJL_LineType.Line)
return 0; return 0;
@ -102,11 +254,60 @@ function ParseModel(model: KJL_ParamModel)
let radius = d.radius; let radius = d.radius;
if (d.clockwise) if (d.clockwise)
radius = -radius; radius = -radius;
return radius; return radius;
} }
}); });
let polylines: Polyline[] = [];
while (ptsAll.length > 1)
{
let pts: Vector2[];
let rads: number[];
let p0 = ptsAll[0];
for (let i = 1; i < ptsAll.length; i++)
{
if (ptsAll[i] === p0)
{
pts = ptsAll.splice(0, i + 1).map(AsVector2);
rads = radiusAll.splice(0, i + 1);
pts.length = i + 1;
rads.length = i + 1;
break;
}
}
if (pts === undefined)
{
pts = ptsAll.map(AsVector2);
rads = radiusAll;
ptsAll = [];
}
if (pts.length > 0)
{
let lined: PolylineProps[] = [];
let buls = Radius2Buls(pts, rads);
for (let i = 0; i < pts.length; i++)
lined.push({ pt: pts[i], bul: buls[i] });
let pl = new Polyline(lined);
pl.CloseMark = true;
polylines.push(pl);
}
else
break;
}
return polylines;
}
/**
*
* @param pts
* @param radiuss
* @returns buls
*/
function Radius2Buls(pts: Vector2[], radiuss: number[]): number[]
{
let buls: number[] = []; let buls: number[] = [];
for (let i = 0; i < radiuss.length; i++) for (let i = 0; i < radiuss.length; i++)
@ -122,28 +323,5 @@ function ParseModel(model: KJL_ParamModel)
buls.push(bul); buls.push(bul);
} }
} }
return buls;
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);
} }

@ -1,5 +1,4 @@
import { Box3, Math, Matrix4, Vector3 } from "three"; import { Box3, Math, Matrix4, Vector3 } from "three";
import { arrayRemoveOnce } from "../../Common/ArrayExt";
import { AutoRecord, ISPROXYKEY } from "../AutoRecord"; import { AutoRecord, ISPROXYKEY } from "../AutoRecord";
import { Factory } from "../CADFactory"; import { Factory } from "../CADFactory";
import { CADFiler } from "../CADFiler"; import { CADFiler } from "../CADFiler";

Loading…
Cancel
Save