123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- 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
- {
-
-
-
- /// <summary>
- /// RGB三原色
- /// </summary>
- 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;
- }
- }
-
-
-
- /// <summary>
- /// 对图层的操作处理,包括新建图层、
- /// 把指定图层设置为当前图层和检查是否包含某个图层名
- /// </summary>
- class LayerControl
- {
- /// <summary>
- /// 创建高程点错误标记层
- /// </summary>
- /// <param name="layername">要创建的图层的名字</param>
- /// <param name="colo">RGB三原色</param>
- 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();
- }
- }
-
-
- /// <summary>
- /// 将指定图层设置为当前图层
- /// </summary>
- /// <param name="layername">要设置为当前图层的图层名字</param>
- //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();
- // }
- //}
-
-
-
- /// <summary>
- /// 判断所命图层名是否已经存在
- /// </summary>
- /// <param name="layername">要判断的图层名字</param>
- /// <returns>返回true表示在层表中存在要判断的图层名,
- /// 返回false表示层表中不存在判断的图层名</returns>
- 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)
- {
-
- }
- }
- }
- }
|