008集—— Keyword关键字及getstring的用法(CAD—C#二次开发入门)

CAD二开中,经常会用到用户交互功能,常见的方法如下:

GetKeyword 方法提示用户在 AutoCAD 命令提示下输入关键字,GetString 方法提示用户在 AutoCAD 命令提示下输入字符串。两者就有区别又有相似处,getkeyword只允许用户输入指定的字符。

GetKeyword 方法:AllowNone 属性为False强制用户输入关键字,而禁止输入 NULL(按回车键)。Keywords 用于添加有效的关键字列表。

GetString 方法:该方法接收两个参数。PromptStringOptions 对象允许控制用户输入以及怎样显示提示信息。PromptStringOptions 对象的 AllowSpaces 属性控制是否允许输入空格。如果为 False ,按空格键将中止用户输入。

getkeyword案例如下:

附代码:

cs 复制代码
using AcTools;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Acdemo
{
    public  class Acdemo
    {

        [CommandMethod("xx")]
        public void ACDE()
        {
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database db = HostApplicationServices.WorkingDatabase;
            Line line = new Line(Point3d.Origin, new Point3d(100, 100, 0));
            Line line1 = new Line(Point3d.Origin, new Point3d(50, 200, 0));
            //db.AddEntityToModeSpace(line);
            PromptKeywordOptions pKeyOpts = new PromptKeywordOptions("");
            pKeyOpts.Message = "\nEnter an option ";
            pKeyOpts.Keywords.Add("Line");
            pKeyOpts.Keywords.Add("Circle");
            pKeyOpts.Keywords.Add("Arc");
            pKeyOpts.AllowNone = false;

            PromptResult pKeyRes = acDoc.Editor.GetKeywords(pKeyOpts);

            //Application.ShowAlertDialog("Entered keyword: " +
            //                            pKeyRes.StringResult);
            //if (pKeyRes.StringResult == "Line")
            //{
            //    db.AddLineToModeSpace(new Point3d(), new Point3d(100, 100, 0));

            //}
            switch (pKeyRes.StringResult)
            {
                case "Line":
                    Database db1 = HostApplicationServices.WorkingDatabase;
                    db1.AddEntityToModeSpace(line1);
                    break;
                case "Circle":
                    db.AddCircleModeSpace(new Point3d(), 100);
                    break;
                case "Arc":
                    db.AddArcToModeSpace(new Point3d(0,0,0), new Point3d(500, 100, 0), new Point3d(200, 200, 0));
                    break;

            }

        }
    }
}

getstring用法:

cs 复制代码
using AcTools;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Acdemo
{
    public  class Acdemo
    {
        public static void GetStringFromUser(string message)
        {
            Document acDoc = Application.DocumentManager.MdiActiveDocument;

            PromptStringOptions pStrOpts = new PromptStringOptions("\nEnter your name: ");
            pStrOpts.AllowSpaces = true;
            PromptResult pStrRes = acDoc.Editor.GetString(pStrOpts);

            Application.ShowAlertDialog(message +  "\nThe name entered was: " +
                                        pStrRes.StringResult);
        }

        [CommandMethod("xx")]
        public void ACDE()
        {  
            GetStringFromUser("Hello,world !");
        }
    }
}
相关推荐
钢门狂鸭2 小时前
关于rust的crates.io
开发语言·后端·rust
qq_508823402 小时前
金融数据库--3Baostock
数据库·金融
Lionel_SSL2 小时前
《深入理解Java虚拟机》第三章读书笔记:垃圾回收机制与内存管理
java·开发语言·jvm
技术猿188702783513 小时前
PHP 与 WebAssembly 的 “天然隔阂”
开发语言·php·wasm
悦数图数据库3 小时前
图技术重塑金融未来:悦数图数据库如何驱动行业创新与风控变革
数据库·金融
薄荷撞~可乐3 小时前
C#Task(Api)应用
开发语言·c#
九河云3 小时前
华为云 GaussDB:金融级高可用数据库,为核心业务保驾护航
网络·数据库·科技·金融·华为云·gaussdb
老华带你飞3 小时前
租房平台|租房管理平台小程序系统|基于java的租房系统 设计与实现(源码+数据库+文档)
java·数据库·小程序·vue·论文·毕设·租房系统管理平台
another heaven5 小时前
【Qt VS2022调试时无法查看QString等Qt变量信息】解决方法
开发语言·qt
A黄俊辉A5 小时前
axios+ts封装
开发语言·前端·javascript