autocad 中的以下代码 运行 怎么样?

Hows the following code run in autocad?

你好,这是我的代码,我不知道如何 运行 并获取此代码的输出。请给我建议 this.And 的答案我想使用此代码为 autocad 创建命令,因此请根据此要求向我建议。

 using System;
 using System.IO;
 using System.Globalization;
 using UDC;
 using AutoCAD = Autodesk.AutoCAD.Interop;

 namespace AutoCADtoPDF
 {
class Program
{
    static void PrintAutoCADtoPDF(string AutoCADFilePath)
    {
        //Create a UDC object and get its interfaces
        IUDC objUDC = new APIWrapper();
        IUDCPrinter Printer = objUDC.get_Printers("Universal Document Converter");
        IProfile Profile = Printer.Profile;

        //Use Universal Document Converter API to change settings of converterd drawing

        //Load profile located in folder "%APPDATA%\UDC Profiles".
        //Value of %APPDATA% variable should be received using Environment.GetFolderPath method.
        //Or you can move default profiles into a folder you prefer.          
        string AppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        string ProfilePath = Path.Combine(AppDataPath, @"UDC Profiles\Drawing to PDF.xml");
        Profile.Load(ProfilePath);

        Profile.OutputLocation.Mode = LocationModeID.LM_PREDEFINED;
        Profile.OutputLocation.FolderPath = @"c:\UDC Output Files";

        Profile.PostProcessing.Mode = PostProcessingModeID.PP_OPEN_FOLDER;

        AutoCAD.AcadApplication App = new AutoCAD.AcadApplicationClass();

        double Version = double.Parse(App.Version.Substring(0, 4), new CultureInfo("en-US"));

        //Open drawing from file
        Object ReadOnly = false;
        Object Password = Type.Missing;
        AutoCAD.AcadDocument Doc = App.Documents.Open(AutoCADFilePath, ReadOnly, Password);

        //AutoCAD.Common.AcadPaperSpace ActiveSpace;
        AutoCAD.Common.AcadLayout Layout;

        //Change AutoCAD preferences for scaling the drawing to page
        if (Doc.ActiveSpace == 0)
            Layout = Doc.PaperSpace.Layout;
        else
            Layout = Doc.ModelSpace.Layout;

        Layout.PlotType = AutoCAD.Common.AcPlotType.acExtents;
        Layout.UseStandardScale = true;
        Layout.StandardScale = AutoCAD.Common.AcPlotScale.acScaleToFit;
        Layout.CenterPlot = true;

        Object nBACKGROUNDPLOT = 0, nFILEDIA = 0, nCMDDIA = 0;
        if (Version >= 16.1f)
        {
            nBACKGROUNDPLOT = Doc.GetVariable("BACKGROUNDPLOT");
            nFILEDIA = Doc.GetVariable("FILEDIA");
            nCMDDIA = Doc.GetVariable("CMDDIA");

            Object xNull = 0;
            Doc.SetVariable("BACKGROUNDPLOT", xNull);
            Doc.SetVariable("FILEDIA", xNull);
            Doc.SetVariable("CMDDIA", xNull);
        }

        Doc.Plot.QuietErrorMode = true;

        //Plot the drawing
        Doc.Plot.PlotToDevice("Universal Document Converter");

        if (Version >= 16.1f)
        {
            //Restore AutoCAD default preferences
            Doc.SetVariable("BACKGROUNDPLOT", nBACKGROUNDPLOT);
            Doc.SetVariable("FILEDIA", nFILEDIA);
            Doc.SetVariable("CMDDIA", nCMDDIA);
        }

        //Close drawing
        Object SaveChanges = false;
        Doc.Close(SaveChanges, Type.Missing);

        //Close Autodesk AutoCAD
        App.Quit();
    }

    static void Main(string[] args)
    {
        string TestFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFile.dwg");
        PrintAutoCADtoPDF(TestFilePath);
    }
}
}

首先:在运行时添加一个using。

using Autodesk.AutoCAD.Runtime;

下一步:为您的方法添加属性。

[CommandMethod("YOURCOMMANDNAMEINAUTOCAD")]

最后:您的 class 和方法需要 public,AutoCAD 才能看到它们。

更新:(最后):你的方法不能带参数。

您阅读 original source 中的评论了吗?

此代码是使用第三方应用程序名称 Universal Document Converter (UDC) 构建独立应用程序 (exe) 以将 dwg 文件的活动 space 打印到 pdf 文件中的示例. 它需要安装 UDC 软件。 它不能转换为 AutoCAD 插件(带有 CommandMethod 的 dll)。 您当然可以通过 UDC Support.

获得更多相关信息

您不会通过复制网上找到的您不理解的代码并请这里或其他地方的人修改它们以满足您的需要来学习 .NET 和 AutoCAD API。