下面的代码如何在autocad中运行

本文关键字:autocad 运行 代码 | 更新日期: 2023-09-27 18:04:09

你好,这是我的代码,我不知道如何运行和得到这段代码的输出。请给我一个答案。我想用这个代码创建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);
    }
}
}

下面的代码如何在autocad中运行

你看过原文中的评论了吗?

此代码是使用第三部分应用程序名称通用文档转换器(Universal Document Converter, UDC)构建独立应用程序(exe)以将dwg文件的活动空间打印成pdf文件的示例。需要安装UDC软件。它不能转换为AutoCAD插件(带有CommandMethod的dll)。您当然可以通过UDC支持获得有关此的更多信息。

通过复制在网上找到的你不理解的代码并要求别人在这里或其他地方修改它们以适应你的需要,你将无法学习。net和AutoCAD API。

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

using Autodesk.AutoCAD.Runtime;

next:为你的方法添加一个属性

[CommandMethod("YOURCOMMANDNAMEINAUTOCAD")]

最后:您的类和方法需要是公共的,以便AutoCAD可以看到它们。

更新:(最后):你的方法不能接受参数