<?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: Iterate through each instance of a block type in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/iterate-through-each-instance-of-a-block-type/m-p/6990442#M32019</link>
    <description>The Entity.BlockName property is the name of the block that contains the entity, not the name of the block that is referenced by the Entity/BlockReference.&lt;BR /&gt;&lt;BR /&gt;You also should not be casting each ObjectId to Entity, you should be casting them to BlockReference, because that's what they all are.</description>
    <pubDate>Mon, 03 Apr 2017 01:26:40 GMT</pubDate>
    <dc:creator>ActivistInvestor</dc:creator>
    <dc:date>2017-04-03T01:26:40Z</dc:date>
    <item>
      <title>Iterate through each instance of a block type</title>
      <link>https://forums.autodesk.com/t5/net-forum/iterate-through-each-instance-of-a-block-type/m-p/6989356#M32015</link>
      <description>&lt;P&gt;Hi All&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to get information from each inserted block in a drawing... Specificaly, the geometry extents of that instance. For example, I have inserted three of the same blocks into a drawing:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2017-04-01_19h42_32.png" style="width: 705px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/340180i27D7101DF75E6338/image-size/large?v=v2&amp;amp;px=999" role="button" title="2017-04-01_19h42_32.png" alt="2017-04-01_19h42_32.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How would i go about getting the information from each of these? I have tried several different ways, but I just can't seem to extract this information. I hace managed to just get the whole block defenition from the drawing (just shows one instance).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this makes sense!!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be greatly appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Apr 2017 18:53:25 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/iterate-through-each-instance-of-a-block-type/m-p/6989356#M32015</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-04-01T18:53:25Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate through each instance of a block type</title>
      <link>https://forums.autodesk.com/t5/net-forum/iterate-through-each-instance-of-a-block-type/m-p/6989412#M32016</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To get all inserted references, have a look at the BlockTableRecord.GetBlockReferenceIds() method.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;        public ObjectIdCollection GetAllReferenceIds(string blockName, Database db)
        {
            var ids = new ObjectIdCollection();
            using (var tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                if (bt.Has(blockName))
                {
                    var btr = (BlockTableRecord)tr.GetObject(bt[blockName], OpenMode.ForRead);
                    foreach (ObjectId brId in btr.GetBlockReferenceIds(true, false))
                    {
                        ids.Add(brId);
                    }
                    if (btr.IsDynamicBlock)
                    {
                        foreach (ObjectId btrId in btr.GetAnonymousBlockIds())
                        {
                            var anonymousBtr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
                            foreach (ObjectId brId in anonymousBtr.GetBlockReferenceIds(true, false))
                            {
                                ids.Add(brId);
                            }
                        }
                    }
                }
            }
            return ids;
        }&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Apr 2017 20:00:37 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/iterate-through-each-instance-of-a-block-type/m-p/6989412#M32016</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2017-04-01T20:00:37Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate through each instance of a block type</title>
      <link>https://forums.autodesk.com/t5/net-forum/iterate-through-each-instance-of-a-block-type/m-p/6990005#M32017</link>
      <description>&lt;P&gt;Thank you for your help. I have just implemented this and am getting some results... I am iterating through the blocks and trying to get th valuses like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;            string blockName = "SAMPLE";
            ObjectIdCollection ids = GetAllReferenceIds(blockName, db);
            Matrix3d ucs = ed.CurrentUserCoordinateSystem;
            //get current UCS matrix
            try
            {
                using (DocumentLock docloc = doc.LockDocument())
                {
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        
                        doc.TransactionManager.EnableGraphicsFlush(true);

                        BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                                              
                         if (!bt.Has(blockName))
                            {
                                Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Block " + blockName + " does not exist.");
                                return;
                         }

                        var btr = (BlockTableRecord)tr.GetObject(bt[blockName], OpenMode.ForWrite);
                        foreach (ObjectId brId in btr.GetBlockReferenceIds(true, false))
                        {
                            ids.Add(brId);
                        }
                        if (btr.IsDynamicBlock)
                        {
                            foreach (ObjectId btrId in btr.GetAnonymousBlockIds())
                            {
                                var anonymousBr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
                                foreach (ObjectId brId in anonymousBr.GetBlockReferenceIds(true, false))
                                {
                                    ids.Add(brId);
                                }
                            }
                        }

                            foreach (ObjectId id in ids)
                            {
                                Entity e = (Entity)tr.GetObject(id, OpenMode.ForWrite);
                                    System.Windows.Forms.MessageBox.Show(e.GeometricExtents.MinPoint.X.ToString() + e.BlockName + e.GeometricExtents.MinPoint.Y.ToString());
                             }      
                                &lt;/PRE&gt;&lt;P&gt;However, the messagebox shows the blockname as MODEL_SPACE? I also get a 'max readers' error if i try to run it again &lt;span class="lia-unicode-emoji" title=":confused_face:"&gt;😕&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Apr 2017 14:38:13 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/iterate-through-each-instance-of-a-block-type/m-p/6990005#M32017</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-04-02T14:38:13Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate through each instance of a block type</title>
      <link>https://forums.autodesk.com/t5/net-forum/iterate-through-each-instance-of-a-block-type/m-p/6990051#M32018</link>
      <description>&lt;P&gt;looks like the XY positions are correct... thats good enough for me &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; I'll work out how to fix the errors now!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot&lt;/P&gt;</description>
      <pubDate>Sun, 02 Apr 2017 15:28:27 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/iterate-through-each-instance-of-a-block-type/m-p/6990051#M32018</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-04-02T15:28:27Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate through each instance of a block type</title>
      <link>https://forums.autodesk.com/t5/net-forum/iterate-through-each-instance-of-a-block-type/m-p/6990442#M32019</link>
      <description>The Entity.BlockName property is the name of the block that contains the entity, not the name of the block that is referenced by the Entity/BlockReference.&lt;BR /&gt;&lt;BR /&gt;You also should not be casting each ObjectId to Entity, you should be casting them to BlockReference, because that's what they all are.</description>
      <pubDate>Mon, 03 Apr 2017 01:26:40 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/iterate-through-each-instance-of-a-block-type/m-p/6990442#M32019</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2017-04-03T01:26:40Z</dc:date>
    </item>
  </channel>
</rss>

