BRXNET: Clearing a layer
Hello all,
I'm trying to clear a layer in a specific layout (Model, Layout1), using code below. The behaviour is very unstable and unpredictable and I can't find what's going wrong. Is the nested Transaction a problem?
Any comments would be greatly appreciated.
Arno van Eeuwen
[code]
public static bool ClearLayerInLayout(string layoutName, string layerName) { Document doc = Application.DocumentManager.MdiActiveDocument; try { using (Transaction trans = doc.TransactionManager.StartTransaction()) { DBDictionary layoutDictionary = (DBDictionary)trans.GetObject(doc.Database.LayoutDictionaryId, OpenMode.ForRead); if (layoutDictionary.Contains(layoutName)) { ObjectId layoutId = layoutDictionary.GetAt(layoutName); Layout layout = (Layout)trans.GetObject(layoutId, OpenMode.ForRead); BlockTableRecord layoutBlock = (BlockTableRecord)trans.GetObject(layout.BlockTableRecordId, OpenMode.ForWrite); ClearLayerInBlock(layoutBlock, layerName); trans.Commit(); } } return true; } catch (System.Exception ex) { AcAp.Application.ShowAlertDialog(string.Format("\nError: {0}\nStackTrace: {1}", ex.Message, ex.StackTrace)); return false; } }
[/code]
[code]
public static void ClearLayerInBlock(BlockTableRecord block, string layerName) { Document doc = Application.DocumentManager.MdiActiveDocument; try { ObjectIdCollection eraseIds = new ObjectIdCollection(); doc.Editor.WriteMessage(string.Format("Clearing layer : {0} in block : {1}\n", layerName, block.Name)); using (Transaction trans = doc.TransactionManager.StartTransaction()) { foreach (Entity entity in block.Cast<<SPAN style="COLOR: #2b91af">ObjectId>().Select(id => (Entity)trans.GetObject(id, OpenMode.ForRead, true, true)) .Where(entity => entity.Layer == layerName)) { eraseIds.Add(entity.Id); } foreach (ObjectId id in eraseIds) { Entity ent = (Entity)trans.GetObject(id, OpenMode.ForWrite, true, true); ent.Erase(); } trans.Commit(); } } catch (System.Exception ex) { AcAp.Application.ShowAlertDialog(string.Format("\nError: {0}\nStackTrace: {1}", ex.Message, ex.StackTrace)); } }
[/code]
Comments
-
Hi Arno,
Try passing the transaction as a reference argrument I.e. ClearLayerInBlock(layoutBlock, layerName, ref Transaction trans);
if it turns out, you might file a support request.
Dan
0 -
Hi Daniel,
This one start giving me a headache.
I've tried your suggestion but with no success.
I guess it's upto Bricscad to see if there's an explanation.
Just curious: why do you suggest to pass the transaction byref?
I've tried without 'ref' as well but both cases failed.
Thanks for your ever swift reply.
Arno
0