Getting Fatal Error in Explode method

Getting Fatal Error in Explode method

Anonymous
Not applicable
1,783 Views
12 Replies
Message 1 of 13

Getting Fatal Error in Explode method

Anonymous
Not applicable

Hello Gentlemen,

 

I am getting Fatal error at the explode method in the following highlighed (red) code.

Can you please help me?

 

public class ExplodeDynamicBlocks
    {
        [CommandMethod("EDB")]
        public static void ExplodeDymanicBlockReference()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord btr = tr.GetObject(bt["JDuct-SP"], OpenMode.ForWrite) as BlockTableRecord;

                ObjectIdCollection oidc1 = btr.GetAnonymousBlockIds();
                ObjectIdCollection oidc2 = btr.GetBlockReferenceIds(true, false);

                DBObjectCollection dboc = new DBObjectCollection();               

                foreach (ObjectId id in oidc1)
                {
                    BlockReference br = tr.GetObject(id, OpenMode.ForWrite) as BlockReference;
                    br.ExplodeToOwnerSpace();   // I am getting Fatal Error at this point                
                }
            }
        }
    }

 

Also, if possible, how to combine two objectidcollection into one?

0 Likes
Accepted solutions (1)
1,784 Views
12 Replies
Replies (12)
Message 2 of 13

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

Two questions:

  • have you verified what the owner for the blockreference is (in case of crashing)?
  • have you tried to do br.Explode instead of br.ExplodeToOwnerspace ... if that also crashes?
  •  

    - alfred -

    BTW: you have not commited the transaction.

    ------------------------------------------------------------------------------------
    Alfred NESWADBA
    ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
    www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
    ------------------------------------------------------------------------------------

    (not an Autodesk consultant)
    0 Likes
    Message 3 of 13

    Anonymous
    Not applicable
    Hi Alfred,
    Thanks for your response.
    For your second question,
    I first tried with the explode only, by creating new dbobject collection object

    DBObjectCollection dboc = new DBObjectCollection();
    BlockTableRecord curSpace = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

    foreach (ObjectId id in oidc1)
    {
    BlockReference br = tr.GetObject(id, OpenMode.ForWrite) as BlockReference;
    br.Explode(dboc); // same problem here
    foreach (Entity e in dboc)
    {
    curSpace.AppendEntity(e);
    tr.AddNewlyCreatedDBObject(e, true);
    }
    }
    I got the same fatal error. After that only I changed to ExplodeToOwnerspace.

    For your first question,
    Sorry. I don't know how to verify owner?
    0 Likes
    Message 4 of 13

    Alfred.NESWADBA
    Consultant
    Consultant

    Hi,

     

    >> I don't know how to verify owner?

    Every object has a property .OwnerID that gives you the ObjectID to the owner object.

     

    As there are some differences in the usage of dynamic blocks and the resulting anonymous blocks can you upload a sample drawing that shows the problem when running your code?

     

    - alfred -

    ------------------------------------------------------------------------------------
    Alfred NESWADBA
    ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
    www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
    ------------------------------------------------------------------------------------

    (not an Autodesk consultant)
    0 Likes
    Message 5 of 13

    Anonymous
    Not applicable

     

    Here I found the problem when I debugged.

    In my code, there are two objectid collection.

     

    1. ObjectIdCollection oidc1 = btr.GetAnonymousBlockIds();

    2. ObjectIdCollection oidc2 = btr.GetBlockReferenceIds(true, false);

     

    when I tried with the second object "oidc2" it works fine.

    foreach (ObjectId id in oidc2)
                    {
                        BlockReference br = tr.GetObject(id, OpenMode.ForWrite) as BlockReference;
                        br.ExplodeToOwnerSpace();
                    }

     

    The problem is with the first object "oidc1".

    foreach (ObjectId id in oidc1)
                    {
                        BlockReference br = tr.GetObject(id, OpenMode.ForWrite) as BlockReference;
                        br.ExplodeToOwnerSpace(); // Problem
                    }

     In the above code,  the "br" object is null. That is why the autocad is crashing when trying to explode null object.

     

    Can you help me now how to explode annonumous blocks?

    BlockReference br = tr.GetObject(id, OpenMode.ForWrite) as BlockReference;

    This is code, we have to change.

     

     

    (Thanks for your immediate response)

    0 Likes
    Message 6 of 13

    Alfred.NESWADBA
    Consultant
    Consultant

    Hi,

     

    >> In the above code,  the "br" object is null

    And when you check the following two properties (before accessing the blockreference):

       id.isValid
       id.isErased

    how are they set?

     

    - alfred -

    ------------------------------------------------------------------------------------
    Alfred NESWADBA
    ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
    www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
    ------------------------------------------------------------------------------------

    (not an Autodesk consultant)
    0 Likes
    Message 7 of 13

    Anonymous
    Not applicable

    id.isValid = true;
    id.isErased = false;

    0 Likes
    Message 8 of 13

    Alfred.NESWADBA
    Consultant
    Consultant

    Hi,

     

    can you upload a drawing that shows that problem?

    And let us know what type of AutoCAD (vertical product?), what release and what servicepack you have.

     

    - alfred -

    ------------------------------------------------------------------------------------
    Alfred NESWADBA
    ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
    www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
    ------------------------------------------------------------------------------------

    (not an Autodesk consultant)
    0 Likes
    Message 9 of 13

    Anonymous
    Not applicable

    Here is the attached drawing.

    0 Likes
    Message 10 of 13

    Anonymous
    Not applicable
    Sorry. I didn't see the second question.
    My Autocad Version is Autocad 2011 MEP.
    I don't know about service pack.
    0 Likes
    Message 11 of 13

    Alfred.NESWADBA
    Consultant
    Consultant
    Accepted solution

    Hi,

     

    well, got it to run (and learned something 😉 😞

     

    When you look to that object:

       tr.GetObject(id, OpenMode.ForWrite)

    AutoCAD returns a BlockTableRecord, not a BlockReference!

     

    So I tried then to get the Blockreferences for that BlockTableRecord and exploded them ... and that worked:

       For Each id As ObjectId In oidc1
          Dim btr2 As BlockTableRecord = TryCast(tr.GetObject(id, OpenMode.ForWrite), BlockTableRecord)
          For Each id2 As ObjectId In btr2.GetBlockReferenceIds(True, False)
             Dim br2 As BlockReference = TryCast(tr.GetObject(id2, OpenMode.ForWrite), BlockReference)
             br2.ExplodeToOwnerSpace()
          Next
       Next
    (no cast checking, no error handling, no disposes, just shows the workflow!)

     

    HTH, - alfred -

    ------------------------------------------------------------------------------------
    Alfred NESWADBA
    ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
    www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
    ------------------------------------------------------------------------------------

    (not an Autodesk consultant)
    Message 12 of 13

    Anonymous
    Not applicable
    oh.....Great Alfred. Fantastic.
    It works fine.

    Thanks a lot. Thank you for spending your time and gave response.
    0 Likes
    Message 13 of 13

    Alfred.NESWADBA
    Consultant
    Consultant

    glad I could help, you are welcome, - alfred -

    ------------------------------------------------------------------------------------
    Alfred NESWADBA
    ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
    www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
    ------------------------------------------------------------------------------------

    (not an Autodesk consultant)
    0 Likes