中望CAD下C#添加一个图元

环境配置完了之后,现在开始正式进入开发阶段,此处使用一个示例,选择图纸中的一个Text,计算器OBB包围盒,并显示出来。
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;

            //选择一个DBText
            var entOpts = new PromptEntityOptions("\n选择Text对象: ");
            entOpts.SetRejectMessage("\n所选对象非Text类型.");
            entOpts.AddAllowedClass(typeof(DBText), true);
            var entRes = ed.GetEntity(entOpts);
            if (entRes.Status != PromptStatus.OK)
                return;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                // 提取DBText的转换矩阵
                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());

                // 计算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);
                    //应用DBText的转换矩阵
                    pline.TransformBy(xform);
                }

                // 恢复DBText的位置
                text.TransformBy(xform);
                tr.Commit();
            }
        }
    }
}

2.测试
(1)F5执行代码
(2)进入界面后,执行NETLOAD,选择加载生成的类库文件。
(3)输入CR命令,选择一个DBText对象。(此处请先添加好)
(4)查看效果


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

你可能感兴趣的:(中望CAD下C#添加一个图元)