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

Getting Fatal Error in Explode method

12 REPLIES 12
SOLVED
Reply
Message 1 of 13
Anonymous
1482 Views, 12 Replies

Getting Fatal Error in Explode method

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?

12 REPLIES 12
Message 2 of 13
Alfred.NESWADBA
in reply to: Anonymous

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 2024
    ------------------------------------------------------------------------------------

    (not an Autodesk consultant)
    Message 3 of 13
    Anonymous
    in reply to: Alfred.NESWADBA

    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?
    Message 4 of 13
    Alfred.NESWADBA
    in reply to: Anonymous

    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 2024
    ------------------------------------------------------------------------------------

    (not an Autodesk consultant)
    Message 5 of 13
    Anonymous
    in reply to: Alfred.NESWADBA

     

    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)

    Message 6 of 13
    Alfred.NESWADBA
    in reply to: Anonymous

    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 2024
    ------------------------------------------------------------------------------------

    (not an Autodesk consultant)
    Message 7 of 13
    Anonymous
    in reply to: Alfred.NESWADBA

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

    Message 8 of 13
    Alfred.NESWADBA
    in reply to: Anonymous

    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 2024
    ------------------------------------------------------------------------------------

    (not an Autodesk consultant)
    Message 9 of 13
    Anonymous
    in reply to: Alfred.NESWADBA

    Here is the attached drawing.

    Message 10 of 13
    Anonymous
    in reply to: Alfred.NESWADBA

    Sorry. I didn't see the second question.
    My Autocad Version is Autocad 2011 MEP.
    I don't know about service pack.
    Message 11 of 13
    Alfred.NESWADBA
    in reply to: Anonymous

    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 2024
    ------------------------------------------------------------------------------------

    (not an Autodesk consultant)
    Message 12 of 13
    Anonymous
    in reply to: Alfred.NESWADBA

    oh.....Great Alfred. Fantastic.
    It works fine.

    Thanks a lot. Thank you for spending your time and gave response.
    Message 13 of 13
    Alfred.NESWADBA
    in reply to: Anonymous

    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 2024
    ------------------------------------------------------------------------------------

    (not an Autodesk consultant)

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

    Post to forums  

    AutoCAD Inside the Factory


    Autodesk Design & Make Report