/// <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(); } }
//点击右键菜单“删除”触发 private void t1Delete_ItemClick(object sender, EventArgs e) { Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; var focusNode = treeList1.FocusedNode; List <ProjectTreeNode> tree = treeList1.DataSource as List <ProjectTreeNode>; var node = getProjectNode(focusNode); if (node == null) { treeList1.Nodes.Remove(focusNode); return; } if (containNode(node, Project.Instance.getCurrentSurface(doc))) { Autodesk.AutoCAD.ApplicationServices.Application. ShowAlertDialog("删除的节点不能包含当前工作面节点"); return; } var pNode = node.GetParentNode(); if (pNode != null) { pNode.Children.Remove(node); } List <ProjectTreeLeafNode> ls = new List <ProjectTreeLeafNode>(); getLeafNodes(node, ls);//得到所有的叶子节点 treeList1.DeleteNode(focusNode); DBEntityControl dbControl = Project.Instance.GetTmpEntCol(doc); foreach (var leafNode in ls) { long handleValue = leafNode.HandleValue; dbControl.Delete(Query.EQ("HandleValue", handleValue), this); } doc.Editor.Regen(); }
/// <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); } } }
//添加右键菜单,在指定位置添加或删除坐标 private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { int rowNum = 0; if ((e.ClickedItem).Name == "AddItemNex")//右键插入下一点坐标 { if (tunnels.Count == 0) { Autodesk.AutoCAD.ApplicationServices.Application. ShowAlertDialog("请先添加一个巷道!"); } else { tunnels.Insert(gridView1.FocusedRowHandle + 1, new Tunnel(tunnels[gridView1.FocusedRowHandle].HandleValue, "巷道", 100, 100, 100)); } } if ((e.ClickedItem).Name == "AddItemBef")//右键插入上一点坐标 { if (tunnels.Count == 0) { Autodesk.AutoCAD.ApplicationServices.Application. ShowAlertDialog("请先添加一个巷道!"); } else { if (gridView1.FocusedRowHandle == 0) { tunnels.Insert(0, new Tunnel(tunnels[gridView1.FocusedRowHandle].HandleValue, "巷道", 100, 100, 100)); } else if (gridView1.FocusedRowHandle == 1) { tunnels.Insert(1, new Tunnel(tunnels[gridView1.FocusedRowHandle].HandleValue, "巷道", 100, 100, 100)); } else { tunnels.Insert(gridView1.FocusedRowHandle, new Tunnel(tunnels[gridView1.FocusedRowHandle].HandleValue, "巷道", 100, 100, 100)); } } } if ((e.ClickedItem).Text == "删除坐标")//右键删除坐标 { if (gridView1.RowCount > 0) { foreach (var cot in tunnels) { if (tunnels[gridView1.FocusedRowHandle].HandleValue == cot.HandleValue) { rowNum++; } } if (rowNum > 2) { deleDataRow(); } else { Autodesk.AutoCAD.ApplicationServices.Application. ShowAlertDialog("当前坐标不可删除!"); return; } } } if ((e.ClickedItem).Text == "删除巷道")//右键删除巷道 { long handle = tunnels[gridView1.FocusedRowHandle].HandleValue; if (handle > 0)//删除已生成图形的巷道 { Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; DBEntityControl dbControl = Project.Instance.GetTmpEntCol(doc); dbControl.Delete(Query.EQ("HandleValue", handle), Project.Instance.GetActiveGridList(doc)); doc.Editor.Regen(); } else//删除未生成图形的巷道 { for (int i = 0; i < gridView1.RowCount; i++) { if (tunnels[i].HandleValue == tunnels[gridView1.FocusedRowHandle].HandleValue) { cdTunnels.Add(tunnels[i]); } } for (int i = 0; i < cdTunnels.Count; i++) { tunnels.Remove(cdTunnels[i]); } } } gridControl.RefreshDataSource(); }