using System; using System.Collections.Generic; using System.Linq; using System.Text; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Colors; using Autodesk.AutoCAD.Windows; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.DatabaseServices; namespace T_cad { /// /// RGB三原色 /// public struct colorgb { public byte red; public byte green; public byte blue; public colorgb(byte r, byte g, byte b) { this.red = r; this.green = g; this.blue = b; } } /// /// 对图层的操作处理,包括新建图层、 /// 把指定图层设置为当前图层和检查是否包含某个图层名 /// class LayerControl { /// /// 创建高程点错误标记层 /// /// 要创建的图层的名字 /// RGB三原色 public void creatlayer(string layername, colorgb colo) { Database database = Autodesk.AutoCAD.DatabaseServices.HostApplicationServices.WorkingDatabase; DocumentLock doclock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument(); Transaction traction = database.TransactionManager.StartTransaction(); LayerTable lt = traction.GetObject(database.LayerTableId, OpenMode.ForWrite) as LayerTable; try { LayerTableRecord layertablerecord = new LayerTableRecord(); layertablerecord.Name = layername;//设置层表记录的名字 layertablerecord.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(colo.red, colo.green, colo.blue);//为层表记录赋颜色紫色 //layertablerecord.Color = Autodesk.AutoCAD.Colors.Color.FromColor(System.Drawing.Color.Magenta); lt.Add(layertablerecord); traction.AddNewlyCreatedDBObject(layertablerecord, true); traction.Commit(); } catch (Autodesk.AutoCAD.Runtime.Exception) { traction.Abort(); } finally { traction.Dispose(); doclock.Dispose(); } } /// /// 将指定图层设置为当前图层 /// /// 要设置为当前图层的图层名字 //public void movelayertofront(string layername) //{ // Database database = Autodesk.AutoCAD.DatabaseServices.HostApplicationServices.WorkingDatabase; // DocumentLock doclock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument(); // Transaction traction = database.TransactionManager.StartTransaction(); // try // { // LayerTable layertable = traction.GetObject(database.LayerTableId, OpenMode.ForRead) as LayerTable; // database.Clayer = layertable[layername];//把图层选为当前图层 // traction.Commit(); // } // catch (Autodesk.AutoCAD.Runtime.Exception) // { // traction.Abort(); // } // finally // { // traction.Dispose(); // doclock.Dispose(); // } //} /// /// 判断所命图层名是否已经存在 /// /// 要判断的图层名字 /// 返回true表示在层表中存在要判断的图层名, /// 返回false表示层表中不存在判断的图层名 public bool haslayername(string layername) { Database database = Autodesk.AutoCAD.DatabaseServices.HostApplicationServices.WorkingDatabase; using (Transaction traction = database.TransactionManager.StartTransaction()) { LayerTable lt = traction.GetObject(database.LayerTableId, OpenMode.ForRead) as LayerTable; if (lt.Has(layername)) { traction.Commit(); return true; } else { traction.Commit(); return false; } } } public void clear(string layername) { Document document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database database = HostApplicationServices.WorkingDatabase; DocumentLock documentlock = null; Transaction traction = null; try { traction = database.TransactionManager.StartTransaction(); documentlock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument(); BlockTable blocktable = traction.GetObject(database.BlockTableId, OpenMode.ForWrite) as BlockTable; BlockTableRecord blocktablerecord = traction.GetObject(blocktable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; Editor editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; TypedValue[] typedvalue = new TypedValue[2]; typedvalue.SetValue(new TypedValue((int)DxfCode.Start, "Circle"), 0); typedvalue.SetValue(new TypedValue((int)DxfCode.LayerName, "Cir"), 1); SelectionFilter selectionfilter = new SelectionFilter(typedvalue); PromptSelectionResult select = editor.SelectAll(selectionfilter); if (select.Status == PromptStatus.OK) { SelectionSet set = select.Value; ObjectId[] obj = new ObjectId[set.Count]; obj = set.GetObjectIds(); for (int i = 0; i < obj.Length; i++) { ObjectId objid = obj[i]; Entity entity = (Entity)traction.GetObject(objid, OpenMode.ForWrite); entity.Erase(); } } traction.Commit(); } catch (Autodesk.AutoCAD.Runtime.Exception) { } } } }