mirror of https://gitee.com/cf-fz/WebCAD.git
parent
eddeac7c1a
commit
2438a0dd92
@ -0,0 +1,300 @@
|
||||
/*
|
||||
关于webCAD 文件系统的原型.
|
||||
包括了对象的保存,撤销,还原.
|
||||
|
||||
对象的绘制 更新.
|
||||
|
||||
对象ID更新 引用
|
||||
|
||||
材质字典
|
||||
|
||||
关于对象的撤销和重做设计.
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
class CADObject
|
||||
{
|
||||
|
||||
ApplyUndoData(data: any)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 表示了一个图纸该有的数据结构
|
||||
*
|
||||
* @class Database
|
||||
*/
|
||||
class Database extends CADObject
|
||||
{
|
||||
|
||||
private curUndoIndex = -1;
|
||||
//命令撤销数据列表
|
||||
commandUndoData: { cmdName: string, undoData: Map<CADObject, UndoFile> }[] = [];
|
||||
|
||||
|
||||
get UndoData(): Map<CADObject, UndoFile>
|
||||
{
|
||||
if (this.commandUndoData.length == 0)
|
||||
{
|
||||
this.startCmd("");
|
||||
}
|
||||
return this.commandUndoData[this.commandUndoData.length - 1].undoData;
|
||||
}
|
||||
|
||||
startCmd(cmdName: string)
|
||||
{
|
||||
if (this.commandUndoData.length > 0 && this.curUndoIndex != this.commandUndoData.length - 1)
|
||||
{
|
||||
this.commandUndoData.splice(this.curUndoIndex, this.commandUndoData.length - this.curUndoIndex);
|
||||
}
|
||||
|
||||
this.commandUndoData.push({ cmdName: "", undoData: new Map<CADObject, UndoFile>() });
|
||||
this.curUndoIndex = this.commandUndoData.length - 1;
|
||||
}
|
||||
|
||||
Undo(cout: number)
|
||||
{
|
||||
if (this.curUndoIndex == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (let [obj, data] of this.commandUndoData[this.curUndoIndex].undoData)
|
||||
{
|
||||
for (let i = data.m_UndoDataList.length; i--;)
|
||||
{
|
||||
let d = data.m_UndoDataList[i];
|
||||
obj.ApplyUndoData(d);
|
||||
}
|
||||
}
|
||||
|
||||
this.curUndoIndex--;
|
||||
}
|
||||
Redo(cout: number)
|
||||
{
|
||||
for (let i = this.curUndoIndex + 1; i < this.commandUndoData.length; i++)
|
||||
{
|
||||
for (let [obj, data] of this.commandUndoData[i].undoData)
|
||||
{
|
||||
for (let d of data.m_UndoDataList)
|
||||
{
|
||||
obj.ApplyUndoData(d);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let arr = [0, 1, 2, 3, 4, 5, 6];
|
||||
arr.splice(3, arr.length - 3);
|
||||
|
||||
arr
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @class UndoFile
|
||||
*/
|
||||
class UndoFile
|
||||
{
|
||||
m_UndoDataList = [];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
enum EntityUndoType
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
interface EntityData
|
||||
{
|
||||
id: number;
|
||||
isErase: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 所有图元的基类
|
||||
*
|
||||
* @class Entity
|
||||
*/
|
||||
class Entity extends CADObject
|
||||
{
|
||||
protected m_Data: EntityData;
|
||||
m_Db: Database;
|
||||
constructor()
|
||||
{
|
||||
super();
|
||||
this.m_Data = {
|
||||
id: -1,
|
||||
isErase: false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
get UndoFile(): UndoFile
|
||||
{
|
||||
if (this.m_Db)
|
||||
{
|
||||
let undoData = this.m_Db.UndoData;
|
||||
if (!undoData.has(this))
|
||||
{
|
||||
undoData.set(this, new UndoFile());
|
||||
}
|
||||
return undoData.get(this);
|
||||
}
|
||||
}
|
||||
|
||||
WriteUndoData(data: any)
|
||||
{
|
||||
let undoFile = this.UndoFile;
|
||||
if (undoFile)
|
||||
{
|
||||
undoFile.m_UndoDataList.push(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 直线撤销类型
|
||||
*
|
||||
* @enum {number}
|
||||
*/
|
||||
enum LineUndoType
|
||||
{
|
||||
Full = 0,
|
||||
StartPoint = 1,
|
||||
EndPoint = 2,
|
||||
}
|
||||
|
||||
//
|
||||
interface LineData extends EntityData
|
||||
{
|
||||
startPoint: number[],
|
||||
endPoint: number[]
|
||||
}
|
||||
|
||||
/**
|
||||
* 直线对象
|
||||
*
|
||||
* @class Line
|
||||
*/
|
||||
class Line extends Entity
|
||||
{
|
||||
protected m_Data: LineData;
|
||||
constructor()
|
||||
{
|
||||
super();
|
||||
this.m_Data.startPoint = [0, 0, 0];
|
||||
this.m_Data.endPoint = [0, 0, 0];
|
||||
}
|
||||
get StartPoint(): number[]
|
||||
{
|
||||
return this.m_Data.startPoint;
|
||||
}
|
||||
set StartPoint(value: number[])
|
||||
{
|
||||
this.WriteUndoData({ type: LineUndoType.StartPoint, data: { startPoint: this.m_Data.startPoint } });
|
||||
this.m_Data.startPoint = value;
|
||||
}
|
||||
|
||||
ApplyUndoData(data: any)
|
||||
{
|
||||
switch (data.type)
|
||||
{
|
||||
case LineUndoType.Full:
|
||||
break;
|
||||
case LineUndoType.StartPoint:
|
||||
this.m_Data.startPoint = data.data.startPoint;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 图纸绘制适配器,使用适配器绘制对象图元.
|
||||
*
|
||||
* @class EentiyDrawAdapter
|
||||
*/
|
||||
class EentiyDrawAdapter
|
||||
{
|
||||
Draw(): THREE.Object3D
|
||||
{
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 直线的绘制适配
|
||||
*
|
||||
* @class LineAdapter
|
||||
* @extends {EentiyDrawAdapter}
|
||||
*/
|
||||
class LineAdapter extends EentiyDrawAdapter
|
||||
{
|
||||
Draw(): THREE.Object3D
|
||||
{
|
||||
return undefined;
|
||||
}
|
||||
|
||||
Update(type: number)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case LineUndoType.Full:
|
||||
break;
|
||||
case LineUndoType.StartPoint:
|
||||
break;
|
||||
case LineUndoType.EndPoint:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let line = new Line();
|
||||
|
||||
console.log(line);
|
||||
|
||||
line.StartPoint = [1, 2, 3];
|
||||
|
||||
console.log(line);
|
||||
|
||||
|
||||
let db = new Database();
|
||||
|
||||
line.m_Db = db;
|
||||
|
||||
|
||||
line.StartPoint = [3, 3, 3];
|
||||
|
||||
console.log(line);
|
||||
line.StartPoint = [3, 3, 5];
|
||||
|
||||
line.StartPoint = [12, 3, 5];
|
||||
console.log(line);
|
||||
|
||||
console.log(db.commandUndoData);
|
||||
|
||||
db.Undo(1);
|
||||
|
||||
console.log(line);
|
||||
|
||||
db.Redo(1);
|
||||
|
||||
console.log(line.StartPoint);
|
Loading…
Reference in new issue