<?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: Custom implementation of select similar in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/custom-implementation-of-select-similar/m-p/8227819#M24934</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a little example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;        [CommandMethod("SELECTBLOCKBYNAME")]
        public static void SelectBlockByName()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            var peo = new PromptEntityOptions("\nSelect a block: ");
            peo.SetRejectMessage("\nselected object is not a block reference.");
            peo.AddAllowedClass(typeof(BlockReference), true);
            var per = ed.GetEntity(peo);
            if (per.Status == PromptStatus.OK)
            {
                ObjectId[] ids;
                using (var tr = db.TransactionManager.StartOpenCloseTransaction())
                {
                    string GetEffectiveName(BlockReference br) =&amp;gt;
                        br.IsDynamicBlock ?
                        ((BlockTableRecord)tr.GetObject(br.DynamicBlockTableRecord, OpenMode.ForRead)).Name :
                        br.Name;

                    var source = (BlockReference)tr.GetObject(per.ObjectId, OpenMode.ForRead);
                    var name = GetEffectiveName(source);
                    ids = ((BlockTableRecord)tr.GetObject(source.OwnerId, OpenMode.ForRead))
                        .Cast&amp;lt;ObjectId&amp;gt;()
                        .Where(id =&amp;gt; id.ObjectClass.DxfName == "INSERT")
                        .Select(id =&amp;gt; (BlockReference)tr.GetObject(id, OpenMode.ForRead))
                        .Where(br =&amp;gt; GetEffectiveName(br) == name)
                        .Select(br =&amp;gt; br.ObjectId)
                        .ToArray();
                }
                ed.SetImpliedSelection(ids);
            }
        }&lt;/PRE&gt;</description>
    <pubDate>Mon, 27 Aug 2018 18:57:01 GMT</pubDate>
    <dc:creator>_gile</dc:creator>
    <dc:date>2018-08-27T18:57:01Z</dc:date>
    <item>
      <title>Custom implementation of select similar</title>
      <link>https://forums.autodesk.com/t5/net-forum/custom-implementation-of-select-similar/m-p/8227518#M24933</link>
      <description>&lt;P&gt;Can any one guide how to implement select similar functionality using .net interface.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Issue facing: Am trying to get all the entities in drawing by block reference name ..But only one element is coming as result set ..But if right click and use select similar command all the similar block references are getting selected.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am clueless what am i missing in this case?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Aug 2018 17:15:18 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/custom-implementation-of-select-similar/m-p/8227518#M24933</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-08-27T17:15:18Z</dc:date>
    </item>
    <item>
      <title>Re: Custom implementation of select similar</title>
      <link>https://forums.autodesk.com/t5/net-forum/custom-implementation-of-select-similar/m-p/8227819#M24934</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a little example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;        [CommandMethod("SELECTBLOCKBYNAME")]
        public static void SelectBlockByName()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            var peo = new PromptEntityOptions("\nSelect a block: ");
            peo.SetRejectMessage("\nselected object is not a block reference.");
            peo.AddAllowedClass(typeof(BlockReference), true);
            var per = ed.GetEntity(peo);
            if (per.Status == PromptStatus.OK)
            {
                ObjectId[] ids;
                using (var tr = db.TransactionManager.StartOpenCloseTransaction())
                {
                    string GetEffectiveName(BlockReference br) =&amp;gt;
                        br.IsDynamicBlock ?
                        ((BlockTableRecord)tr.GetObject(br.DynamicBlockTableRecord, OpenMode.ForRead)).Name :
                        br.Name;

                    var source = (BlockReference)tr.GetObject(per.ObjectId, OpenMode.ForRead);
                    var name = GetEffectiveName(source);
                    ids = ((BlockTableRecord)tr.GetObject(source.OwnerId, OpenMode.ForRead))
                        .Cast&amp;lt;ObjectId&amp;gt;()
                        .Where(id =&amp;gt; id.ObjectClass.DxfName == "INSERT")
                        .Select(id =&amp;gt; (BlockReference)tr.GetObject(id, OpenMode.ForRead))
                        .Where(br =&amp;gt; GetEffectiveName(br) == name)
                        .Select(br =&amp;gt; br.ObjectId)
                        .ToArray();
                }
                ed.SetImpliedSelection(ids);
            }
        }&lt;/PRE&gt;</description>
      <pubDate>Mon, 27 Aug 2018 18:57:01 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/custom-implementation-of-select-similar/m-p/8227819#M24934</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-08-27T18:57:01Z</dc:date>
    </item>
    <item>
      <title>Re: Custom implementation of select similar</title>
      <link>https://forums.autodesk.com/t5/net-forum/custom-implementation-of-select-similar/m-p/8228806#M24935</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424"&gt;@_gile&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the instant reply. But how to change this particular code&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt; ids = ((BlockTableRecord)tr.GetObject(source.OwnerId, OpenMode.ForRead))
                        .Cast&amp;lt;ObjectId&amp;gt;()
                        .Where(id =&amp;gt; id.ObjectClass.DxfName == "INSERT")
                        .Select(id =&amp;gt; (BlockReference)tr.GetObject(id, OpenMode.ForRead))
                        .Where(br =&amp;gt; GetEffectiveName(br) == name)
                        .Select(br =&amp;gt; br.ObjectId)
                        .ToArray();&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;In&amp;nbsp; the above code snippet&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;((BlockTableRecord)tr.GetObject(source.OwnerId, OpenMode.ForRead))
                        .Cast&amp;lt;ObjectId&amp;gt;()&lt;/PRE&gt;&lt;PRE&gt;How to avoid the above if i have only block name and not prompting &lt;BR /&gt;user to select a type from the user interface&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Previously was using a function like this&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;static private ObjectIdCollection GetAllBlockReferenceByName(string name, Database db)&lt;BR /&gt;{&lt;BR /&gt;ObjectIdCollection ids_temp = null, ids = new ObjectIdCollection();&lt;BR /&gt;Transaction tr = db.TransactionManager.StartTransaction();&lt;BR /&gt;try&lt;BR /&gt;{&lt;BR /&gt;BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);&lt;BR /&gt;BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[name], OpenMode.ForRead);&lt;BR /&gt;BlockTableRecord btr_model = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);&lt;BR /&gt;ObjectId id_model = btr_model.ObjectId;&lt;BR /&gt;ids_temp = btr.GetBlockReferenceIds(true, true);&lt;BR /&gt;foreach (ObjectId id in ids_temp)&lt;BR /&gt;{&lt;BR /&gt;DBObject obj = (DBObject)tr.GetObject(id, OpenMode.ForWrite);&lt;BR /&gt;if (obj.OwnerId == id_model)&lt;BR /&gt;{&lt;BR /&gt;ids.Add(id);&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;tr.Commit();&lt;BR /&gt;}&lt;BR /&gt;finally&lt;BR /&gt;{&lt;BR /&gt;tr.Dispose();&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;return ids;&lt;BR /&gt;}&lt;/P&gt;</description>
      <pubDate>Tue, 28 Aug 2018 05:48:59 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/custom-implementation-of-select-similar/m-p/8228806#M24935</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-08-28T05:48:59Z</dc:date>
    </item>
    <item>
      <title>Re: Custom implementation of select similar</title>
      <link>https://forums.autodesk.com/t5/net-forum/custom-implementation-of-select-similar/m-p/8230646#M24936</link>
      <description>&lt;P&gt;Assuming the block is not a dynamic block, you can try this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;        private static ObjectId[] GetAllBlockReferenceByName(string name, Database db)
        {
            using (var tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                if (!bt.Has(name))
                    return null;
                var msId = SymbolUtilityServices.GetBlockModelSpaceId(db);
                var btr = (BlockTableRecord)tr.GetObject(bt[name], OpenMode.ForRead);
                return btr.GetBlockReferenceIds(true, true)
                    .Cast&amp;lt;ObjectId&amp;gt;()
                    .Where(id =&amp;gt; tr.GetObject(id, OpenMode.ForRead).OwnerId == msId)
                    .ToArray();
            }
        }&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Testing command:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;        [CommandMethod("TEST")]
        public static void Test()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            var pr = ed.GetString("\nBlock Name: ");
            if (pr.Status != PromptStatus.OK)
                return;
            var ids = GetAllBlockReferenceByName(pr.StringResult, db);
            if (ids == null)
                ed.WriteMessage($"\nBloc '{pr.StringResult}' not found.");
            else
                ed.SetImpliedSelection(ids);
        }&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Aug 2018 18:52:35 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/custom-implementation-of-select-similar/m-p/8230646#M24936</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-08-28T18:52:35Z</dc:date>
    </item>
    <item>
      <title>Re: Custom implementation of select similar</title>
      <link>https://forums.autodesk.com/t5/net-forum/custom-implementation-of-select-similar/m-p/8230688#M24937</link>
      <description>&lt;P&gt;This one will also work with dynamic blocks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;        private static ObjectId[] GetAllBlockReferenceByName(string name, Database db)
        {
            using (var tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                if (!bt.Has(name))
                    return null;
                var msId = SymbolUtilityServices.GetBlockModelSpaceId(db);
                var btr = (BlockTableRecord)tr.GetObject(bt[name], OpenMode.ForRead);
                return btr.GetBlockReferenceIds(true, true)
                    .Cast&amp;lt;ObjectId&amp;gt;()
                    .Concat(btr.GetAnonymousBlockIds()
                            .Cast&amp;lt;ObjectId&amp;gt;()
                            .SelectMany(anonId =&amp;gt; ((BlockTableRecord)tr.GetObject(anonId, OpenMode.ForRead))
                                                    .GetBlockReferenceIds(true, true)
                                                    .Cast&amp;lt;ObjectId&amp;gt;()))
                    .Where(id =&amp;gt; tr.GetObject(id, OpenMode.ForRead).OwnerId == msId)
                    .ToArray();
            }
        }&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Aug 2018 19:05:21 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/custom-implementation-of-select-similar/m-p/8230688#M24937</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2018-08-28T19:05:21Z</dc:date>
    </item>
  </channel>
</rss>

