Building on my earlier post, here's an incremental update which makes available a LispFunction Method named
vla-SetActivePageSetup.
A help file for this function is attached to the bottom of this post. 2011 .chm colors used. The help file is in HTM format, but a .TXT extension has been added, as .HTM is not a supported upload file format here at AUGI.
Please note that while I have tested this enough to determine that it functions as designed (tested for 2010-2012, using Civil 3D), I have not yet had an opportunity to test this via ObjectDBX, etc., so constructive criticism is welcomed.
vla-SetActivePageSetup
Sets a named page setup active for a given layout by name
Code:
(vla-SetActivePageSetup layoutName pageSetupName)
Arguments
layoutName
A string specifying the name of a layout to apply the named page setup.
pageSetupName
A string specifying the page setup to apply to the layout.
Return Values
Nil if either the
layoutName, or
pageSetupName cannot be found. Otherwise,
T if successful.
Example
Code:
(foreach layoutname (layoutlist)
(vla-SetActivePageSetup layoutname “YourPageSetupName”)
)
Like me, most of you would prefer to compile the .NET assembly yourself, so here's the source code for your use:
Code:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using acApp = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly: CommandClass(typeof(BlackBox.AutoCAD.PageSetup.Commands))]
namespace BlackBox.AutoCAD.PageSetup
{
public class Commands
{
[LispFunction("vla-SetActivePageSetup")]
public bool vlaSetActivePageSetup(ResultBuffer resbuf)
{
Document doc = acApp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
bool ret = false;
try
{
if (resbuf == null)
throw new TooFewArgsException();
TypedValue[] args = resbuf.AsArray();
if (args.Length < 1)
throw new TooFewArgsException();
if (args.Length > 2)
throw new TooManyArgsException();
if (args[0].TypeCode != (int)LispDataType.Text)
throw new ArgumentTypeException("stringp", args[0]);
if (args.Length == 2 && args[1].TypeCode != (int)LispDataType.Text)
throw new ArgumentTypeException("stringp", args[1]);
string layoutName = args[0].Value.ToString().ToUpper();
string pageSetupName = args[1].Value.ToString().ToUpper();
using (DocumentLock dl = doc.LockDocument())
{
using (Transaction tr =
doc.Database.TransactionManager.StartOpenCloseTransaction())
{
DBDictionary layoutDic =
tr.GetObject(doc.Database.LayoutDictionaryId,
OpenMode.ForRead, false) as DBDictionary;
if (layoutDic != null)
{
foreach (DBDictionaryEntry entry in layoutDic)
{
Layout layout =
tr.GetObject(entry.Value,
OpenMode.ForRead) as Layout;
if (layout != null &&
layout.LayoutName.ToUpper() == layoutName)
{
DBDictionary acPlotSettings =
tr.GetObject(db.PlotSettingsDictionaryId,
OpenMode.ForRead) as DBDictionary;
if (acPlotSettings.Contains(pageSetupName))
{
PlotSettings ps =
(PlotSettings)tr.GetObject(
acPlotSettings.GetAt(pageSetupName), OpenMode.ForRead);
layout.UpgradeOpen();
layout.CopyFrom(ps);
ret = true;
}
}
}
}
tr.Commit();
}
}
}
catch (LispException ex)
{
doc.Editor.WriteMessage("\n; error: LispException: " + ex.Message);
}
catch (System.Exception ex)
{
doc.Editor.WriteMessage("\n; error: System.Exception: " + ex.Message);
}
return ret;
}
}
// Special thanks to Gile, for his work on the following Exception helpers
class LispException : System.Exception
{
public LispException(string msg) : base(msg) { }
}
class TooFewArgsException : LispException
{
public TooFewArgsException() : base("too few arguments") { }
}
class TooManyArgsException : LispException
{
public TooManyArgsException() : base("too many arguments") { }
}
class ArgumentTypeException : LispException
{
public ArgumentTypeException(string s, TypedValue tv)
: base(string.Format(
"invalid argument type: {0}: {1}",
s, tv.TypeCode == (int)LispDataType.Nil ? "nil" : tv.Value))
{ }
}
}