工具箱相关
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

LayerControl.cs 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Autodesk.AutoCAD.Runtime;
  6. using Autodesk.AutoCAD.ApplicationServices;
  7. using Autodesk.AutoCAD.EditorInput;
  8. using Autodesk.AutoCAD.Colors;
  9. using Autodesk.AutoCAD.Windows;
  10. using Autodesk.AutoCAD.Geometry;
  11. using Autodesk.AutoCAD.DatabaseServices;
  12. namespace T_cad
  13. {
  14. /// <summary>
  15. /// RGB三原色
  16. /// </summary>
  17. public struct colorgb
  18. {
  19. public byte red;
  20. public byte green;
  21. public byte blue;
  22. public colorgb(byte r, byte g, byte b)
  23. {
  24. this.red = r;
  25. this.green = g;
  26. this.blue = b;
  27. }
  28. }
  29. /// <summary>
  30. /// 对图层的操作处理,包括新建图层、
  31. /// 把指定图层设置为当前图层和检查是否包含某个图层名
  32. /// </summary>
  33. class LayerControl
  34. {
  35. /// <summary>
  36. /// 创建高程点错误标记层
  37. /// </summary>
  38. /// <param name="layername">要创建的图层的名字</param>
  39. /// <param name="colo">RGB三原色</param>
  40. public void creatlayer(string layername, colorgb colo)
  41. {
  42. Database database = Autodesk.AutoCAD.DatabaseServices.HostApplicationServices.WorkingDatabase;
  43. DocumentLock doclock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
  44. Transaction traction = database.TransactionManager.StartTransaction();
  45. LayerTable lt = traction.GetObject(database.LayerTableId, OpenMode.ForWrite) as LayerTable;
  46. try
  47. {
  48. LayerTableRecord layertablerecord = new LayerTableRecord();
  49. layertablerecord.Name = layername;//设置层表记录的名字
  50. layertablerecord.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(colo.red, colo.green, colo.blue);//为层表记录赋颜色紫色
  51. //layertablerecord.Color = Autodesk.AutoCAD.Colors.Color.FromColor(System.Drawing.Color.Magenta);
  52. lt.Add(layertablerecord);
  53. traction.AddNewlyCreatedDBObject(layertablerecord, true);
  54. traction.Commit();
  55. }
  56. catch (Autodesk.AutoCAD.Runtime.Exception)
  57. {
  58. traction.Abort();
  59. }
  60. finally
  61. {
  62. traction.Dispose();
  63. doclock.Dispose();
  64. }
  65. }
  66. /// <summary>
  67. /// 将指定图层设置为当前图层
  68. /// </summary>
  69. /// <param name="layername">要设置为当前图层的图层名字</param>
  70. //public void movelayertofront(string layername)
  71. //{
  72. // Database database = Autodesk.AutoCAD.DatabaseServices.HostApplicationServices.WorkingDatabase;
  73. // DocumentLock doclock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
  74. // Transaction traction = database.TransactionManager.StartTransaction();
  75. // try
  76. // {
  77. // LayerTable layertable = traction.GetObject(database.LayerTableId, OpenMode.ForRead) as LayerTable;
  78. // database.Clayer = layertable[layername];//把图层选为当前图层
  79. // traction.Commit();
  80. // }
  81. // catch (Autodesk.AutoCAD.Runtime.Exception)
  82. // {
  83. // traction.Abort();
  84. // }
  85. // finally
  86. // {
  87. // traction.Dispose();
  88. // doclock.Dispose();
  89. // }
  90. //}
  91. /// <summary>
  92. /// 判断所命图层名是否已经存在
  93. /// </summary>
  94. /// <param name="layername">要判断的图层名字</param>
  95. /// <returns>返回true表示在层表中存在要判断的图层名,
  96. /// 返回false表示层表中不存在判断的图层名</returns>
  97. public bool haslayername(string layername)
  98. {
  99. Database database = Autodesk.AutoCAD.DatabaseServices.HostApplicationServices.WorkingDatabase;
  100. using (Transaction traction = database.TransactionManager.StartTransaction())
  101. {
  102. LayerTable lt = traction.GetObject(database.LayerTableId, OpenMode.ForRead) as LayerTable;
  103. if (lt.Has(layername))
  104. {
  105. traction.Commit();
  106. return true;
  107. }
  108. else
  109. {
  110. traction.Commit();
  111. return false;
  112. }
  113. }
  114. }
  115. public void clear(string layername)
  116. {
  117. Document document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  118. Database database = HostApplicationServices.WorkingDatabase;
  119. DocumentLock documentlock = null;
  120. Transaction traction = null;
  121. try
  122. {
  123. traction = database.TransactionManager.StartTransaction();
  124. documentlock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
  125. BlockTable blocktable = traction.GetObject(database.BlockTableId,
  126. OpenMode.ForWrite) as BlockTable;
  127. BlockTableRecord blocktablerecord = traction.GetObject(blocktable[BlockTableRecord.ModelSpace],
  128. OpenMode.ForWrite) as BlockTableRecord;
  129. Editor editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  130. TypedValue[] typedvalue = new TypedValue[2];
  131. typedvalue.SetValue(new TypedValue((int)DxfCode.Start, "Circle"), 0);
  132. typedvalue.SetValue(new TypedValue((int)DxfCode.LayerName, "Cir"), 1);
  133. SelectionFilter selectionfilter = new SelectionFilter(typedvalue);
  134. PromptSelectionResult select = editor.SelectAll(selectionfilter);
  135. if (select.Status == PromptStatus.OK)
  136. {
  137. SelectionSet set = select.Value;
  138. ObjectId[] obj = new ObjectId[set.Count];
  139. obj = set.GetObjectIds();
  140. for (int i = 0; i < obj.Length; i++)
  141. {
  142. ObjectId objid = obj[i];
  143. Entity entity = (Entity)traction.GetObject(objid, OpenMode.ForWrite);
  144. entity.Erase();
  145. }
  146. }
  147. traction.Commit();
  148. }
  149. catch (Autodesk.AutoCAD.Runtime.Exception)
  150. {
  151. }
  152. }
  153. }
  154. }