从autocad命令返回值到windows窗体应用程序

本文关键字:windows 窗体 应用程序 返回值 autocad 命令 | 更新日期: 2023-09-27 17:53:12

我创建了AutoCAD插件来测量线的距离。此外,我还创建了一个windows窗体应用程序来加载我创建的插件。我试图在我的AutoCAD插件中返回使用命令测量的值到windows窗体应用程序,但都是徒劳的。我做的一些方法是:

我插入autocad中获得的结果并尝试检索。我尝试了接口技术。

从autocad命令返回值到windows窗体应用程序

您可以将您的距离存储在USERR1到USERR5系统变量中,然后使用Document.GetVariable COM API从外部进程读取它。

您可以在EndCommand事件上安装一个处理程序来检测命令何时完成。

下面是一些代码:
using Autodesk.AutoCAD.Interop;
[..]
void button1_Click(object sender, EventArgs e)
{
    const uint MK_E_UNAVAILABLE = 0x800401e3;
    AcadApplication acad;
    try
    {
        // Try to get a running instance of AutoCAD 2016
        acad = (AcadApplication) Marshal.GetActiveObject("AutoCAD.Application.20.1");
    }
    catch (COMException ex) when ((uint) ex.ErrorCode == MK_E_UNAVAILABLE)
    {
        // AutoCAD is not running, we start it
        acad = (AcadApplication) Activator.CreateInstance(Type.GetTypeFromProgID("AutoCAD.Application.20.1"));
    }
    activeDocument = acad.ActiveDocument;
    activeDocument.EndCommand += ActiveDocument_EndCommand;
    activeDocument.SendCommand("YOURCOMMAND ");
}
void ActiveDocument_EndCommand(string CommandName)
{
    if (CommandName != "YOURCOMMAND") return;
    try
    {
        double value = activeDocument.GetVariable("USERR1");
        // Process the value
        MessageBox.Show(value.ToString());
    }
    finally
    {
        // Remove the handler
        activeDocument.EndCommand -= ActiveDocument_EndCommand;
    }
}