////////////////////////////////////////////////////////////////////////////////////////////////////// This example was designed for using in Microsoft Visual C# from // Microsoft Visual Studio 2003 or above.//// 1. Autodesk AutoCAD 2000 or above should be installed and activated on your PC.// Autodesk AutoCAD LT does not have COM interface and cannot be used as COM-server!//// 2. Universal Document Converter 5.2 or above should be installed, too.//// 3. Add references to "AutoCAD 2006 Type Library", "AutoCAD/ObjectDBX Common 16.0 Type Library" and // "Universal Document Converter Type Library" using the Project | Add Reference menu > COM tab.// The version numbers in the type library names may be different depending on AutoCAD's version // installed on your computer.////////////////////////////////////////////////////////////////////////////////////////////////////using System;
using System.IO;
using System.Globalization;
using UDC;
using AutoCAD = Autodesk.AutoCAD.Interop;
namespace AutoCADtoPDF
{
classProgram
{
staticvoid PrintAutoCADtoPDF(string AutoCADFilePath)
{
//Create a UDC object and get its interfacesIUDC objUDC = newAPIWrapper();
IUDCPrinter Printer = objUDC.get_Printers("Universal Document Converter");
IProfile Profile = Printer.Profile;
//Use Universal Document Converter API to change settings of converterd drawing
Profile.Load(@"C:\Program Files\Universal Document Converter\UDC Profiles\Drawing to PDF.xml");
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), newCultureInfo("en-US"));
//Open drawing from fileObject 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 pageif(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 drawingObject SaveChanges = false;
Doc.Close(SaveChanges, Type.Missing);
//Close Autodesk AutoCAD
App.Quit();
}
staticvoid Main(string[] args)
{
string TestFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFile.dwg");
PrintAutoCADtoPDF(TestFilePath);
}
}
}