Dear All,
I got an error when I explode the region.
Please help me correct below code.
Thanks,
Naing Oo
public static void ExplodeRegion(Database db, Editor edt) { using (Transaction acTrans = db.TransactionManager.StartTransaction()) { BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; SelectionSet acSSet = edt.SelectAll().Value; if (acSSet == null) return; ObjectId[] oids = acSSet.GetObjectIds(); foreach (ObjectId id in oids) { Region reg = (DBObject)acTrans.GetObject(id, OpenMode.ForWrite) as Region; if (reg != null) { DBObjectCollection acDBObjColl = new DBObjectCollection(); reg.Explode(acDBObjColl); acBlkTblRec.UpgradeOpen(); foreach (DBObject dbObj in acDBObjColl) { Entity acEnt = dbObj as Entity; acBlkTblRec.AppendEntity(acEnt); acTrans.AddNewlyCreatedDBObject(dbObj, true); } } } acTrans.Commit(); } }
Solved! Go to Solution.
Solved by _gile. Go to Solution.
Hi,
You should provide more details about the error you get and perhaps a dwg example.
I do not see anything wrong in your code,except if some regions are on locked layers.
It just could be more efficient by checking the ObjectId type before opening it, see the code below.
private static void ExplodeRegion(Database db, Editor ed) { try { var regionClass = RXObject.GetClass(typeof(Region)); using (var tr = db.TransactionManager.StartTransaction()) { var btr = (BlockTableRecord)tr.GetObject( SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite); foreach (ObjectId id in btr) { if (id.ObjectClass == regionClass) { var region = (Region)tr.GetObject(id, OpenMode.ForWrite, false, true); var ents = new DBObjectCollection(); region.Explode(ents); foreach (Entity ent in ents) { btr.AppendEntity(ent); tr.AddNewlyCreatedDBObject(ent, true); } region.Erase(); } } tr.Commit(); } } catch (System.Exception ex) { ed.WriteMessage($"\n{ex.Message}\n{ex.StackTrace}"); } }
Hi,
Thanks for your reply and code reference.
I got still an error. Kindly check below link which is my dwg and code.
Thanks,
Naing Oo
https://drive.google.com/open?id=1Wp04frJHtg9MbfanESFk6c4ARJOxOzh9
Your drawing contains a region which Area and Perimeter is equal to 0. This entity generates an exception with the Explode() method.
Try like this:
private static void ExplodeRegion(Database db, Editor ed) { try { var regionClass = RXObject.GetClass(typeof(Region)); using (var tr = db.TransactionManager.StartTransaction()) { var btr = (BlockTableRecord)tr.GetObject( SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite); foreach (ObjectId id in btr) { if (id.ObjectClass == regionClass) { var region = (Region)tr.GetObject(id, OpenMode.ForWrite, false, true); if (region.Area > 0.0) { var ents = new DBObjectCollection(); region.Explode(ents); foreach (Entity ent in ents) { btr.AppendEntity(ent); tr.AddNewlyCreatedDBObject(ent, true); } } region.Erase(); } } tr.Commit(); } } catch (System.Exception ex) { ed.WriteMessage($"\n{ex.Message}\n{ex.StackTrace}"); } }
Can't find what you're looking for? Ask the community or share your knowledge.