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

Region Explode

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
thannaingoo.api
998 Views, 4 Replies

Region Explode

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();
            }
        }
4 REPLIES 4
Message 2 of 5
_gile
in reply to: thannaingoo.api

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

Message 3 of 5
thannaingoo.api
in reply to: _gile

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

 

Message 4 of 5
_gile
in reply to: thannaingoo.api

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
in reply to: _gile

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

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report