.NET

Reply
Active Contributor
stardust1611
Posts: 40
Registered: ‎11-29-2008
Message 1 of 4 (320 Views)
Accepted Solution

Can't access BlockReference in database

320 Views, 3 Replies
11-23-2012 03:56 AM

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?

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 -

*Expert Elite*
Alfred.NESWADBA
Posts: 9,420
Registered: ‎06-29-2007
Message 2 of 4 (317 Views)

Re: Can't access BlockReference in database

11-23-2012 04:09 AM in reply to: stardust1611

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
-------------------------------------------------------------------------
Active Contributor
stardust1611
Posts: 40
Registered: ‎11-29-2008
Message 3 of 4 (308 Views)

Re: Can't access BlockReference in database

11-23-2012 05:29 AM in reply to: Alfred.NESWADBA

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

 

*Expert Elite*
Alfred.NESWADBA
Posts: 9,420
Registered: ‎06-29-2007
Message 4 of 4 (296 Views)

Re: Can't access BlockReference in database

11-23-2012 07:02 AM in reply to: stardust1611

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
-------------------------------------------------------------------------
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!