C# CAD二次开发消息提示技巧

在 C# 中进行 CAD 二次开发时,向用户提示消息主要有以下几种方式,具体选择取决于开发平台(如 AutoCAD .NET API 或 NX Open)和消息的用途(如信息提示、警告、错误或命令行交互)。

1. 使用 AutoCAD .NET API

AutoCAD .NET API 提供了 Editor 类来与命令行和用户交互。

a. 在命令行输出简单消息

使用 Editor.WriteMessage 方法将消息输出到 AutoCAD 命令行。

csharp 复制代码
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;

public void ShowMessageInCommandLine()
{
    // 获取当前文档的编辑器    Document doc = Application.DocumentManager.MdiActiveDocument;
    Editor ed = doc.Editor;

    // 向命令行输出消息
    ed.WriteMessage("
操作已完成!请检查图形。");
}

b. 弹出消息对话框

使用 Autodesk.AutoCAD.ApplicationServices.ApplicationShowAlertDialog 方法弹出模态警告框。

csharp 复制代码
using Autodesk.AutoCAD.ApplicationServices;

public void ShowAlertDialog()
{
    // 弹出警告对话框 Application.ShowAlertDialog("文件保存失败,请检查磁盘空间!");
}

c. 使用更灵活的任务对话框 (AcadMessageBox)

对于更复杂的交互,可以使用 Autodesk.AutoCAD.Runtime 命名空间下的 MessageBox(它是 System.Windows.Forms.MessageBox 的包装)。

csharp 复制代码
using Autodesk.AutoCAD.Runtime;
using System.Windows.Forms;

public void ShowTaskDialog()
{
    // 弹出信息提示框
    MessageBox.Show("是否继续执行批量操作?", "确认", MessageBoxButtons.YesNo);
}

2. 使用 NX Open API (针对 Siemens NX)

在 NX 二次开发中,主要通过 UI 命名空间下的类来显示消息。

a. 显示信息消息框

使用 UI.GetUI().ShowMessage 方法。

csharp 复制代码
using NXOpen;
using NXOpen.UI;

public void ShowNXMessage()
{
    // 获取UI对象
    UI theUI = UI.GetUI();

    // 显示信息提示框
    theUI.ShowMessage("NX 提示", NXMessageBox.DialogType.Information, "零件计算完成!");
}

b. 在NX信息窗口输出消息

使用 ListingWindow 在 NX 的信息窗口输出日志或提示。

csharp 复制代码
using NXOpen;

public void WriteToListingWindow()
{
    // 获取Session对象
    Session theSession = Session.GetSession();

    // 获取ListingWindow对象    ListingWindow lw = theSession.ListingWindow;

    // 打开并写入信息
    lw.Open();
    lw.WriteLine("正在执行分析,请稍候...");
    lw.WriteLine("分析步骤1: 已完成");
}

3. 方法对比与选择建议

下表对比了不同场景下的推荐方法:

消息类型 / 需求场景 AutoCAD .NET API 推荐方法 NX Open API 推荐方法 说明
操作状态提示 (非阻塞) Editor.WriteMessage() Session.ListingWindow.WriteLine() 适合将进度、结果等提示输出到命令行或信息窗口,不中断用户操作。
关键警告或错误 (需用户确认) Application.ShowAlertDialog() UI.GetUI().ShowMessage() (类型为 Warning或 Error) 弹出模态对话框,强制用户注意并处理。
用户决策交互 (是/否/取消) Autodesk.AutoCAD.Runtime.MessageBox.Show() UI.GetUI().ShowMessage() (类型为 Question) 提供按钮让用户选择后续操作流程。
获取用户输入 (如坐标、文字) Editor.GetPoint(), Editor.GetString() UI.GetUI().GetInput() 系列方法 用于在命令执行过程中请求用户输入特定数据。

###4. 综合示例:一个自定义命令的消息提示

以下是一个 AutoCAD .NET API 的示例,它结合了命令行提示和对话框 。

csharp 复制代码
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System.Windows.Forms;

public class MessageCommands
{
    [CommandMethod("ShowMyMessage")]
    public void ShowMyMessage()
    {
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Editor ed = doc.Editor;

        try
        {
            // 1. 在命令行提示用户 ed.WriteMessage("
正在执行自定义命令...");

            // 2. 弹出信息对话框获取用户确认 DialogResult result = MessageBox.Show(
                "是否要在模型中绘制一条测试线?",
                "用户确认",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question
            );

            if (result == DialogResult.Yes)
            {
                // 3. 获取用户输入的点(这里简化了错误处理)
                PromptPointOptions ppo = new PromptPointOptions("
请指定直线的起点: ");
                PromptPointResult ppr = ed.GetPoint(ppo);
                if (ppr.Status == PromptStatus.OK)
                {
                    // ... 执行绘图操作 ...
                    ed.WriteMessage($"
直线起点已设置为: {ppr.Value}");
                }
            }
            else {
                // 4. 操作取消提示 ed.WriteMessage("
用户取消了绘图操作。");
            }
        }
        catch (System.Exception ex)
        {
            // 5. 错误处理:使用警告对话框 Application.ShowAlertDialog($"命令执行出错:
{ex.Message}");
        }
    }
}

核心要点

  • 命令行提示 (WriteMessage) 适用于流程性、非阻塞的信息反馈 。
  • 对话框 (ShowAlertDialog, MessageBox) 适用于需要用户立即注意或做出决定的场景。
  • NX OpenUI.ShowMessageListingWindow 的作用与上述类似,是 NX 环境下的标准交互方式 。
  • 在代码中合理组合这些方法,可以构建出用户友好、交互清晰的 CAD 二次开发应用程序。

参考来源