AutoCAD二次開發(.net教程)C#版——學習筆記(二)

選擇點和計算兩點的距離

選擇點和計算兩點的距離主要是通過PromptPointOptions和 PromptDistanceOptions兩個類獲取並通過PromptPointResult和 PromptDoubleResult兩個類獲取對象的值,最後通過Application.DocumentManager.MdiActiveDocument.Editor輸入到CAD的命令窗口。

其中,PromptPointOptions類用來設置提示字符串和其他的一些控制提示的選項。兩個功能的代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

namespace SelectAPoint
{
    public class Class1
    {
        [CommandMethod("SelectAPoint")]
        public void SelectAPoint()
        {
            //實例化一個 PromptPointOptions類用來設置提示字符串和其他的一些控制提示
            PromptPointOptions prPointOptions = new PromptPointOptions("Select a point");
            PromptPointResult prPointRes;
            // 實例化一個Editor類,使用GetPoint方法返回
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            prPointRes = ed.GetPoint(prPointOptions);
            if (prPointRes.Status != PromptStatus.OK)
            {
                ed.WriteMessage("Error");
            }
            else
            {
                ed.WriteMessage("選擇的點爲:" + prPointRes.Value.ToString());
            }
        }
        [CommandMethod("getDistance")]
        public void GetDistance()
        {
            PromptDistanceOptions prDistOptions = new
            PromptDistanceOptions("計算兩點距離,請選擇第一個點:");
            PromptDoubleResult prDistRes;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            prDistRes = ed.GetDistance(prDistOptions);
            if (prDistRes.Status != PromptStatus.OK)
            {
                ed.WriteMessage("選擇錯誤!");
            }
            else
            {
                ed.WriteMessage("兩點的距離爲:" + prDistRes.Value.ToString());
            }
        }
    }
}


發佈了40 篇原創文章 · 獲贊 11 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章