-1

Hello this is my code and i don't know how to run and get output of this code. Please suggest me the answer for this.And I want to create command for autocad using this code so suggest me according to this requirement.

 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);
    }
}
}
ArK
  • 20,698
  • 67
  • 109
  • 136
jackson
  • 27
  • 6

2 Answers2

3

Did you read the comments in the original source ?

This code is a example of using a third part application name Universal Document Converter (UDC) to build a standalone application (exe) to print the active space of a dwg file into a pdf file. It requires the UDC software to be installed. It cannot be transformed into an AutoCAD plugin (dll with CommandMethod). You certainly can get more informations about this with the UDC Support.

You will not learn .NET and AutoCAD API by copying codes found on the web that you do not understand and asking someone here or elsewhere to modify them to suit your needs.

gileCAD
  • 2,295
  • 1
  • 10
  • 10
  • Ah... I didn't even read trough the entire code. About the second statement: So true :-) – Alain Aug 26 '16 at 08:34
0

first: add a using to the runtime.

using Autodesk.AutoCAD.Runtime;

next: Add an attribute to your method.

[CommandMethod("YOURCOMMANDNAMEINAUTOCAD")]

Last: Your class and method need to be public, for AutoCAD to see them.

Update: (Very last): your Method cannot take parameters.

Alain
  • 304
  • 3
  • 9