<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Can't access BlockReference in database in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/can-t-access-blockreference-in-database/m-p/3709748#M52356</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;misstyped the two codelines on the end?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You will get the blocktablerecord by this:&lt;/P&gt;&lt;P&gt;&lt;FONT color="#666699" face="lucida sans unicode,lucida sans"&gt;trans.GetObject(blockId, OpenMode.ForWrite)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and the blockreference with that code:&lt;/P&gt;&lt;P&gt;&lt;FONT color="#666699" face="lucida sans unicode,lucida sans"&gt;trans.GetObject(blockToInsert.ObjectID, OpenMode.ForWrite)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in both cases you have used the variable &amp;lt;blockrefid&amp;gt; which I could not find within your snippet.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;HTH, - alfred -&lt;/P&gt;</description>
    <pubDate>Fri, 23 Nov 2012 12:09:50 GMT</pubDate>
    <dc:creator>Alfred.NESWADBA</dc:creator>
    <dc:date>2012-11-23T12:09:50Z</dc:date>
    <item>
      <title>Can't access BlockReference in database</title>
      <link>https://forums.autodesk.com/t5/net-forum/can-t-access-blockreference-in-database/m-p/3709736#M52355</link>
      <description>&lt;P&gt;Hi. I inserted a block from a dwg drawing with the following method:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;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;

        }&lt;/PRE&gt;&lt;P&gt;After inserting a block, i can access it's BlockTableRecord like this:&lt;/P&gt;&lt;PRE&gt;var block = (BlockTableRecord)trans.GetObject(blockRefId, OpenMode.ForWrite);&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;but i can't access the&amp;nbsp;BlockReference object like this:&lt;/P&gt;&lt;PRE&gt; var blockRef = trans.GetObject(blockRefId, OpenMode.ForWrite) as BlockReference;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;It's a null. What is going on?&lt;/P&gt;</description>
      <pubDate>Fri, 23 Nov 2012 11:56:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/can-t-access-blockreference-in-database/m-p/3709736#M52355</guid>
      <dc:creator>stardust1611</dc:creator>
      <dc:date>2012-11-23T11:56:22Z</dc:date>
    </item>
    <item>
      <title>Re: Can't access BlockReference in database</title>
      <link>https://forums.autodesk.com/t5/net-forum/can-t-access-blockreference-in-database/m-p/3709748#M52356</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;misstyped the two codelines on the end?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You will get the blocktablerecord by this:&lt;/P&gt;&lt;P&gt;&lt;FONT color="#666699" face="lucida sans unicode,lucida sans"&gt;trans.GetObject(blockId, OpenMode.ForWrite)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and the blockreference with that code:&lt;/P&gt;&lt;P&gt;&lt;FONT color="#666699" face="lucida sans unicode,lucida sans"&gt;trans.GetObject(blockToInsert.ObjectID, OpenMode.ForWrite)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in both cases you have used the variable &amp;lt;blockrefid&amp;gt; which I could not find within your snippet.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;HTH, - alfred -&lt;/P&gt;</description>
      <pubDate>Fri, 23 Nov 2012 12:09:50 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/can-t-access-blockreference-in-database/m-p/3709748#M52356</guid>
      <dc:creator>Alfred.NESWADBA</dc:creator>
      <dc:date>2012-11-23T12:09:50Z</dc:date>
    </item>
    <item>
      <title>Re: Can't access BlockReference in database</title>
      <link>https://forums.autodesk.com/t5/net-forum/can-t-access-blockreference-in-database/m-p/3709806#M52357</link>
      <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;blockId = acadDb.Insert(blockName, dbTemp, false);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;, and once like this in the transaction:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;activeSpace.AppendEntity(blockToInsert);&lt;BR /&gt;acadTrans.AddNewlyCreatedDBObject(blockToInsert,true);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This looks like doing one and the same thing twice. Is there something that is not needed there?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Haris&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Nov 2012 13:29:11 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/can-t-access-blockreference-in-database/m-p/3709806#M52357</guid>
      <dc:creator>stardust1611</dc:creator>
      <dc:date>2012-11-23T13:29:11Z</dc:date>
    </item>
    <item>
      <title>Re: Can't access BlockReference in database</title>
      <link>https://forums.autodesk.com/t5/net-forum/can-t-access-blockreference-in-database/m-p/3709902#M52358</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;this line:&lt;/P&gt;&lt;P&gt;&lt;FONT color="#666699" face="lucida sans unicode,lucida sans"&gt;blockId = acadDb.Insert(blockName, dbTemp, false);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;just defines the blockdefintion.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and that line:&lt;/P&gt;&lt;P&gt;&lt;FONT color="#666699" face="lucida sans unicode,lucida sans"&gt;activeSpace.AppendEntity(blockToInsert);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;creates the reference (the insert) in the current space (modelspace or paperspace)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;HTH, - alfred -&lt;/P&gt;</description>
      <pubDate>Fri, 23 Nov 2012 15:02:58 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/can-t-access-blockreference-in-database/m-p/3709902#M52358</guid>
      <dc:creator>Alfred.NESWADBA</dc:creator>
      <dc:date>2012-11-23T15:02:58Z</dc:date>
    </item>
  </channel>
</rss>

