New Member
Posts: 1
Registered: ‎04-17-2014
Message 1 of 2 (150 Views)

Why does manual plotting to PDF yield a different file than plotting with C#?

150 Views, 1 Replies
04-21-2014 04:32 PM

I've written some C# code to plot a CAD to pdf, and it seems to work. The trouble is, the PDF file is slightly different than the one I get by manually going through the Plot dialog in AutoCAD. I need it to be exactly the same for post-processing. I have saved a plot configuration called "PI.pc3" with my desired page setup settings, but it does not seem to ever be applied to the open CAD. Can anyone see why "PI.pc3" is not being used as the plotting device when plotting to pdf?


Here's the relevant code:

pub2pdf(dwgFileName, csvPath);

public static void AssignPageSetupToLayout(String dwfFileName)
            DocumentCollection acDocMgr = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
            Document Drawing1 = acDocMgr.MdiActiveDocument;

            if (File.Exists(dwfFileName))
                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument = acDocMgr.Open(dwfFileName);
            AcadApplication app = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication as AcadApplication;
            // Get the current document and database, and start a transaction
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;


            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
                // Open the Layer table for read 
                LayerTable acLyrTbl;
                acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable;
                foreach (ObjectId oID in acLyrTbl)
                    LayerTableRecord ltr = (LayerTableRecord)acTrans.GetObject(oID, OpenMode.ForWrite);
                    if (ltr.Name != "Fixture-Shapes")
                        ltr.IsOff = true;

                // Reference the Layout Manager
                LayoutManager acLayoutMgr = LayoutManager.Current;

                // Get the current layout and output its name in the Command Line window
                Layout acLayout = acTrans.GetObject(acLayoutMgr.GetLayoutId(acLayoutMgr.CurrentLayout), OpenMode.ForRead) as Layout;

                DBDictionary acPlSet = acTrans.GetObject(acCurDb.PlotSettingsDictionaryId, OpenMode.ForRead) as DBDictionary;

                // Check to see if the page setup exists
                if (acPlSet.Contains("PI") == true)
                    PlotSettings plSet = acPlSet.GetAt("PI").GetObject(OpenMode.ForRead) as PlotSettings;

                    // Update the layout
                    acDoc.Database.SaveAs(dwfFileName, DwgVersion.Current);

                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument = Drawing1;
            // Update the display

public void pub2pdf(string filepath, string output)
            Document oDoc = AcadApp.DocumentManager.MdiActiveDocument;
            Editor ed = oDoc.Editor;
            Database db = oDoc.Database;

            short bgPlot = (short)AcadApp.GetSystemVariable("BACKGROUNDPLOT");
            AcadApp.SetSystemVariable("BACKGROUNDPLOT", 1);

            string dwgPath = filepath;
            string dwgFName = Path.GetFileName(dwgPath);
            string docName = Path.GetFileNameWithoutExtension(dwgPath);

            using (Transaction trans = db.TransactionManager.StartTransaction())
                    DsdEntryCollection dColl = new DsdEntryCollection();
                    DBDictionary dbd = trans.GetObject(db.LayoutDictionaryId, OpenMode.ForRead, false) as DBDictionary;
                    List<string> layoutList = new List<string>();

                    //get layouts for active doc
                    foreach (DBDictionaryEntry dbe in dbd)
                        ObjectId oid = dbe.Value;
                        Layout lo = trans.GetObject(oid, OpenMode.ForRead) as Layout;

                    //setup layouts
                    foreach (string layoutName in layoutList)
                        if (layoutName.ToLower().Contains("model"))
                            DsdEntry dEntry = new DsdEntry();
                            dEntry.DwgName = dwgPath;
                            dEntry.Layout = layoutName;
                            dEntry.Title = docName + "-" + layoutName;
                            dEntry.NpsSourceDwg = dEntry.DwgName;

                    //setup dsd files
                    DsdData dData = new DsdData();
                    dData.SheetType = SheetType.MultiPdf;
                    string pdfname = docName;
                    dData.ProjectPath = Path.GetDirectoryName(filepath) + @"\";
                    dData.LogFilePath = dData.ProjectPath + pdfname + ".log";
                    dData.NoOfCopies = 1;
                    dData.DestinationName = output + @"\" + pdfname + ".pdf";
                    string dfile = output + @"\" + pdfname + ".dsd";

                    //make sure dwf file name prompt doesn't show up
                    string str = File.ReadAllText(dfile, Encoding.Default);
                    str = str.Replace("PromptForDwfName=TRUE", "PromptForDwfName=FALSE");
                    File.WriteAllText(dfile, str, Encoding.Default);

                    //print each layout
                    int iLayouts = dColl.Count;

                    using (PlotProgressDialog oProg = new PlotProgressDialog(false, iLayouts, false))
                        oProg.set_PlotMsgString(PlotMessageIndex.DialogTitle, "Publish Job Progress");
                        oProg.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage, "Cancel job");
                        oProg.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage, "Cancel Sheet");
                        oProg.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, "Job Progress");
                        oProg.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, "Sheet Progress");

                        oProg.UpperPlotProgressRange = 100;
                        oProg.LowerPlotProgressRange = 0;
                        oProg.UpperSheetProgressRange = 100;
                        oProg.LowerSheetProgressRange = 0;
                        oProg.IsVisible = false;

                        //publish layouts
                        Autodesk.AutoCAD.Publishing.Publisher publisher = AcadApp.Publisher;
                        PlotConfig plotconfig = Autodesk.AutoCAD.PlottingServices.PlotConfigManager.SetCurrentConfig("PI.pc3");
                        publisher.PublishDsd(dfile, oProg);
                catch (Autodesk.AutoCAD.Runtime.Exception ex)
                    ed.WriteMessage((System.Environment.NewLine + "Problem publishing DSD file. " + ex.Message));
                    AcadApp.SetSystemVariable("BACKGROUNDPLOT", bgPlot);

                AcadApplication test = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication as AcadApplication;

 I hope someone can make heads or tails of this. I'm an AutoCAD noob, so any assistance is greatly appreciated.

ADN Support Specialist
Posts: 715
Registered: ‎03-21-2011
Message 2 of 2 (81 Views)

Re: Why does manual plotting to PDF yield a different file than plotting with C#

05-07-2014 04:58 AM in reply to: asmyth01



Sorry for the delay.


I think you are missing the call to "SetPlotConfigurationName". Please try modifying the code from this blog post to use your pc3 file. That might provide some clue.






Developer Technical Services
Autodesk Developer Network

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.