Estilo de ploteado en AutoCAD usando .NET

A veces es necesario asignar un estilo de ploteado a varias layouts a la vez, del tirón. Para eso precisamente sirve esta función, que toma un string con el nombre el estilo a asignar como argumento.

Este post se inspiró en una entrada previa de AutoCAD DevBlog. (Inglés)Todo el crédito para ellos.

Como siempre, siéntete libre de usar, mejorar y compartir.

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