中望CAD下C#添加一個(gè)圖元

環(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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 相對(duì)于開(kāi)發(fā)中望CAD獨(dú)立的EXE,開(kāi)發(fā)中望CAD的插件跟AUTOCAD幾乎沒(méi)有差別。 1.新建工程 正常新建一個(gè)C...
    qlaiaqu閱讀 2,280評(píng)論 0 0
  • 簡(jiǎn)介 很多規(guī)劃領(lǐng)域的用戶在線修改規(guī)劃設(shè)計(jì)方案時(shí),需要修改建筑位置和朝向,想要進(jìn)行平移旋轉(zhuǎn)和改變基面高度操作,以重新...
    Xuds閱讀 1,468評(píng)論 3 2
  • 16宿命:用概率思維提高你的勝算 以前的我是風(fēng)險(xiǎn)厭惡者,不喜歡去冒險(xiǎn),但是人生放棄了冒險(xiǎn),也就放棄了無(wú)數(shù)的可能。 ...
    yichen大刀閱讀 7,721評(píng)論 0 4
  • 公元:2019年11月28日19時(shí)42分農(nóng)歷:二零一九年 十一月 初三日 戌時(shí)干支:己亥乙亥己巳甲戌當(dāng)月節(jié)氣:立冬...
    石放閱讀 7,419評(píng)論 0 2

友情鏈接更多精彩內(nèi)容