pull/7/head
ChenX 7 years ago
parent 2a9677a89f
commit 1419864b79

@ -29,65 +29,55 @@ import * as THREE from 'three';
? ?
db.AppendEntity(ent); db.AppendEntity(ent);
,.
,
*/ */
/** //所有cad对象的基类
* cad
*
* @class CADObject
*/
class CADObject class CADObject
{ {
/**
* //对象是否已经被删除
* private isErase: boolean = false;
*
* @param {number} type get IsErase(): boolean
* @param {*} data
* @memberof CADObject
*/
ApplyPartialUndo(type: number, data: any)
{ {
return this.isErase;
} }
DataIn(data) //-----------------------------File-----------------------------
{ //对象应该实现dataIn和DataOut的方法,为了对象的序列化和反序列化
//对象从文件中读取数据,初始化自身
FileIn(file: CADFile)
{
} }
DataOut() //对象将自身数据写入到文件.
FileOut(file: CADFile)
{
}
//局部撤销
ApplyPartialUndo(file: CADFile)
{ {
} }
} }
/** //cad文件数据
* cad
*
* @class CADFile
*/
class CADFile class CADFile
{ {
databaseData; //图纸数据
objectDataCol: any[]; //对象集合
}
class HistoricManage
{
curIndex: number; //当前执行位置
historicRecord: CommandHistoricRecord[];//历史记录
} }
//命令历史记录集合 //命令历史记录集合
class CommandHistoricRecord class CommandHistoricRecord extends CADObject
{ {
//命令名称 //命令名称
commandName: string; commandName: string;
//历史记录表 //历史记录表
historicCol: CommandHistoricRecord[]; historicCol: HistoricRecord[];
} }
//历史记录 //历史记录
@ -98,102 +88,114 @@ class HistoricRecord
redoData: any; redoData: any;
} }
/**
*
*
* @class HistoryManager
*/
class HistoryManager extends CADObject
{
private curUndoIndex = -1;
//命令撤销数据列表
commandUndoData: { cmdName: string, undoData: Map<CADObject, UndoFile> }[] = [];
get UndoData(): Map<CADObject, UndoFile> class HistoricManage
{
m_Db: Database;
curIndex: number = -1; //当前执行位置
historicRecord: CommandHistoricRecord[] = [];//历史记录
get UndoData(): CommandHistoricRecord
{ {
if (this.commandUndoData.length == 0) if (this.historicRecord.length == 0)
{ {
this.startCmd(""); this.startCmd("");
} }
return this.commandUndoData[this.commandUndoData.length - 1].undoData; return this.historicRecord[this.historicRecord.length - 1];
} }
startCmd(cmdName: string) startCmd(cmdName: string)
{ {
this.commandUndoData.splice(this.curUndoIndex, this.commandUndoData.length - this.curUndoIndex); this.historicRecord.splice(this.curIndex, this.historicRecord.length - this.curIndex);
this.commandUndoData.push({ cmdName: "", undoData: new Map<CADObject, UndoFile>() }); this.historicRecord.push(new CommandHistoricRecord());
this.curUndoIndex = this.commandUndoData.length - 1; this.curIndex = this.historicRecord.length - 1;
} }
Undo(cout: number) Undo()
{ {
let undoFile = this.commandUndoData[this.curUndoIndex]; let historicRec = this.historicRecord[this.curIndex];
if (!undoFile) if (!historicRec)
{ {
return; return;
} }
for (let [obj, data] of undoFile.undoData) let recList = historicRec.historicCol;
for (let i = recList.length; i--;)
{ {
for (let i = data.m_UndoDataList.length; i--;) let data = recList[i];
{ let obj = this.m_Db.getObject(data.objectId);
let d = data.m_UndoDataList[i]; obj.ApplyPartialUndo(data.undoData);
obj.ApplyPartialUndo(d.type, d.undoData);
}
} }
this.curUndoIndex--; this.curIndex--;
} }
Redo(cout: number) Redo(cout: number)
{ {
let undoFile = this.commandUndoData[this.curUndoIndex + 1]; let undoData = this.historicRecord[this.curIndex + 1];
if (!undoFile) if (!undoData)
{ {
return; return;
} }
for (let [obj, data] of undoFile.undoData) for (let rec of undoData.historicCol)
{ {
for (let d of data.m_UndoDataList) let obj = this.m_Db.getObject(rec.objectId);
{ obj.ApplyPartialUndo(rec.redoData);
obj.ApplyPartialUndo(d.type, d.redoData);
}
} }
this.curUndoIndex++; this.curIndex++;
} }
} }
class Database
{
class Database extends CADObject
{
className: "Database";
hm: HistoricManage;
//块表记录 //块表记录
blockTableCol: BlockTableRecord[]; blockTableCol: BlockTableRecord[];
//材质字典 //材质字典
}
objectCol = new Map<number, CADObject>();
constructor()
{
super();
this.objectCol.set(0, this);
}
getObject(id: number): CADObject
{
return this.objectCol.get(id);
}
class BlockTableRecord extends CADObject appendObject(obj: CADObject)
{ {
//对象池. 管理所有的对象 let id = this.objectCol.size;
objectMap = new Map<number, CADObject>(); this.objectCol.set(id, obj);
}
/** this.hm.UndoData.historicCol.push(
* {
* objectId: 0,
* @class UndoFile undoData: { type: 1, i: id },//remove
*/ redoData: { type: 2, i: id },//add
class UndoFile }
{ )
m_UndoDataList: UndoData[] = []; }
ApplyPartialUndo(data)
{
switch (data)
{
case 1:
// this.objectCol.get(data.i).IsErase = true;
break;
case 2:
// this.objectCol.get(data.i).isErase = false;
break;
}
}
} }
interface UndoData
class BlockTableRecord extends CADObject
{ {
type: number; objectMap = new Map<number, CADObject>();
undoData: any;
redoData: any;
userData?: any;
} }
@ -218,38 +220,10 @@ interface EntityData
*/ */
class Entity extends CADObject class Entity extends CADObject
{ {
protected m_Data: EntityData; m_Db: Database;
m_Db: HistoryManager;
constructor() constructor()
{ {
super(); 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: UndoData)
{
let undoFile = this.UndoFile;
if (undoFile)
{
undoFile.m_UndoDataList.push(data);
}
} }
} }
@ -280,35 +254,17 @@ interface LineData extends EntityData
*/ */
class Line extends Entity class Line extends Entity
{ {
protected m_Data: LineData;
constructor() constructor()
{ {
super(); 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,
undoData: { startPoint: this.m_Data.startPoint },
redoData: { startPoint: value }
});
this.m_Data.startPoint = value;
} }
ApplyPartialUndo(data)
ApplyPartialUndo(type, data)
{ {
switch (type) switch (data.type)
{ {
case LineUndoType.Full: case LineUndoType.Full:
break; break;
case LineUndoType.StartPoint: case LineUndoType.StartPoint:
this.m_Data.startPoint = data.startPoint;
break; break;
default: default:
break; break;
@ -357,88 +313,4 @@ class LineAdapter extends EentiyDrawAdapter
break; break;
} }
} }
} }
let line = new Line();
console.log(line);
line.StartPoint = [1, 2, 3];
console.log(line);
let db = new HistoryManager();
line.m_Db = db;
line.StartPoint = [3, 3, 3];
line.StartPoint = [3, 3, 5];
line.StartPoint = [12, 3, 5];
db.Undo(1);
console.log(line.StartPoint);
db.Redo(1);
db.startCmd("");
console.log(line.StartPoint);
function per()
{
for (let i = 0; i < 5000; i++)
{
new Line();
}
}
function per2()
{
for (let i = 0; i < 5000; i++)
{
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);
db.startCmd("");
// console.log(line.StartPoint);
}
}
// per();/*?.*/
// per2();/*?.*/
// per2();/*?.*/
// per2();/*?.*/
// per2();/*?.*/
// // console.log(3);
let undoManage = new HistoricManage();
let l = new Line();
let ms = new BlockTableRecord();
Loading…
Cancel
Save