<?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: Programmatically determine BlockReference.BlockName of AttributeCollection in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/programmatically-determine-blockreference-blockname-of/m-p/4987718#M44285</link>
    <description>&lt;P&gt;It looks like you need to understanding a bit more about dynamic block.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I believe what you are after is BlockReference.Name, not BlockReference.BlockName. BlockName property refers to blockreference's owner (a blocktablerecord, either ModelSpace, or one of the PaperSpace corresponding to a layout).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For regular blockreference, its Name property is the same name as the name of a BlockTableRecord (block definition), from which it is referenced to. However, if the block definition is a dynamic block, the BlockReference's name may not be the same as its block definition, depending on how the dynamic properties are set. It name is very likely be a anonymous block name, something like *Uxxx". You need to use Blockreference.DynamicBlockTableRecord to find its dynamic blcok definition and then its name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To find effective name (as iin VBA) of a blockreference, you do:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;//tran is an open Transaction&lt;/P&gt;
&lt;P&gt;//modelSpaceBlockTableRecord is the BlockTableRecord for ModelSpace&lt;/P&gt;
&lt;P&gt;foreach (ObjectId id in modelSpaceBlockTableRecord)&lt;/P&gt;
&lt;P&gt;{&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; string blockName;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; BlockReference bref=tran.GetObject(id, OpenMode.ForRead) as BlockReference;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (bref!=null)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (bref.IsDynamicBlock)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BlockTableRecord br=(BlockTableRecord)tran.GetObject(bref.DynamicBlockTableRecord,OpenMode.ForRead);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; blockName=br.Name;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; blockName=bref.Name;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Debug.WriteLine("Found block reference: " +&amp;nbsp;blockName);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;</description>
    <pubDate>Thu, 24 Apr 2014 19:28:22 GMT</pubDate>
    <dc:creator>norman.yuan</dc:creator>
    <dc:date>2014-04-24T19:28:22Z</dc:date>
    <item>
      <title>Programmatically determine BlockReference.BlockName of AttributeCollection</title>
      <link>https://forums.autodesk.com/t5/net-forum/programmatically-determine-blockreference-blockname-of/m-p/4983260#M44284</link>
      <description>&lt;P&gt;Here is a quick background for what I am trying to do:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Essentially, I need to loop through and update a set of attributes which match the following set of requisites:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; 1. The AttributeCollection must contain a provided Tag name&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; 2. The AttributeCollection must be a property on a BlockReference which matches a specific Block name&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; 3. The AttributeCollection must be a property on a BlockReference which is a DynamicBlock&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I believe I have issues 1 and 3 solved, but 2 is throwing me off. While looping through the BlockReferences, the BlockReference which contains the AttributeCollection does not have a Name which matches what is displayed in the Enhanced Attribute Editor. For example, the .NET code will report that the BlockReference.Name is something similar to *U16, and BlockReference.BlockName is *Model_Space, when the Enhanced Attribute Editor states that "Block: My_Expected_Block_Name". What I really need to do is tie the BlockReference back to "My_Expected_Block_Name".&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Am I looking in the wrong spot for matching an AttributeCollection to a specific Block Name, or using the wrong properties? Any direction I can get would be helpful. I don't know my way around AutoCAD very well, as I am just stepping in to assist another user with some .NET programming.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If more information is necessary, let me know - I will post the code I am using to loop below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;// Assume that "db" is a Transaction which has been opened by a StartTransaction() call&lt;/P&gt;&lt;P&gt;// Assume that "AutoCADHelper.CurrentAcadDb" is DocumentManager.MdiActiveDocument.Database&lt;/P&gt;&lt;P&gt;var blockTable = (BlockTable)db.GetObject(AutoCADHelper.CurrentAcadDb.BlockTableId, OpenMode.ForRead);&lt;BR /&gt;foreach (ObjectId blockTableId in blockTable) {&lt;BR /&gt;&amp;nbsp; &amp;nbsp; var blockTableRecord = db.GetObject(blockTableId, OpenMode.ForRead) as BlockTableRecord;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; if (blockTableRecord != null) {&lt;BR /&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;foreach (ObjectId blockTableRecordId in blockTableRecord) {&lt;BR /&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;var blockReference = db.GetObject(blockTableRecordId, OpenMode.ForWrite) as BlockReference;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;if (blockReference != null &amp;amp;&amp;amp; blockReference.IsDynamicBlock) {&lt;BR /&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;Debug.WriteLine(" blockReference.Name: " + blockReference.Name); // I would assume one of these would be the expected name&lt;BR /&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;Debug.WriteLine(" blockReference.BlockName: " + blockReference.BlockName); // but I am clearly wrong. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; }&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; }&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Apr 2014 15:17:06 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/programmatically-determine-blockreference-blockname-of/m-p/4983260#M44284</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2014-04-23T15:17:06Z</dc:date>
    </item>
    <item>
      <title>Re: Programmatically determine BlockReference.BlockName of AttributeCollection</title>
      <link>https://forums.autodesk.com/t5/net-forum/programmatically-determine-blockreference-blockname-of/m-p/4987718#M44285</link>
      <description>&lt;P&gt;It looks like you need to understanding a bit more about dynamic block.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I believe what you are after is BlockReference.Name, not BlockReference.BlockName. BlockName property refers to blockreference's owner (a blocktablerecord, either ModelSpace, or one of the PaperSpace corresponding to a layout).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For regular blockreference, its Name property is the same name as the name of a BlockTableRecord (block definition), from which it is referenced to. However, if the block definition is a dynamic block, the BlockReference's name may not be the same as its block definition, depending on how the dynamic properties are set. It name is very likely be a anonymous block name, something like *Uxxx". You need to use Blockreference.DynamicBlockTableRecord to find its dynamic blcok definition and then its name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To find effective name (as iin VBA) of a blockreference, you do:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;//tran is an open Transaction&lt;/P&gt;
&lt;P&gt;//modelSpaceBlockTableRecord is the BlockTableRecord for ModelSpace&lt;/P&gt;
&lt;P&gt;foreach (ObjectId id in modelSpaceBlockTableRecord)&lt;/P&gt;
&lt;P&gt;{&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; string blockName;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; BlockReference bref=tran.GetObject(id, OpenMode.ForRead) as BlockReference;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (bref!=null)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (bref.IsDynamicBlock)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BlockTableRecord br=(BlockTableRecord)tran.GetObject(bref.DynamicBlockTableRecord,OpenMode.ForRead);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; blockName=br.Name;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; blockName=bref.Name;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Debug.WriteLine("Found block reference: " +&amp;nbsp;blockName);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;</description>
      <pubDate>Thu, 24 Apr 2014 19:28:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/programmatically-determine-blockreference-blockname-of/m-p/4987718#M44285</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2014-04-24T19:28:22Z</dc:date>
    </item>
    <item>
      <title>Re: Programmatically determine BlockReference.BlockName of AttributeCollection</title>
      <link>https://forums.autodesk.com/t5/net-forum/programmatically-determine-blockreference-blockname-of/m-p/4987730#M44286</link>
      <description>&lt;P&gt;I certainly do need more understanding. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; So t&lt;SPAN&gt;hank you! That's very helpful to me.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Apr 2014 19:34:32 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/programmatically-determine-blockreference-blockname-of/m-p/4987730#M44286</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2014-04-24T19:34:32Z</dc:date>
    </item>
  </channel>
</rss>

