Advertisement
Guest User

Untitled

a guest
May 30th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.01 KB | None | 0 0
  1. using System;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.Geometry;
  5. using Autodesk.AutoCAD.Runtime;
  6. using lab6_task3;
  7. using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application;
  8.  
  9. [assembly: CommandClass(typeof(MyCommands))]
  10.  
  11. namespace lab6_task3
  12. {
  13. public class MyCommands
  14. {
  15. [CommandMethod("addAnEnt")]
  16. public void AddAnEnt()
  17. {
  18. var ed = Application.DocumentManager.MdiActiveDocument.Editor;
  19. var getWhichEntityOptions = new PromptKeywordOptions(
  20. "Which entity do you want to create? [Circle/Block] : ", "Circle Block");
  21. var getWhichEntityResult = ed.GetKeywords(getWhichEntityOptions);
  22.  
  23. if (getWhichEntityResult.Status == PromptStatus.OK)
  24. {
  25. switch (getWhichEntityResult.StringResult)
  26. {
  27. case "Circle":
  28. var getPointOptions = new PromptPointOptions("Pick Center Point : ");
  29. var getPointResult = ed.GetPoint(getPointOptions);
  30.  
  31. if (getPointResult.Status == PromptStatus.OK)
  32. {
  33. var getRadiusOptions = new PromptDistanceOptions("Pick Radius : ")
  34. {
  35. BasePoint = getPointResult.Value,
  36. UseBasePoint = true
  37. };
  38. var getRadiusResult = ed.GetDistance(getRadiusOptions);
  39.  
  40. if (getRadiusResult.Status == PromptStatus.OK)
  41. {
  42. var dwg = ed.Document.Database;
  43. var trans = dwg.TransactionManager.StartTransaction();
  44.  
  45. try
  46. {
  47. var circle = new Circle(getPointResult.Value, Vector3d.ZAxis, getRadiusResult.Value);
  48. var btr = (BlockTableRecord)trans.GetObject(dwg.CurrentSpaceId, OpenMode.ForWrite);
  49.  
  50. btr.AppendEntity(circle);
  51. trans.AddNewlyCreatedDBObject(circle, true);
  52. trans.Commit();
  53. }
  54. catch (System.Exception ex)
  55. {
  56. ed.WriteMessage("problem due to " + ex.Message);
  57. }
  58. finally
  59. {
  60. trans.Dispose();
  61. }
  62. }
  63. }
  64. break;
  65. case "Block":
  66. var blockNameOptions = new PromptStringOptions("Enter name of the Block to create : ")
  67. {
  68. AllowSpaces = false
  69. };
  70. var blockNameResult = ed.GetString(blockNameOptions);
  71.  
  72. if (blockNameResult.Status == PromptStatus.OK)
  73. {
  74. var dwg = ed.Document.Database;
  75. var trans = dwg.TransactionManager.StartTransaction();
  76.  
  77. try
  78. {
  79. var newBlockDef = new BlockTableRecord
  80. {
  81. Name = blockNameResult.StringResult
  82. };
  83. var blockTable = (BlockTable) trans.GetObject(dwg.BlockTableId, OpenMode.ForRead);
  84.  
  85. if (blockTable.Has(blockNameResult.StringResult) == false)
  86. {
  87. blockTable.UpgradeOpen();
  88. blockTable.Add(newBlockDef);
  89. trans.AddNewlyCreatedDBObject(newBlockDef, true);
  90.  
  91. var circle1 = new Circle(new Point3d(0, 0, 0), Vector3d.ZAxis, 10);
  92. newBlockDef.AppendEntity(circle1);
  93.  
  94. var circle2 = new Circle(new Point3d(20, 10, 0), Vector3d.ZAxis, 10);
  95. newBlockDef.AppendEntity(circle2);
  96.  
  97. trans.AddNewlyCreatedDBObject(circle1, true);
  98. trans.AddNewlyCreatedDBObject(circle2, true);
  99.  
  100. var blockRefPointOptions =
  101. new PromptPointOptions("Pick insertion point of BlockRef : ");
  102. var blockRefPointResult = ed.GetPoint(blockRefPointOptions);
  103.  
  104. if (blockRefPointResult.Status != PromptStatus.OK)
  105. {
  106. trans.Dispose();
  107.  
  108. return;
  109. }
  110.  
  111. var blockRef = new BlockReference(blockRefPointResult.Value, newBlockDef.ObjectId);
  112. var curSpace =
  113. (BlockTableRecord) trans.GetObject(dwg.CurrentSpaceId, OpenMode.ForWrite);
  114.  
  115. curSpace.AppendEntity(blockRef);
  116. trans.AddNewlyCreatedDBObject(blockRef, true);
  117. trans.Commit();
  118. }
  119. }
  120. catch (System.Exception ex)
  121. {
  122. ed.WriteMessage("a problem occurred because " + ex.Message);
  123. }
  124. finally
  125. {
  126. trans.Dispose();
  127. }
  128. }
  129. break;
  130. }
  131.  
  132. }
  133. }
  134.  
  135. [CommandMethod("additionalTask")]
  136. public void additionalTask()
  137. {
  138. var ed = Application.DocumentManager.MdiActiveDocument.Editor;
  139. var blockNameOptions = new PromptStringOptions("Enter name of the Block to create : ")
  140. {
  141. AllowSpaces = false
  142. };
  143. var blockNameResult = ed.GetString(blockNameOptions);
  144.  
  145. if (blockNameResult.Status == PromptStatus.OK)
  146. {
  147. var dwg = ed.Document.Database;
  148. var trans = dwg.TransactionManager.StartTransaction();
  149.  
  150. try
  151. {
  152. var newBlockDef = new BlockTableRecord
  153. {
  154. Name = blockNameResult.StringResult
  155. };
  156. var blockTable = (BlockTable)trans.GetObject(dwg.BlockTableId, OpenMode.ForRead);
  157.  
  158. if (blockTable.Has(blockNameResult.StringResult) == false)
  159. {
  160. blockTable.UpgradeOpen();
  161. blockTable.Add(newBlockDef);
  162. trans.AddNewlyCreatedDBObject(newBlockDef, true);
  163.  
  164. var circles = new Circle[5];
  165.  
  166. var x = 0;
  167. for (var i = 0; i < 5; i++, x += 10)
  168. {
  169. circles[i] = new Circle(new Point3d(2*x, 0, 0), Vector3d.ZAxis, 5);
  170. newBlockDef.AppendEntity(circles[i]);
  171. trans.AddNewlyCreatedDBObject(circles[i], true);
  172. }
  173.  
  174. var blockRefPointOptions = new PromptPointOptions("Pick insertion point of BlockRef : ");
  175. var blockRefPointResult = ed.GetPoint(blockRefPointOptions);
  176.  
  177. if (blockRefPointResult.Status != PromptStatus.OK)
  178. {
  179. trans.Dispose();
  180. return;
  181. }
  182.  
  183. var blockRef = new BlockReference(blockRefPointResult.Value, newBlockDef.ObjectId);
  184. var curSpace = (BlockTableRecord)trans.GetObject(dwg.CurrentSpaceId, OpenMode.ForWrite);
  185.  
  186. curSpace.AppendEntity(blockRef);
  187. trans.AddNewlyCreatedDBObject(blockRef, true);
  188. trans.Commit();
  189. }
  190. }
  191. catch (System.Exception ex)
  192. {
  193. ed.WriteMessage("a problem occurred because " + ex.Message);
  194. }
  195. finally
  196. {
  197. trans.Dispose();
  198. }
  199. }
  200. }
  201.  
  202. [CommandMethod("addTask")]
  203. public void addTask()
  204. {
  205. var ed = Application.DocumentManager.MdiActiveDocument.Editor;
  206. var blockNameOptions = new PromptStringOptions("Enter name of the Block to create : ")
  207. {
  208. AllowSpaces = false
  209. };
  210. var blockNameResult = ed.GetString(blockNameOptions);
  211.  
  212. if (blockNameResult.Status == PromptStatus.OK)
  213. {
  214. var dwg = ed.Document.Database;
  215. var trans = dwg.TransactionManager.StartTransaction();
  216.  
  217. try
  218. {
  219. var newBlockDef = new BlockTableRecord
  220. {
  221. Name = blockNameResult.StringResult
  222. };
  223. var blockTable = (BlockTable)trans.GetObject(dwg.BlockTableId, OpenMode.ForRead);
  224.  
  225. if (blockTable.Has(blockNameResult.StringResult) == false)
  226. {
  227. blockTable.UpgradeOpen();
  228. blockTable.Add(newBlockDef);
  229. trans.AddNewlyCreatedDBObject(newBlockDef, true);
  230.  
  231. const int number = 50;
  232. var circles = new Circle[number];
  233.  
  234. for (var i = 0; i < number; i++)
  235. {
  236. const int leafs = 10;
  237. const int leaf_size = 4;
  238. const int rad = 20;
  239. var r = rad + leaf_size*Math.Sin(2*leafs*Math.PI*i/number);
  240. var x = r*Math.Cos(2*Math.PI*i/number);
  241. var y = r*Math.Sin(2*Math.PI*i/number);
  242.  
  243. circles[i] = new Circle(new Point3d(x, y, 0), Vector3d.ZAxis, 1);
  244. newBlockDef.AppendEntity(circles[i]);
  245. trans.AddNewlyCreatedDBObject(circles[i], true);
  246. }
  247.  
  248. var blockRefPointOptions = new PromptPointOptions("Pick insertion point of BlockRef : ");
  249. var blockRefPointResult = ed.GetPoint(blockRefPointOptions);
  250.  
  251. if (blockRefPointResult.Status != PromptStatus.OK)
  252. {
  253. trans.Dispose();
  254. return;
  255. }
  256.  
  257. var blockRef = new BlockReference(blockRefPointResult.Value, newBlockDef.ObjectId);
  258. var curSpace = (BlockTableRecord)trans.GetObject(dwg.CurrentSpaceId, OpenMode.ForWrite);
  259.  
  260. curSpace.AppendEntity(blockRef);
  261. trans.AddNewlyCreatedDBObject(blockRef, true);
  262. trans.Commit();
  263. }
  264. }
  265. catch (System.Exception ex)
  266. {
  267. ed.WriteMessage("a problem occurred because " + ex.Message);
  268. }
  269. finally
  270. {
  271. trans.Dispose();
  272. }
  273. }
  274. }
  275. }
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement