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); }
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); }
/// <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; } } }