Can't access BlockReference in database

Can't access BlockReference in database

stardust1611
Enthusiast Enthusiast
997 Views
3 Replies
Message 1 of 4

Can't access BlockReference in database

stardust1611
Enthusiast
Enthusiast

Hi. I inserted a block from a dwg drawing with the following method:

 

public static ObjectId InsertBlock(string fileName, Point3d insPoint)
        {
            var acadDoc = Application.DocumentManager.MdiActiveDocument;
            var blockId = default(ObjectId);

            using (var acadLockDoc = acadDoc.LockDocument())
            {
                var dbTemp = new Database(false, true);
                dbTemp.ReadDwgFile(fileName, FileShare.Read, false, "");
                var blockName = Path.GetFileNameWithoutExtension(fileName);
                SymbolUtilityServices.ValidateSymbolName(blockName, false);

                var acadDb = acadDoc.Database;
                blockId = acadDb.Insert(blockName, dbTemp, false);
                var blockToInsert = new BlockReference(insPoint, blockId);

                using (Transaction acadTrans = acadDoc.TransactionManager.StartTransaction())
                {
                    var blockTable = (BlockTable)acadTrans.GetObject(acadDoc.Database.BlockTableId, OpenMode.ForRead);
                    var activeSpace = (BlockTableRecord)acadTrans.GetObject(_db.CurrentSpaceId, OpenMode.ForWrite);
                    activeSpace.AppendEntity(blockToInsert);
                    acadTrans.AddNewlyCreatedDBObject(blockToInsert,true);

                    var block = (BlockTableRecord)acadTrans.GetObject(blockId, OpenMode.ForWrite);
                    
                    foreach (ObjectId elementId in block)
                    {
                        var element = acadTrans.GetObject(elementId, OpenMode.ForRead);

                        if (element.GetType().Name.Equals("AttributeDefinition"))
                        {
                            var attDef = (AttributeDefinition)acadTrans.GetObject(elementId, OpenMode.ForRead);
                            var attRef = new AttributeReference();
                            attRef.SetAttributeFromBlock(attDef, blockToInsert.BlockTransform);
                            blockToInsert.AttributeCollection.AppendAttribute(attRef);
                            acadTrans.AddNewlyCreatedDBObject(attRef, true);
                        }
                    }

                    acadTrans.Commit();
                }

            }

            Application.UpdateScreen();

            return blockId;

        }

After inserting a block, i can access it's BlockTableRecord like this:

var block = (BlockTableRecord)trans.GetObject(blockRefId, OpenMode.ForWrite);

 but i can't access the BlockReference object like this:

 var blockRef = trans.GetObject(blockRefId, OpenMode.ForWrite) as BlockReference;

 It's a null. What is going on?

0 Likes
Accepted solutions (1)
998 Views
3 Replies
Replies (3)
Message 2 of 4

Alfred.NESWADBA
Consultant
Consultant
Accepted solution

Hi,

 

misstyped the two codelines on the end?

 

You will get the blocktablerecord by this:

trans.GetObject(blockId, OpenMode.ForWrite)

 

and the blockreference with that code:

trans.GetObject(blockToInsert.ObjectID, OpenMode.ForWrite)

 

in both cases you have used the variable <blockrefid> which I could not find within your snippet.

 

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 3 of 4

stardust1611
Enthusiast
Enthusiast

Thank you Alfred, this worked. You couldn't find blockrefid variable because I'm calling the above method from another method where I have this variable, which holds the ObjectId of the inserted block.

But I'm not clear about one thing in the above method. I actually found this method on the internet. Why do I have to insert the block twice? Once like this, before starting transaction:

 

blockId = acadDb.Insert(blockName, dbTemp, false);

 

, and once like this in the transaction:

 

activeSpace.AppendEntity(blockToInsert);
acadTrans.AddNewlyCreatedDBObject(blockToInsert,true);

 

This looks like doing one and the same thing twice. Is there something that is not needed there?

 

Haris

 

0 Likes
Message 4 of 4

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

this line:

blockId = acadDb.Insert(blockName, dbTemp, false);

just defines the blockdefintion.

 

and that line:

activeSpace.AppendEntity(blockToInsert);

creates the reference (the insert) in the current space (modelspace or paperspace)

 

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)
0 Likes