<?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: Find all blocks on a certain layer in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/find-all-blocks-on-a-certain-layer/m-p/8207330#M24977</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your question is not clear. Do you want to get all blocks (more accurately block references) on a layer or all entities (including bloc references, lines, circles, etc.) ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To get all model space entities on a specified layer:&lt;/P&gt;
&lt;P&gt;Iteratiing the model space&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;        static ObjectIdCollection GetByLayer(string layerName)
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ids = new ObjectIdCollection();
            using (var tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                var ms = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
                foreach (ObjectId id in ms)
                {
                    var ent = (Entity)tr.GetObject(id, OpenMode.ForRead);
                    if (ent.Layer.Equals(layerName, StringComparison.CurrentCultureIgnoreCase))
                        ids.Add(id);
                }
                tr.Commit();
            }
            return ids;
        }&lt;/PRE&gt;
&lt;P&gt;Using a selection filter&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;        static ObjectIdCollection SelectByLayer (string layerName)
        {
            var ed = Application.DocumentManager.MdiActiveDocument.Editor;
            var filter = new SelectionFilter(new[] 
            {
                new TypedValue(8, layerName),   // layer
                new TypedValue(410, "Model")   // model space
            });
            var psr = ed.SelectAll(filter);
            if (psr.Status == PromptStatus.OK)
                return new ObjectIdCollection(psr.Value.GetObjectIds());
            else
                return new ObjectIdCollection();
        }&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To get all model space block references on a specified layer:&lt;/P&gt;
&lt;P&gt;Iteratiing the model space&lt;/P&gt;
&lt;PRE&gt;        static ObjectIdCollection GetBlocksByLayer(string layerName)
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ids = new ObjectIdCollection();
            using (var tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                var ms = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
                foreach (ObjectId id in ms)
                {
                    if (id.ObjectClass.DxfName == "INSERT")
                    {
                        var ent = (Entity)tr.GetObject(id, OpenMode.ForRead);
                        if (ent.Layer.Equals(layerName, StringComparison.CurrentCultureIgnoreCase))
                            ids.Add(id);
                    }
                }
                tr.Commit();
            }
            return ids;
        }&lt;/PRE&gt;
&lt;P&gt;Using a selection filter&lt;/P&gt;
&lt;PRE&gt;        static ObjectIdCollection SelectBlocksByLayer(string layerName)
        {
            var ed = Application.DocumentManager.MdiActiveDocument.Editor;
            var filter = new SelectionFilter(new[]
            {
                new TypedValue(0, "INSERT"),    // block reference
                new TypedValue(8, layerName),   // layer
                new TypedValue(410, "Model")    // model space
            });
            var psr = ed.SelectAll(filter);
            if (psr.Status == PromptStatus.OK)
                return new ObjectIdCollection(psr.Value.GetObjectIds());
            else
                return new ObjectIdCollection();
        }&lt;/PRE&gt;</description>
    <pubDate>Fri, 17 Aug 2018 17:04:02 GMT</pubDate>
    <dc:creator>_gile</dc:creator>
    <dc:date>2018-08-17T17:04:02Z</dc:date>
    <item>
      <title>Find all blocks on a certain layer</title>
      <link>https://forums.autodesk.com/t5/net-forum/find-all-blocks-on-a-certain-layer/m-p/8207149#M24976</link>
      <description>&lt;P&gt;I want to find all the blocks, lines, circles etc. on a layer. I have found two possible way of doing this but I am having problems with&amp;nbsp; them both. My first method was from &lt;A href="http://spiderinnet1.typepad.com/blog/2012/04/various-ways-to-check-object-types-in-autocad-net.html" target="_blank"&gt;here&lt;/A&gt; where I used something like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;        public static void LayerInfoGrab(string LayerName, int LayerType)
        {
            //link to the current document and databases
            Document Doc = Application.DocumentManager.MdiActiveDocument;
            Database db = Doc.Database;
            Editor ed = Doc.Editor;

            using (Transaction Trans = db.TransactionManager.StartTransaction())
            {
                //Open the block table open for read
                BlockTable Btr = Trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                //Open the model space database for read
                BlockTableRecord BtrModel = Trans.GetObject(Btr[BlockTableRecord.ModelSpace],OpenMode.ForRead) as BlockTableRecord;

                //Step through the objects and find any on the layer we want
                foreach (object SPAobject in BtrModel)
                {
                    //Test the object for block type
                    if (SPAobject is Line)
                    {

                    }

                    if (SPAobject is Circle)&lt;/PRE&gt;&lt;P&gt;But (SPAobject is Block) does not work. My second method is the &lt;A href="https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2015/ENU/AutoCAD-NET/files/GUID-125398A5-184C-4114-9212-A2FF28FC1F1D-htm.html" target="_blank"&gt;selection filter&lt;/A&gt; but I don't have a name for the block, but I do want all the blocks on the layer. I didn't know if I could use something like "*" instead of the block name?&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="2018-08-18.png" style="width: 400px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/535926i1F112A30AFF34B10/image-size/medium?v=v2&amp;amp;px=400" role="button" title="2018-08-18.png" alt="2018-08-18.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>Fri, 17 Aug 2018 16:04:57 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/find-all-blocks-on-a-certain-layer/m-p/8207149#M24976</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-08-17T16:04:57Z</dc:date>
    </item>
    <item>
      <title>Re: Find all blocks on a certain layer</title>
      <link>https://forums.autodesk.com/t5/net-forum/find-all-blocks-on-a-certain-layer/m-p/8207330#M24977</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your question is not clear. Do you want to get all blocks (more accurately block references) on a layer or all entities (including bloc references, lines, circles, etc.) ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To get all model space entities on a specified layer:&lt;/P&gt;
&lt;P&gt;Iteratiing the model space&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;        static ObjectIdCollection GetByLayer(string layerName)
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ids = new ObjectIdCollection();
            using (var tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                var ms = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
                foreach (ObjectId id in ms)
                {
                    var ent = (Entity)tr.GetObject(id, OpenMode.ForRead);
                    if (ent.Layer.Equals(layerName, StringComparison.CurrentCultureIgnoreCase))
                        ids.Add(id);
                }
                tr.Commit();
            }
            return ids;
        }&lt;/PRE&gt;
&lt;P&gt;Using a selection filter&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;        static ObjectIdCollection SelectByLayer (string layerName)
        {
            var ed = Application.DocumentManager.MdiActiveDocument.Editor;
            var filter = new SelectionFilter(new[] 
            {
                new TypedValue(8, layerName),   // layer
                new TypedValue(410, "Model")   // model space
            });
            var psr = ed.SelectAll(filter);
            if (psr.Status == PromptStatus.OK)
                return new ObjectIdCollection(psr.Value.GetObjectIds());
            else
                return new ObjectIdCollection();
        }&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To get all model space block references on a specified layer:&lt;/P&gt;
&lt;P&gt;Iteratiing the model space&lt;/P&gt;
&lt;PRE&gt;        static ObjectIdCollection GetBlocksByLayer(string layerName)
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ids = new ObjectIdCollection();
            using (var tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                var ms = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
                foreach (ObjectId id in ms)
                {
                    if (id.ObjectClass.DxfName == "INSERT")
                    {
                        var ent = (Entity)tr.GetObject(id, OpenMode.ForRead);
                        if (ent.Layer.Equals(layerName, StringComparison.CurrentCultureIgnoreCase))
                            ids.Add(id);
                    }
                }
                tr.Commit();
            }
            return ids;
        }&lt;/PRE&gt;
&lt;P&gt;Using a selection filter&lt;/P&gt;
&lt;PRE&gt;        static ObjectIdCollection SelectBlocksByLayer(string layerName)
        {
            var ed = Application.DocumentManager.MdiActiveDocument.Editor;
            var filter = new SelectionFilter(new[]
            {
                new TypedValue(0, "INSERT"),    // block reference
                new TypedValue(8, layerName),   // layer
                new TypedValue(410, "Model")    // model space
            });
            var psr = ed.SelectAll(filter);
            if (psr.Status == PromptStatus.OK)
                return new ObjectIdCollection(psr.Value.GetObjectIds());
            else
                return new ObjectIdCollection();
        }&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Aug 2018 17:04:02 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/find-all-blocks-on-a-certain-layer/m-p/8207330#M24977</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-08-17T17:04:02Z</dc:date>
    </item>
  </channel>
</rss>

