////////////////////////////////////////////////////////////////////////////////////////////////////
// This example was designed for using in Delphi 7 or higher.
//
// 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 "Universal Document Converter Type Library" and "AutoCAD XXXX Type Library" type libraries to the project.
// XXXX is the AutoCAD version installed on your computer.
//
// Delphi 7:
// Use the Project | Import Type Library menu.
// Delphi 2006 or latter:
// Use the Component | Import Component menu.
//
// Clear the "Generate Component Wrapper" checkbox and click the "Create Unit" button (Delphi 7) or
// select the "Create Unit" option (Delphi 2006 or latter).
//
////////////////////////////////////////////////////////////////////////////////////////////////////
program AutoCADtoPDF;
{$APPTYPE CONSOLE}
uses
SysUtils,
Variants,
Windows,
Dialogs,
ActiveX,
UDC_TLB,
AXDBLib_TLB,
AutoCAD_TLB;
procedure PrintAutoCADtoPDF(AutoCADFilePath: string);
var
objUDC: IUDC;
Printer: IUDCPrinter;
Profile: IProfile;
App: AcadApplication;
Doc: AcadDocument;
Layout: AcadLayout;
Version: Double;
FormatSettings: TFormatSettings;
ReadOnly, Password: OleVariant;
nBACKGROUNDPLOT, nFILEDIA, nCMDDIA: OleVariant;
xNull: OleVariant;
SaveChanges: OleVariant;
begin
//Create a UDC object and get its interfaces
objUDC := CoAPIWrapper.Create;
Printer := objUDC.get_Printers('Universal Document Converter');
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 Windows API's SHGetSpecialFolderPath//or JCL's JclSysInfo.GetAppdataFolder function.//Or you can move default profiles into a folder you prefer.Profile.Load('Drawing to PDF.xml');
Profile.OutputLocation.Mode := LM_PREDEFINED;
Profile.OutputLocation.FolderPath := 'c:\UDC Output Files';
Profile.PostProcessing.Mode := PP_OPEN_FOLDER;
App := CoAcadApplication.Create;
//double Version = double.Parse(App.Version.Substring(0, 4), new CultureInfo("en-US"));
GetLocaleFormatSettings($0409{en_US}, FormatSettings);
Version := StrToFloat(Copy(App.Version, 1, 4), FormatSettings);
//Open drawing from file
ReadOnly := False;
Password := Variants.EmptyParam;
Doc := App.Documents.Open(AutoCADFilePath, ReadOnly, Password);
//Change AutoCAD preferences for scaling the drawing to page
if Doc.ActiveSpace = acPaperSpace then
Layout := Doc.PaperSpace.Layout
else//Doc.ActiveSpace = acModelSpace
Layout := Doc.ModelSpace.Layout;
Layout.PlotType := acExtents;
Layout.UseStandardScale := True;
Layout.StandardScale := acScaleToFit;
Layout.CenterPlot := True;
nBACKGROUNDPLOT := 0;
nFILEDIA := 0;
nCMDDIA := 0;
if Version >= 16.1then
begin
nBACKGROUNDPLOT := Doc.GetVariable('BACKGROUNDPLOT');
nFILEDIA := Doc.GetVariable('FILEDIA');
nCMDDIA := Doc.GetVariable('CMDDIA');
xNull := 0;
Doc.SetVariable('BACKGROUNDPLOT', xNull);
Doc.SetVariable('FILEDIA', xNull);
Doc.SetVariable('CMDDIA', xNull);
end;
Doc.Plot.QuietErrorMode := True;
//Plot the drawing
Doc.Plot.PlotToDevice('Universal Document Converter');
if Version >= 16.1then
begin
//Restore AutoCAD default preferences
Doc.SetVariable('BACKGROUNDPLOT', nBACKGROUNDPLOT);
Doc.SetVariable('FILEDIA', nFILEDIA);
Doc.SetVariable('CMDDIA', nCMDDIA);
end;
//Close drawing
SaveChanges := False;
Doc.Close(SaveChanges, Variants.EmptyParam);
//Close Autodesk AutoCAD
App.Quit();
end;
var
TestFilePath: string;
begin
TestFilePath := ExtractFilePath(ParamStr(0)) + 'TestFile.dwg';
try
CoInitialize(nil);
try
PrintAutoCADtoPDF(TestFilePath);
finally
CoUninitialize;
end;
except
on E: Exception do
MessageDlg(E.ClassName + ' : ' + E.Message, mtError, [mbOK], 0);
end;
end.