C# 在AutoCAD中构建dll以运行python脚本

C# 在AutoCAD中构建dll以运行python脚本,c#,python,dll,ironpython,C#,Python,Dll,Ironpython,我的目标是在Autocad中运行python脚本。为了做到这一点,我正在使用IronPython2.7.9创建一个NetAPI。遇到的一个困难是Autocad使用自定义属性来标识命令,因此我的计划是使用以下代码,该代码允许选择和加载Python脚本: using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Runtime; usin

我的目标是在Autocad中运行python脚本。为了做到这一点,我正在使用IronPython2.7.9创建一个NetAPI。遇到的一个困难是Autocad使用自定义属性来标识命令,因此我的计划是使用以下代码,该代码允许选择和加载Python脚本:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.EditorInput;

using IronPython.Hosting;

using Microsoft.Scripting.Hosting;

using System;



namespace PythonLoader

{

  public class CommandsAndFunctions

  {

    [CommandMethod("-PYLOAD")]

    public static void PythonLoadCmdLine()

    {

      PythonLoad(true);

    }



    [CommandMethod("PYLOAD")]

    public static void PythonLoadUI()

    {

      PythonLoad(false);

    }



    public static void PythonLoad(bool useCmdLine)

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Editor ed = doc.Editor;



      short fd =

        (short)Application.GetSystemVariable("FILEDIA");



      // As the user to select a .py file



      PromptOpenFileOptions pfo =

          new PromptOpenFileOptions(

            "Select Python script to load"

          );

      pfo.Filter = "Python script (*.py)|*.py";

      pfo.PreferCommandLine =

        (useCmdLine || fd == 0);

      PromptFileNameResult pr =

        ed.GetFileNameForOpen(pfo);



      // And then try to load and execute it



      if (pr.Status == PromptStatus.OK)

        ExecutePythonScript(pr.StringResult);

    }



    [LispFunction("PYLOAD")]

    public ResultBuffer PythonLoadLISP(ResultBuffer rb)

    {

      const int RTSTR = 5005;



      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Editor ed = doc.Editor;



      if (rb == null)

      {

        ed.WriteMessage("\nError: too few arguments\n");

      }

      else

      {

        // We're only really interested in the first argument



        Array args = rb.AsArray();

        TypedValue tv = (TypedValue)args.GetValue(0);



        // Which should be the filename of our script



        if (tv != null && tv.TypeCode == RTSTR)

        {

          // If we manage to execute it, let's return the

          // filename as the result of the function

          // (just as (arxload) does)



          bool success =

            ExecutePythonScript(Convert.ToString(tv.Value));

          return

            (success ?

              new ResultBuffer(

                new TypedValue(RTSTR, tv.Value)

              )

              : null);

        }

      }

      return null;

    }



    private static bool ExecutePythonScript(string file)

    {

      // If the file exists, let's load and execute it

      // (we could/should probably add some more robust

      // exception handling here)



      bool ret = System.IO.File.Exists(file);

      if (ret)

      {

        ScriptEngine engine = Python.CreateEngine();

        engine.ExecuteFile(file);

      }

      return ret;

    }

  }

}

在本文中,作者说我需要为该脚本构建一个.dll,并添加对以下内容的引用:

IronPython.dll
IronPythonmodules.dll
Microsoft.Scripting.dll
Microsoft.Scripting.Core.dll
以及“标准参考文件”中的

我已经下载了一个反编译器,并开始查看这些.dll的代码,当然还有许多不同的代码要查看。如何确定在何处添加上面发布的PYLOAD脚本的引用,以便开始在Autocad中执行python脚本


非常感谢,

首先,您需要编译发布到程序集中的.NET代码(例如,将其称为“PythonLoader.dll”)。一旦您用PythonLoader代码创建了一个程序集,您就可以在pyloadironpython脚本中引用它,方法是按照其中一组指令加载程序集。作者给出的示例使用了
clr.AddReferenceToFileAndPath
,因此我可能也会从这一点开始

>>> import clr
>>> clr.AddReferenceToFileAndPath(path_to_PythonLoader.dll)

请注意,您需要将.NET代码编译为与IronPython环境目标框架版本相匹配的.NET核心或.NET Framework版本。(从上看,它的目标似乎是.NET Framework 4.5、.NET Core 2.0和.NET Core 2.1。)

请注意,您引用的博客文章来自2009年,最新的IronPython版本已经有一年多的历史,并且是根据Python 2标准编写的。由组织正式维护Python,因此该组织将来不会更新其标准。
>>> import clr
>>> clr.AddReferenceToFileAndPath(path_to_PythonLoader.dll)