.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

1 REPLY 1
Reply
Message 1 of 2
asmyth01
518 Views, 1 Reply

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

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:

AssignPageSetupToLayout(dwgFileName);
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;
            acDoc.LockDocument();
            Database acCurDb = acDoc.Database;

            //Application.ShowAlertDialog(acDoc.Name);

            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
                    acLayout.UpgradeOpen();
                    acLayout.CopyFrom(plSet);
                    acDoc.Database.SaveAs(dwfFileName, DwgVersion.Current);
                    acTrans.Commit();
                }
                else
                {
                    acTrans.Abort();
                }

                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument = Drawing1;
                acDoc.CloseAndDiscard();
            }
            // Update the display
            acDoc.Editor.Regen();
        }

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);

            //transaction!
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                try
                {
                    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;
                        layoutList.Add(lo.LayoutName);
                    }

                    //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;
                            dColl.Add(dEntry);
                        }
                    }

                    //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";
                    dData.SetDsdEntryCollection(dColl);
                    string dfile = output + @"\" + pdfname + ".dsd";
                    dData.WriteDsd(dfile);

                    //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);
                    dData.ReadDsd(dfile);

                    //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);
                        File.Delete(dfile);
                    }
                    trans.Commit();
                }
                catch (Autodesk.AutoCAD.Runtime.Exception ex)
                {
                    ed.WriteMessage((System.Environment.NewLine + "Problem publishing DSD file. " + ex.Message));
                }
                finally
                {
                    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.

1 REPLY 1
Message 2 of 2
Balaji_Ram
in reply to: asmyth01

Hello,

 

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.

 

http://adndevblog.typepad.com/autocad/2013/10/publishing-model-views-to-a-multi-sheet-dwfpdf.html

 

Regards,

Balaji



Balaji
Developer Technical Services
Autodesk Developer Network

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost