/// <summary> /// 保存事件使数据库与图纸中的已保存数据相一致 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void db_SaveComplete(object sender, DatabaseIOEventArgs e) { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; //保存树结构 DBTreeControl treeControl = Project.Instance.GetMainTreeCol(doc); treeControl.StoreTree(Project.Instance.GetProjectTree(doc)); //保存模型数据 DBEntityControl dbControl = Project.Instance.GetMainEntCol(doc, true); TypedValue[] value = { new TypedValue((int)DxfCode.Start, "TUNNEL_SQUARE,TUNNEL_CYLINDER,TUNNELNODE") }; SelectionFilter sf = new SelectionFilter(value); PromptSelectionResult res = ed.SelectAll(sf); if (res.Status != PromptStatus.OK) { dbControl.Delete(Query.All(), db); return; } SelectionSet SS = res.Value; if (SS == null) { return; } Autodesk.AutoCAD.DatabaseServices.ObjectId[] idArray = SS.GetObjectIds(); Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = db.TransactionManager; using (Transaction myT = tm.StartTransaction()) { //储存之前先把之前数据的删除干净 int deleteCounts = dbControl.Delete(Query.All(), db); foreach (var id in idArray) { Entity entity = (Entity)tm.GetObject(id, OpenMode.ForRead, true); if (entity is BaseTunnel) { DBTunnel dbTunnel = new DBTunnel(); dbTunnel.SetProperty(entity); dbControl.Insert(dbTunnel, db); } else if (entity is Node) { DBNode dbNode = new DBNode(); dbNode.SetProperty(entity); dbControl.Insert(dbNode, db); } } myT.Commit(); } }
/// <summary> /// 添加事件使临时数据库与图纸中所有效的数据相一致 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void db_ObjectAppend(object sender, ObjectEventArgs e) { if (!(e.DBObject is BaseTunnel) && !(e.DBObject is Node)) { return; } Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Entity entity = e.DBObject as Entity; if (entity == null || entity.BlockName != "*Model_Space") { return; } if (entity is BaseTunnel) { DBEntityControl dbControl = Project.Instance.GetTmpEntCol(doc); DBTunnel dbTunnel = new DBTunnel(); dbTunnel.SetProperty(entity); dbControl.Insert(dbTunnel, db); } else if (entity is Node) { DBEntityControl dbControl = Project.Instance.GetTmpEntCol(doc); DBNode dbNode = new DBNode(); dbNode.SetProperty(entity); dbControl.Insert(dbNode, db); } }
/// <summary> /// 删除事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void db_ObjectErased(object sender, ObjectErasedEventArgs e) { if (!(e.DBObject is BaseTunnel) && !(e.DBObject is Node)) { return; } Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; if (e.Erased == true) //这时候是删除触发的 { DBEntityControl dbControl = Project.Instance.GetTmpEntCol(doc); dbControl.Delete(Query.EQ("HandleValue", e.DBObject.ObjectId.Handle.Value), db); if (e.DBObject is BaseTunnel) { Global.EraseSelectedTunnel(sender, e.DBObject.ObjectId.Handle.Value); } } else //这时候是删除后的撤销动作触发的 { Entity entity = e.DBObject as Entity; if (entity is BaseTunnel) { DBEntityControl dbControl = Project.Instance.GetTmpEntCol(doc); DBTunnel dbTunnel = new DBTunnel(); dbTunnel.SetProperty(entity); dbControl.Insert(dbTunnel, db); } else if (entity is Node) { DBEntityControl dbControl = Project.Instance.GetTmpEntCol(doc); DBNode dbNode = new DBNode(); dbNode.SetProperty(entity); dbControl.Insert(dbNode, db); } } }