调试环境:中望CAD2014;
引用:ZwDatabaseMgd.dll、ZwManaged.dll两个类库!
using System;
using ZwSoft.ZwCAD.ApplicationServices;
using ZwSoft.ZwCAD.DatabaseServices;
using ZwSoft.ZwCAD.EditorInput;
using ZwSoft.ZwCAD.Geometry;
using ZwSoft.ZwCAD.Runtime;
namespace TextBox
{
public static class Class1
{
[CommandMethod("TBox")]
public static void MakeTextBox()
{
SelectionSet ss = SelectOnScreen(new TypedValue[] { new TypedValue(0, "*TEXT") });//ed.GetSelection().Value;
foreach (ObjectId id in ss.GetObjectIds())
{
id.MakeRectangle();
}
}
/// <summary>
/// 创建文字外框
/// </summary>
/// <param name="id">旋转对象ObjectId</param>
/// <param name="center">旋转中心点</param>
/// <param name="degree">旋转弧度</param>
public static void MakeRectangle(this ObjectId id)//, Point3d center, double degree, double wl, double wr, double hb, double ht
{
bool ismtxt = false;
double degree = 0, wl = 0, wr = 0, ht = 0, hb = 0;
Point3d center = new Point3d(0, 0, 0), minpt = new Point3d(0, 0, 0), maxpt = new Point3d(0, 0, 0);
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
//打开块表
//BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
//打开块表记录
//BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
//打开块表记录
BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
Entity ent = (Entity)id.GetObject(OpenMode.ForWrite);
if ("MText".Equals(ent.GetType().Name, StringComparison.CurrentCultureIgnoreCase))
{
MText mtxt = (MText)ent;
ismtxt = true;
double width = mtxt.ActualWidth;
double height = mtxt.ActualHeight;
switch (mtxt.Attachment)
{
case AttachmentPoint.TopCenter:
case AttachmentPoint.MiddleCenter:
case AttachmentPoint.BottomCenter:
wl = width * -0.5;
break;
case AttachmentPoint.TopRight:
case AttachmentPoint.MiddleRight:
case AttachmentPoint.BottomRight:
wl = -width;
break;
default:
wl = 0.0;
break;
}
switch (mtxt.Attachment)
{
case AttachmentPoint.TopLeft:
case AttachmentPoint.TopCenter:
case AttachmentPoint.TopRight:
hb = -height;
break;
case AttachmentPoint.MiddleLeft:
case AttachmentPoint.MiddleCenter:
case AttachmentPoint.MiddleRight:
hb = height * -0.5;
break;
default:
hb = 0.0;
break;
}
wr = width + wl;
ht = height + hb;
degree = mtxt.Rotation;
center = mtxt.Location;
}
else if ("DBText".Equals(ent.GetType().Name, StringComparison.CurrentCultureIgnoreCase))
{
DBText txt = (DBText)ent;
ismtxt = false;
degree = txt.Rotation;
center = txt.Position;
}
ent.TransformBy(Matrix3d.Rotation(-degree, Vector3d.ZAxis, center));
Extents3d textExtents = ent.GeometricExtents;
ent.TransformBy(Matrix3d.Rotation(degree, Vector3d.ZAxis, center));
if (ismtxt)
{
minpt = new Point3d(center.X + wl, center.Y + hb, 0);
maxpt = new Point3d(center.X + wr, center.Y + ht, 0);
}
else
{
minpt = textExtents.MinPoint;
maxpt = textExtents.MaxPoint;
}
Polyline pl = db.AddRectToModelSpace(minpt, maxpt);
pl.TransformBy(Matrix3d.Rotation(degree, Vector3d.ZAxis, center));
//添加图形到块表记录
btr.AppendEntity(pl);
//更新数据信息
tr.AddNewlyCreatedDBObject(pl, true);
//提交事务
tr.Commit();
}
}
/// <summary>
/// 通过两点绘制矩形
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="point1">第一点</param>
/// <param name="point2">第二点</param>
/// <returns>Entity图形</returns>
public static Polyline AddRectToModelSpace(this Database db, Point3d point1, Point3d point2)
{
//声明多段线
Polyline pline = new Polyline();
//计算矩形的四个顶点
Point2d p1 = new Point2d(Math.Min(point1.X, point2.X), Math.Min(point1.Y, point2.Y));
Point2d p2 = new Point2d(Math.Max(point1.X, point2.X), Math.Min(point1.Y, point2.Y));
Point2d p3 = new Point2d(Math.Max(point1.X, point2.X), Math.Max(point1.Y, point2.Y));
Point2d p4 = new Point2d(Math.Min(point1.X, point2.X), Math.Max(point1.Y, point2.Y));
//添加多段线的顶点
pline.AddVertexAt(0, p1, 0, 0, 0);
pline.AddVertexAt(1, p2, 0, 0, 0);
pline.AddVertexAt(2, p3, 0, 0, 0);
pline.AddVertexAt(3, p4, 0, 0, 0);
//闭合多段线
pline.Closed = true;
return pline;
}
/// <summary>
/// 通过组码获取对象
/// </summary>
/// <param name="value">组码数组</param>
/// <returns>选择集</returns>
public static SelectionSet SelectOnScreen(TypedValue[] value)
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
SelectionFilter filter = new SelectionFilter(value);
PromptSelectionResult psr = ed.GetSelection(filter);
if (psr.Status == PromptStatus.OK)
{
return psr.Value;
}
return null;
}
}
}