You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
WebCAD/__test__/CFImport/CFImport.test.ts

315 lines
9.1 KiB

import { ParseCFDoc } from "../../src/Add-on/CF/Import/CFImport";
import { ICFBoard, ICFDoc, ICFEntityTree, ICFHardware, ICFTemplate, ICFTopline } from "../../src/Add-on/CF/Import/CFInterface";
import { ParseCFBoard } from "../../src/Add-on/CF/Parse/ParseBoard";
import { CFParseAddEntity, ParseEntityTree } from "../../src/Add-on/CF/Parse/ParseEntityTree";
import { ParseTemplate } from "../../src/Add-on/CF/Parse/ParseHardwareAndTemplate";
import { ParseCFTemplateInfo, ParseHardwareInfo, ParseToplineInfo } from "../../src/Add-on/CF/Parse/ParseHardwareAndTemplateInfo";
import { CADFiler } from "../../src/DatabaseServices/CADFiler";
import { Database } from "../../src/DatabaseServices/Database";
import { Board } from "../../src/DatabaseServices/Entity/Board";
import { BoardType } from "../../src/DatabaseServices/Entity/BoardInterface";
import { Entity } from "../../src/DatabaseServices/Entity/Entity";
import { Polyline } from "../../src/DatabaseServices/Entity/Polyline";
import { HardwareCompositeEntity } from "../../src/DatabaseServices/Hardware/HardwareCompositeEntity";
import { HardwareTopline } from "../../src/DatabaseServices/Hardware/HardwareTopline";
import { TemplateRecord } from "../../src/DatabaseServices/Template/TemplateRecord";
const testBr: ICFBoard = {
Name: "层板",
Type: "Board",
ColorIndex: 2,
RoomName: "板主卧",
CabinetName: "板标准柜",
Rot: [90, 0, 0],
Pos: [20, 0, 0],
ContourCurve: [
{ pt: [0, 0], bul: 0 },
{ pt: [0, 100], bul: 0 },
{ pt: [80, 100], bul: 0 },
{ pt: [80, 0], bul: 0 },
{ pt: [0, 0], bul: 0 }
],
Thickness: 18,
BrMatName: "默认板材5厘",
Matrial: "生态板",
Color: "经典檀木",
BrType: 1,
PositionType: 0,
OpenDir: 0,
Lines: 0,
BigHole: 0,
ComposingFace: 2,
UpSealed: "1.5",
DownSealed: "1.5",
LeftSealed: "1.5",
RightSealed: "1.5",
EachEdgeDrills: ["三合一", "三合一", "三合一", "三合一"],
Remarks: [["备注1", "434"], ["备注2", "434"]],
Holes: [
{
Type: "ExtrudeSolid",
ColorIndex: 2,
Pos: [10, 0, 0],
ContourCurve: [
{ pt: [0, 0], bul: 0 },
{ pt: [0, 20], bul: 0 },
{ pt: [10, 20], bul: 0 },
{ pt: [10, 0], bul: 0 },
]
}],
GroovesAddLength: 0,
GroovesAddWidth: 0,
GroovesAddDepth: 0,
KnifeRadius: 3,
};
const testBr2 = Object.assign({}, testBr);
testBr2.Pos = [150, 0, 0];
const testBr3 = Object.assign({}, testBr);
testBr2.Pos = [300, 0, 0];
const entityTree2: ICFEntityTree = {
Type: "EntityTree",
Name: "实体2",
Entitys: [
testBr3
],
};
const entityTree: ICFEntityTree = {
Type: "EntityTree",
Name: "实体1",
Childen: [
entityTree2,
],
Entitys: [
testBr2
]
};
const testTemp: ICFTemplate = {
Type: "Template",
TempalteId: "88888888",
BoxSize: [300, 100, 500],
RoomName: "模板主卧",
CabinetName: "模板标准柜",
Rot: [90, 0, 0],
Pos: [400, 0, 0],
ParamMap: [
{
name: "CZ",
value: "1137"
},
{
name: "QT",
value: "123"
}
]
};
const testHardware: ICFHardware = {
Type: "Hardware",
BoxSize: [50, 30, 60],
RoomName: "五金主卧",
CabinetName: "五金标准柜",
Name: "五金",
Unit: "个",
ActualExpr: "L*W*H",
Model: "X-1",
Factory: "厂家晨丰",
Brand: "品牌晨丰",
Spec: "{L*2}",
Comments: "五金备注",
IsHole: true,
Material: "五金材质",
Color: "五金颜色",
};
const testHardware2 = Object.assign({}, testHardware);
testHardware2.HardwarerId = "1111111";
const testTopline: ICFTopline = {
Type: "Topline",
BoxSize: [50, 30, 60],
RoomName: "顶线主卧",
CabinetName: "顶线标准柜",
Name: "顶线",
Unit: "个",
ActualExpr: "L*W*H",
Model: "X-1",
Factory: "厂家晨丰",
Brand: "品牌晨丰",
Spec: "{L*2}",
Comments: "顶线备注",
Contour: [
{ pt: [0, 0], bul: 0 },
{ pt: [0, 20], bul: 0 },
{ pt: [20, 20], bul: 0 },
{ pt: [20, 0], bul: 0 },
{ pt: [0, 0], bul: 0 },
],
PathCurve: [
{ pt: [0, 0], bul: 0 },
{ pt: [0, 100], bul: 0 },
{ pt: [100, 100], bul: 0 },
{ pt: [100, 0], bul: 0 },
{ pt: [0, 0], bul: 0 },
],
};
const testTopline2 = Object.assign({}, testTopline);
testTopline2.ToplineId = "222222";
jest.mock("../../src/Add-on/CF/Parse/ParseHardwareAndTemplate", () =>
{
return {
__esModule: true,
ParseTemplate: async (testTemp: ICFTemplate, cuDb: Database) =>
{
const newTemp = TestTemplate(cuDb);
await ParseCFTemplateInfo(newTemp, testTemp, cuDb);
return newTemp;
},
ParseHardware: async (testHardware: ICFHardware, cuDb: Database) =>
{
let newTemp: TemplateRecord;
if (testHardware.HardwarerId)
{
newTemp = TestTemplate(cuDb);
await ParseCFTemplateInfo(newTemp, testTemp, cuDb);
}
const hardware = await ParseHardwareInfo(newTemp, testHardware, cuDb);
return hardware;
},
ParseTopline: async (testTopline: ICFTopline, cuDb: Database) =>
{
let contour: Polyline;
if (testTopline.ToplineId)
{
contour = new Polyline().Rectangle(10, 10);
}
const topline = await ParseToplineInfo(contour, testTemp, cuDb);
return topline;
}
};
});
jest.mock("../../src/Add-on/CF/Import/CFImport", () =>
{
return {
__esModule: true,
async ParseCFDoc(document: ICFDoc, cuDb: Database)
{
for (const model of document.ModelSpace)
{
if (model.Type === "EntityTree")
{
await ParseEntityTree(model as ICFEntityTree, cuDb);
}
else
{
await CFParseAddEntity(model, cuDb);
}
}
}
};
});
test('晨丰导入板解析', () =>
{
const br = ParseCFBoard(testBr);
expect(br).toBeInstanceOf(Board);
const file = EntitysOut(br);
expect(file).toMatchSnapshot();
});
test('晨丰导入模板解析', async () =>
{
const cuDb = new Database();
const newTemp = await ParseTemplate(testTemp, cuDb);
expect(newTemp).toBeInstanceOf(TemplateRecord);
const data = cuDb.FileWrite().ToString();
expect(data).toMatchSnapshot();
});
test('晨丰导入五金解析', async () =>
{
const cuDb = new Database();
let newTemp: TemplateRecord;
const hardware = await ParseHardwareInfo(newTemp, testHardware, cuDb) as HardwareCompositeEntity;
expect(hardware).toBeInstanceOf(HardwareCompositeEntity);
const data = EntitysOut(hardware);
expect(data).toMatchSnapshot();
const cuDb2 = new Database();
let newTemp2: TemplateRecord = TestTemplate(cuDb2);
const hardware2 = await ParseHardwareInfo(newTemp2, testHardware2, cuDb2) as TemplateRecord;
expect(hardware2).toBeInstanceOf(TemplateRecord);
const data2 = cuDb2.FileWrite().ToString();
expect(data2).toMatchSnapshot();
});
test('晨丰导入顶线解析', async () =>
{
const cuDb = new Database();
let contour: Polyline;
const topline = await ParseToplineInfo(contour, testTopline, cuDb);
expect(topline).toBeInstanceOf(HardwareTopline);
const data = EntitysOut(topline);
expect(data).toMatchSnapshot();
let contour2 = new Polyline().Rectangle(10, 10);
const topline2 = await ParseToplineInfo(contour2, testTopline2, cuDb);
expect(topline2).toBeInstanceOf(HardwareTopline);
const data2 = EntitysOut(topline2);
expect(data2).toMatchSnapshot();
});
test('晨丰导入实体树解析', async () =>
{
const cuDb = new Database();
const temp = await ParseEntityTree(entityTree, cuDb) as TemplateRecord;
expect(cuDb.TemplateTable.Objects.length).toBe(2);
expect(temp).toBeInstanceOf(TemplateRecord);
expect((temp.Children[0].Object as TemplateRecord).Name).toBe(entityTree.Childen[0].Name);
});
test('晨丰导入CAD解析', async () =>
{
const cuDb = new Database();
let cfDoc = require("./CFDoc.json");
await ParseCFDoc(cfDoc, cuDb);
const data = cuDb.FileWrite().ToString();
expect(data).toMatchSnapshot();
});
function TestTemplate(cuDb: Database): TemplateRecord
{
const newTemp = new TemplateRecord().InitBaseParams();
const board = Board.CreateBoard(100, 100, 100, BoardType.Layer);
const hardware = new HardwareCompositeEntity();
hardware.Entitys.push(board);
cuDb.ModelSpace.Append(hardware);
cuDb.ModelSpace.Append(board);
newTemp.Objects.push(board.Id, hardware.Id);
newTemp.SetParamExpr("CZ", "1137");
newTemp.SetParamExpr("QT", "123");
newTemp.UpdateTemplateTree();
return newTemp;
}
export function EntitysOut(en: Entity, callback?: (en) => void)
{
let vf = new CADFiler();
en.WriteFile(vf);
return JSON.stringify(vf.Data);
}