Region Explode

Region Explode

thannaingoo.api
Advocate Advocate
1,755 Views
4 Replies
Message 1 of 5

Region Explode

thannaingoo.api
Advocate
Advocate

Dear All,

I got an error when I explode the region. 

Please help me correct below code.

Thanks,

Naing Oo

 

ExplodeRegion.JPG

 

 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();
            }
        }
0 Likes
Accepted solutions (1)
1,756 Views
4 Replies
Replies (4)
Message 2 of 5

_gile
Consultant
Consultant

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


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 5

thannaingoo.api
Advocate
Advocate

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

 

0 Likes
Message 4 of 5

_gile
Consultant
Consultant
Accepted solution

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


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 5

thannaingoo.api
Advocate
Advocate

Dear @_gile,

 

Thanks for your solution.

It is working for me.

I didn't know that we have Area and Perimeter is equal to 0 regions.

 

Regards,

Naing Oo

 

 

0 Likes