Author Topic: eErased Exception  (Read 1459 times)

0 Members and 1 Guest are viewing this topic.

MexicanCustard

  • Swamp Rat
  • Posts: 705
eErased Exception
« on: October 17, 2011, 02:54:16 PM »
Please see the note indicating where the eErased Exception is happening. I'd love to know why it's hapenning. Seems like after using the ObjectId to create the BlockReference it gets its Erased property set to true.

Code: [Select]
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead, false);

ObjectId targetId = ObjectId.Null;
if (!bt.Has("LayoutTarget"))
  targetId = CreateLayoutTarget(); // Create BlockTableRecord
else
  targetId = bt["LayoutTarget"];

BlockReference bref = new BlockReference(startpt, targetId);
bref.ScaleFactors = new Scale3d(layoutScale);
bref.Layer = "0";

BlockTableRecord btr = (BlockTableRecord)tr.GetObject(targetId, OpenMode.ForRead); <--- eErased Exception!
foreach (ObjectId id in btr)
{
  //iterate attributes
}

*EDIT the ObjectId Erased property is set to false inside the method CreateLayoutTarget but the returned ObjectId has the property set to true.
« Last Edit: October 17, 2011, 03:13:49 PM by MexicanCustard »
Revit 2019, AMEP 2019 64bit Win 10

Jeff H

  • Needs a day job
  • Posts: 6151
Re: eErased Exception
« Reply #1 on: October 17, 2011, 03:26:01 PM »
Can you create a Block named 'LayoutTarget' then try it and see if you are somehow returning a incorrect ObjectId in CreateLayoutTarget() method?

I can not recreate error it in 2012
 
Code: [Select]
            [CommandMethod("AddBlockReference")]
        public void AddBlockReference()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            using (Transaction trx = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)trx.GetObject(db.BlockTableId, OpenMode.ForRead, false);
                ObjectId targetId = ObjectId.Null;
                if (!bt.Has("LayoutTarget"))
                    targetId = CreateLayoutTarget(); // Create BlockTableRecord
                else
                    targetId = bt["LayoutTarget"];
                BlockReference bref = new BlockReference(Point3d.Origin, targetId);               
                bref.Layer = "0";
                BlockTableRecord modelBtr = (BlockTableRecord)trx.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                ObjectId brefId = modelBtr.AppendEntity(bref);
                trx.AddNewlyCreatedDBObject(bref, true);
 
                BlockTableRecord btr = (BlockTableRecord)trx.GetObject(targetId, OpenMode.ForRead); //<--- eErased Exception!
                foreach (ObjectId id in btr)
                {
                    ed.WriteMessage("\n" + id.ObjectClass.Name);
                }
                trx.Commit();
            }
               
           
        }
        public ObjectId CreateLayoutTarget()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            using (Transaction trx = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)trx.GetObject(db.BlockTableId, OpenMode.ForWrite, false);

                BlockTableRecord newBtr = new BlockTableRecord();
                newBtr.Name = "LayoutTarget";
                ObjectId id = bt.Add(newBtr);
                trx.AddNewlyCreatedDBObject(newBtr, true);
                Circle c = new Circle(Point3d.Origin, Vector3d.ZAxis, 10);
                newBtr.AppendEntity(c);
                trx.AddNewlyCreatedDBObject(c, true);
               
                trx.Commit();
                return id;
            }
        }

 
 
 

 

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: eErased Exception
« Reply #2 on: October 17, 2011, 03:43:50 PM »
Jeff, I am only calling AddNewlyCreatedDBObject() once after adding all my entities instead of after adding each entity. This was working when I had the code all in one method. My problems started when moving the block creation code into its own method. This looks like the only difference between your test code and my actual code.

*EDIT stupid new Autocad programmer mistake! No transaction.Commit() in my called method. Caught it making the changes to AddNewlyCreatedDBObject(). BTW, I was successful at only calling AddNewlyCreatedDBObject once after building the BlockTableRecord. That may not be the wise way to do it but it worked in this instance.

As always, grateful for your help Jeff.
« Last Edit: October 17, 2011, 03:51:29 PM by MexicanCustard »
Revit 2019, AMEP 2019 64bit Win 10