Author Topic: Plot layouts to a single multi sheets PDF file.  (Read 1867 times)

0 Members and 1 Guest are viewing this topic.

restemo

  • Guest
Plot layouts to a single multi sheets PDF file.
« on: January 09, 2015, 06:46:36 AM »
Hi,

I am looking for a solution because, I don't get something.

I am on Ac2013 Sp2 and I try a class writen by (gile) that I found http://www.theswamp.org/index.php?topic=31897.15:

The main idea is to plot all the layers in a single PDF.

I can compil my class. But In Acad2013 I have  "Unhandle Exception has occured []... No parameterless costructor definird for this object.

Have you got any ideas?

I am sure that is a a stupid problem but I can't see it, and I would like to have a good week-end.

Code: [Select]
using System.Collections.Generic;
using System.IO;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.PlottingServices;
using Autodesk.AutoCAD.Publishing;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.EditorInput;


namespace Impression
{
    public class MultiSheetsPdf
    {
        private string dwgFile, pdfFile, dsdFile, outputDir;
        private int sheetNum;
        IEnumerable<Layout> layouts;

        private const string LOG = "publish.log";

        public  MultiSheetsPdf(string pdfFile, IEnumerable<Layout> layouts)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            this.dwgFile = db.Filename;
            this.pdfFile = pdfFile;
            this.outputDir = Path.GetDirectoryName(this.pdfFile);
            this.dsdFile = Path.ChangeExtension(this.pdfFile, "dsd");
            this.layouts = layouts;
        }

        public void Publish()
        {
            if (TryCreateDSD())
            {
                Publisher publisher = AcAp.Publisher;
                PlotProgressDialog plotDlg = new PlotProgressDialog(false, this.sheetNum, true);
                publisher.PublishDsd(this.dsdFile, plotDlg);
                plotDlg.Destroy();
                File.Delete(this.dsdFile);
            }
        }

        private bool TryCreateDSD()
        {
            using (DsdData dsd = new DsdData())
            using (DsdEntryCollection dsdEntries = CreateDsdEntryCollection(this.layouts))
            {
                if (dsdEntries == null || dsdEntries.Count <= 0) return false;

                if (!Directory.Exists(this.outputDir))
                    Directory.CreateDirectory(this.outputDir);

                this.sheetNum = dsdEntries.Count;

                dsd.SetDsdEntryCollection(dsdEntries);

                dsd.SetUnrecognizedData("PwdProtectPublishedDWF", "FALSE");
                dsd.SetUnrecognizedData("PromptForPwd", "FALSE");
                dsd.SheetType = SheetType.MultiDwf;
                dsd.NoOfCopies = 1;
                dsd.DestinationName = this.pdfFile;
                dsd.IsHomogeneous = false;
                dsd.LogFilePath = Path.Combine(this.outputDir, LOG);

                PostProcessDSD(dsd);

                return true;
            }
        }

        private DsdEntryCollection CreateDsdEntryCollection(IEnumerable<Layout> layouts)
        {
            DsdEntryCollection entries = new DsdEntryCollection();

            foreach (Layout layout in layouts)
            {
                DsdEntry dsdEntry = new DsdEntry();
                dsdEntry.DwgName = this.dwgFile;
                dsdEntry.Layout = layout.LayoutName;
                dsdEntry.Title = Path.GetFileNameWithoutExtension(this.dwgFile) + "-" + layout.LayoutName;
                dsdEntry.Nps = layout.TabOrder.ToString();
                entries.Add(dsdEntry);
            }
            return entries;
        }

        private void PostProcessDSD(DsdData dsd)
        {
            string str, newStr;
            string tmpFile = Path.Combine(this.outputDir, "temp.dsd");

            dsd.WriteDsd(tmpFile);

            using (StreamReader reader = new StreamReader(tmpFile, Encoding.Default))
            using (StreamWriter writer = new StreamWriter(this.dsdFile, false, Encoding.Default))
            {
                while (!reader.EndOfStream)
                {
                    str = reader.ReadLine();
                    if (str.Contains("Has3DDWF"))
                    {
                        newStr = "Has3DDWF=0";
                    }
                    else if (str.Contains("OriginalSheetPath"))
                    {
                        newStr = "OriginalSheetPath=" + this.dwgFile;
                    }
                    else if (str.Contains("Type"))
                    {
                        newStr = "Type=6";
                    }
                    else if (str.Contains("OUT"))
                    {
                        newStr = "OUT=" + this.outputDir;
                    }
                    else if (str.Contains("IncludeLayer"))
                    {
                        newStr = "IncludeLayer=TRUE";
                    }
                    else if (str.Contains("PromptForDwfName"))
                    {
                        newStr = "PromptForDwfName=FALSE";
                    }
                    else if (str.Contains("LogFilePath"))
                    {
                        newStr = "LogFilePath=" + Path.Combine(this.outputDir, LOG);
                    }
                    else
                    {
                        newStr = str;
                    }
                    writer.WriteLine(newStr);
                }
            }
            File.Delete(tmpFile);
        }

[CommandMethod("PlotPdf")]
           public void PlotPdf()
           {
               Database db = HostApplicationServices.WorkingDatabase;
               short bgp = (short)AcAp.GetSystemVariable("BACKGROUNDPLOT");
               try
               {
                   AcAp.SetSystemVariable("BACKGROUNDPLOT", 0);
                   using (Transaction tr = db.TransactionManager.StartTransaction())
                   {
                       List<Layout> layouts = new List<Layout>();
                       DBDictionary layoutDict =
                           (DBDictionary)db.LayoutDictionaryId.GetObject(OpenMode.ForRead);
                       foreach (DBDictionaryEntry entry in layoutDict)
                       {
                           if (entry.Key != "Model")
                           {
                               layouts.Add((Layout)tr.GetObject(entry.Value, OpenMode.ForRead));
                           }
                       }
                       layouts.Sort((l1, l2) => l1.TabOrder.CompareTo(l2.TabOrder));
     
                       string filename = Path.ChangeExtension(db.Filename, "pdf");
     
                       MultiSheetsPdf plotter = new MultiSheetsPdf(filename, layouts);
                       plotter.Publish();
     
                       tr.Commit();
                   }
               }
               catch (System.Exception e)
               {
                   Editor ed = AcAp.DocumentManager.MdiActiveDocument.Editor;
                   ed.WriteMessage("\nError: {0}\n{1}", e.Message, e.StackTrace);
               }
               finally
               {
                   AcAp.SetSystemVariable("BACKGROUNDPLOT", bgp);
               }
           }

   
}
}


gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Plot layouts to a single multi sheets PDF file.
« Reply #1 on: January 09, 2015, 07:26:13 AM »
Hi,

"No parameterless constructor definied for this object" means a class constructor (probably: new MultiSheetsPdf()) had been called without passing the required parameters (arguments).

How did you construct your MultiSheetsPdf instance ?
Did you tried the provided test command example ?
Speaking English as a French Frog

restemo

  • Guest
Re: Plot layouts to a single multi sheets PDF file.
« Reply #2 on: January 09, 2015, 07:40:48 AM »
Hi,

The instance is created here :

   MultiSheetsPdf plotter = new MultiSheetsPdf(filename, layouts);

The test example? I don't see what you mean

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Plot layouts to a single multi sheets PDF file.
« Reply #3 on: January 09, 2015, 10:35:28 AM »
Ok, I see.
You pasted the PlotPdf command method code within the MultiSheetsPdf class.
A command method have to be defined in a separate public class which have a default constructor (without parameter).
Speaking English as a French Frog

restemo

  • Guest
Re: Plot layouts to a single multi sheets PDF file.
« Reply #4 on: January 09, 2015, 11:01:24 AM »
My fault,

Thank you for being patient and for your reply,