// The current page setup plot style is "monochrome.stb", and it contains "Style 1".
internal static void SetLayersStyle()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
LayerTable acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable;
foreach (ObjectId acObjId in acLyrTbl)
{
LayerTableRecord acLyrTblRec = acTrans.GetObject(acObjId, OpenMode.ForWrite) as LayerTableRecord;
//acLyrTblRec.PlotStyleName = "Normal"; // OK
// The error still occurred: "eKeyNotFound".
acLyrTblRec.PlotStyleName = "Style 1";
}
acTrans.Commit();
}
}
Hi,
A good practice is to always check if the collection (symbol table or dictionary) contains the the item before using it.
internal static void SetLayersStyle()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
using (var tr = db.TransactionManager.StartTransaction())
{
var plotStyleDictionary = (DBDictionary)tr.GetObject(db.PlotStyleNameDictionaryId, OpenMode.ForRead);
if (plotStyleDictionary.Contains("Style 1"))
{
var layerTable = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
foreach (ObjectId id in layerTable)
{
var layer = (LayerTableRecord)tr.GetObject(id, OpenMode.ForWrite);
layer.PlotStyleName = "Style 1";
}
}
else
{
Application.ShowAlertDialog("'Normal' plot style not found.");
}
tr.Commit();
}
}
I traversed the dbdictionary and found that its print style did not belong to the current pagesetup. How to change the print style associated with the current database?
internal static void GetDbStyles()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
using (var tr = db.TransactionManager.StartTransaction())
{
var plotStyleDictionary = (DBDictionary)tr.GetObject(db.PlotStyleNameDictionaryId, OpenMode.ForRead);
string result = "";
foreach (var ps in plotStyleDictionary)
{
result += ps.Key + "\r\n";
}
Application.ShowAlertDialog(result);
tr.Commit();
}
}
Can't find what you're looking for? Ask the community or share your knowledge.