Example #1
1
        private void PlotFiles()
        {
            DsdEntryCollection dsdCol = new DsdEntryCollection();

            int indexfile = 0;

            title = $"Печать {filesDwg.Length} файлов dwg...";

            foreach (var fileDwg in filesDwg)
            {
                if (HostApplicationServices.Current.UserBreak())
                    throw new Exception(General.CanceledByUser);

                indexfile++;
                using (var dbTemp = new Database(false, true))
                {
                    dbTemp.ReadDwgFile(fileDwg, FileOpenMode.OpenForReadAndAllShare, false, "");
                    dbTemp.CloseInput(true);
                    using (var t = dbTemp.TransactionManager.StartTransaction())
                    {
                        DBDictionary layoutDict = (DBDictionary)dbTemp.LayoutDictionaryId.GetObject(OpenMode.ForRead);

                        List<Layout> layouts = new List<Layout>();
                        foreach (DBDictionaryEntry entry in layoutDict)
                        {
                            if (entry.Key != "Model")
                            {
                                if (!entry.Value.IsErased)
                                {
                                    var layout = entry.Value.GetObject(OpenMode.ForRead) as Layout;
                                    layouts.Add(layout);
                                }
                            }
                        }
                        // Фильтр листов
                        if (Options.FilterState)
                        {
                            layouts = FilterLayouts(layouts, Options);
                        }

                        List<Tuple<Layout, DsdEntry>> layoutsDsd = new List<Tuple<Layout, DsdEntry>>();
                        foreach (var layout in layouts)
                        {
                            DsdEntry dsdEntry = new DsdEntry();
                            dsdEntry.Layout = layout.LayoutName;
                            dsdEntry.DwgName = fileDwg;
                            //dsdEntry.Nps = "Setup1";
                            dsdEntry.NpsSourceDwg = fileDwg;
                            dsdEntry.Title = indexfile + "-" + layout.LayoutName;
                            layoutsDsd.Add(new Tuple<Layout, DsdEntry>(layout, dsdEntry));
                            //dsdCol.Add(dsdEntry);
                        }

                        if (Options.SortTabOrName)
                        {
                            layoutsDsd.Sort((l1, l2) => l1.Item1.TabOrder.CompareTo(l2.Item1.TabOrder));
                        }
                        else
                        {
                            layoutsDsd.Sort((l1, l2) => l1.Item1.LayoutName.CompareTo(l2.Item1.LayoutName));
                        }
                        layoutsDsd.ForEach(l => dsdCol.Add(l.Item2));
                        t.Commit();
                    }
                }
            }
            PublisherDSD(dsdCol);
        }
Example #2
0
        public void Plot()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;

            dir = Path.GetDirectoryName(doc.Name);
            filePdfOutputName = Path.GetFileNameWithoutExtension(doc.Name);
            using (var dsdCol = new DsdEntryCollection())
            {
                using (var t = doc.TransactionManager.StartTransaction())
                {
                    var layouts = GetLayouts(doc.Database);
                    foreach (var layout in layouts)
                    {
                        var dsdEntry = new DsdEntry
                        {
                            Layout       = layout.LayoutName,
                            DwgName      = doc.Name,
                            NpsSourceDwg = doc.Name,
                            Title        = layout.LayoutName
                        };
                        dsdCol.Add(dsdEntry);
                    }

                    t.Commit();
                }

                PublisherDSD(dsdCol);
            }
        }
Example #3
0
        Stream(ArrayList data, DsdEntry dsd)
        {
            data.Add(new Snoop.Data.ClassSeparator(typeof(DsdEntry)));

            data.Add(new Snoop.Data.String("Title", dsd.Title));
            data.Add(new Snoop.Data.String("DWG name", dsd.DwgName));
            data.Add(new Snoop.Data.String("Layout", dsd.Layout));
            data.Add(new Snoop.Data.String("Nps", dsd.Nps));
            data.Add(new Snoop.Data.String("Nps source DWG", dsd.NpsSourceDwg));
        }
Example #4
0
        public void PublishPackage(PrintPackageModel package)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db  = doc.Database;

            short bgPlot = (short)Application.GetSystemVariable("BACKGROUNDPLOT");

            Application.SetSystemVariable("BACKGROUNDPLOT", 0);

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                DsdEntryCollection collection = new DsdEntryCollection();
                foreach (var layoutModel in package.Layouts)
                {
                    DsdEntry entry = new DsdEntry();
                    entry.DwgName      = package.DwgPath + package.DwgFileName;
                    entry.Layout       = layoutModel.LayoutName;
                    entry.Title        = "Layout_" + layoutModel.LayoutName;
                    entry.NpsSourceDwg = entry.DwgName;
                    entry.Nps          = "Setup1";

                    collection.Add(entry);
                }

                DsdData dsdData = new DsdData();
                dsdData.SheetType       = SheetType.MultiPdf; //SheetType.MultiPdf
                dsdData.ProjectPath     = package.DwgPath;
                dsdData.DestinationName =
                    $"{dsdData.ProjectPath}{package.PdfFileName}.pdf";
                if (System.IO.File.Exists(dsdData.DestinationName))
                {
                    System.IO.File.Delete(dsdData.DestinationName);
                }
                dsdData.SetDsdEntryCollection(collection);
                string dsdFile = $"{dsdData.ProjectPath}{package.PdfFileName}.dsd";

                dsdData.WriteDsd(dsdFile);
                dsdData.ReadDsd(dsdFile);

                System.IO.File.Delete(dsdFile);
                var plotConfig = PlotConfigManager.SetCurrentConfig("DWG_To_PDF_Uzle.pc3");
                var publisher  = Application.Publisher;

                publisher.AboutToBeginPublishing +=
                    new Autodesk.AutoCAD.Publishing.
                    AboutToBeginPublishingEventHandler(
                        Publisher_AboutToBeginPublishing);
                dsdData.PromptForDwfName = false;
                publisher.PublishExecute(dsdData, plotConfig);
                tr.Commit();
            }

            Application.SetSystemVariable("BACKGROUNDPLOT", bgPlot);
        }
        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 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 DsdEntryCollection CreateDsdEntryCollection(IEnumerable <Layout> layouts)
        {
            DsdEntryCollection entries = new DsdEntryCollection();

            foreach (Layout layout in layouts)
            {
                //layout.Initialize();  --//giving exception
                DsdEntry dsdEntry = new DsdEntry();
                dsdEntry.DwgName = this._dwgFile;
                dsdEntry.Layout  = layout.LayoutName;
                //File.AppendAllText("D:\\layout_names.txt", layout.LayoutName);     //test
                dsdEntry.Title = Path.GetFileNameWithoutExtension(this._dwgFile) + "-" + layout.LayoutName;
                dsdEntry.Nps   = layout.TabOrder.ToString();
                entries.Add(dsdEntry);
            }
            return(entries);
        }
Example #8
0
        private DsdEntryCollection CreateDsdEntryCollection([NotNull] IEnumerable <Layout> layouts)
        {
            var entries = new DsdEntryCollection();

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

            return(entries);
        }
Example #9
0
            // Creates an entry collection (one per layout) for the DSD file
            private DsdEntryCollection CreateDsdEntryCollection(ArrayList layouts, Database db, string layerName)
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    DsdEntryCollection entries = new DsdEntryCollection();

                    foreach (ObjectId layoutId in layouts)
                    {
                        Layout layout = tr.GetObject(layoutId, OpenMode.ForRead) as Layout;

                        DsdEntry dsdEntry = new DsdEntry();
                        dsdEntry.DwgName = this.dwgFile;
                        dsdEntry.Layout  = layout.LayoutName;
                        dsdEntry.Title   = Path.GetFileNameWithoutExtension(this.dwgFile) + "-" + layerName + "-" + layout.LayoutName;
                        dsdEntry.Nps     = layout.TabOrder.ToString();

                        entries.Add(dsdEntry);
                    }

                    tr.Commit();
                    return(entries);
                }
            }
Example #10
0
        public static void PublishViews2MultiSheet()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db  = doc.Database;
            var ed  = doc.Editor;

            var viewsToPlot = new StringCollection();

            viewsToPlot.Add("Test1");
            viewsToPlot.Add("Test2");
            viewsToPlot.Add("Current");
            // Create page setup based on the views
            using (var Tx = db.TransactionManager.StartTransaction())
            {
                var layoutId = LayoutManager.Current.GetLayoutId
                                   (LayoutManager.Current.CurrentLayout);

                var layout
                    = Tx.GetObject(layoutId, OpenMode.ForWrite) as Layout;

                foreach (var viewName in viewsToPlot)
                {
                    var plotSettings
                        = new PlotSettings(layout.ModelType);
                    plotSettings.CopyFrom(layout);

                    var psv
                        = PlotSettingsValidator.Current;

                    psv.SetPlotConfigurationName
                        (plotSettings,
                        "DWF6 ePlot.pc3",
                        "ANSI_A_(8.50_x_11.00_Inches)"
                        );
                    psv.RefreshLists(plotSettings);
                    psv.SetPlotViewName(plotSettings, viewName);
                    psv.SetPlotType
                    (
                        plotSettings,
                        PlotType.View
                    );
                    psv.SetUseStandardScale(plotSettings, true);
                    psv.SetStdScaleType
                    (
                        plotSettings,
                        StdScaleType.ScaleToFit
                    );
                    psv.SetPlotCentered(plotSettings, true);
                    psv.SetPlotRotation
                    (
                        plotSettings,
                        PlotRotation.Degrees000
                    );
                    psv.SetPlotPaperUnits
                    (
                        plotSettings,
                        PlotPaperUnit.Inches
                    );

                    plotSettings.PlotSettingsName
                        = String.Format("{0}{1}", viewName, "PS");
                    plotSettings.PrintLineweights = true;
                    plotSettings.AddToPlotSettingsDictionary(db);

                    Tx.AddNewlyCreatedDBObject(plotSettings, true);
                    psv.RefreshLists(plotSettings);

                    layout.CopyFrom(plotSettings);
                }

                Tx.Commit();
            }

            //put the plot in foreground
            var bgPlot = (short)Application.GetSystemVariable("BACKGROUNDPLOT");

            Application.SetSystemVariable("BACKGROUNDPLOT", 0);

            var dwgFileName
                = Application.GetSystemVariable("DWGNAME") as string;
            var dwgPath
                = Application.GetSystemVariable("DWGPREFIX") as string;

            using (var Tx = db.TransactionManager.StartTransaction())
            {
                var collection = new DsdEntryCollection();
                var activeLayoutId
                    = LayoutManager.Current.GetLayoutId
                          (LayoutManager.Current.CurrentLayout);

                foreach (var viewName in viewsToPlot)
                {
                    var layout = Tx.GetObject(
                        activeLayoutId,
                        OpenMode.ForRead
                        ) as Layout;

                    var entry = new DsdEntry();

                    entry.DwgName      = dwgPath + dwgFileName;
                    entry.Layout       = layout.LayoutName;
                    entry.Title        = viewName;
                    entry.NpsSourceDwg = entry.DwgName;
                    entry.Nps          = String.Format("{0}{1}", viewName, "PS");

                    collection.Add(entry);
                }

                // remove the ".dwg" extension
                dwgFileName = dwgFileName.Substring(0, dwgFileName.Length - 4);

                var dsdData = new DsdData();

                dsdData.SheetType   = SheetType.MultiDwf;
                dsdData.ProjectPath = dwgPath;
                dsdData.DestinationName
                    = dsdData.ProjectPath + dwgFileName + ".dwf";

                if (File.Exists(dsdData.DestinationName))
                {
                    File.Delete(dsdData.DestinationName);
                }

                dsdData.SetDsdEntryCollection(collection);

                var dsdFile
                    = dsdData.ProjectPath + dwgFileName + ".dsd";

                //Workaround to avoid promp for pdf file name
                //set PromptForDwfName=FALSE in dsdData using StreamReader/StreamWriter

                dsdData.WriteDsd(dsdFile);

                var sr  = new StreamReader(dsdFile);
                var str = sr.ReadToEnd();
                sr.Close();

                // Replace PromptForDwfName
                str = str.Replace("PromptForDwfName=TRUE", "PromptForDwfName=FALSE");

                // Workaround to have the page setup names included in the DSD file
                // Replace Setup names based on the created page setups
                // May not be required if Nps is output to the DSD
                var occ        = 0;
                var index      = str.IndexOf("Setup=");
                var startIndex = 0;
                var dsdText    = new StringBuilder();
                while (index != -1)
                {
                    // 6 for length of "Setup="
                    var str1 =
                        str.Substring(startIndex, index + 6 - startIndex);

                    dsdText.Append(str1);
                    dsdText.Append(
                        String.Format("{0}{1}", viewsToPlot[occ], "PS"));

                    startIndex = index + 6;
                    index      = str.IndexOf("Setup=", index + 6);

                    if (index == -1)
                    {
                        dsdText.Append(
                            str.Substring(startIndex, str.Length - startIndex));
                    }
                    occ++;
                }

                // Write the DSD
                var sw
                    = new StreamWriter(dsdFile);
                sw.Write(dsdText.ToString());
                sw.Close();

                // Read the updated DSD file
                dsdData.ReadDsd(dsdFile);

                // Erase DSD as it is no longer needed
                File.Delete(dsdFile);

                var plotConfig
                    = PlotConfigManager.SetCurrentConfig("DWF6 ePlot.pc3");

                var publisher = Application.Publisher;

                // Publish it
                publisher.PublishExecute(dsdData, plotConfig);

                Tx.Commit();
            }

            //reset the background plot value
            Application.SetSystemVariable("BACKGROUNDPLOT", bgPlot);
        }
Example #11
0
        static public void BatchPublish(System.Collections.Generic.List <string> docsToPlot)
        {
            DsdEntryCollection collection = new DsdEntryCollection();
            Document           doc        = Application.DocumentManager.MdiActiveDocument;

            foreach (string filename in docsToPlot)
            {
                using (DocumentLock doclock = doc.LockDocument())
                {
                    Database db = new Database(false, true);
                    db.ReadDwgFile(
                        filename, System.IO.FileShare.Read, true, "");
                    System.IO.FileInfo fi      = new System.IO.FileInfo(filename);
                    string             docName =
                        fi.Name.Substring(0, fi.Name.Length - 4);
                    using (Transaction Tx =
                               db.TransactionManager.StartTransaction())
                    {
                        foreach (ObjectId layoutId in getLayoutIds(db))
                        {
                            Layout layout = Tx.GetObject(layoutId, OpenMode.ForRead) as Layout;

                            DsdEntry entry = new DsdEntry
                            {
                                DwgName = filename,
                                Layout  = layout.LayoutName,
                                Title   = docName + "_" + layout.LayoutName
                            };
                            entry.NpsSourceDwg = entry.DwgName;
                            entry.Nps          = "Setup1";
                            collection.Add(entry);
                        }
                        Tx.Commit();
                    }
                }
            }
            DsdData dsdData = new DsdData();

            dsdData.SheetType   = SheetType.MultiPdf;
            dsdData.ProjectPath = @"C:\Users\yusufzhon.marasulov\Desktop\test";
            //Not used for "SheetType.SingleDwf"
            //dsdData.DestinationName = dsdData.ProjectPath + "\\output.dwf";
            dsdData.SetDsdEntryCollection(collection);
            string dsdFile = dsdData.ProjectPath + "\\dsdData.dsd";

            //Workaround to avoid promp for dwf file name
            //set PromptForDwfName=FALSE in dsdData
            //using StreamReader/StreamWriter
            if (System.IO.File.Exists(dsdFile))
            {
                System.IO.File.Delete(dsdFile);
            }
            dsdData.WriteDsd(dsdFile);
            System.IO.StreamReader sr = new System.IO.StreamReader(dsdFile);
            string str = sr.ReadToEnd();

            sr.Close();
            str = str.Replace(
                "PromptForDwfName=TRUE", "PromptForDwfName=FALSE");
            System.IO.StreamWriter sw = new System.IO.StreamWriter(dsdFile);
            sw.Write(str);
            sw.Close();
            dsdData.ReadDsd(dsdFile);
            System.IO.File.Delete(dsdFile);
            PlotConfig plotConfig =
                Autodesk.AutoCAD.PlottingServices.PlotConfigManager.SetCurrentConfig("DWG_To_PDF_Uzle.pc3");

            Autodesk.AutoCAD.Publishing.Publisher publisher =
                Autodesk.AutoCAD.ApplicationServices.Application.Publisher;
            publisher.PublishExecute(dsdData, plotConfig);
        }
Example #12
0
        public static void PublishViews2MultiSheet()
        {
            pwdWindow = new PasswordWindow();
            pwdWindow.ShowDialog();
            Document doc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            StringCollection viewsToPlot = new StringCollection();
            viewsToPlot.Add("Test1");
            viewsToPlot.Add("Test2");
            using (Transaction Tx = db.TransactionManager.StartTransaction())
            {
                ObjectId layoutId = LayoutManager.Current.GetLayoutId(LayoutManager.Current.CurrentLayout);
                Layout layout = Tx.GetObject(layoutId, OpenMode.ForWrite) as Layout;
                foreach (String viewName in viewsToPlot)
                {
                    PlotSettings plotSettings = new PlotSettings(layout.ModelType);
                    plotSettings.CopyFrom(layout);
                    PlotSettingsValidator psv = PlotSettingsValidator.Current;
                    psv.SetPlotConfigurationName(plotSettings, "DWF6 ePlot.pc3", "ANSI_A_(8.50_x_11.00_Inches)");
                    psv.RefreshLists(plotSettings);
                    psv.SetPlotViewName(plotSettings, viewName);
                    psv.SetPlotType(plotSettings, Autodesk.AutoCAD.DatabaseServices.PlotType.View);
                    psv.SetUseStandardScale(plotSettings, true);
                    psv.SetStdScaleType(plotSettings, StdScaleType.ScaleToFit);
                    psv.SetPlotCentered(plotSettings, true);
                    psv.SetPlotRotation(plotSettings, PlotRotation.Degrees000);
                    psv.SetPlotPaperUnits(plotSettings, PlotPaperUnit.Inches);
                    plotSettings.PlotSettingsName = String.Format("{0}{1}", viewName, "PS");
                    plotSettings.PrintLineweights = true;
                    plotSettings.AddToPlotSettingsDictionary(db);
                    Tx.AddNewlyCreatedDBObject(plotSettings, true);
                    psv.RefreshLists(plotSettings);
                    layout.CopyFrom(plotSettings);
                }
                Tx.Commit();
            }
            short bgPlot = (short)Autodesk.AutoCAD.ApplicationServices.Core.Application.GetSystemVariable("BACKGROUNDPLOT");
            Autodesk.AutoCAD.ApplicationServices.Core.Application.SetSystemVariable("BACKGROUNDPLOT", 0);
            string dwgFileName = Autodesk.AutoCAD.ApplicationServices.Core.Application.GetSystemVariable("DWGNAME") as string;
            string dwgPath = Autodesk.AutoCAD.ApplicationServices.Core.Application.GetSystemVariable("DWGPREFIX") as string;
            using (Transaction Tx = db.TransactionManager.StartTransaction())
            {
                DsdEntryCollection collection = new DsdEntryCollection();
                ObjectId activeLayoutId = LayoutManager.Current.GetLayoutId(LayoutManager.Current.CurrentLayout);
                foreach (String viewName in viewsToPlot)
                {
                    Layout layout = Tx.GetObject(activeLayoutId, OpenMode.ForRead) as Layout;
                    DsdEntry entry = new DsdEntry();
                    entry.DwgName = dwgPath + dwgFileName;
                    entry.Layout = layout.LayoutName;
                    entry.Title = viewName;
                    entry.NpsSourceDwg = entry.DwgName;
                    entry.Nps = String.Format("{0}{1}", viewName, "PS");
                    collection.Add(entry);
                }
                dwgFileName = dwgFileName.Substring(0, dwgFileName.Length - 4);
                DsdData dsdData = new DsdData();
                dsdData.SheetType = SheetType.MultiDwf;
                dsdData.ProjectPath = dwgPath;
                dsdData.DestinationName = dsdData.ProjectPath + dwgFileName + ".dwf";
                /*Get password from user*/
                dsdData.Password = pwdWindow.passwordBox.Password;
                if (System.IO.File.Exists(dsdData.DestinationName)) System.IO.File.Delete(dsdData.DestinationName);
                dsdData.SetDsdEntryCollection(collection);

                /*DsdFile */
                string dsdFile = dsdData.ProjectPath + dwgFileName + ".dsd";
                dsdData.WriteDsd(dsdFile);
                System.IO.StreamReader sr = new System.IO.StreamReader(dsdFile);
                string str = sr.ReadToEnd();
                sr.Close();
                str = str.Replace("PromptForDwfName=TRUE",
                                   "PromptForDwfName=FALSE");
                /*Prompts User to Enter Password and Reconfirms*/
                //str = str.Replace("PromptForPwd=FALSE",
                //                   "PromptForPwd=TRUE");
                //str = str.Replace("PwdProtectPublishedDWF=FALSE",
                //                   "PwdProtectPublishedDWF=TRUE");
                int occ = 0;
                int index = str.IndexOf("Setup=");
                int startIndex = 0;
                StringBuilder dsdText = new StringBuilder();
                while (index != -1)
                {
                    String str1 = str.Substring(startIndex, index + 6 - startIndex);
                    dsdText.Append(str1);
                    dsdText.Append(String.Format("{0}{1}", viewsToPlot[occ], "PS"));
                    startIndex = index + 6;
                    index = str.IndexOf("Setup=", index + 6);
                    if (index == -1)
                    {
                        dsdText.Append(str.Substring(startIndex, str.Length - startIndex));
                    }
                    occ++;
                }
                System.IO.StreamWriter sw = new System.IO.StreamWriter(dsdFile);
                sw.Write(dsdText.ToString());
                sw.Close();
                dsdData.ReadDsd(dsdFile);
                System.IO.File.Delete(dsdFile);
                PlotConfig plotConfig = PlotConfigManager.SetCurrentConfig("DWF6 ePlot.pc3");
                Publisher publisher = Autodesk.AutoCAD.ApplicationServices.Core.Application.Publisher;
                publisher.PublishExecute(dsdData, plotConfig);
                Tx.Commit();
            }
            Autodesk.AutoCAD.ApplicationServices.Core.Application.SetSystemVariable("BACKGROUNDPLOT", bgPlot);
        }
Example #13
0
        /// <summary>
        /// The BatchPublish.
        /// </summary>
        /// <param name="docsToPlot">The docsToPlot<see cref="List{string}"/>.</param>
        private static void BatchPublish(List <string> docsToPlot)
        {
            using (DsdEntryCollection collection = new DsdEntryCollection())
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                foreach (string filename in docsToPlot)
                {
                    using (DocumentLock doclock = doc.LockDocument())
                    {
                        using (Database db = new Database(false, true))
                        {
                            db.ReadDwgFile(filename, System.IO.FileShare.Read, true, "");
                            System.IO.FileInfo fi      = new FileInfo(filename);
                            string             docName = fi.Name.Substring(0, fi.Name.Length - 4);
                            using (Transaction Tx = db.TransactionManager.StartTransaction())
                            {
                                foreach (ObjectId layoutId in GeLayoutIds(db))
                                {
                                    Layout layout = Tx.GetObject(layoutId, OpenMode.ForRead) as Layout;
                                    if (!layout.ModelType)
                                    {
                                        var ids = layout.GetViewports();
                                        if (ids.Count == 0)
                                        {
                                            doc.Editor.WriteMessage($"\n{layout.LayoutName} is not initialized, so no plottable sheets in the current drawing ");
                                        }
                                    }

                                    DsdEntry entry = new DsdEntry
                                    {
                                        DwgName = filename,
                                        Layout  = layout.LayoutName,
                                        Title   = docName + "_" + layout.LayoutName
                                    };
                                    entry.NpsSourceDwg = entry.DwgName;
                                    entry.Nps          = "Setup1";
                                    collection.Add(entry);
                                }
                                Tx.Commit();
                            }
                        }
                    }
                }
                using (DsdData dsdData = new DsdData())
                {
                    dsdData.SheetType   = SheetType.MultiPdf;
                    dsdData.ProjectPath = rootDir;
                    dsdData.SetDsdEntryCollection(collection);
                    string dsdFile = dsdData.ProjectPath + "\\dsdData.dsd";
                    dsdData.WriteDsd(dsdFile);
                    System.IO.StreamReader sr = new System.IO.StreamReader(dsdFile);
                    string str = sr.ReadToEnd();
                    sr.Close();
                    str = str.Replace("PromptForDwfName=TRUE", "PromptForDwfName=FALSE");
                    string strToApped = "[PublishOptions]\nInitLayouts = TRUE";
                    str += strToApped;
                    System.IO.StreamWriter sw = new System.IO.StreamWriter(dsdFile);
                    sw.Write(str);
                    sw.Close();
                    dsdData.ReadDsd(dsdFile);
                    System.IO.File.Delete(dsdFile);
                    PlotConfig plotConfig = Autodesk.AutoCAD.PlottingServices.PlotConfigManager.SetCurrentConfig("DWG To PDF.pc3");
                    Autodesk.AutoCAD.Publishing.Publisher publisher = Autodesk.AutoCAD.ApplicationServices.Core.Application.Publisher;
                    try
                    {
                        publisher.PublishExecute(dsdData, plotConfig);
                    }
                    catch (Exception ex) {
                        doc.Editor.WriteMessage($"Exeception: {ex.StackTrace}");
                    }
                }
            }


            //This is hack to prevent from not finding result.pdf
            foreach (var file in Directory.GetFiles(rootDir, "*.pdf"))
            {
                System.IO.FileInfo fi = new System.IO.FileInfo(file);
                // Check if file is there
                if (fi.Exists)
                {
                    // Move file with a new name. Hence renamed.
                    fi.MoveTo(Path.Combine(Directory.GetCurrentDirectory(), "result.pdf"));
                    break;
                }
            }
        }
Example #14
0
        static public void PublishAllLayouts()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db  = doc.Database;
            //put the plot in foreground
            short bgPlot = (short)Application.GetSystemVariable("BACKGROUNDPLOT");

            Application.SetSystemVariable("BACKGROUNDPLOT", 0);
            //get the layout ObjectId List
            List <ObjectId> layoutIds   = GetLayoutIds(db);
            string          dwgFileName = (string)Application.GetSystemVariable("DWGNAME");
            string          dwgPath     = (string)Application.GetSystemVariable("DWGPREFIX");

            using (Transaction Tx = db.TransactionManager.StartTransaction())
            {
                DsdEntryCollection collection = new DsdEntryCollection();
                foreach (ObjectId layoutId in layoutIds)
                {
                    Layout layout = Tx.GetObject(layoutId, OpenMode.ForRead)
                                    as Layout;
                    if (layout.LayoutName != "Model")
                    {
                        DsdEntry entry = new DsdEntry();
                        entry.DwgName      = dwgPath + dwgFileName;
                        entry.Layout       = layout.LayoutName;
                        entry.Title        = "Layout_" + layout.LayoutName;
                        entry.NpsSourceDwg = entry.DwgName;
                        entry.Nps          = "Setup1";

                        collection.Add(entry);
                    }

                    //TODO have to think about creating collection depend of block view
                }
                dwgFileName = dwgFileName.Substring(0, dwgFileName.Length - 4);
                DsdData dsdData = new DsdData();
                dsdData.SheetType       = SheetType.MultiPdf; //SheetType.MultiPdf
                dsdData.ProjectPath     = dwgPath;
                dsdData.DestinationName =
                    $"{dsdData.ProjectPath}{dwgFileName}.pdf";
                if (System.IO.File.Exists(dsdData.DestinationName))
                {
                    System.IO.File.Delete(dsdData.DestinationName);
                }
                dsdData.SetDsdEntryCollection(collection);
                string dsdFile = $"{dsdData.ProjectPath}{dwgFileName}.dsd";
                //Workaround to avoid promp for dwf file name
                //set PromptForDwfName=FALSE in dsdData using
                //StreamReader/StreamWriter
                dsdData.WriteDsd(dsdFile);
                //System.IO.StreamReader sr =
                //    new System.IO.StreamReader(dsdFile, Encoding.Default);
                //string str = sr.ReadToEnd();
                //sr.Close();
                //str = str.Replace(
                //    "PromptForDwfName=TRUE", "PromptForDwfName=FALSE");
                //System.IO.StreamWriter sw =
                //    new System.IO.StreamWriter(dsdFile, true, Encoding.Default);
                //sw.Write(str);
                //sw.Close();
                dsdData.ReadDsd(dsdFile);

                System.IO.File.Delete(dsdFile);
                PlotConfig plotConfig =
                    Autodesk.AutoCAD.PlottingServices.PlotConfigManager.SetCurrentConfig("DWG_To_PDF_Uzle.pc3");
                //PlotConfig pc = Autodesk.AutoCAD.PlottingServices.
                //  PlotConfigManager.SetCurrentConfig("DWG To PDF.pc3");
                Autodesk.AutoCAD.Publishing.Publisher publisher =
                    Autodesk.AutoCAD.ApplicationServices.Application.Publisher;
                publisher.AboutToBeginPublishing +=
                    new Autodesk.AutoCAD.Publishing.
                    AboutToBeginPublishingEventHandler(
                        Publisher_AboutToBeginPublishing);
                dsdData.PromptForDwfName = false;
                publisher.PublishExecute(dsdData, plotConfig);
                Tx.Commit();
            }
            //reset the background plot value
            Application.SetSystemVariable("BACKGROUNDPLOT", bgPlot);
        }
Example #15
0
        private void PlotFiles()
        {
            using (var dsdCol = new DsdEntryCollection())
            {
                var indexfile = 0;

                title = $"Печать {filesDwg.Length} файлов dwg...";

                foreach (var fileDwg in filesDwg)
                {
                    if (HostApplicationServices.Current.UserBreak())
                    {
                        throw new OperationCanceledException();
                    }
                    indexfile++;
                    using (var dbTemp = new Database(false, true))
                    {
                        dbTemp.ReadDwgFile(fileDwg, FileOpenMode.OpenForReadAndAllShare, false, string.Empty);
                        dbTemp.CloseInput(true);
                        using (var t = dbTemp.TransactionManager.StartTransaction())
                        {
                            var layouts = dbTemp.GetLayouts();

                            // Фильтр листов
                            if (Options.FilterState)
                            {
                                layouts = FilterLayouts(layouts, Options);
                            }

                            var layoutsDsd = new List <Tuple <Layout, DsdEntry> >();
                            foreach (var layout in layouts)
                            {
                                var dsdEntry = new DsdEntry()
                                {
                                    Layout       = layout.LayoutName,
                                    DwgName      = fileDwg,
                                    NpsSourceDwg = fileDwg,
                                    Title        = indexfile + "-" + layout.LayoutName
                                };
                                layoutsDsd.Add(new Tuple <Layout, DsdEntry>(layout, dsdEntry));
                            }

                            if (Options.SortTabOrName)
                            {
                                layoutsDsd.Sort((l1, l2) => l1.Item1.TabOrder.CompareTo(l2.Item1.TabOrder));
                            }
                            else
                            {
                                layoutsDsd.Sort((l1, l2) =>
                                                string.Compare(l1.Item1.LayoutName, l2.Item1.LayoutName, StringComparison.Ordinal));
                            }

                            layoutsDsd.ForEach(l => dsdCol.Add(l.Item2));
                            t.Commit();
                        }
                    }
                }

                PublisherDSD(dsdCol);
            }
        }
Example #16
0
        static public void PublishViews2MultiSheet()
        {
            pwdWindow = new PasswordWindow();
            pwdWindow.ShowDialog();
            Document         doc         = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
            Database         db          = doc.Database;
            Editor           ed          = doc.Editor;
            StringCollection viewsToPlot = new StringCollection();

            viewsToPlot.Add("Test1");
            viewsToPlot.Add("Test2");
            using (Transaction Tx = db.TransactionManager.StartTransaction())
            {
                ObjectId layoutId = LayoutManager.Current.GetLayoutId(LayoutManager.Current.CurrentLayout);
                Layout   layout   = Tx.GetObject(layoutId, OpenMode.ForWrite) as Layout;
                foreach (String viewName in viewsToPlot)
                {
                    PlotSettings plotSettings = new PlotSettings(layout.ModelType);
                    plotSettings.CopyFrom(layout);
                    PlotSettingsValidator psv = PlotSettingsValidator.Current;
                    psv.SetPlotConfigurationName(plotSettings, "DWF6 ePlot.pc3", "ANSI_A_(8.50_x_11.00_Inches)");
                    psv.RefreshLists(plotSettings);
                    psv.SetPlotViewName(plotSettings, viewName);
                    psv.SetPlotType(plotSettings, Autodesk.AutoCAD.DatabaseServices.PlotType.View);
                    psv.SetUseStandardScale(plotSettings, true);
                    psv.SetStdScaleType(plotSettings, StdScaleType.ScaleToFit);
                    psv.SetPlotCentered(plotSettings, true);
                    psv.SetPlotRotation(plotSettings, PlotRotation.Degrees000);
                    psv.SetPlotPaperUnits(plotSettings, PlotPaperUnit.Inches);
                    plotSettings.PlotSettingsName = String.Format("{0}{1}", viewName, "PS");
                    plotSettings.PrintLineweights = true;
                    plotSettings.AddToPlotSettingsDictionary(db);
                    Tx.AddNewlyCreatedDBObject(plotSettings, true);
                    psv.RefreshLists(plotSettings);
                    layout.CopyFrom(plotSettings);
                }
                Tx.Commit();
            }
            short bgPlot = (short)Autodesk.AutoCAD.ApplicationServices.Core.Application.GetSystemVariable("BACKGROUNDPLOT");

            Autodesk.AutoCAD.ApplicationServices.Core.Application.SetSystemVariable("BACKGROUNDPLOT", 0);
            string dwgFileName = Autodesk.AutoCAD.ApplicationServices.Core.Application.GetSystemVariable("DWGNAME") as string;
            string dwgPath     = Autodesk.AutoCAD.ApplicationServices.Core.Application.GetSystemVariable("DWGPREFIX") as string;

            using (Transaction Tx = db.TransactionManager.StartTransaction())
            {
                DsdEntryCollection collection     = new DsdEntryCollection();
                ObjectId           activeLayoutId = LayoutManager.Current.GetLayoutId(LayoutManager.Current.CurrentLayout);
                foreach (String viewName in viewsToPlot)
                {
                    Layout   layout = Tx.GetObject(activeLayoutId, OpenMode.ForRead) as Layout;
                    DsdEntry entry  = new DsdEntry();
                    entry.DwgName      = dwgPath + dwgFileName;
                    entry.Layout       = layout.LayoutName;
                    entry.Title        = viewName;
                    entry.NpsSourceDwg = entry.DwgName;
                    entry.Nps          = String.Format("{0}{1}", viewName, "PS");
                    collection.Add(entry);
                }
                dwgFileName = dwgFileName.Substring(0, dwgFileName.Length - 4);
                DsdData dsdData = new DsdData();
                dsdData.SheetType       = SheetType.MultiDwf;
                dsdData.ProjectPath     = dwgPath;
                dsdData.DestinationName = dsdData.ProjectPath + dwgFileName + ".dwf";
                /*Get password from user*/
                dsdData.Password = pwdWindow.passwordBox.Password;
                if (System.IO.File.Exists(dsdData.DestinationName))
                {
                    System.IO.File.Delete(dsdData.DestinationName);
                }
                dsdData.SetDsdEntryCollection(collection);

                /*DsdFile */
                string dsdFile = dsdData.ProjectPath + dwgFileName + ".dsd";
                dsdData.WriteDsd(dsdFile);
                System.IO.StreamReader sr = new System.IO.StreamReader(dsdFile);
                string str = sr.ReadToEnd();
                sr.Close();
                str = str.Replace("PromptForDwfName=TRUE",
                                  "PromptForDwfName=FALSE");
                /*Prompts User to Enter Password and Reconfirms*/
                //str = str.Replace("PromptForPwd=FALSE",
                //                   "PromptForPwd=TRUE");
                //str = str.Replace("PwdProtectPublishedDWF=FALSE",
                //                   "PwdProtectPublishedDWF=TRUE");
                int           occ        = 0;
                int           index      = str.IndexOf("Setup=");
                int           startIndex = 0;
                StringBuilder dsdText    = new StringBuilder();
                while (index != -1)
                {
                    String str1 = str.Substring(startIndex, index + 6 - startIndex);
                    dsdText.Append(str1);
                    dsdText.Append(String.Format("{0}{1}", viewsToPlot[occ], "PS"));
                    startIndex = index + 6;
                    index      = str.IndexOf("Setup=", index + 6);
                    if (index == -1)
                    {
                        dsdText.Append(str.Substring(startIndex, str.Length - startIndex));
                    }
                    occ++;
                }
                System.IO.StreamWriter sw = new System.IO.StreamWriter(dsdFile);
                sw.Write(dsdText.ToString());
                sw.Close();
                dsdData.ReadDsd(dsdFile);
                System.IO.File.Delete(dsdFile);
                PlotConfig plotConfig = PlotConfigManager.SetCurrentConfig("DWF6 ePlot.pc3");
                Publisher  publisher  = Autodesk.AutoCAD.ApplicationServices.Core.Application.Publisher;
                publisher.PublishExecute(dsdData, plotConfig);
                Tx.Commit();
            }
            Autodesk.AutoCAD.ApplicationServices.Core.Application.SetSystemVariable("BACKGROUNDPLOT", bgPlot);
        }
Example #17
0
        CollectEvent(object sender, CollectorEventArgs e)
        {
            // cast the sender object to the SnoopCollector we are expecting
            Collector snoopCollector = sender as Collector;

            if (snoopCollector == null)
            {
                Debug.Assert(false);    // why did someone else send us the message?
                return;
            }

            // see if it is a type we are responsible for
            PlotLogger log = e.ObjToSnoop as PlotLogger;

            if (log != null)
            {
                Stream(snoopCollector.Data(), log);
                return;
            }

            DsdData dsdData = e.ObjToSnoop as DsdData;

            if (dsdData != null)
            {
                Stream(snoopCollector.Data(), dsdData);
                return;
            }

            DsdEntry dsdEntry = e.ObjToSnoop as DsdEntry;

            if (dsdEntry != null)
            {
                Stream(snoopCollector.Data(), dsdEntry);
                return;
            }

            Dwf3dOptions dwf3dOpt = e.ObjToSnoop as Dwf3dOptions;

            if (dwf3dOpt != null)
            {
                Stream(snoopCollector.Data(), dwf3dOpt);
                return;
            }

            BeginDocumentEventArgs beginDocArgs = e.ObjToSnoop as BeginDocumentEventArgs;

            if (beginDocArgs != null)
            {
                Stream(snoopCollector.Data(), beginDocArgs);
                return;
            }

            BeginPageEventArgs beginPageArgs = e.ObjToSnoop as BeginPageEventArgs;

            if (beginPageArgs != null)
            {
                Stream(snoopCollector.Data(), beginPageArgs);
                return;
            }

            BeginPlotEventArgs beginPlotArgs = e.ObjToSnoop as BeginPlotEventArgs;

            if (beginPlotArgs != null)
            {
                Stream(snoopCollector.Data(), beginPlotArgs);
                return;
            }

            EndDocumentEventArgs endDocArgs = e.ObjToSnoop as EndDocumentEventArgs;

            if (endDocArgs != null)
            {
                Stream(snoopCollector.Data(), endDocArgs);
                return;
            }

            EndPageEventArgs endPageArgs = e.ObjToSnoop as EndPageEventArgs;

            if (endPageArgs != null)
            {
                Stream(snoopCollector.Data(), endPageArgs);
                return;
            }

            EndPlotEventArgs endPlotArgs = e.ObjToSnoop as EndPlotEventArgs;

            if (endPlotArgs != null)
            {
                Stream(snoopCollector.Data(), endPlotArgs);
                return;
            }

            PlotInfo plotInfo = e.ObjToSnoop as PlotInfo;

            if (plotInfo != null)
            {
                Stream(snoopCollector.Data(), plotInfo);
                return;
            }

            PlotPageInfo plotPageInfo = e.ObjToSnoop as PlotPageInfo;

            if (plotPageInfo != null)
            {
                Stream(snoopCollector.Data(), plotPageInfo);
                return;
            }

            PlotConfig plotConfig = e.ObjToSnoop as PlotConfig;

            if (plotConfig != null)
            {
                Stream(snoopCollector.Data(), plotConfig);
                return;
            }

            PlotConfigInfo plotConfigInfo = e.ObjToSnoop as PlotConfigInfo;

            if (plotConfigInfo != null)
            {
                Stream(snoopCollector.Data(), plotConfigInfo);
                return;
            }

            PlotEngine plotEngine = e.ObjToSnoop as PlotEngine;

            if (plotEngine != null)
            {
                Stream(snoopCollector.Data(), plotEngine);
                return;
            }

            PlotInfoValidator plotInfoValidator = e.ObjToSnoop as PlotInfoValidator;

            if (plotInfoValidator != null)
            {
                Stream(snoopCollector.Data(), plotInfoValidator);
                return;
            }

            PlotProgress plotProgress = e.ObjToSnoop as PlotProgress;

            if (plotProgress != null)
            {
                Stream(snoopCollector.Data(), plotProgress);
                return;
            }

            // ValueTypes we have to treat a bit different
            if (e.ObjToSnoop is MediaBounds)
            {
                Stream(snoopCollector.Data(), (MediaBounds)e.ObjToSnoop);
                return;
            }
        }
Example #18
0
        static public void PublishExecute()
        {
            try
            {
                string dsdFilePath = "c:\\Temp\\publisher.dsd";
                string dsdLogPath  = "c:\\Temp\\logdwf.log";
                string dwfDestPath = "c:\\Temp\\PublisherTest.dwf";

                DsdEntryCollection collection = new DsdEntryCollection();
                DsdEntry           entry;

                entry              = new DsdEntry();
                entry.Layout       = "Layout1";
                entry.DwgName      = "c:\\Temp\\Drawing1.dwg";
                entry.NpsSourceDwg = entry.DwgName;
                entry.Title        = "Sheet1";
                collection.Add(entry);

                entry              = new DsdEntry();
                entry.Layout       = "Layout1";
                entry.DwgName      = "c:\\Temp\\Drawing2.dwg";
                entry.NpsSourceDwg = entry.DwgName;
                entry.Title        = "Sheet2";
                collection.Add(entry);

                DsdData dsd = new DsdData();
                dsd.SetDsdEntryCollection(collection);
                dsd.IsSheetSet      = true;
                dsd.LogFilePath     = dsdLogPath;
                dsd.SheetType       = SheetType.MultiDwf;
                dsd.NoOfCopies      = 1;
                dsd.DestinationName = dwfDestPath;
                dsd.SheetSetName    = "PublisherSet";
                dsd.WriteDsd(dsdFilePath);

                //Workaround to avoid promp for dwf file name
                //set PromptForDwfName=FALSE in dsdData using StreamReader/StreamWriter
                System.IO.StreamReader sr = new System.IO.StreamReader(dsdFilePath);
                string str = sr.ReadToEnd();
                sr.Close();

                str = str.Replace("PromptForDwfName=TRUE", "PromptForDwfName=FALSE");
                System.IO.StreamWriter sw = new System.IO.StreamWriter(dsdFilePath);
                sw.Write(str);
                sw.Close();

                dsd.ReadDsd(dsdFilePath);

                Autodesk.AutoCAD.PlottingServices.PlotConfigManager.SetCurrentConfig(
                    "DWF6 ePlot.pc3");

                AboutToBeginPublishingEventHandler Publisher_AboutToBeginPublishing = null;
                Application.Publisher.AboutToBeginPublishing +=
                    new Autodesk.AutoCAD.Publishing.AboutToBeginPublishingEventHandler(
                        Publisher_AboutToBeginPublishing);

                Application.Publisher.PublishExecute(
                    dsd,
                    Autodesk.AutoCAD.PlottingServices.PlotConfigManager.CurrentConfig);

                System.IO.File.Delete(dsdFilePath);
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.Message);
            }
        }
Example #19
0
        static public void PublisherDSD()
        {
            try
            {
                DsdEntryCollection collection = new DsdEntryCollection();
                DsdEntry           entry;

                entry         = new DsdEntry();
                entry.Layout  = "Layout1";
                entry.DwgName = "c:\\Temp\\Drawing1.dwg";
                entry.Nps     = "Setup1";
                entry.Title   = "Sheet1";
                collection.Add(entry);

                entry         = new DsdEntry();
                entry.Layout  = "Layout1";
                entry.DwgName = "c:\\Temp\\Drawing2.dwg";
                entry.Nps     = "Setup1";
                entry.Title   = "Sheet2";
                collection.Add(entry);

                DsdData dsd = new DsdData();

                dsd.SetDsdEntryCollection(collection);

                dsd.ProjectPath     = "c:\\Temp\\";
                dsd.LogFilePath     = "c:\\Temp\\logdwf.log";
                dsd.SheetType       = SheetType.MultiDwf;
                dsd.NoOfCopies      = 1;
                dsd.DestinationName = "c:\\Temp\\PublisherTest.dwf";
                dsd.SheetSetName    = "PublisherSet";
                dsd.WriteDsd("c:\\Temp\\publisher.dsd");


                int nbSheets = collection.Count;

                using (PlotProgressDialog progressDlg =
                           new PlotProgressDialog(false, nbSheets, true))
                {
                    progressDlg.set_PlotMsgString(
                        PlotMessageIndex.DialogTitle,
                        "Plot API Progress");
                    progressDlg.set_PlotMsgString(
                        PlotMessageIndex.CancelJobButtonMessage,
                        "Cancel Job");
                    progressDlg.set_PlotMsgString(
                        PlotMessageIndex.CancelSheetButtonMessage,
                        "Cancel Sheet");
                    progressDlg.set_PlotMsgString(
                        PlotMessageIndex.SheetSetProgressCaption,
                        "Job Progress");
                    progressDlg.set_PlotMsgString(
                        PlotMessageIndex.SheetProgressCaption,
                        "Sheet Progress");

                    progressDlg.UpperPlotProgressRange = 100;
                    progressDlg.LowerPlotProgressRange = 0;

                    progressDlg.UpperSheetProgressRange = 100;
                    progressDlg.LowerSheetProgressRange = 0;

                    progressDlg.IsVisible = true;

                    Autodesk.AutoCAD.Publishing.Publisher publisher =
                        Application.Publisher;

                    Autodesk.AutoCAD.PlottingServices.PlotConfigManager.
                    SetCurrentConfig("DWF6 ePlot.pc3");

                    publisher.PublishDsd(
                        "c:\\Temp\\publisher.dsd", progressDlg);
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                PGA.MessengerManager.MessengerManager.AddLog(ex.Message);
            }
        }
Example #20
0
            public void PublishNew(string pdfLocation, string layerName)
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;

                Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication.GetType().InvokeMember("ZoomExtents", BindingFlags.InvokeMethod, null, Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication, null);
                Database db = doc.Database;
                Editor   ed = doc.Editor;

                string dwgFileName =
                    Application.GetSystemVariable("DWGNAME") as string;
                string dwgPath =
                    Application.GetSystemVariable("DWGPREFIX") as string;

                string name =
                    System.IO.Path.GetFileNameWithoutExtension(dwgFileName);

                //find a temp location.
                string strTemp = System.IO.Path.GetTempPath();

                //PromptStringOptions options =
                //    new PromptStringOptions("Specific the DWF file name");
                //options.DefaultValue = "c:\\temp\\" + name + ".dwf";
                //PromptResult result = ed.GetString(options);

                //if (result.Status != PromptStatus.OK)
                //    return;

                //get the layout ObjectId List
                System.Collections.ArrayList layoutList = null;
                try
                {
                    layoutList = layouts;
                }
                catch
                {
                    //  Application.ShowAlertDialog("Unable to get layouts name");
                    return;
                }

                Publisher publisher = Application.Publisher;

                //put the plot in foreground
                short bgPlot =
                    (short)Application.GetSystemVariable("BACKGROUNDPLOT");

                Application.SetSystemVariable("BACKGROUNDPLOT", 0);

                using (Transaction tr =
                           db.TransactionManager.StartTransaction())
                {
                    DsdEntryCollection collection =
                        new DsdEntryCollection();

                    Layout _layout = null;
                    foreach (ObjectId layoutId in layoutList)
                    {
                        Layout layout = tr.GetObject(layoutId, OpenMode.ForWrite) as Layout;

                        #region plot Extends

                        Point3d pMin = layout.Extents.MinPoint;
                        Point3d pMax = layout.Extents.MaxPoint;
                        PlotSettingsValidator psv = PlotSettingsValidator.Current;
                        PlotSettings          ps  = new PlotSettings(layout.ModelType);
                        ps.CopyFrom(layout);
                        psv.SetPlotType(ps, Autodesk.AutoCAD.DatabaseServices.PlotType.Extents);
                        bool bAreaIsLandscape = true;
                        if (layout.ModelType)
                        {
                            bAreaIsLandscape = (db.Extmax.X - db.Extmin.X) > (db.Extmax.Y - db.Extmin.Y) ? true : false;
                        }
                        else
                        {
                            bAreaIsLandscape = (layout.Extents.MaxPoint.X - layout.Extents.MinPoint.X) > (layout.Extents.MaxPoint.Y - layout.Extents.MinPoint.Y) ? true : false;
                        }

                        bool bPaperIsLandscape = (ps.PlotPaperSize.X > ps.PlotPaperSize.Y) ? true : false;
                        if (bPaperIsLandscape != bAreaIsLandscape)
                        {
                            psv.SetPlotRotation(ps, PlotRotation.Degrees270);
                        }
                        ps.PlotSettingsName = layout.LayoutName + "_plot";
                        ps.AddToPlotSettingsDictionary(db);
                        tr.AddNewlyCreatedDBObject(ps, true);
                        psv.RefreshLists(ps);
                        layout.CopyFrom(ps);

                        #endregion

                        DsdEntry entry = new DsdEntry();

                        entry.DwgName = dwgPath + dwgFileName;
                        entry.Layout  = layout.LayoutName;
                        entry.Title   = Path.GetFileNameWithoutExtension(this.dwgFile) + "-" + layerName + "-" + layout.LayoutName;
                        entry.Nps     = ps.PlotSettingsName;// "AA";
                        _layout       = layout;
                        collection.Add(entry);
                    }

                    DsdData dsdData = new DsdData();

                    dsdData.SheetType       = SheetType.SinglePdf;
                    dsdData.ProjectPath     = pdfLocation;
                    dsdData.DestinationName = pdfLocation + name + " " + layerName + ".pdf";


                    dsdData.SetUnrecognizedData("PwdProtectPublishedDWF", "FALSE");
                    dsdData.SetUnrecognizedData("PromptForPwd", "FALSE");
                    dsdData.SetUnrecognizedData("INCLUDELAYER", "FALSE");
                    dsdData.NoOfCopies    = 1;
                    dsdData.IsHomogeneous = false;

                    string dsdFile = pdfLocation + name + layerName + ".dsd";

                    if (System.IO.File.Exists(dsdFile))
                    {
                        System.IO.File.Delete(dsdFile);
                    }

                    dsdData.SetDsdEntryCollection(collection);
                    dsdData.PromptForDwfName = false;

                    //Workaround to avoid promp for dwf file name
                    //set PromptForDwfName=FALSE in
                    //dsdData using StreamReader/StreamWriter
                    dsdData.WriteDsd(dsdFile);

                    StreamReader sr  = new StreamReader(dsdFile);
                    string       str = sr.ReadToEnd();
                    sr.Close();

                    //str =
                    //    str.Replace("PromptForDwfName=TRUE",
                    //                    "PromptForDwfName=FALSE");
                    str =
                        str.Replace("IncludeLayer=TRUE",
                                    "IncludeLayer=FALSE");

                    StreamWriter sw = new StreamWriter(dsdFile);
                    sw.Write(str);
                    sw.Close();

                    dsdData.ReadDsd(dsdFile);

                    //18-07-2018
                    //PlotSettings ps = new PlotSettings(_layout.ModelType);
                    //ps.CopyFrom(_layout);

                    //PlotSettingsValidator psv = PlotSettingsValidator.Current;
                    //psv.SetPlotType(ps, Autodesk.AutoCAD.DatabaseServices.PlotType.Extents);
                    //psv.SetUseStandardScale(ps, true);
                    //psv.SetStdScaleType(ps, StdScaleType.ScaleToFit);
                    //psv.SetPlotCentered(ps, true);


                    //// We'll use the standard DWF PC3, as

                    //// for today we're just plotting to file


                    //psv.SetPlotConfigurationName(ps, "DWG to PDF.PC3", "ANSI_A_(8.50_x_11.00_Inches)");

                    using (PlotConfig pc = PlotConfigManager.SetCurrentConfig("DWG to PDF.PC3"))
                    {
                        publisher.PublishExecute(dsdData, pc);
                    }

                    System.IO.File.Delete(dsdFile);

                    tr.Commit();
                }

                //reset the background plot value
                Application.SetSystemVariable("BACKGROUNDPLOT", bgPlot);
            }
Example #21
0
        public static void PublishLayouts(string path, string hole)
        {
            var completepath = Path.ChangeExtension(path, "pdf");

            var filename = Application.DocumentManager.MdiActiveDocument.Name;

            using (DsdEntryCollection dsdDwgFiles = new DsdEntryCollection())
            {
                // Define the first layout
                using (DsdEntry dsdDwgFile1 = new DsdEntry())
                {
                    // Set the file name and layout
                    dsdDwgFile1.DwgName = filename;
                    dsdDwgFile1.Layout  = "PGA-OUTPUT";
                    dsdDwgFile1.Title   = "PGA TOUR AUTOGENERATED SURFACE";

                    // Set the page setup override
                    dsdDwgFile1.Nps          = "";
                    dsdDwgFile1.NpsSourceDwg = "";

                    dsdDwgFiles.Add(dsdDwgFile1);
                }
                #region Define Second Layout

                //// Define the second layout
                //using (DsdEntry dsdDwgFile2 = new DsdEntry())
                //{
                //    // Set the file name and layout
                //    dsdDwgFile2.DwgName = "C:\\AutoCAD\\Samples\\Sheet Sets\\Architectural\\A-02.dwg";
                //    dsdDwgFile2.Layout = "ELEVATIONS";
                //    dsdDwgFile2.Title = "A-02 ELEVATIONS";

                //    // Set the page setup override
                //    dsdDwgFile2.Nps = "";
                //    dsdDwgFile2.NpsSourceDwg = "";

                //    dsdDwgFiles.Add(dsdDwgFile2);
                //}
                #endregion


                #region Define third layout
                //// Define the second layout
                using (DsdEntry dsdDwgFile2 = new DsdEntry())
                {
                    // Set the file name and layout
                    dsdDwgFile2.DwgName = filename;
                    dsdDwgFile2.Layout  = "ELEVATIONS";
                    dsdDwgFile2.Title   = "A-02 ELEVATIONS";


                    // Set the page setup override
                    dsdDwgFile2.Nps          = "";
                    dsdDwgFile2.NpsSourceDwg = @"C:\Users\m4800\AppData\Roaming\Autodesk\ApplicationPlugins\PGA-CivilTinSurf2016.bundle\Contents\Template\PGA_PRINT_TMPLT.dwg";
                    dsdDwgFiles.Add(dsdDwgFile2);
                }
                #endregion

                // Set the properties for the DSD file and then write it out
                using (DsdData dsdFileData = new DsdData())
                {
                    dsdFileData.PromptForDwfName = false;
                    // Set the target information for publishing
                    //dsdFileData.DestinationName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\PGA-Publish.pdf";
                    //dsdFileData.ProjectPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\";
                    dsdFileData.DestinationName = completepath;    //PGA-Publish.pdf;
                    dsdFileData.ProjectPath     = Path.GetDirectoryName(completepath);

                    dsdFileData.SheetType = SheetType.MultiPdf;

                    // Set the drawings that should be added to the publication
                    dsdFileData.SetDsdEntryCollection(dsdDwgFiles);

                    // Set the general publishing properties
                    //dsdFileData.LogFilePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\myBatch.txt";
                    dsdFileData.LogFilePath = Path.ChangeExtension(path, "txt"); // "\\myBatch.txt";


                    // Create the DSD file
                    //dsdFileData.WriteDsd(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\batchdrawings2.dsd");
                    dsdFileData.WriteDsd(Path.ChangeExtension(path, "dsd")); //"\\batchdrawings2.dsd");
                    try
                    {
                        // Publish the specified drawing files in the DSD file, and
                        // honor the behavior of the BACKGROUNDPLOT system variable

                        using (DsdData dsdDataFile = new DsdData())
                        {
                            //dsdDataFile.ReadDsd(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\batchdrawings2.dsd");
                            dsdDataFile.ReadDsd(Path.ChangeExtension(path, "dsd")); // + "\\batchdrawings2.dsd");
                            // Get the DWG to PDF.pc3 and use it as a
                            // device override for all the layouts
                            PlotConfig acPlCfg = PlotConfigManager.SetCurrentConfig("DWG to PDF.PC3");

                            Application.Publisher.PublishExecute(dsdDataFile, acPlCfg);
                        }
                    }
                    catch (Autodesk.AutoCAD.Runtime.Exception es)
                    {
                        MessengerManager.MessengerManager.LogException(es);
                    }
                }
            }
        }