Message 1 of 5

Not applicable
10-09-2019
07:16 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
I'm able to successfully execute my application below, however AutoCAD freezes at the end of the code execution. This is a modified version of _gile's publisher to open DWGs by passing the file location.
Any ideas why it could be hanging? My first thought is that I'm not closing the drawing database properly in PlotPdf()
Resource usage does get higher than normal, however there are no errors, other than VB debugger:
Managed Debugging Assistant 'ContextSwitchDeadlock' has detected a problem in 'C:\Program Files\Autodesk\AutoCAD 2017\acad.exe'. Additional Information: The CLR has been unable to transition from COM context 0x248689a8 to COM context 0x24868758 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations. occurred
// Modified version of gileCAD code public abstract class PlotToFileConfig { // Private fields private string dsdFile, dwgFile, outputDir, outputFile, plotType; private int sheetNum; private IEnumerable<Layout> layouts; private const string LOG = "publish.log"; // Base constructor public PlotToFileConfig(string outputDir, IEnumerable<Layout> layouts, string plotType) { Database db = HostApplicationServices.WorkingDatabase; this.dwgFile = db.Filename; this.outputDir = outputDir; this.dsdFile = Path.ChangeExtension(this.dwgFile, "dsd"); this.layouts = layouts; this.plotType = plotType; string ext = plotType == "0" || plotType == "1" ? "dwf" : "pdf"; this.outputFile = Path.Combine( this.outputDir, Path.ChangeExtension(Path.GetFileName(this.dwgFile), ext)); } // Plot the layouts public void Publish() { if (TryCreateDSD()) { object bgp = Application.GetSystemVariable("BACKGROUNDPLOT"); object ctab = Application.GetSystemVariable("CTAB"); try { Application.SetSystemVariable("BACKGROUNDPLOT", 0); Publisher publisher = Application.Publisher; PlotProgressDialog plotDlg = new PlotProgressDialog(false, this.sheetNum, true); publisher.PublishDsd(this.dsdFile, plotDlg); plotDlg.Destroy(); File.Delete(this.dsdFile); } catch (System.Exception exn) { System.Windows.Forms.MessageBox.Show("Error: " + exn.Message + "\n\n" + exn.StackTrace, "Issue Drawings", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); throw; } finally { Application.SetSystemVariable("BACKGROUNDPLOT", bgp); Application.SetSystemVariable("CTAB", ctab); } } } // Creates the DSD file from a template (default options) 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.NoOfCopies = 1; dsd.DestinationName = this.outputFile; dsd.IsHomogeneous = false; dsd.LogFilePath = Path.Combine(this.outputDir, LOG); PostProcessDSD(dsd); return true; } } // Creates an entry collection (one per layout) for the DSD file 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; } // Writes the definitive DSD file from the templates and additional informations 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=" + this.plotType; } 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); } } // Class to plot one PDF file per sheet public class SingleSheetPdf : PlotToFileConfig { public SingleSheetPdf(string outputDir, IEnumerable<Layout> layouts) : base(outputDir, layouts, "5") { } } public static void PlotPdf(string dwg, string rev) { short bgp = (short)Application.GetSystemVariable("BACKGROUNDPLOT"); using (Database db = new Database(false, true)) { HostApplicationServices.WorkingDatabase = db; db.ReadDwgFile(dwg, FileShare.ReadWrite, false, null); 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"); SingleSheetPdf plotter = new SingleSheetPdf(filename, layouts); plotter.Publish(); tr.Commit(); } } catch (System.Exception e) { // Show this message System.Windows.Forms.MessageBox.Show("Error: " + e.Message + "\n\n" + e.StackTrace, "Issue Drawings", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); } finally { Application.SetSystemVariable("BACKGROUNDPLOT", bgp); } } } }
Solved! Go to Solution.