Hi Gile,
What you've coded is amazing, and I've tried it with the default AutoCAD layout and they are publishing fine.
However, once I attempt to publish a customized layout, AutoCAD sent me error message says that there were no plottable sheets.
Here are some details if you can take a look:
//Delete viewport
private static void DeleteViewports(ref Document doc)
{
Database db = doc.Database;
Editor ed = doc.Editor;
ObjectIdCollection erased = new ObjectIdCollection();
using (DocumentLock dlock = doc.LockDocument())
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
ed.SwitchToPaperSpace();
BlockTableRecord acBlkTblRec;
acBlkTblRec = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForRead);
BlockTableRecordEnumerator pBTREnum = acBlkTblRec.GetEnumerator();
int i = 0;
while (pBTREnum.MoveNext())
{
ObjectId pObjId = pBTREnum.Current;
DBObject pDbObj = (DBObject)trans.GetObject(pObjId, OpenMode.ForRead);
if (i == 0)
{
i = i + 1;
continue;
}
else
{
if (!pDbObj.IsErased)
{
pDbObj.UpgradeOpen();
pDbObj.Erase(true);
}
}
}
trans.Commit();
}
}
}
//paperName = @"ANSI_A_(8.50_x_11.00_Inches)"
//deviceName = "DWF6 ePlot.pc3"
private static void CreateOrEditPageSetUp(string deviceName, string paperName, string pageSetUp)
{
Document acDoc = AcadApp.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
DBDictionary plSets = acTrans.GetObject(acCurDb.PlotSettingsDictionaryId, OpenMode.ForRead) as DBDictionary;
DBDictionary vStyles = acTrans.GetObject(acCurDb.VisualStyleDictionaryId, OpenMode.ForRead) as DBDictionary;
PlotSettings acPlSet = default(PlotSettings);
bool createNew = false;
LayoutManager acLaoutMgr = LayoutManager.Current;
Layout acLayout = acTrans.GetObject(acLaoutMgr.GetLayoutId(acLaoutMgr.CurrentLayout), OpenMode.ForRead) as Layout;
if (plSets.Contains(pageSetUp) == false)
{
createNew = true;
acPlSet = new PlotSettings(acLayout.ModelType);
acPlSet.CopyFrom(acLayout);
acPlSet.PlotSettingsName = pageSetUp;
acPlSet.AddToPlotSettingsDictionary(acCurDb);
acTrans.AddNewlyCreatedDBObject(acPlSet, true);
}
else
{
acPlSet = plSets.GetAt(pageSetUp).GetObject(OpenMode.ForWrite) as PlotSettings;
}
try
{
PlotSettingsValidator acPlSetVdr = PlotSettingsValidator.Current;
acPlSetVdr.SetPlotConfigurationName(acPlSet, deviceName, paperName);
if (acLayout.ModelType == false)
{
acPlSetVdr.SetPlotType(acPlSet, Autodesk.AutoCAD.DatabaseServices.PlotType.Layout);
}
else
{
acPlSetVdr.SetPlotType(acPlSet, Autodesk.AutoCAD.DatabaseServices.PlotType.Extents);
acPlSetVdr.SetPlotCentered(acPlSet, true);
}
// Set the plot offset
acPlSetVdr.SetPlotOrigin(acPlSet, new Point2d(0, 0));
// Set the plot scale
acPlSetVdr.SetUseStandardScale(acPlSet, true);
acPlSetVdr.SetStdScaleType(acPlSet, StdScaleType.ScaleToFit);
acPlSetVdr.SetPlotPaperUnits(acPlSet, PlotPaperUnit.Millimeters);
acPlSet.ScaleLineweights = true;
acPlSet.ShowPlotStyles = true;
acPlSetVdr.RefreshLists(acPlSet);
// Specify the shaded viewport options
acPlSet.ShadePlot = PlotSettingsShadePlotType.AsDisplayed;
acPlSet.ShadePlotResLevel = ShadePlotResLevel.Normal;
// Specify the plot options
acPlSet.PrintLineweights = true;
acPlSet.PlotTransparency = false;
acPlSet.PlotPlotStyles = true;
acPlSet.DrawViewportsFirst = true;
// Specify the plot orientation
acPlSetVdr.SetPlotRotation(acPlSet, PlotRotation.Degrees000);
// Set the plot style
if (acCurDb.PlotStyleMode == true)
{
acPlSetVdr.SetCurrentStyleSheet(acPlSet, "acad.ctb");
}
else
{
acPlSetVdr.SetCurrentStyleSheet(acPlSet, "acad.stb");
}
// Zoom to show the whole paper
acPlSetVdr.SetZoomToPaperOnUpdate(acPlSet, true);
}
catch (Autodesk.AutoCAD.Runtime.Exception es)
{
System.Windows.Forms.MessageBox.Show(es.Message);
}
// Save the changes made
acTrans.Commit();
if (createNew == true)
{
acPlSet.Dispose();
}
}
}
public static void AssignPageSetupToLayout(string pageSetup)
{
// Get the current document and database, and start a transaction
Document acDoc = AcadApp.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Reference the Layout Manager
LayoutManager acLayoutMgr = LayoutManager.Current;
// Get the current layout and output its name in the Command Line window
Layout acLayout = acTrans.GetObject(acLayoutMgr.GetLayoutId(acLayoutMgr.CurrentLayout),
OpenMode.ForRead) as Layout;
DBDictionary acPlSet = acTrans.GetObject(acCurDb.PlotSettingsDictionaryId,
OpenMode.ForRead) as DBDictionary;
// Check to see if the page setup exists
if (acPlSet.Contains(pageSetup) == true)
{
PlotSettings plSet = acPlSet.GetAt(pageSetup).GetObject(OpenMode.ForRead) as PlotSettings;
// Update the layout
acLayout.UpgradeOpen();
acLayout.CopyFrom(plSet);
// Save the new objects to the database
acTrans.Commit();
}
else
{
// Ignore the changes made
acTrans.Abort();
}
}
// Update the display
acDoc.Editor.Regen();
}