mirror of https://gitee.com/cf-fz/WebCAD.git
commit
f936c0c5df
@ -0,0 +1,14 @@
|
||||
import { Command } from "../Editor/CommandMachine";
|
||||
import { Board } from "../DatabaseServices/Board";
|
||||
import { app } from "../ApplicationServices/Application";
|
||||
|
||||
|
||||
export class Command_DrawBoard2 implements Command
|
||||
{
|
||||
async exec()
|
||||
{
|
||||
let br = new Board();
|
||||
br.InitBoard(1.200, 0.600, 0.018);
|
||||
app.m_Database.ModelSpace.Append(br);
|
||||
}
|
||||
}
|
@ -0,0 +1,174 @@
|
||||
import { ExtrudeGeometry, Matrix4, Mesh, Object3D, Vector3 } from 'three';
|
||||
|
||||
import { CoordinateSystem } from '../Geometry/CoordinateSystem';
|
||||
import { RenderType } from '../GraphicsSystem/Enum';
|
||||
import { Factory } from './CADFactory';
|
||||
import { CADFile } from './CADFile';
|
||||
import { CADObject } from './CADObject';
|
||||
import { Entity } from './Entity';
|
||||
import { Line } from './Line';
|
||||
import { DbPhysicalMaterial } from './PhysicalMaterial';
|
||||
import { Region } from './Region';
|
||||
|
||||
|
||||
/**
|
||||
* 板件实体
|
||||
*
|
||||
* @class Board
|
||||
* @extends {Entity}
|
||||
*/
|
||||
@Factory
|
||||
export class Board extends Entity
|
||||
{
|
||||
//板件厚度
|
||||
private m_Height: number = 18;
|
||||
private m_Region: Region;
|
||||
|
||||
//自身坐标系
|
||||
private m_Matrix: Matrix4 = new Matrix4();//标准的世界坐标系
|
||||
constructor(reg?: Region, height?: number)
|
||||
{
|
||||
super();
|
||||
|
||||
this.m_Region = reg;
|
||||
this.m_Height = height || this.m_Height;
|
||||
}
|
||||
|
||||
//初始化板件 来自长宽高
|
||||
InitBoard(length: number, width: number, height: number)
|
||||
{
|
||||
this.m_Height = height;
|
||||
let px1 = new Vector3(0, 0, 0);
|
||||
let px3 = new Vector3(length, width, 0);
|
||||
let px2 = new Vector3(px3.x, px1.y);
|
||||
let px4 = new Vector3(px1.x, px3.y);
|
||||
|
||||
let l1 = new Line();
|
||||
let l2 = new Line();
|
||||
let l3 = new Line();
|
||||
let l4 = new Line();
|
||||
|
||||
l1.StartPoint = px1;
|
||||
l1.EndPoint = px2;
|
||||
|
||||
l2.StartPoint = px2;
|
||||
l2.EndPoint = px3;
|
||||
|
||||
l3.StartPoint = px3;
|
||||
l3.EndPoint = px4;
|
||||
|
||||
l4.StartPoint = px1;
|
||||
l4.EndPoint = px4;
|
||||
|
||||
this.m_Region = new Region([l1, l2, l3, l4]);
|
||||
|
||||
this.Update();
|
||||
}
|
||||
|
||||
ApplyMatrix(m: Matrix4)
|
||||
{
|
||||
this.WriteAllObjectRecord();
|
||||
let coor = new CoordinateSystem();
|
||||
coor.copyForm(this.m_Matrix);
|
||||
coor.applyMatrix4(m);
|
||||
|
||||
this.m_Matrix.copy(coor.getMatrix4());
|
||||
|
||||
this.Update();
|
||||
}
|
||||
|
||||
Draw(renderType: RenderType): Object3D
|
||||
{
|
||||
let obj = super.Draw(renderType);
|
||||
if (obj) return obj;
|
||||
|
||||
if (!this.m_Region)
|
||||
{
|
||||
//避免空的板件生成
|
||||
this.InitBoard(1e-5, 1e-5, 1e-5);
|
||||
}
|
||||
|
||||
let extrudeSettings = {
|
||||
steps: 1,
|
||||
bevelEnabled: false,
|
||||
amount: this.m_Height
|
||||
};
|
||||
let extGeo = new ExtrudeGeometry(this.m_Region.Shape, extrudeSettings);
|
||||
let mesh = new Mesh(extGeo);
|
||||
mesh.userData = this;
|
||||
|
||||
if (this.m_MaterialId && this.m_MaterialId.Object)
|
||||
{
|
||||
let material = this.m_MaterialId.Object as DbPhysicalMaterial;
|
||||
mesh.material = material.Material;
|
||||
}
|
||||
|
||||
this.m_DrawEntity.set(renderType, mesh);
|
||||
|
||||
this.Update();
|
||||
|
||||
return mesh;
|
||||
}
|
||||
|
||||
Update()
|
||||
{
|
||||
super.Update();
|
||||
for (let [, obj] of this.m_DrawEntity)
|
||||
{
|
||||
let mesh = obj as Mesh;
|
||||
mesh.geometry.dispose();
|
||||
|
||||
let extrudeSettings = {
|
||||
steps: 1,
|
||||
bevelEnabled: false,
|
||||
amount: this.m_Height
|
||||
};
|
||||
mesh.geometry = new ExtrudeGeometry(this.m_Region.Shape, extrudeSettings);
|
||||
mesh.geometry.computeBoundingSphere();
|
||||
|
||||
if (this.m_MaterialId)
|
||||
{
|
||||
let material = this.m_MaterialId.Object as DbPhysicalMaterial;
|
||||
mesh.material = material.Material;
|
||||
}
|
||||
for (let [, obj] of this.m_DrawEntity)
|
||||
{
|
||||
let mesh = obj as Mesh;
|
||||
|
||||
let p = new Vector3().setFromMatrixColumn(this.m_Matrix, 3);
|
||||
obj.position.copy(p);
|
||||
obj.quaternion.setFromRotationMatrix(this.m_Matrix);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//#region -------------------------File-------------------------
|
||||
//对象应该实现dataIn和DataOut的方法,为了对象的序列化和反序列化
|
||||
|
||||
//对象从文件中读取数据,初始化自身
|
||||
ReadFile(file: CADFile)
|
||||
{
|
||||
super.ReadFile(file);
|
||||
let ver = file.Read();
|
||||
this.m_Region = file.ReadObject(this._db) as Region;
|
||||
this.m_Height = file.Read();
|
||||
this.m_Matrix.fromArray(file.Read())
|
||||
this.Update();
|
||||
}
|
||||
//对象将自身数据写入到文件.
|
||||
WriteFile(file: CADFile)
|
||||
{
|
||||
super.WriteFile(file);
|
||||
file.Write(1);
|
||||
file.WriteObject(this.m_Region);
|
||||
file.Write(this.m_Height);
|
||||
file.Write(this.m_Matrix.toArray())
|
||||
}
|
||||
//局部撤销
|
||||
ApplyPartialUndo(undoData: CADObject)
|
||||
{
|
||||
super.ApplyPartialUndo(undoData);
|
||||
}
|
||||
//#endregion
|
||||
}
|
Loading…
Reference in new issue