Use filters to select entities in a drawing C#

Top  Previous  Next

These three functions show you how to get a selection set (ObjectCollection) filtering by object type or layer.

 

       [CommandMethod("EOL")]

      static public void EntitiesOnLayer()

 

       {

 

          Document doc = Application.DocumentManager.MdiActiveDocument;

 

          Editor ed = doc.Editor;

 

          PromptResult pr = ed.GetString("\nEnter type (CIRCLE,LINE...): ");

 

          if (pr.Status == PromptStatus.OK)

 

           {

 

               // Comment out one of the two following lines...  

              // ObjectIdCollection ents = GetEntitiesOnLayer(pr.StringResult);

              ObjectIdCollection ents = GetEntitiesOfType(pr.StringResult);

 

               ed.WriteMessage(

                "\nFound {0} entit{1} on layer {2}",

                 ents.Count,

                 (ents.Count == 1 ? "y" : "ies"),

                 pr.StringResult

               );

           }

 

       }

 

      #endregion

 

      private static ObjectIdCollection GetEntitiesOnLayer(string layerName)

       {

          Document doc = Application.DocumentManager.MdiActiveDocument;

          Editor ed = doc.Editor;

 

          // Build a filter list so that only entities

          // on the specified layer are selected

          TypedValue[] tvs = new TypedValue[1] {

                                  new TypedValue(

                                   (int)DxfCode.LayerName,

                                   layerName)

               };

 

          // Build the filter...

          SelectionFilter sf = new SelectionFilter(tvs);

 

          // Get all entities in the layer...

          PromptSelectionResult psr = ed.SelectAll(sf);

 

          if (psr.Status == PromptStatus.OK)

           {

              // Return a collection of objects which are on the layer

              return new ObjectIdCollection(psr.Value.GetObjectIds());

           }

          else

           {

              // Return an empty list...

              return new ObjectIdCollection();

           }

       }

 

 

      private static ObjectIdCollection GetEntitiesOfType (string sTypeName)

       {

          Document doc = Application.DocumentManager.MdiActiveDocument;

          Editor ed = doc.Editor;

 

          // Build a filter list so that only entities

          // on the specified layer are selected

          TypedValue[] tvs = new TypedValue[1] {

                                  new TypedValue(

                                   (int)DxfCode.Start,

                                   sTypeName)

               };

 

          // Build the filter...

          SelectionFilter sf = new SelectionFilter(tvs);

 

          // Get all entities in the layer...

          PromptSelectionResult psr = ed.SelectAll(sf);

 

          if (psr.Status == PromptStatus.OK)

           {

              // Return a collection of objects which are on the layer

              return new ObjectIdCollection(psr.Value.GetObjectIds());

           }

          else

           {

              // Return an empty list...

              return new ObjectIdCollection();

           }

       }