调试环境:中望CAD2014;
    引用:ZwDatabaseMgd.dll、ZwManaged.dll两个类库!

    1. using System;
    2. using ZwSoft.ZwCAD.ApplicationServices;
    3. using ZwSoft.ZwCAD.DatabaseServices;
    4. using ZwSoft.ZwCAD.EditorInput;
    5. using ZwSoft.ZwCAD.Geometry;
    6. using ZwSoft.ZwCAD.Runtime;
    7. namespace TextBox
    8. {
    9. public static class Class1
    10. {
    11. [CommandMethod("TBox")]
    12. public static void MakeTextBox()
    13. {
    14. SelectionSet ss = SelectOnScreen(new TypedValue[] { new TypedValue(0, "*TEXT") });//ed.GetSelection().Value;
    15. foreach (ObjectId id in ss.GetObjectIds())
    16. {
    17. id.MakeRectangle();
    18. }
    19. }
    20. /// <summary>
    21. /// 创建文字外框
    22. /// </summary>
    23. /// <param name="id">旋转对象ObjectId</param>
    24. /// <param name="center">旋转中心点</param>
    25. /// <param name="degree">旋转弧度</param>
    26. public static void MakeRectangle(this ObjectId id)//, Point3d center, double degree, double wl, double wr, double hb, double ht
    27. {
    28. bool ismtxt = false;
    29. double degree = 0, wl = 0, wr = 0, ht = 0, hb = 0;
    30. Point3d center = new Point3d(0, 0, 0), minpt = new Point3d(0, 0, 0), maxpt = new Point3d(0, 0, 0);
    31. Database db = HostApplicationServices.WorkingDatabase;
    32. using (Transaction tr = db.TransactionManager.StartTransaction())
    33. {
    34. //打开块表
    35. //BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
    36. //打开块表记录
    37. //BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
    38. //打开块表记录
    39. BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
    40. Entity ent = (Entity)id.GetObject(OpenMode.ForWrite);
    41. if ("MText".Equals(ent.GetType().Name, StringComparison.CurrentCultureIgnoreCase))
    42. {
    43. MText mtxt = (MText)ent;
    44. ismtxt = true;
    45. double width = mtxt.ActualWidth;
    46. double height = mtxt.ActualHeight;
    47. switch (mtxt.Attachment)
    48. {
    49. case AttachmentPoint.TopCenter:
    50. case AttachmentPoint.MiddleCenter:
    51. case AttachmentPoint.BottomCenter:
    52. wl = width * -0.5;
    53. break;
    54. case AttachmentPoint.TopRight:
    55. case AttachmentPoint.MiddleRight:
    56. case AttachmentPoint.BottomRight:
    57. wl = -width;
    58. break;
    59. default:
    60. wl = 0.0;
    61. break;
    62. }
    63. switch (mtxt.Attachment)
    64. {
    65. case AttachmentPoint.TopLeft:
    66. case AttachmentPoint.TopCenter:
    67. case AttachmentPoint.TopRight:
    68. hb = -height;
    69. break;
    70. case AttachmentPoint.MiddleLeft:
    71. case AttachmentPoint.MiddleCenter:
    72. case AttachmentPoint.MiddleRight:
    73. hb = height * -0.5;
    74. break;
    75. default:
    76. hb = 0.0;
    77. break;
    78. }
    79. wr = width + wl;
    80. ht = height + hb;
    81. degree = mtxt.Rotation;
    82. center = mtxt.Location;
    83. }
    84. else if ("DBText".Equals(ent.GetType().Name, StringComparison.CurrentCultureIgnoreCase))
    85. {
    86. DBText txt = (DBText)ent;
    87. ismtxt = false;
    88. degree = txt.Rotation;
    89. center = txt.Position;
    90. }
    91. ent.TransformBy(Matrix3d.Rotation(-degree, Vector3d.ZAxis, center));
    92. Extents3d textExtents = ent.GeometricExtents;
    93. ent.TransformBy(Matrix3d.Rotation(degree, Vector3d.ZAxis, center));
    94. if (ismtxt)
    95. {
    96. minpt = new Point3d(center.X + wl, center.Y + hb, 0);
    97. maxpt = new Point3d(center.X + wr, center.Y + ht, 0);
    98. }
    99. else
    100. {
    101. minpt = textExtents.MinPoint;
    102. maxpt = textExtents.MaxPoint;
    103. }
    104. Polyline pl = db.AddRectToModelSpace(minpt, maxpt);
    105. pl.TransformBy(Matrix3d.Rotation(degree, Vector3d.ZAxis, center));
    106. //添加图形到块表记录
    107. btr.AppendEntity(pl);
    108. //更新数据信息
    109. tr.AddNewlyCreatedDBObject(pl, true);
    110. //提交事务
    111. tr.Commit();
    112. }
    113. }
    114. /// <summary>
    115. /// 通过两点绘制矩形
    116. /// </summary>
    117. /// <param name="db">图形数据库</param>
    118. /// <param name="point1">第一点</param>
    119. /// <param name="point2">第二点</param>
    120. /// <returns>Entity图形</returns>
    121. public static Polyline AddRectToModelSpace(this Database db, Point3d point1, Point3d point2)
    122. {
    123. //声明多段线
    124. Polyline pline = new Polyline();
    125. //计算矩形的四个顶点
    126. Point2d p1 = new Point2d(Math.Min(point1.X, point2.X), Math.Min(point1.Y, point2.Y));
    127. Point2d p2 = new Point2d(Math.Max(point1.X, point2.X), Math.Min(point1.Y, point2.Y));
    128. Point2d p3 = new Point2d(Math.Max(point1.X, point2.X), Math.Max(point1.Y, point2.Y));
    129. Point2d p4 = new Point2d(Math.Min(point1.X, point2.X), Math.Max(point1.Y, point2.Y));
    130. //添加多段线的顶点
    131. pline.AddVertexAt(0, p1, 0, 0, 0);
    132. pline.AddVertexAt(1, p2, 0, 0, 0);
    133. pline.AddVertexAt(2, p3, 0, 0, 0);
    134. pline.AddVertexAt(3, p4, 0, 0, 0);
    135. //闭合多段线
    136. pline.Closed = true;
    137. return pline;
    138. }
    139. /// <summary>
    140. /// 通过组码获取对象
    141. /// </summary>
    142. /// <param name="value">组码数组</param>
    143. /// <returns>选择集</returns>
    144. public static SelectionSet SelectOnScreen(TypedValue[] value)
    145. {
    146. Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
    147. SelectionFilter filter = new SelectionFilter(value);
    148. PromptSelectionResult psr = ed.GetSelection(filter);
    149. if (psr.Status == PromptStatus.OK)
    150. {
    151. return psr.Value;
    152. }
    153. return null;
    154. }
    155. }
    156. }

    TBox.mp4 (2.16MB)