Publish Side Database

Publish Side Database

yoitsarun
Advocate Advocate
681 Views
0 Replies
Message 1 of 1

Publish Side Database

yoitsarun
Advocate
Advocate

Can anyone help me on this.

Getting error: There were no plottable sheets in the current operation.

source for this code from this link.

    [CommandMethod("Plot2024", CommandFlags.Session)]
    public static void BatchPublishSideDB()
    {
        // Disable background plotting for batch operations
        short bgPlot = (short)Application.GetSystemVariable("BACKGROUNDPLOT");
        Application.SetSystemVariable("BACKGROUNDPLOT", 0);

        try
        {
            // List of DWG files to process
            List<string> docsToPlot = new List<string>
            {
                "D:\\Test.dwg",

            };

            BatchPublish(docsToPlot);
        }
        finally
        {
            // Restore background plotting setting
            Application.SetSystemVariable("BACKGROUNDPLOT", bgPlot);
        }
    }

    public static void BatchPublish(List<string> docsToPlot)
    {
        DsdEntryCollection dsdEntries = new DsdEntryCollection();
        string outputFolder = @"D:\PDF"; // Output folder for the PDFs

        foreach (string fileName in docsToPlot)
        {
            try
            {
                using (var database = new Database(false, true))
                {
                    // Open the DWG file in read mode with all shared access
                    database.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndAllShare, false, null);

                    // Save the current working database
                    Database originalDb = HostApplicationServices.WorkingDatabase;

                    // Set the side database (the one you just loaded) as the working database
                    HostApplicationServices.WorkingDatabase = database;

                    // Start a transaction to access the layout dictionary and layouts
                    using (Transaction acTrans = database.TransactionManager.StartTransaction())
                    {
                        // Get the layout dictionary from the side database (opened DWG file)
                        DBDictionary layoutDict = acTrans.GetObject(database.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;

                        string fileNameOnly = Path.GetFileNameWithoutExtension(fileName);
                        ObjectId layoutId = ObjectId.Null;

                        // Loop through the layouts in the dictionary
                        foreach (DBDictionaryEntry entry in layoutDict)
                        {
                            // Get the Layout object
                            Layout layout = acTrans.GetObject(entry.Value, OpenMode.ForRead) as Layout;

                            // Skip "Model" layout
                            if (layout.LayoutName.Equals("Model", StringComparison.OrdinalIgnoreCase)) continue;

                            // Display the layout name in a message box for each layout
                            MessageBox.Show($"Processing Layout: {layout.LayoutName}", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            layoutId = entry.Value;

                            // Create a DsdEntry for the layout (used for batch publishing)
                            DsdEntry dsdEntry = new DsdEntry
                            {
                                DwgName = fileName,  // DWG file name
                                Layout = layout.LayoutName,  // Layout name
                                Title = $"{fileName}_{layout.LayoutName}",  // Title for the entry
                                NpsSourceDwg = fileName,  // Source DWG name (for NPS setup)
                                Nps = "Setup1"  // A named page setup, if available (you can adjust this)
                            };

                            // Add the DsdEntry to the collection for batch publishing
                            dsdEntries.Add(dsdEntry);
                        }

                        // Commit the transaction (this is necessary to close the transaction cleanly)
                        acTrans.Commit();
                    }

                    // Reset the working database to the original database after processing
                    HostApplicationServices.WorkingDatabase = originalDb;
                }

            }

            catch (System.Exception ex)
            {
                var ed = Application.DocumentManager.MdiActiveDocument.Editor;
                ed.WriteMessage($"\nError with '{fileName}': {ex.Message}");
            }

            // Configure the DSD data
            DsdData dsdData = new DsdData
            {
                SheetType = SheetType.MultiPdf,
                ProjectPath = outputFolder,
                DestinationName = Path.Combine(outputFolder, "BatchOutput.pdf")
            };
            dsdData.SetDsdEntryCollection(dsdEntries);

            // Write the DSD file and adjust settings
            string dsdFilePath = Path.Combine(outputFolder, "batchPublish.dsd");

            if (File.Exists(dsdFilePath))
                File.Delete(dsdFilePath);

            dsdData.WriteDsd(dsdFilePath);

            // Modify the DSD file to disable prompts
            string dsdContent = File.ReadAllText(dsdFilePath);
            dsdContent = dsdContent.Replace("PromptForDwfName=TRUE", "PromptForDwfName=FALSE");
            File.WriteAllText(dsdFilePath, dsdContent);

            // Re-read the modified DSD file
            dsdData.ReadDsd(dsdFilePath);
            File.Delete(dsdFilePath); // Clean up temporary DSD file

            // Execute the publishing process
            PlotConfig plotConfig = PlotConfigManager.SetCurrentConfig("DWG To PDF.pc3");
            Publisher publisher = Application.Publisher;

            publisher.PublishExecute(dsdData, plotConfig);

            Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"\nBatch publishing completed. Output folder: {outputFolder}");
        }
    }
}

 

0 Likes
682 Views
0 Replies
Replies (0)