Author Topic: Selection of single entity of certain type  (Read 2523 times)

0 Members and 1 Guest are viewing this topic.

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Selection of single entity of certain type
« on: August 26, 2015, 12:42:31 AM »
Hi All,

pretty much related to this thread but more pointed at what I'm trying to do http://www.theswamp.org/index.php?topic=39799.msg450962#msg450962

I haven't worked with selection sets with filters much in the past and I normally use GetEntity for single objects - no problem there.
Now I need to select a single object of a certain type and GetSelection with options and a filter sounds like it should work.

The way I understand it, if I set the SingleOnly and/or SinglePickInSpace properties to true I should only be able to select one item (and preferably not by selection window).

The filter is also failing as I can select not only multiple entities but I can select any entity type as well.
Code - C#: [Select]
  1.         [CommandMethod("test")]
  2.         public static void testSinglePick()
  3.         {
  4.             PromptSelectionOptions pso = new PromptSelectionOptions();
  5.             pso.MessageForAdding = "Select a single POLYLINE object:\n";
  6.             pso.MessageForRemoval = "Wrong selection, try again.";
  7.             pso.SinglePickInSpace = true;
  8.             pso.SingleOnly = true;
  9.             TypedValue[] filterList = new TypedValue[1]{
  10.                         new TypedValue((int) DxfCode.Start, "*POLYLINE"),
  11.                     };
  12.             SelectionFilter filter = new SelectionFilter(filterList);
  13.             var ed = Bricscad.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  14.             var promptResult = ed.GetSelection(pso, filter);
  15.             ed.WriteMessage("You selected {0} objects.\n", promptResult.Value.Count);
  16.         }
  17.  

Thank you,
Mick
« Last Edit: August 26, 2015, 12:55:52 AM by MickD »
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Selection of single entity of certain type
« Reply #1 on: August 26, 2015, 12:53:00 AM »
Hi Mick,
I am pretty sure I got idea from gile
but I use
Code - C#: [Select]
  1.                 AllowedClassFilter acf = new AllowedClassFilter(typeof(BlockReference));
  2.                 PromptSelectionResult psr = Ed.GetSelection(acf);
  3.  
with

Code - C#: [Select]
  1.     public class AllowedClassFilter
  2.     {
  3.         /// <summary>
  4.         /// The PTRS
  5.         /// </summary>
  6.         private HashSet<IntPtr> ptrs = new HashSet<IntPtr>();
  7.  
  8.         /// <summary>
  9.         /// Gets the allowed class PTRS.
  10.         /// </summary>
  11.         /// <value>
  12.         /// The allowed class PTRS.
  13.         /// </value>
  14.         internal ICollection<IntPtr> AllowedClassPtrs { get { return ptrs; } }
  15.  
  16.         /// <summary>
  17.         /// Initializes a new instance of the <see cref="AllowedClassFilter"/> class.
  18.         /// </summary>
  19.         /// <param name="types">The types.</param>
  20.         public AllowedClassFilter(params Type[] types)
  21.         {
  22.             foreach (Type typ in types)
  23.             {
  24.                 AddAllowedClass(typ);
  25.             }
  26.         }
  27.  
  28.         /// <summary>
  29.         /// Adds the allowed class.
  30.         /// </summary>
  31.         /// <param name="type">The type.</param>
  32.         public void AddAllowedClass(Type type)
  33.         {
  34.             ptrs.Add(RXClass.GetClass(type).UnmanagedObject);
  35.         }
  36.     }
  37.  

And extension methods to call it
Code - C#: [Select]
  1.   public static PromptSelectionResult GetSelection(this Editor ed, PromptSelectionOptions options, AllowedClassFilter classFilter)
  2.         {
  3.             return ed.GetSelection((PromptSelectionOptions)null, classFilter);
  4.         }
  5.  
  6.         /// <summary>
  7.         /// Gets the selection.
  8.         /// </summary>
  9.         /// <param name="ed">The ed.</param>
  10.         /// <param name="filter">The filter.</param>
  11.         /// <param name="classFilter">The class filter.</param>
  12.         /// <returns></returns>
  13.         public static PromptSelectionResult GetSelection(this Editor ed, SelectionFilter filter, AllowedClassFilter classFilter)
  14.         {
  15.             return ed.GetSelection((SelectionFilter)null, classFilter);
  16.         }
  17.  
  18.         /// <summary>
  19.         /// Gets the selection.
  20.         /// </summary>
  21.         /// <param name="ed">The ed.</param>
  22.         /// <param name="classFilter">The class filter.</param>
  23.         /// <returns></returns>
  24.         public static PromptSelectionResult GetSelection(this Editor ed, AllowedClassFilter classFilter)
  25.         {
  26.             return ed.GetSelection((PromptSelectionOptions)null, (SelectionFilter)null, classFilter);
  27.         }
  28.  
  29.         /// <summary>
  30.         /// Gets the selection.
  31.         /// </summary>
  32.         /// <param name="ed">The ed.</param>
  33.         /// <param name="options">The options.</param>
  34.         /// <param name="filter">The filter.</param>
  35.         /// <param name="classFilter">The class filter.</param>
  36.         /// <returns></returns>
  37.         public static PromptSelectionResult GetSelection(this Editor ed, PromptSelectionOptions options, SelectionFilter filter, AllowedClassFilter classFilter)
  38.         {
  39.             AllowedClassPtrs = classFilter.AllowedClassPtrs;
  40.             ed.SelectionAdded += new SelectionAddedEventHandler(ed_ClassFilterSelectionAdded);
  41.  
  42.             try
  43.             {
  44.                 if (options != null)
  45.                 {
  46.                     if (filter != null)
  47.                     {
  48.                         return ed.GetSelection(options, filter);
  49.                     }
  50.                     else
  51.                     {
  52.                         return ed.GetSelection(options);
  53.                     }
  54.                 }
  55.                 else if (filter != null)
  56.                 {
  57.                     return ed.GetSelection(filter);
  58.                 }
  59.                 else
  60.                 {
  61.                     return ed.GetSelection();
  62.                 }
  63.             }
  64.             finally
  65.             {
  66.                 ed.SelectionAdded -= new SelectionAddedEventHandler(ed_ClassFilterSelectionAdded);
  67.             }
  68.         }
  69.  
  70.         /// <summary>
  71.         /// The allowed class PTRS
  72.         /// </summary>
  73.         private static ICollection<IntPtr> AllowedClassPtrs;
  74.  
  75.         /// <summary>
  76.         /// Handles the ClassFilterSelectionAdded event of the ed control.
  77.         /// </summary>
  78.         /// <param name="sender">The source of the event.</param>
  79.         /// <param name="e">The <see cref="SelectionAddedEventArgs"/> instance containing the event data.</param>
  80.         private static void ed_ClassFilterSelectionAdded(object sender, SelectionAddedEventArgs e)
  81.         {
  82.             ObjectId[] ids = e.AddedObjects.GetObjectIds();
  83.             for (int i = 0; i < ids.Length; i++)
  84.             {
  85.                 if (!AllowedClassPtrs.Contains(ids[i].ObjectClass.UnmanagedObject))
  86.                     e.Remove(i);
  87.             }
  88.         }
  89.  


MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Selection of single entity of certain type
« Reply #2 on: August 26, 2015, 12:55:24 AM »
Why is that you always find/remember the solution just after you post a question.....:)

I used GetEntity with PromptEntityOptions like so:

Code - C#: [Select]
  1.         [CommandMethod("test")]
  2.         public static void testSinglePick()
  3.         {
  4.             PromptEntityOptions peo = new PromptEntityOptions("Select a polyline");
  5.             peo.SetRejectMessage("wrong entity type sucker!"); // must set this first!!
  6.             peo.AddAllowedClass(typeof(Polyline), true);
  7.  
  8.             var ed = Bricscad.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  9.             var promptResult = ed.GetEntity(peo);
  10.             ed.WriteMessage("You selected an {0} object.\n", promptResult.ObjectId.ObjectClass.DxfName);
  11.         }
  12.  

Edit:
Thanks Jeff! Will soak that in as it will come in handy, cheers.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Selection of single entity of certain type
« Reply #3 on: August 26, 2015, 04:22:00 PM »
Why is that you always find/remember the solution just after you post a question..... :)


Has never happened to me.   :uglystupid2: :uglystupid2: :uglystupid2:
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013