Assign a PlotStyle to all the Layouts in an AutoCAD Drawing Using .NET
Sometimes you might want to assign a certain PlotStyle to all the layouts in an drawing. That is what this function is for. It takes a string as argument and if the PlotStyle name is found in the current dictionary, will assign it to every layout.
This post was inspired by another from the AutoCAD DevBlog.
As always, feel free to use, improve and share.
public void SetPlotSheet(string plotStyleToSet)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
DBDictionary lays = tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
foreach (DBDictionaryEntry item in lays)
{
ObjectId layoutId = item.Value;
Layout layout = tr.GetObject(layoutId, OpenMode.ForWrite) as Layout;
PlotSettingsValidator plotSetVal = PlotSettingsValidator.Current;
plotSetVal.RefreshLists(layout);
System.Collections.Specialized.StringCollection sheetList = plotSetVal.GetPlotStyleSheetList();
System.Object test = Application.GetSystemVariable("PSTYLEMODE");
if (test.ToString().Equals("0") && (sheetList.Contains(plotStyleToSet)))
{
plotSetVal.SetCurrentStyleSheet(layout, plotStyleToSet);
ed.WriteMessage("nThe plot style sheet is being set to {0}nn", plotStyleToSet);
} else{
ed.WriteMessage("nUnable to set plot style in this file. Either the drawing is in CTB mode or the required Style does not exist in this context.nn");
}
}
tr.Commit();
}
}