.NET

Reply
Active Contributor
aliensinearth
Posts: 31
Registered: ‎09-02-2013
Message 1 of 13 (297 Views)
Accepted Solution

Getting Fatal Error in Explode method

297 Views, 12 Replies
11-24-2013 10:48 PM

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?

Hi,

 

well, got it to run (and learned something :smileywink: ):

 

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 -

*Expert Elite*
Alfred.NESWADBA
Posts: 9,207
Registered: ‎06-29-2007
Message 2 of 13 (289 Views)

Re: Getting Fatal Error in Explode method

11-24-2013 11:58 PM in reply to: aliensinearth

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
    Ingenieur Studio HOLLAUS ... www.hollaus.at
    -------------------------------------------------------------------------
    Active Contributor
    aliensinearth
    Posts: 31
    Registered: ‎09-02-2013
    Message 3 of 13 (285 Views)

    Re: Getting Fatal Error in Explode method

    11-25-2013 12:07 AM 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?
    *Expert Elite*
    Alfred.NESWADBA
    Posts: 9,207
    Registered: ‎06-29-2007
    Message 4 of 13 (282 Views)

    Re: Getting Fatal Error in Explode method

    11-25-2013 12:14 AM in reply to: aliensinearth

    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
    Ingenieur Studio HOLLAUS ... www.hollaus.at
    -------------------------------------------------------------------------
    Active Contributor
    aliensinearth
    Posts: 31
    Registered: ‎09-02-2013
    Message 5 of 13 (277 Views)

    Re: Getting Fatal Error in Explode method

    11-25-2013 12:24 AM 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)

    *Expert Elite*
    Alfred.NESWADBA
    Posts: 9,207
    Registered: ‎06-29-2007
    Message 6 of 13 (274 Views)

    Re: Getting Fatal Error in Explode method

    11-25-2013 12:29 AM in reply to: aliensinearth

    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
    Ingenieur Studio HOLLAUS ... www.hollaus.at
    -------------------------------------------------------------------------
    Active Contributor
    aliensinearth
    Posts: 31
    Registered: ‎09-02-2013
    Message 7 of 13 (271 Views)

    Re: Getting Fatal Error in Explode method

    11-25-2013 12:33 AM in reply to: Alfred.NESWADBA

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

    *Expert Elite*
    Alfred.NESWADBA
    Posts: 9,207
    Registered: ‎06-29-2007
    Message 8 of 13 (268 Views)

    Re: Getting Fatal Error in Explode method

    11-25-2013 12:36 AM in reply to: aliensinearth

    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
    Ingenieur Studio HOLLAUS ... www.hollaus.at
    -------------------------------------------------------------------------
    Active Contributor
    aliensinearth
    Posts: 31
    Registered: ‎09-02-2013
    Message 9 of 13 (265 Views)

    Re: Getting Fatal Error in Explode method

    11-25-2013 12:48 AM in reply to: Alfred.NESWADBA

    Here is the attached drawing.

    Active Contributor
    aliensinearth
    Posts: 31
    Registered: ‎09-02-2013
    Message 10 of 13 (254 Views)

    Re: Getting Fatal Error in Explode method

    11-25-2013 01:03 AM 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.
    Post to the Community

    Have questions about Autodesk products? Ask the community.

    New Post
    Need installation help?

    Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.