<?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: Iterating through all block references in Model Space in C# to get their nam in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/iterating-through-all-block-references-in-model-space-in-c-to/m-p/8938542#M21707</link>
    <description>&lt;P&gt;Thanks again for the heads-up. I'll keep the memory save in mind in future projects.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 30 Jul 2019 13:35:06 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2019-07-30T13:35:06Z</dc:date>
    <item>
      <title>Iterating through all block references in Model Space in C# to get their names?</title>
      <link>https://forums.autodesk.com/t5/net-forum/iterating-through-all-block-references-in-model-space-in-c-to/m-p/8936898#M21701</link>
      <description>&lt;P&gt;I'm having a hard time with the .NET API. To begin with, I find the object hierarchy documentation cumbersome, even more, I haven't managed to find a proper "map" showing object relationships and dependencies. Is it just me or it's way more complicated than VBA? I've read the Autodesk documentation to no avail.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been lately challenging myself through coding in order to make sure I have a good grasp of object relationships. Next, I'm pasting the code corresponding to a custom command meant to list all block references names in my drawing - just for practising purposes - . It turns out AutoCad crashes and I suspect is because I haven't understood how the Blockreference object relates to the BlockTableRecord, I guess. Aren't block references contained in the Model Space block table? Can anybody point me in the right direction?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;[CommandMethod("ListarBloques")]&lt;BR /&gt;public void ListarBloques()&lt;BR /&gt;{&lt;BR /&gt;Document miDibujo = Application.DocumentManager.MdiActiveDocument;&lt;BR /&gt;Database misElementos = miDibujo.Database;&lt;/P&gt;&lt;P&gt;using(Transaction miTransaccion = misElementos.TransactionManager.StartTransaction())&lt;BR /&gt;{&lt;BR /&gt;BlockTable blckTbl;&lt;BR /&gt;blckTbl = miTransaccion.GetObject(misElementos.BlockTableId, OpenMode.ForRead) as BlockTable;&lt;/P&gt;&lt;P&gt;BlockTableRecord blckTblRcrd;&lt;BR /&gt;blckTblRcrd = miTransaccion.GetObject(blckTbl[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;&lt;/P&gt;&lt;P&gt;foreach (ObjectId id in blckTblRcrd)&lt;BR /&gt;{&lt;BR /&gt;BlockReference blckRef;&lt;BR /&gt;blckRef = miTransaccion.GetObject(id, OpenMode.ForRead) as BlockReference;&lt;BR /&gt;miDibujo.Editor.WriteMessage("" + blckRef.Name);&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;miTransaccion.Commit();&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jul 2019 18:01:03 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/iterating-through-all-block-references-in-model-space-in-c-to/m-p/8936898#M21701</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2019-07-29T18:01:03Z</dc:date>
    </item>
    <item>
      <title>Re: Iterating through all block references in Model Space in C# to get their nam</title>
      <link>https://forums.autodesk.com/t5/net-forum/iterating-through-all-block-references-in-model-space-in-c-to/m-p/8937001#M21702</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can see a simplified example of the AutoCAD .NET object hierarchy &lt;A href="https://help.autodesk.com/view/OARX/2019/ENU/?guid=GUID-7E64FDE7-C818-4566-ADF8-C40D50D91E32" target="_blank" rel="noopener"&gt;&lt;STRONG&gt;here&lt;/STRONG&gt;&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To get all the block references from model space you can start with this:&lt;/P&gt;
&lt;PRE&gt;        [CommandMethod("ListarBloques")]
        public void ListarBloques()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                // open the block table which contains all the BlockTableRecords (block definitions and spaces)
                var blockTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);

                // open the model space BlockTableRecord
                var modelSpace = (BlockTableRecord)tr.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead);

                // iterate through the model space 
                foreach (ObjectId id in modelSpace)
                {
                    // check if the current ObjectId is a block reference one
                    if (id.ObjectClass.DxfName == "INSERT")
                    {
                        // open the block reference
                        var blockReference = (BlockReference)tr.GetObject(id, OpenMode.ForRead);

                        // print the block name to the command line
                        ed.WriteMessage("\n" + blockReference.Name);
                    }
                }

                tr.Commit();
            }
        }&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jul 2019 19:02:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/iterating-through-all-block-references-in-model-space-in-c-to/m-p/8937001#M21702</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2019-07-29T19:02:34Z</dc:date>
    </item>
    <item>
      <title>Re: Iterating through all block references in Model Space in C# to get their nam</title>
      <link>https://forums.autodesk.com/t5/net-forum/iterating-through-all-block-references-in-model-space-in-c-to/m-p/8937841#M21703</link>
      <description>&lt;P&gt;Thanks for your reply, gile, I had already checked your link and the whole manual before opening this post. I feel there should a much more comprehensive map out there. For instance, where are the block references, where are the DBObjects? I think the object hierarchy map for VBA is clearer. That's what I'm after in .NET, but apparently doesn't exist. Does it?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Going back to your code, hmmm it seems pretty similar to mine, the only difference being the DxfName property (does it have anything to do with the Dxf codes that one would use, for example, with selection sets?). Instead of filtering based on that I use the "As" keyword. Could you please take a look at my code and tell me if you see anything wrong to make AutoCad crash?&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Thank you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 08:39:59 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/iterating-through-all-block-references-in-model-space-in-c-to/m-p/8937841#M21703</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2019-07-30T08:39:59Z</dc:date>
    </item>
    <item>
      <title>Re: Iterating through all block references in Model Space in C# to get their nam</title>
      <link>https://forums.autodesk.com/t5/net-forum/iterating-through-all-block-references-in-model-space-in-c-to/m-p/8937967#M21704</link>
      <description>&lt;P&gt;I think the issue of your code is that you are casting everything in ModelSpace to a BlockReference object which is not necessarily true. I modify your code a bit and I found it running without any issues.&lt;/P&gt;&lt;PRE&gt;        [CommandMethod("TestMe")]
        public void ListarBloques()
        {
            Document miDibujo = Application.DocumentManager.MdiActiveDocument;
            Database misElementos = miDibujo.Database;

            using (Transaction miTransaccion = misElementos.TransactionManager.StartTransaction())
            {
                BlockTable blckTbl;
                blckTbl = miTransaccion.GetObject(misElementos.BlockTableId, OpenMode.ForRead) as BlockTable;

                BlockTableRecord blckTblRcrd;
                blckTblRcrd = miTransaccion.GetObject(blckTbl[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;

                foreach (ObjectId id in blckTblRcrd)
                {
                    
                    &lt;FONT color="#FF0000"&gt;var  dbObj = miTransaccion.GetObject(id, OpenMode.ForRead);
                    if(dbObj is BlockReference)
                    {
                        var blckRef = (BlockReference)dbObj;&lt;/FONT&gt;
                        miDibujo.Editor.WriteMessage("" + blckRef.Name);
                    }
                }

                miTransaccion.Commit();
            }
        }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 10:17:48 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/iterating-through-all-block-references-in-model-space-in-c-to/m-p/8937967#M21704</guid>
      <dc:creator>ilovejingle</dc:creator>
      <dc:date>2019-07-30T10:17:48Z</dc:date>
    </item>
    <item>
      <title>Re: Iterating through all block references in Model Space in C# to get their nam</title>
      <link>https://forums.autodesk.com/t5/net-forum/iterating-through-all-block-references-in-model-space-in-c-to/m-p/8938087#M21705</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;@Anonymous&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for your reply, gile, I had already checked your link and the whole manual before opening this post. I feel there should a much more comprehensive map out there. For instance, where are the block references, where are the DBObjects? I think the object hierarchy map for VBA is clearer. That's what I'm after in .NET, but apparently doesn't exist. Does it?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The .NET API is much greater than the COM/ActiveX API (the one use by VBA).&lt;/P&gt;
&lt;P&gt;If you've downloaded the &lt;A href="https://www.autodesk.com/developer-network/platform-technologies/autocad/objectarx-license-download" target="_blank" rel="noopener"&gt;ObjectARX SDK&lt;/A&gt; for the version(s) of AutoCAD you use (as you should have), you'll find the ObjectARX 20XX\classmap\classma.dwg file which shows the class hierachies for ObjectARX C++, Managed ObjectARX (.NET) and ActiveX.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;@Anonymous&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Going back to your code, hmmm it seems pretty similar to mine, the only difference being the DxfName property (does it have anything to do with the Dxf codes that one would use, for example, with selection sets?). Instead of filtering based on that I use the "As" keyword. Could you please take a look at my code and tell me if you see anything wrong to make AutoCad crash? &lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Checking the type from ObjectId.ObjectClass is cheaper than opening&amp;nbsp; the object before checking its type.&lt;/P&gt;
&lt;P&gt;As said by &lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/4381318"&gt;@ilovejingle&lt;/a&gt;, your code craches because you do not check if blockReference is null (i.e. the object was not a block reference).&lt;/P&gt;
&lt;PRE&gt;// open the object as block reference
var blockReference = tr.GetObject(id, OpenMode.ForRead) as BlockReference;
&lt;BR /&gt;// check if the object was a block reference&lt;BR /&gt;if (blockReference != null)&lt;BR /&gt;{
    // print the block name to the command line
    ed.WriteMessage("\n" + blockReference.Name);&lt;BR /&gt;}&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 11:11:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/iterating-through-all-block-references-in-model-space-in-c-to/m-p/8938087#M21705</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2019-07-30T11:11:34Z</dc:date>
    </item>
    <item>
      <title>Re: Iterating through all block references in Model Space in C# to get their nam</title>
      <link>https://forums.autodesk.com/t5/net-forum/iterating-through-all-block-references-in-model-space-in-c-to/m-p/8938530#M21706</link>
      <description>&lt;P&gt;I haven't had the chance to review my code but I've been thinking all the time about the solution and knew I was not using the As keyword properly with the "null" check. I guess that my code was checking for null.Name and that was making it crash.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you or anybody else happens to have a good object hierarchy map, I'd highly appreciate it!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot for your help.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 13:31:54 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/iterating-through-all-block-references-in-model-space-in-c-to/m-p/8938530#M21706</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2019-07-30T13:31:54Z</dc:date>
    </item>
    <item>
      <title>Re: Iterating through all block references in Model Space in C# to get their nam</title>
      <link>https://forums.autodesk.com/t5/net-forum/iterating-through-all-block-references-in-model-space-in-c-to/m-p/8938542#M21707</link>
      <description>&lt;P&gt;Thanks again for the heads-up. I'll keep the memory save in mind in future projects.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 13:35:06 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/iterating-through-all-block-references-in-model-space-in-c-to/m-p/8938542#M21707</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2019-07-30T13:35:06Z</dc:date>
    </item>
    <item>
      <title>Re: Iterating through all block references in Model Space in C# to get their nam</title>
      <link>https://forums.autodesk.com/t5/net-forum/iterating-through-all-block-references-in-model-space-in-c-to/m-p/8938974#M21708</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;@Anonymous&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;If you or anybody else happens to have a good object hierarchy map, I'd highly appreciate it!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;As said upper, you should download the &lt;A href="https://www.autodesk.com/developer-network/platform-technologies/autocad/objectarx-license-download" target="_blank" rel="noopener"&gt;ObjectARX SDK&lt;/A&gt; for the AutoCAD version you use.&lt;/P&gt;
&lt;P&gt;It contains a complete .chm documentation, code samples, AutoCAD libraries to be referenced in VisualStudio and &lt;STRONG&gt;a classmap.dwg with the complete object hierarchy&lt;/STRONG&gt;.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 16:01:44 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/iterating-through-all-block-references-in-model-space-in-c-to/m-p/8938974#M21708</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2019-07-30T16:01:44Z</dc:date>
    </item>
    <item>
      <title>Re: Iterating through all block references in Model Space in C# to get their nam</title>
      <link>https://forums.autodesk.com/t5/net-forum/iterating-through-all-block-references-in-model-space-in-c-to/m-p/12458775#M21709</link>
      <description>&lt;P&gt;Hi, I tried this code, but it does not include the names of the dynamic blocks.&amp;nbsp;&lt;/P&gt;&lt;P&gt;why is that?&lt;/P&gt;</description>
      <pubDate>Fri, 22 Dec 2023 23:24:50 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/iterating-through-all-block-references-in-model-space-in-c-to/m-p/12458775#M21709</guid>
      <dc:creator>0906dokaga</dc:creator>
      <dc:date>2023-12-22T23:24:50Z</dc:date>
    </item>
    <item>
      <title>Re: Iterating through all block references in Model Space in C# to get their nam</title>
      <link>https://forums.autodesk.com/t5/net-forum/iterating-through-all-block-references-in-model-space-in-c-to/m-p/12458793#M21710</link>
      <description>&lt;P&gt;Use the EffectiveName property instead of the Name property. A dynamic block that has changed from the default view will be an anonymous block name, but the EffectiveName will have the name of the original block.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Dec 2023 23:42:39 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/iterating-through-all-block-references-in-model-space-in-c-to/m-p/12458793#M21710</guid>
      <dc:creator>Ed__Jobe</dc:creator>
      <dc:date>2023-12-22T23:42:39Z</dc:date>
    </item>
    <item>
      <title>Re: Iterating through all block references in Model Space in C# to get their nam</title>
      <link>https://forums.autodesk.com/t5/net-forum/iterating-through-all-block-references-in-model-space-in-c-to/m-p/12458987#M21711</link>
      <description>&lt;P&gt;thanks for your reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;first. I could not find the &lt;SPAN&gt;EffectiveName&lt;/SPAN&gt; property.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="0906dokaga_0-1703305428814.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1307532i0FE707221C93C0D9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="0906dokaga_0-1703305428814.png" alt="0906dokaga_0-1703305428814.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;second: The dynamic block will show if it remains to the default, but once change it will not be available in the blckTblRcrd&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="0906dokaga_1-1703304990852.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1307531i9BF6F03CB3140C09/image-size/medium?v=v2&amp;amp;px=400" role="button" title="0906dokaga_1-1703304990852.png" alt="0906dokaga_1-1703304990852.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 23 Dec 2023 04:23:57 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/iterating-through-all-block-references-in-model-space-in-c-to/m-p/12458987#M21711</guid>
      <dc:creator>0906dokaga</dc:creator>
      <dc:date>2023-12-23T04:23:57Z</dc:date>
    </item>
    <item>
      <title>Re: Iterating through all block references in Model Space in C# to get their nam</title>
      <link>https://forums.autodesk.com/t5/net-forum/iterating-through-all-block-references-in-model-space-in-c-to/m-p/12459071#M21712</link>
      <description>&lt;P&gt;EffectivName is not available in the .NET API (it's a COM API property).&lt;/P&gt;
&lt;P&gt;You can get the name of an anonymous dynamic block reference via its dynamic block definition.&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;// iterate through the model space 
foreach (ObjectId id in modelSpace)
{
    if (id.ObjectClass.DxfName == "INSERT")
    {
        // open the block reference
        var blockReference = (BlockReference)tr.GetObject(id, OpenMode.ForRead);
		
	    // get the (dynamic) block definition
	    var dynamicBlockDefinition = (BlockTableRecord)tr.GetObject(blockReference.DynamicBlockTableRecord, OpenMode.ForRead);

        // print the block name to the command line
        ed.WriteMessage("\n" + dynamicBlockDefinition.Name);
    }
}&lt;/LI-CODE&gt;</description>
      <pubDate>Sat, 23 Dec 2023 06:22:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/iterating-through-all-block-references-in-model-space-in-c-to/m-p/12459071#M21712</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2023-12-23T06:22:14Z</dc:date>
    </item>
  </channel>
</rss>

