環(huán)境配置完了之后,現(xiàn)在開(kāi)始正式進(jìn)入開(kāi)發(fā)階段,此處使用一個(gè)示例,選擇圖紙中的一個(gè)Text,計(jì)算器OBB包圍盒,并顯示出來(lái)。
1.示例代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZwSoft.ZwCAD.ApplicationServices;
using ZwSoft.ZwCAD.DatabaseServices;
using ZwSoft.ZwCAD.EditorInput;
using ZwSoft.ZwCAD.Runtime;
using ZwSoft.ZwCAD.Geometry;
namespace ZWCADStartup
{
public class CreateRectangleCommand
{
[CommandMethod("CR")]
public void CreateRectangle()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
//選擇一個(gè)DBText
var entOpts = new PromptEntityOptions("\n選擇Text對(duì)象: ");
entOpts.SetRejectMessage("\n所選對(duì)象非Text類型.");
entOpts.AddAllowedClass(typeof(DBText), true);
var entRes = ed.GetEntity(entOpts);
if (entRes.Status != PromptStatus.OK)
return;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
// 提取DBText的轉(zhuǎn)換矩陣
var text = (DBText)tr.GetObject(entRes.ObjectId, OpenMode.ForWrite);
var plane = new Plane(Point3d.Origin, text.Normal);
var xform =
Matrix3d.Rotation(text.Rotation, text.Normal, text.Position) *
Matrix3d.Displacement(text.Position.GetAsVector()) *
Matrix3d.PlaneToWorld(plane);
// DBText逆向回原始位置
text.TransformBy(xform.Inverse());
// 計(jì)算DBText逆向后的包圍盒
var extents = text.GeometricExtents;
double minX = extents.MinPoint.X;
double minY = extents.MinPoint.Y;
double maxX = extents.MaxPoint.X;
double maxY = extents.MaxPoint.Y;
// 繪制包圍盒
using (var pline = new Polyline())
{
pline.AddVertexAt(0, new Point2d(minX, minY), 0.0, 0.0, 0.0);
pline.AddVertexAt(1, new Point2d(maxX, minY), 0.0, 0.0, 0.0);
pline.AddVertexAt(2, new Point2d(maxX, maxY), 0.0, 0.0, 0.0);
pline.AddVertexAt(3, new Point2d(minX, maxY), 0.0, 0.0, 0.0);
pline.Closed = true;
pline.Color = ZwSoft.ZwCAD.Colors.Color.FromRgb(255, 0, 0);
var space = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
space.AppendEntity(pline);
tr.AddNewlyCreatedDBObject(pline, true);
//應(yīng)用DBText的轉(zhuǎn)換矩陣
pline.TransformBy(xform);
}
// 恢復(fù)DBText的位置
text.TransformBy(xform);
tr.Commit();
}
}
}
}
2.測(cè)試
(1)F5執(zhí)行代碼
(2)進(jìn)入界面后,執(zhí)行NETLOAD,選擇加載生成的類庫(kù)文件。
(3)輸入CR命令,選擇一個(gè)DBText對(duì)象。(此處請(qǐng)先添加好)
(4)查看效果

Snipaste_2021-08-25_10-51-20.png