如何在autocad中运行以下代码?

时间:2016-08-26 05:21:01

标签: c# autocad

您好,这是我的代码,我不知道如何运行并获取此代码的输出。请建议我这个答案。我想使用此代码创建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);
    }
}
}

2 个答案:

答案 0 :(得分:3)

您是否阅读了original source中的评论?

此代码是使用第三方应用程序名称Universal Document Converter(UDC)构建独立应用程序(exe)以将dwg文件的活动空间打印到pdf文件中的示例。 它需要安装UDC软件。 它无法转换为AutoCAD插件(带有CommandMethod的DLL)。 您当然可以使用UDC Support获取更多有关此信息的信息。

您不会通过复制您不理解的网络上的代码来学习.NET和AutoCAD API,并要求此处或其他人修改它们以满足您的需求。

答案 1 :(得分:0)

首先:将一个使用添加到运行时。

using Autodesk.AutoCAD.Runtime;

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

[CommandMethod("YOURCOMMANDNAMEINAUTOCAD")]

上一篇:您的课程和方法需要公开,以便AutoCAD查看。

更新:(最后):您的方法无法获取参数。