-1

This is my code which create PDF of a dwg file but it gives me error near MultiSheetPdf. Please give me the solution for same. I thought that linking is the problem but I am not able to identify please suggest me the solution.

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

    private const string LOG = "publish.log";

    public MultiSheetsPdfPlot(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)Application.GetSystemVariable("BACKGROUNDPLOT");
        try
        {
            Application.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 = Application.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage("\nError: {0}\n{1}", e.Message, e.StackTrace);
        }
        finally
        {
            Application.SetSystemVariable("BACKGROUNDPLOT", bgp);
        }
    }
}
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
jackson
  • 27
  • 6
  • Can you please show what *error* do you get ? – MrClan Aug 24 '16 at 08:18
  • CS1520 Method must have a return type CS1729 'MultiSheetsPdf' does not contain a constructor that takes 2 arguments – jackson Aug 24 '16 at 08:27
  • Okay, since you intend to define a constructor there. Try changing your **public MultiSheetsPdfPlot(string pdfFile, IEnumerable layouts)** to **public MultiSheetsPdf(string pdfFile, IEnumerable layouts)**. – MrClan Aug 24 '16 at 08:34
  • Actually i am new in here can you please edit my code according to the changes please. – jackson Aug 24 '16 at 08:37
  • Normally people don't like it when you ask them to modify your code for you. But since you're very new, see the answer below. You had error in your constructor definition. Atleast, now you should not get any compilation errors if your references and namespaces are correct. – MrClan Aug 24 '16 at 08:45
  • It gives me exception "No parameterless constuctor defined in this object" – jackson Aug 24 '16 at 09:09

2 Answers2

0

Here you go: (Try to note and understand the difference between your version and this version)

    namespace Plottings
    {

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

        private const string LOG = "publish.log";
        public MultiSheetsPdf(){}
        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)Application.GetSystemVariable("BACKGROUNDPLOT");
            try
            {
                Application.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 = Application.DocumentManager.MdiActiveDocument.Editor;
                ed.WriteMessage("\nError: {0}\n{1}", e.Message, e.StackTrace);
            }
            finally
            {
                Application.SetSystemVariable("BACKGROUNDPLOT", bgp);
            }
        }
    }
}
MrClan
  • 6,402
  • 8
  • 28
  • 43
  • Still give error at constructor "public MultiSheetsPdf(){}"as methos must have return type. – jackson Aug 24 '16 at 09:32
  • Sorry wrong placement. Moved that code just above the existing constructor. Now it should not have any constructor related errors. – MrClan Aug 24 '16 at 09:35
  • Last thing i want to ask how can we change the location where to save the pdf file? – jackson Aug 24 '16 at 10:25
  • Here on this line: **string filename = Path.ChangeExtension(db.Filename, "pdf");** . So, set your **db.Filename** to where you want to save. Probably it is coming from database, so better change it there itself. – MrClan Aug 24 '16 at 10:29
  • I wrote and publish the code of the upper MultiSheetsPdf class at TheSwamp: https://www.theswamp.org/index.php?topic=31897.msg494504#msg494504 The 'CommandMethod' was not defined inside the MultiSheetsPdfand, IMO it has nothing to do here – gileCAD Aug 24 '16 at 16:49
  • And please, jackson Aj jags, aj_jags or whatever your avatar, stop double posting: http://forums.autodesk.com/t5/net/the-code-is-giving-exception/m-p/6519328#U6519328 – gileCAD Aug 24 '16 at 17:00
0

Heads up. The method, PostProcessDSD, tests are too generic. Client contacted me complaining that one of his files was not plotting. It was named "SOUTH". The test for "OUT" in the string caused the issue. No errors were thrown. Just a good ol' fashion mystery.
Change all tests to include the "=". ie else if (str.Contains("OUT=")) { ...

midohioboarder
  • 438
  • 5
  • 14