.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to set LayerTableRecord PlotStyleName?

2 REPLIES 2
Reply
Message 1 of 3
583408432
292 Views, 2 Replies

How to set LayerTableRecord PlotStyleName?

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

2 REPLIES 2
Message 2 of 3
_gile
in reply to: 583408432

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


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 3
583408432
in reply to: _gile

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.

Post to forums  

Forma Design Contest


Autodesk Design & Make Report