Entity.Explode() missing lines/not exploding fully

Entity.Explode() missing lines/not exploding fully

Anonymous
Not applicable
913 Views
2 Replies
Message 1 of 3

Entity.Explode() missing lines/not exploding fully

Anonymous
Not applicable

Hi,

 

I have a drawing (attached) that contains a block reference. When I explode the drawing (multiple times, to get the most exploded state), and run my code, I'm seeing a total of 28 entities/objects in my blockTableRecord. However, when I don't explode the drawing, load it into my code and run the .NET Explode() function on the only BlockReference in the blockTableRecord, I get a total of 3 lines (using the code below). It's almost like the remaining 25 lines are in a different database, they're being completely ignored by the Explode() function.

 

 

Document doc = Application.DocumentManager.MdiActiveDocument;

            using (Database db = doc.Database)
            {
                using (Transaction trans = db.TransactionManager.StartOpenCloseTransaction())
                {

                    // Open the Block table record Model space for read
                    BlockTableRecord blockTblRec = (BlockTableRecord)trans.GetObject(
                        SymbolUtilityServices.GetBlockModelSpaceId(db),
                        OpenMode.ForRead
                    );

                    HashSet<DBObject> allExploded = new HashSet<DBObject>();

                    //get all objects
                    foreach (ObjectId obj in blockTblRec)
                    {
                        //convert object to entity to check it's layerID
                        Entity unexEnt = (Entity)trans.GetObject(obj, OpenMode.ForRead);

                        //explode the current object if it does not exist in the list of non explodable objects                  
                        if (unexEnt is BlockReference)
                        {
                            using (DBObjectCollection tempExploded = new DBObjectCollection())
                            {
                                //explode the entity
                                unexEnt.Explode(tempExploded);

                                for (int i = 0; i < tempExploded.Count; i++)
                                {
                                    Entity temp = (Entity)tempExploded[i];

                                    //ONLY 3 LINES BEING ADDED INTO THIS
                                    //NO MORE BLOCK REFERENCES ARE BEING PASSED THROUGH
                                    allExploded.Add(temp);

                                }
                            }
                        }
                    }
                }
            }

 

Above is the modified version of my code. In my solution I'm actually running a recursion to ensure any block reference are exploded multiple times. But for some reason the only thing coming through are the 3 lines.

 

 

Has anybody seen this before or know why it's happening?

 

Cheers

 

 

0 Likes
914 Views
2 Replies
Replies (2)
Message 2 of 3

ActivistInvestor
Mentor
Mentor

Try replacing the HashSet<DBObject> with a List<DBObject> and see if that changes anything.

 

 

Message 3 of 3

_gile
Consultant
Consultant

Hi,

 

I think the problem is due to the anonymous block nesting.


Depending on what you have to do, you do not need to explode the block reference, you can simply get its components by recursively traversing the block definition.

 

        [CommandMethod("Test")]
        public void Test()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            using (var tr = db.TransactionManager.StartTransaction())
            {
                var modelSpace = (BlockTableRecord)tr.GetObject(
                    SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite);
                foreach (var str in GetComponents(modelSpace, tr))
                {
                    ed.WriteMessage("\n" + str);
                }
                tr.Commit();
            }
            Application.DisplayTextScreen = true;
        }

        List<string> GetComponents(BlockTableRecord source, Transaction tr, string prefix = "")
        {
            var lst = new List<string>();
            foreach (ObjectId id in source)
            {
                var objClass = id.ObjectClass;
                if (objClass == brClass)
                {
                    var br = (BlockReference)tr.GetObject(id, OpenMode.ForRead);
                    var btr = (BlockTableRecord)tr.GetObject(br.BlockTableRecord, OpenMode.ForRead);
                    lst.AddRange(GetComponents(btr, tr, prefix + br.Name + "|"));
                }
                else
                    lst.Add(prefix + objClass.Name.Substring(4));
            }
            return lst;
        }
        RXClass brClass = RXObject.GetClass(typeof(BlockReference));


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes