Author Topic: Simple dwg to pdf plot  (Read 5308 times)

0 Members and 1 Guest are viewing this topic.


  • Guest
Simple dwg to pdf plot
« on: May 27, 2011, 11:33:50 AM »
Hey everyone,
   I was simply trying to convert a dwg file into a pdf format without opening AutoCAD or any other external DWG viewer.  I followed the general outline from the .NET development guide ('s%20Guide/index.html?url=WS1a9193826455f5ff2566ffd511ff6f8c7ca-33b0.htm,topicNumber=d0e50852) along with the help of another developer (
) but I’ve been bombarded with countless errors.

First and foremost, what would be the best format for opening an external file?  The DocumentManger.Open() command or the Database.readDWGFile()?

Second, does the DocumentManager.MdiActiveDocument = previously opened file, work at establishing the “current” document?  Every solution requires access the “current” document, so would this work? 

And does the [CommandMethod("XXXX")] have any significance?  If so, what would I use to print/convert a single DWG file?  ‘Simplot?’

And lastly, what is the cause of the errors of the form “Autodesk.AutoCAD.ApplicationServices.XXXXX does not have an implementation”?  I made sure the “Copy Local” feature of all the references was set to False.

Thanks for all the help!!

Code: [Select]
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.ComponentModel;

using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.PlottingServices;
//using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Geometry;
using CAD_AS = Autodesk.AutoCAD.ApplicationServices;

namespace ......(ommitted)
    class AutoCADToPDF : (abstract class ommitted)

        public AutoCADToPDF()
        { }

        public override string Convert(string filein)
           //**Make a try/catch statement

           //Open the drawing; read-only
            Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open(filein, true);
           //Establish as current, active document
           CAD_AS.Application.DocumentManager.MdiActiveDocument = dwg;

           //**Probably unecessary, but setting document to Active
           CAD_AS.Document doc = CAD_AS.Application.DocumentManager.MdiActiveDocument;
           //Initalize the editor
           Editor edit = doc.Editor;

           //Initalize the database by loading the drawing into it
           Database db = doc.Database;
            //Start a new transaction against the open drawing
            using (Transaction tr = db.TransactionManager.StartTransaction())
                //Reference the Layout Manager
                LayoutManager layman = LayoutManager.Current;
                //Open layout as read-only
                Layout layout  = (Layout)tr.GetObject(layman.GetLayoutId(layman.CurrentLayout),OpenMode.ForRead);

                //Create plot info for layout
                PlotInfo plotInfo = new PlotInfo();
                plotInfo.Layout = layout.ObjectId;

                //Copy plot settings from layout
                PlotSettings settings = new PlotSettings(layout.ModelType);

                //Retrieve the current plot settings validator
                PlotSettingsValidator setValidator = PlotSettingsValidator.Current;

                //Set Plot type
                setValidator.SetPlotType(settings, Autodesk.AutoCAD.DatabaseServices.PlotType.Extents);
                //Set the Plot Scale and center the plot
                setValidator.SetUseStandardScale(settings, true);
                setValidator.SetStdScaleType(settings, StdScaleType.ScaleToFit);
                setValidator.SetPlotCentered(settings, true);

                //Set the plotter and media
                setValidator.SetPlotConfigurationName(settings, "DWG to PDF.pc3", "ANSI_A_(8.50 x 11.00 Inches)");

                //Link plot settings to plot info
                //**Override not needed?
                plotInfo.OverrideSettings = settings;

                PlotInfoValidator infoValidator = new PlotInfoValidator();
                infoValidator.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;

                //Check to see if a plot is already in progress
                if(PlotFactory.ProcessPlotState != ProcessPlotState.NotPlotting)
                    throw new ConvertException("Cannot proceed, another plot is in progress !");

                //Plot the document
                using (PlotEngine engine = PlotFactory.CreatePublishEngine())
                    //Silence the plot dialogue
                    PlotProgressDialog dlg = new PlotProgressDialog(false, 1, true);
                    dlg.IsVisible = false;
                    //SPecify plot destination
                    engine.BeginPlot(dlg, null);
                    engine.BeginDocument(plotInfo, filein, null, 1, true, @"D:\");

                    //Plot the first sheet
                    PlotPageInfo pageInfo = new PlotPageInfo();
                    engine.BeginPage(pageInfo, plotInfo, true, null);

                    //End plotting sheet

                    //Finish plot
            return @"D:\";

            return null;
        } //End Convert



  • Bull Frog
  • Posts: 448
Re: Simple dwg to pdf plot
« Reply #1 on: May 27, 2011, 04:45:16 PM »
The .NET API will not work outside of a running instance of AutoCAD. You would need to license AutoCAD OEM so you can build your own host application, or use one of the many other DWG libraries to read and process the DWG file.


  • Guest
Re: Simple dwg to pdf plot
« Reply #2 on: May 28, 2011, 12:23:33 PM »
The .NET API will not work outside of a running instance of AutoCAD. You would need to license AutoCAD OEM so you can build your own host application, or use one of the many other DWG libraries to read and process the DWG file.

Thanks for the reply owenwengerd; this was exactly what I had feared.  For DWG libraries, do you know of any particular one that is free in cost and/or distribution fees?  I'm thinking DWG Viewer/TrueConvert falls under this category, but I'm trying to stay away from too much extraneous software on the client end.   

Thanks for the help!