Cmd.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Autodesk.AutoCAD.ApplicationServices;
  7. using Autodesk.AutoCAD.DatabaseServices;
  8. using Autodesk.AutoCAD.EditorInput;
  9. using Autodesk.AutoCAD.Runtime;
  10. using Autodesk.AutoCAD.Colors;
  11. using DotNetARX2014;
  12. using Autodesk.AutoCAD.Geometry;
  13. namespace CadStudy
  14. {
  15. public class Cmd
  16. {
  17. //Demo1:CmdLine直线
  18. [CommandMethod("CmdLine")]
  19. public void CmdLine()
  20. {
  21. Point3d startPoint = new Point3d(0, 0, 0); //声明起始点
  22. Point3d endPoint = new Point3d(100, 200, 0); //声明终止点
  23. Line line = new Line(startPoint, endPoint); //声明直线
  24. line.Color = Color.FromRgb(255, 0, 0); //设置颜色
  25. //line.Layer = "3"; //设置图层
  26. line.LineWeight = LineWeight.LineWeight050; //设置线宽
  27. //开启事务
  28. Database db = HostApplicationServices.WorkingDatabase;
  29. using (Transaction trans = db.TransactionManager.StartTransaction())
  30. {
  31. //打开块表
  32. BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
  33. //打开块表记录
  34. BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  35. //向块表记录添加线条
  36. btr.AppendEntity(line);
  37. trans.AddNewlyCreatedDBObject(line, true);
  38. trans.Commit();
  39. }
  40. }
  41. //Demo2:CmdCir圆
  42. [CommandMethod("CmdCir")]
  43. public void CmdCir()
  44. {
  45. Circle circle = new Circle();//声明圆
  46. circle.Center = new Point3d(10, 20, 0); //声明圆心
  47. circle.Radius = 10; //声明半径
  48. //调用方法
  49. AddEntity(circle);
  50. }
  51. //Demo3:CmdArc圆弧
  52. [CommandMethod("CmdArc")]
  53. public void CmdArc()
  54. {
  55. //声明圆弧
  56. Arc arc = new Arc()
  57. {
  58. Center = new Point3d(50, 50, 0), //设置圆弧的圆心
  59. Radius = 10, //设置半径
  60. StartAngle = 0, //设置起始的弧度
  61. EndAngle = Math.PI / 2, //设置终点的弧度
  62. };
  63. AddEntity(arc);
  64. }
  65. //Demo4:AddPolyline多段线
  66. [CommandMethod("AddPolyline")]
  67. public void AddPolyline()
  68. {
  69. Polyline polyline = new Polyline();//声明多段线
  70. //AddVertexAt:给多段线加拐点
  71. //第一个参数表示在第几个序列加点
  72. //第二个参数表示这个点的坐标
  73. //第三个参数表示这个点的凸起
  74. //第四个参数表示线的初始宽度
  75. //第五个参数表示线的终止宽度
  76. polyline.AddVertexAt(0, new Point2d(0, 0), 0, 0, 0);
  77. polyline.AddVertexAt(1, new Point2d(10, 10), 0, 0, 0);
  78. polyline.AddVertexAt(2, new Point2d(20, 0), 0, 0, 0);
  79. //重新设置拐点的坐标
  80. //polyline.SetPointAt(1, new Point2d(20, 30));
  81. //移除拐点
  82. //polyline.RemoveVertexAt(1);
  83. //NumberOfVertices:多段线所有的拐点数
  84. for (int i = 0; i < polyline.NumberOfVertices; i++)
  85. {
  86. Point2d point2D = polyline.GetPoint2dAt(i);//获取每一个点
  87. }
  88. polyline.Closed = false; //是否闭合
  89. AddEntity(polyline);
  90. }
  91. //Demo5:SelecetDemo选择集
  92. [CommandMethod("SelectDemo")]
  93. public void SelectDemo()
  94. {
  95. List<Entity> entities = SelectEntities();
  96. }
  97. //Demo6:单行文字
  98. [CommandMethod("AddText")]
  99. public void AddText()
  100. {
  101. DBText text = new DBText
  102. {
  103. TextString = "单行文字", //文字内容
  104. Position = new Point3d(10, 10, 0), //文字坐标
  105. Height = 3, //文字高度
  106. WidthFactor = 0.8, //宽度因子
  107. //Rotation = Math.PI / 4, //旋转角度
  108. //IsMirroredInX = true, //在X轴方向上镜像
  109. //IsMirroredInY = true, //在Y轴方向上镜像
  110. //HorizontalMode = TextHorizontalMode.TextMid,
  111. //AlignmentPoint = new Point3d(20, 20, 0),
  112. //HorizontalMode = TextHorizontalMode.TextAlign,
  113. //AlignmentPoint = new Point3d(20, 20, 0),
  114. };
  115. List<TextStyleTableRecord> textStyleTableRecords = GetTextStyle();
  116. //判断是否有样式名为123
  117. //if表达式的意思是集合中有任意一个的样式等于123就返回true
  118. if (textStyleTableRecords.Any(x => x.Name == "123"))
  119. {
  120. //将第一个匹配的元素找出来
  121. text.TextStyleId = textStyleTableRecords.Find(x => x.Name == "123").ObjectId;
  122. }
  123. AddEntity(text);
  124. }
  125. //将公共方法提取出来
  126. private void AddEntity(Entity entity)
  127. {
  128. //开启事务
  129. Database db = HostApplicationServices.WorkingDatabase;
  130. using (Transaction trans = db.TransactionManager.StartTransaction())
  131. {
  132. //打开块表
  133. BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
  134. //打开块表记录
  135. BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  136. //向块表记录添加对象
  137. btr.AppendEntity(entity);
  138. trans.AddNewlyCreatedDBObject(entity, true);
  139. trans.Commit();
  140. }
  141. }
  142. //获取文字样式的方法
  143. public List<TextStyleTableRecord> GetTextStyle()
  144. {
  145. List<TextStyleTableRecord> result = new List<TextStyleTableRecord>();
  146. //获取数据库
  147. Database db = HostApplicationServices.WorkingDatabase;
  148. //打开事务
  149. using (Transaction trans = db.TransactionManager.StartTransaction())
  150. {
  151. //获取字体样式的样式表
  152. TextStyleTable tt = (TextStyleTable)trans.GetObject(db.TextStyleTableId,OpenMode.ForRead);
  153. //遍历样式表
  154. foreach (var item in tt)
  155. {
  156. //获取到每一个字体样式的记录
  157. TextStyleTableRecord ttr = (TextStyleTableRecord)trans.GetObject(item, OpenMode.ForRead);
  158. result.Add(ttr);
  159. }
  160. }
  161. return result;
  162. }
  163. //获取选择集的公共方法
  164. private List<Entity> SelectEntities()
  165. {
  166. Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;
  167. //获取PickFirst选择集
  168. PromptSelectionResult acSSPrompt;
  169. SelectionSet acSSet;
  170. //清空PickFirst选择集
  171. ObjectId[] idarrayEmpty = new ObjectId[0];
  172. acDocEd.SetImpliedSelection(idarrayEmpty);
  173. //请求从图形区域选择对象
  174. acSSPrompt = acDocEd.GetSelection();
  175. //如果提示状态O看,表示已选择对象
  176. if (acSSPrompt.Status == PromptStatus.OK)
  177. {
  178. acSSet = acSSPrompt.Value;
  179. List<ObjectId> objectIds = acSSet.GetObjectIds().ToList();
  180. Database database = HostApplicationServices.WorkingDatabase;
  181. List<Entity> entities = new List<Entity>();
  182. using (Transaction tran = database.TransactionManager.StartTransaction())
  183. {
  184. foreach (var item in objectIds)
  185. {
  186. Entity entity = (Entity)item.GetObject(OpenMode.ForRead);
  187. entities.Add(entity);
  188. }
  189. }
  190. return entities;
  191. }
  192. else
  193. {
  194. Application.ShowAlertDialog("Number of objects selected:0");
  195. return null;
  196. }
  197. }
  198. }
  199. }