using GrxCAD.ApplicationServices; using GrxCAD.DatabaseServices; using GrxCAD.EditorInput; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace HCTools { class DGXGeneralization { public static ArrayList dgxlayerlist; public static double dgj; public static bool openflag = true; public static void dgxGeneralization() { Document doc = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; DocumentLock docLock = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument(); //try //{ //LayerTable lt = null; //ArrayList layerlist = new ArrayList(); ////获取图层事务 //using (Transaction tran = doc.TransactionManager.StartTransaction()) //{ // lt = tran.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable; // foreach (ObjectId layerid in lt) // { // LayerTableRecord ltrec = tran.GetObject(layerid, OpenMode.ForWrite) as LayerTableRecord; // layerlist.Add(ltrec.Name); // } // doc.Editor.WriteMessage("程序开始运行\r\n......\r\n"); // tran.Commit(); // tran.Dispose(); //} //DGXSBForm sf = new DGXSBForm(layerlist); //if (sf.ShowDialog() == DialogResult.OK) //{ // dgxlayerlist = sf.DGXlayerlist; // dgj = sf.DGJ; // openflag = true; //} if (openflag) { string dgx1 = string.Empty, dgx2 = string.Empty; if (dgxlayerlist.Count > 0) { if (dgxlayerlist.Count == 1) { dgx1 = dgxlayerlist[0].ToString(); } if (dgxlayerlist.Count == 2) { dgx1 = dgxlayerlist[0].ToString(); dgx2 = dgxlayerlist[1].ToString(); } } using (Transaction tran = doc.TransactionManager.StartTransaction()) { //// 以只读方式打开块表记录 Open the Block table record for read BlockTable acbt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; //// 以写方式打开模型空间块表记录 Open the Block table record Model space for write BlockTableRecord acblr = tran.GetObject(acbt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; //设置激活图层 LayerTable layertable = tran.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable; if (!layertable.Has("Delete")) { LayerTableRecord ltr = new LayerTableRecord(); ltr.Name = "Delete"; ObjectId layerid = layertable.Add(ltr); tran.AddNewlyCreatedDBObject(ltr, true); //db.Clayer = layerid; } if (!layertable.Has("8120")) { LayerTableRecord ltr = new LayerTableRecord(); ltr.Name = "8120"; ObjectId layerid = layertable.Add(ltr); tran.AddNewlyCreatedDBObject(ltr, true); //db.Clayer = layerid; } if (!layertable.Has("8110")) { LayerTableRecord ltr = new LayerTableRecord(); ltr.Name = "8110"; ObjectId layerid = layertable.Add(ltr); tran.AddNewlyCreatedDBObject(ltr, true); //db.Clayer = layerid; } tran.Commit(); tran.Dispose(); } PromptSelectionResult psr = doc.Editor.SelectAll(); //doc.Editor.WriteMessage("请选择等高线!"); //PromptSelectionResult psr = doc.Editor.GetSelection(); if (psr.Status == PromptStatus.OK) { SelectionSet ss = psr.Value; foreach (ObjectId oid in ss.GetObjectIds()) { using (Transaction tran = doc.TransactionManager.StartTransaction()) { BlockTable acbt = tran.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord acbtr = tran.GetObject(acbt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; Entity en = tran.GetObject(oid, OpenMode.ForWrite) as Entity; if ((en.Layer == dgx1) || (en.Layer == dgx2)) { Polyline pl = new Polyline(); Polyline2d pl2d = new Polyline2d(); double elevation = 99999999; if (en is Polyline) { pl = en as Polyline; elevation = pl.Elevation; } else if (en is Polyline2d) { pl2d = en as Polyline2d; elevation = pl2d.Elevation; } if (elevation != 99999999) { elevation = Convert.ToDouble(elevation.ToString("f1")); if (dgj != 0) { double remainder = elevation % dgj; if (remainder != 0.0) { en.Layer = "Delete"; en.Color = GrxCAD.Colors.Color.FromColor(System.Drawing.Color.Red); } else { double remainder2 = elevation % (dgj * 5); if (remainder2 == 0.0) { en.Layer = "8120"; en.Color = GrxCAD.Colors.Color.FromColor(System.Drawing.Color.Yellow); } else { en.Layer = "8110"; en.Color = GrxCAD.Colors.Color.FromColor(System.Drawing.Color.White); } } } } } else continue; // tran.Commit(); tran.Dispose(); } } doc.Editor.WriteMessage("程序运行结束,谢谢使用"); } } else { doc.Editor.WriteMessage("请选择相关参数\r\n"); } //} //catch (GrxCAD.Runtime.Exception ex) //{ // doc.Editor.WriteMessage(ex.ToString()); //} docLock.Dispose(); } } }