新增id 重构大量代码

pull/7/head
ChenX 7 years ago
parent 533ea55973
commit 6735ffa171

@ -96,6 +96,9 @@ cmdName:"Ohther" //假设这个是一个复杂度很高的命令
ObjectId.
id..
:id.
*/
@ -106,13 +109,14 @@ id只有在一个情况下才会更新.跨文档复制时.
class CADFactory
{
private constructor() { }
private objectNameMap = new Map<string, any>();
private objectNameMap = new Map<string, () => CADObject>();
private static factory = new CADFactory();
static RegisterObject(C)
{
let obj = new C();
let obj = new C() as CADObject;
this.factory.objectNameMap.set(obj.ClassName.toUpperCase(), () => new C());
}
static CreateObject(name: string): CADObject
{
let createF = this.factory.objectNameMap.get(name.toUpperCase());
@ -126,21 +130,44 @@ class CADFactory
class CADObject
{
//#region -------------------------DB-------------------------
protected db: Database;
//对象是否已经被删除
get Db(): Database
{
return this.db;
}
SetDb(db: Database)
{
if (!this.db)
{
this.db = db;
this.objectId = db.AllocateId(this);
}
else
{
console.warn("同一个对象无法重复设置到数据库中!");
}
}
//#endregion
//#region -------------------------isErase-------------------------
protected isErase: boolean = false;
get IsErase(): boolean
{
return this.isErase;
}
//#endregion
//#region -------------------------id-------------------------
protected objectId: ObjectId;
get Id(): ObjectId
{
return this.objectId;
}
get IsErase(): boolean
{
return this.isErase;
}
//#endregion
//-----------------------------File-----------------------------
//#region -------------------------File-------------------------
//对象应该实现dataIn和DataOut的方法,为了对象的序列化和反序列化
//类名,保证序列化时得到正确的new
@ -161,14 +188,23 @@ class CADObject
ApplyPartialUndo(file: CADFile)
{
}
//-----------------------------File End-----------------------------
//#endregion
}
class ObjectId
{
private id: number;
private constructor() { }
private obj: CADObject;
constructor(id: number, obj: CADObject)
{
this.id = id;
this.obj = obj;
}
get Object(): CADObject
{
return this.obj;
}
}
//cad文件数据
@ -177,12 +213,36 @@ class CADFile
}
//命令历史记录集合
class CommandHistoricRecord extends CADObject
class CommandHistoryRecord extends CADObject
{
constructor(cmdName: string)
{
super();
this.commandName = cmdName;
}
//命令名称
commandName: string;
private commandName: string;
//历史记录表
historicCol: Map<number, HistoricRecord[]>;
private historyCol = new Map<ObjectId, HistoricRecord[]>();
get HistoryList(): Map<ObjectId, HistoricRecord[]>
{
return this.historyCol;
}
private GetObjectHistoryList(id: ObjectId)
{
if (!this.historyCol.has(id))
{
this.historyCol.set(id, []);
}
return this.historyCol.get(id);
}
//对象写入数据
WriteObjectHistoryPath(obj: CADObject, history: HistoricRecord)
{
this.GetObjectHistoryList(obj.Id).push(history);
}
}
//历史记录
@ -198,40 +258,40 @@ class HistoricRecord extends CADObject
class HistoricManage extends CADObject
{
private curIndex: number = -1; //当前执行位置
private historicRecord: CommandHistoricRecord[] = [];//历史记录
private historyRecord: CommandHistoryRecord[] = []; //历史记录
constructor(db: Database)
{
super();
this.db = db;
}
get UndoData(): CommandHistoricRecord
get UndoData(): CommandHistoryRecord
{
if (this.historicRecord.length === 0)
if (this.historyRecord.length === 0)
{
this.StartCmd("");
}
return this.historicRecord[this.historicRecord.length - 1];
return this.historyRecord[this.historyRecord.length - 1];
}
StartCmd(cmdName: string)
{
this.historicRecord.splice(this.curIndex, this.historicRecord.length - this.curIndex);
this.historicRecord.push(new CommandHistoricRecord());
this.curIndex = this.historicRecord.length - 1;
this.historyRecord.splice(this.curIndex, this.historyRecord.length - this.curIndex);
this.historyRecord.push(new CommandHistoryRecord(cmdName));
this.curIndex = this.historyRecord.length - 1;
}
Undo(): boolean
{
let historicRec = this.historicRecord[this.curIndex];
if (!historicRec)
let historyRec = this.historyRecord[this.curIndex];
if (!historyRec)
{
return false;
}
for (let [id, recList] of historicRec.historicCol)
for (let [id, recList] of historyRec.HistoryList)
{
let obj = this.db.getObject(id);
let obj = id.Object;
for (let rec of recList)
{
obj.ApplyPartialUndo(rec.undoData);
@ -243,14 +303,14 @@ class HistoricManage extends CADObject
}
Redo()
{
let historicRec = this.historicRecord[this.curIndex + 1];
if (!historicRec)
let historyRec = this.historyRecord[this.curIndex + 1];
if (!historyRec)
{
return false;
}
for (let [id, recList] of historicRec.historicCol)
for (let [id, recList] of historyRec.HistoryList)
{
let obj = this.db.getObject(id);
let obj = id.Object;
for (let i = recList.length; i--;)
{
obj.ApplyPartialUndo(recList[i].redoData);
@ -265,16 +325,23 @@ class HistoricManage extends CADObject
class Database
{
hm: HistoricManage;
//块表记录
blockTableCol: BlockTableRecord[];
//模型空间
ModelSpace: BlockTableRecord = new BlockTableRecord();
ModelSpace: BlockTableRecord;
//材质字典...
objectCol = new Map<number, CADObject>();
private idCout = -1;
private idMap = new Map<number, ObjectId>();
constructor()
{
this.ModelSpace = new BlockTableRecord(this);
this.hm = new HistoricManage(this);
}
@ -300,12 +367,72 @@ class Database
break;
}
}
//在对象池中创建id.
AllocateId(obj: CADObject): ObjectId
{
if (obj.Db === this)
{
this.idCout++;
let id = new ObjectId(this.idCout, obj);
this.idMap.set(this.idCout, id);
return id;
}
else
{
console.warn("对象不属于该数据库!");
}
}
//获得指定索引的id
GetId(index: number)
{
return this.idMap.get(index);
}
}
class BlockTableRecord extends CADObject
{
private objectMap = new Map<number, CADObject>();
private objectCol: CADObject[] = [];
constructor(db: Database)
{
super();
this.SetDb(db);
}
//#region -----------------------------File-----------------------------
//对象应该实现dataIn和DataOut的方法,为了对象的序列化和反序列化
//类名,保证序列化时得到正确的new
get ClassName(): string
{
return "BlockTableRecord";
}
//对象从文件中读取数据,初始化自身
FileIn(file: CADFile)
{ }
//对象将自身数据写入到文件.
FileOut(file: CADFile)
{ }
//局部撤销
ApplyPartialUndo(file: CADFile)
{ }
//#endregion-----------------------------File End-----------------------------
AppendEntity(ent: Entity): ObjectId
{
if (ent.Db)
{
console.warn("同一个对象无法重复加入图纸中!");
return;
}
ent.SetDb(this.Db);
this.objectCol.push(ent);
let historyData = new HistoricRecord();
// historyData.undoData
this.db.hm.UndoData.WriteObjectHistoryPath(this, historyData);
return ent.Id;
}
}
enum EntityUndoType
@ -317,14 +444,8 @@ enum EntityUndoType
//所有图元的基类
class Entity extends CADObject
{
m_Db: Database;
constructor()
{
super();
}
}
//直线撤销类型
export enum LineUndoType
{
@ -375,12 +496,15 @@ class Line extends Entity
CADFactory.RegisterObject(Line);
let l = CADFactory.CreateObject("Line");
let l = CADFactory.CreateObject("Line") as Entity;
console.log(l);
let db = new Database();
let id = db.ModelSpace.AppendEntity(l);
db.GetId(0).Object.ClassName /*?*/
console.log(id);
console.log(db.hm.Undo());
db.hm.Undo();/*?*/
db.hm.Redo();
db.hm.Redo();/*?*/
Loading…
Cancel
Save