• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Active Contributor
    Posts: 31
    Registered: ‎11-29-2008
    Accepted Solution

    Can't access BlockReference in database

    174 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?

    Please use plain text.
    *Expert Elite*
    Posts: 6,634
    Registered: ‎06-29-2007

    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
    -------------------------------------------------------------------------
    Please use plain text.
    Active Contributor
    Posts: 31
    Registered: ‎11-29-2008

    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

     

    Please use plain text.
    *Expert Elite*
    Posts: 6,634
    Registered: ‎06-29-2007

    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
    -------------------------------------------------------------------------
    Please use plain text.