从 autocad 命令返回值到 windows 表单应用程序

Returning Values from a autocad command to windows form application

我创建了 AutoCAD 插件来测量线的距离。此外,我还创建了一个 windows 表单应用程序来加载我创建的插件。我尝试 return 使用我的 AutoCAD 插件中的命令测量的值到 windows 表单应用程序,但所有进入 vain.Some 的方式是:

I insert the result obtained in autocad and try to retrive that. I try the interface technique.

您可以将您的距离存储在 USERR1 to 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;
    }
}