工具箱相关
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Autodesk.AutoCAD.Runtime;
  5. using Autodesk.AutoCAD.EditorInput;
  6. using Autodesk.AutoCAD.Geometry;
  7. using Autodesk.AutoCAD.DatabaseServices;
  8. using Autodesk.AutoCAD.ApplicationServices;
  9. namespace T_cad
  10. {
  11. class OldCls2
  12. {
  13. [CommandMethod("CTK")]
  14. public void ctk()
  15. {
  16. Document document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  17. Database database = HostApplicationServices.WorkingDatabase;
  18. using (Transaction traction = database.TransactionManager.StartTransaction())
  19. {
  20. DocumentLock documentlock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
  21. BlockTable blocktable = traction.GetObject(database.BlockTableId,
  22. OpenMode.ForWrite) as BlockTable;
  23. BlockTableRecord blocktablerecord = traction.GetObject(blocktable[BlockTableRecord.ModelSpace],
  24. OpenMode.ForWrite) as BlockTableRecord;
  25. // 获得当前文档的编辑器
  26. Editor editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  27. // 创建一个 TypedValue 数组,用于定义过滤条件
  28. TypedValue[] typedvalue = new TypedValue[1];
  29. typedvalue.SetValue(new TypedValue((int)DxfCode.LayerName, "VC"), 0);
  30. // 赋值过滤条件给 SelectionFilter 对象
  31. SelectionFilter selectionfilter = new SelectionFilter(typedvalue);
  32. // 要求在图形区域中手动选择对象
  33. PromptSelectionResult psr = editor.GetSelection(selectionfilter);
  34. if (psr.Status == PromptStatus.OK)
  35. {
  36. SelectionSet selectionset = psr.Value;
  37. ObjectId[] obj = new ObjectId[selectionset.Count];
  38. obj = selectionset.GetObjectIds();
  39. for (int i = 0; i < obj.Length; i++)
  40. {
  41. ObjectId objid = obj[i];
  42. Entity entity = (Entity)traction.GetObject(objid, OpenMode.ForRead);
  43. Polyline line = entity as Polyline;
  44. Point3dCollection coll = new Point3dCollection();
  45. line.GetStretchPoints(coll);
  46. double max_x = coll[0].X;
  47. double max_y = coll[0].Y;
  48. double min_x = coll[0].X;
  49. double min_y = coll[0].Y;
  50. for (int j = 1; j < coll.Count; j++)
  51. {
  52. if (coll[j].X > max_x)
  53. {
  54. max_x = coll[j].X;
  55. }
  56. if (coll[j].Y > max_y)
  57. {
  58. max_y = coll[j].Y;
  59. }
  60. if (coll[j].X < min_x)
  61. {
  62. min_x = coll[j].X;
  63. }
  64. if (coll[j].Y < min_y)
  65. {
  66. min_y = coll[j].Y;
  67. }
  68. }
  69. Point3dCollection coll2 = new Point3dCollection();
  70. Point3d p1 = new Point3d(min_x, min_y, 0);
  71. Point3d p2 = new Point3d(min_x, max_y, 0);
  72. Point3d p3 = new Point3d(max_x, max_y, 0);
  73. Point3d p4 = new Point3d(max_x, min_y, 0);
  74. coll2.Add(p1);
  75. coll2.Add(p2);
  76. coll2.Add(p3);
  77. coll2.Add(p4);
  78. Polyline2d pline = new Polyline2d(Poly2dType.SimplePoly, coll2, 0, true, 0, 0, null);
  79. pline.Layer = "qtk";
  80. pline.SetDatabaseDefaults();
  81. blocktablerecord.AppendEntity(pline);
  82. traction.AddNewlyCreatedDBObject(pline, true);
  83. }
  84. }
  85. traction.Commit();
  86. documentlock.Dispose();
  87. traction.Dispose();
  88. }
  89. }
  90. }
  91. }