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

Can't access BlockReference in database

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
stardust1611
583 Views, 3 Replies

Can't access BlockReference in database

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?

Tags (1)
3 REPLIES 3
Message 2 of 4

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
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 3 of 4

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

 

Message 4 of 4

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
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost