<?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: Identifying specific block references in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/identifying-specific-block-references/m-p/11774872#M9845</link>
    <description>&lt;P&gt;The whole purpose of Handle is to be the ID of an object in drawing database, and ObjectId&amp;nbsp; is the same thing when drawing is loaded into AutoCAD. That is, when drawing is save as file, object's Handle persists and when drawing is loaded into AutoCAD, AutoCAD runtime will create an ObjectId, based on each Handle.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, if you know the Handle, you can get the object's ObjectId, and then the object itself (BlockRefernece in your case):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;var blkHandle=new Handle(long.Parse(handleString, NumberStyle.HexNumber));&lt;/P&gt;
&lt;P&gt;if (db.TryGetObjectId(blkHandle, out ObjectId blkId))&lt;/P&gt;
&lt;P&gt;{&lt;/P&gt;
&lt;P&gt;&amp;nbsp; var blockReference=(BlockReference)tran.GetObject(blkId, OpenMode.ForRead);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; // Do work with the BlockReference&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;</description>
    <pubDate>Thu, 23 Feb 2023 04:10:49 GMT</pubDate>
    <dc:creator>norman.yuan</dc:creator>
    <dc:date>2023-02-23T04:10:49Z</dc:date>
    <item>
      <title>Identifying specific block references</title>
      <link>https://forums.autodesk.com/t5/net-forum/identifying-specific-block-references/m-p/11768790#M9842</link>
      <description>&lt;P&gt;We are creating an application where the drafters have created templates with blocks and we read data from a few sources and populate the block attributes with the correct data. To do this I have a separate windows exe app that gets all of the data and writes it to a JSON file then the AutoCAD command reads the JSON file and builds the drawing as required. To date, we have had single references to unique block definitions in each drawing. So, I've been able to search for the block def, get all references (only one), and then select the first item from the collection (of length 1).&lt;BR /&gt;&lt;BR /&gt;But, what happens when I have more than one block reference to the same definition? How do I know it's the one I want? For instance, let's say I know that x, y, z should be in the block in the upper left and that a, b, and c data should be in the block in the lower right. I used relative coordinates in the example, but just as an example, I'd like something that can robustly determine the "correct" block for the data I have grabbed.&lt;BR /&gt;&lt;BR /&gt;I've dug into this a bit and the best I can come up with is to have the drafters run the LIST command on a block and get me the HANDLE, which I could use to identify that block, but I'm unfamiliar with handles in AutoCAD and if they change as the drafters make changes to the drawings, or if they change each time I open a drawing.&lt;BR /&gt;&lt;BR /&gt;Looking for advice here.&lt;BR /&gt;&lt;BR /&gt;Below is some of the code that I am using, which I have received some help with already. These functions are inside of a class that has the database and transaction as instance variables. As you can see, I go and get all block references, but I also assume that I only get 1 back. If I get more than one I have no idea how to deal with it.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;public void ProcessBlock(AcadBlockData block)
{
    BlockReference br = GetAcadBlockReference(block.Name);
    if (br != null)
    {
        if(block.Name == "VALVE_BODY")
        {
            SetDynamicPropertyValue(br, "Visibility1", block.Attributes["VALVE_TYPE"]);
        }
        ProcessBlockRefAttributes(br, block.Attributes);
    }
}

private BlockReference GetAcadBlockReference(string blockName)
{
    ObjectIdCollection ids = GetAllBlockReferences(blockName);
    return ids.Count &amp;gt; 0 ? (BlockReference)tr.GetObject(ids[0], OpenMode.ForWrite) : null;
}

private ObjectIdCollection GetAllBlockReferences(string blockName)
{
    var blockReferenceIds = new ObjectIdCollection();
    var bt = GetBlocktable();
    if (bt.Has(blockName))
    {
        var btr = (BlockTableRecord)tr.GetObject(bt[blockName], OpenMode.ForRead);
        foreach (ObjectId id in btr.GetBlockReferenceIds(true, false))
        {
            blockReferenceIds.Add(id);
        }
        foreach (ObjectId anonymousBlockId in btr.GetAnonymousBlockIds())
        {
            var anonymousBtr = (BlockTableRecord)tr.GetObject(anonymousBlockId, OpenMode.ForRead);
            foreach (ObjectId id in anonymousBtr.GetBlockReferenceIds(true, false))
            {
                blockReferenceIds.Add(id);
            }
        }
    }
    return blockReferenceIds;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Feb 2023 03:16:32 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/identifying-specific-block-references/m-p/11768790#M9842</guid>
      <dc:creator>mdoddKFBCF</dc:creator>
      <dc:date>2023-02-21T03:16:32Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying specific block references</title>
      <link>https://forums.autodesk.com/t5/net-forum/identifying-specific-block-references/m-p/11769039#M9843</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using Handle is the reliable way to keep object identifiers outside of a DWG file.&lt;/P&gt;
&lt;P&gt;A Handle is persitent and unique per drawing.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Feb 2023 07:02:54 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/identifying-specific-block-references/m-p/11769039#M9843</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2023-02-21T07:02:54Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying specific block references</title>
      <link>https://forums.autodesk.com/t5/net-forum/identifying-specific-block-references/m-p/11774815#M9844</link>
      <description>&lt;P&gt;Ok, that's unfortunate, but at least it gives us a path forward.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It begs another question though. If I know the handle is there a way for me to query the database directly for that block reference?&lt;/P&gt;</description>
      <pubDate>Thu, 23 Feb 2023 03:26:30 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/identifying-specific-block-references/m-p/11774815#M9844</guid>
      <dc:creator>mdoddKFBCF</dc:creator>
      <dc:date>2023-02-23T03:26:30Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying specific block references</title>
      <link>https://forums.autodesk.com/t5/net-forum/identifying-specific-block-references/m-p/11774872#M9845</link>
      <description>&lt;P&gt;The whole purpose of Handle is to be the ID of an object in drawing database, and ObjectId&amp;nbsp; is the same thing when drawing is loaded into AutoCAD. That is, when drawing is save as file, object's Handle persists and when drawing is loaded into AutoCAD, AutoCAD runtime will create an ObjectId, based on each Handle.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, if you know the Handle, you can get the object's ObjectId, and then the object itself (BlockRefernece in your case):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;var blkHandle=new Handle(long.Parse(handleString, NumberStyle.HexNumber));&lt;/P&gt;
&lt;P&gt;if (db.TryGetObjectId(blkHandle, out ObjectId blkId))&lt;/P&gt;
&lt;P&gt;{&lt;/P&gt;
&lt;P&gt;&amp;nbsp; var blockReference=(BlockReference)tran.GetObject(blkId, OpenMode.ForRead);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; // Do work with the BlockReference&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;</description>
      <pubDate>Thu, 23 Feb 2023 04:10:49 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/identifying-specific-block-references/m-p/11774872#M9845</guid>
      <dc:creator>norman.yuan</dc:creator>
      <dc:date>2023-02-23T04:10:49Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying specific block references</title>
      <link>https://forums.autodesk.com/t5/net-forum/identifying-specific-block-references/m-p/11774882#M9846</link>
      <description>&lt;P&gt;Perhaps have a look at :&lt;/P&gt;&lt;P&gt;&lt;A href="https://www.keanw.com/2007/02/getting_access_.html" target="_blank" rel="noopener"&gt;https://www.keanw.com/2007/02/getting_access_.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Feb 2023 04:21:54 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/identifying-specific-block-references/m-p/11774882#M9846</guid>
      <dc:creator>kerry_w_brown</dc:creator>
      <dc:date>2023-02-23T04:21:54Z</dc:date>
    </item>
  </channel>
</rss>

