The following C# sample shows you how to publish to DWF all layouts in a drawing. It illustrates the use of Autodesk.AutoCAD.Publishing.Publisher.PublishExecute API.
//////////////////////////////////////////////////////////////////////////
//Use: This command will publish all the layouts in the current
// drawing to a multi sheet dwf.
//////////////////////////////////////////////////////////////////////////
[CommandMethod("PublishAllLayouts")]
static public void PublishAllLayouts()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
//put the plot in foreground
short bgPlot =
(short)Application.GetSystemVariable("BACKGROUNDPLOT");
Application.SetSystemVariable("BACKGROUNDPLOT", 0);
//get the layout ObjectId List
System.Collections.Generic.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;
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);
}
dwgFileName = dwgFileName.Substring(
0, dwgFileName.Length - 4);
DsdData dsdData = new DsdData();
dsdData.SheetType = SheetType.MultiDwf; //SheetType.MultiPdf
dsdData.ProjectPath = dwgPath;
dsdData.DestinationName =
dsdData.ProjectPath + dwgFileName + ".dwf";
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);
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("DWF6 ePlot.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);
publisher.PublishExecute(dsdData, plotConfig);
Tx.Commit();
}
//reset the background plot value
Application.SetSystemVariable("BACKGROUNDPLOT", bgPlot);
}
private static System.Collections.Generic.List<ObjectId>
GetLayoutIds(Database db)
{
System.Collections.Generic.List<ObjectId> layoutIds =
new System.Collections.Generic.List<ObjectId>();
using (Transaction Tx = db.TransactionManager.StartTransaction())
{
DBDictionary layoutDic = Tx.GetObject(
db.LayoutDictionaryId,
OpenMode.ForRead, false)
as DBDictionary;
foreach (DBDictionaryEntry entry in layoutDic)
{
layoutIds.Add(entry.Value);
}
}
return layoutIds;
}
static void Publisher_AboutToBeginPublishing(
object sender,
Autodesk.AutoCAD.Publishing.AboutToBeginPublishingEventArgs e)
{
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
"\nAboutToBeginPublishing!!");
}
The following sample uses a similar approach but illustrates how to use "PublishExecute" API to batch plot layouts from several different drawings without need to worry about synchronization issues linked to opening/closing the files:
//////////////////////////////////////////////////////////////////////////
//Use: Will batch publish to single dwf every layout of
// each document provided as input
// Make sure each of your drawing contains a page setup
// named Setup1 before running.
//
//////////////////////////////////////////////////////////////////////////
[CommandMethod("BatchPublishCmd", CommandFlags.Session)]
static public void BatchPublishCmd()
{
short bgPlot =
(short)Application.GetSystemVariable("BACKGROUNDPLOT");
Application.SetSystemVariable("BACKGROUNDPLOT", 0);
System.Collections.Generic.List<string> docsToPlot =
new System.Collections.Generic.List<string>();
docsToPlot.Add("C:\\Temp\\Drawing1.dwg");
docsToPlot.Add("C:\\Temp\\Drawing2.dwg");
docsToPlot.Add("C:\\Temp\\Drawing3.dwg");
BatchPublish(docsToPlot);
}
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();
entry.DwgName = filename;
entry.Layout = layout.LayoutName;
entry.Title = docName + "_" + layout.LayoutName;
entry.NpsSourceDwg = entry.DwgName;
entry.Nps = "Setup1";
collection.Add(entry);
}
Tx.Commit();
}
}
}
DsdData dsdData = new DsdData();
dsdData.SheetType = SheetType.SingleDwf;
dsdData.ProjectPath = "C:\\Temp";
//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("DWF6 ePlot.pc3");
Autodesk.AutoCAD.Publishing.Publisher publisher =
Autodesk.AutoCAD.ApplicationServices.Application.Publisher;
publisher.PublishExecute(dsdData, plotConfig);
}
private static System.Collections.Generic.List<ObjectId>
getLayoutIds(Database db)
{
System.Collections.Generic.List<ObjectId> layoutIds =
new System.Collections.Generic.List<ObjectId>();
using (Transaction Tx = db.TransactionManager.StartTransaction())
{
DBDictionary layoutDic = Tx.GetObject(
db.LayoutDictionaryId,
OpenMode.ForRead, false)
as DBDictionary;
foreach (DBDictionaryEntry entry in layoutDic)
{
layoutIds.Add(entry.Value);
}
}
return layoutIds;
}