Author Topic: Editing a selection set  (Read 4337 times)

0 Members and 1 Guest are viewing this topic.

TheMaster

  • Guest
Editing a selection set
« on: February 19, 2013, 11:59:03 PM »
Someone posted a question on Autodesk's discussion groups about how to edit a selection set.

I had this laying around, and added a few extension methods to make it simpler to use:

Code - C#: [Select]
  1. /// ssedit.cs  (c) 2012 Tony Tanzillo
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using Autodesk.AutoCAD.Runtime;
  7. using Autodesk.AutoCAD.DatabaseServices;
  8. using Autodesk.AutoCAD.ApplicationServices;
  9.  
  10. namespace Autodesk.AutoCAD.EditorInput.MyExtensions
  11. {
  12.    public static class EditorExtensionMethods
  13.    {
  14.       /// Edit existing SelectionSet
  15.       public static PromptSelectionResult EditSelection( this Editor ed,
  16.           SelectionSet ss,
  17.           PromptSelectionOptions options = null )
  18.       {
  19.          return new SelectionSetEditor( ed, ss, options ).EditSelection();
  20.       }
  21.  
  22.       /// Edit a sequence or array of ObjectId[]
  23.       public static PromptSelectionResult EditSelection( this Editor ed,
  24.          IEnumerable<ObjectId> ids,
  25.          PromptSelectionOptions options = null )
  26.       {
  27.          return new SelectionSetEditor( ed, ids, options ).EditSelection();
  28.       }
  29.  
  30.    }
  31.  
  32.    public class SelectionSetEditor
  33.    {
  34.       Editor ed = null;
  35.       PromptSelectionOptions options = null;
  36.       IEnumerable<ObjectId> selection = null;
  37.  
  38.       public SelectionSetEditor( Editor editor, SelectionSet ss,
  39.          PromptSelectionOptions options = null )
  40.       {
  41.          if( editor == null )
  42.             throw new ArgumentNullException( "editor" );
  43.          if( ss == null )
  44.             throw new ArgumentNullException( "ss" );
  45.          this.ed = editor;
  46.          this.options = options ?? new PromptSelectionOptions();
  47.          this.selection = ss.GetObjectIds();
  48.       }
  49.  
  50.       public SelectionSetEditor( Editor editor, IEnumerable<ObjectId> ss,
  51.           PromptSelectionOptions options = null )
  52.       {
  53.          if( editor == null )
  54.             throw new ArgumentNullException( "editor" );
  55.          if( ss == null )
  56.             throw new ArgumentNullException( "ss" );
  57.          this.ed = editor;
  58.          this.options = options ?? new PromptSelectionOptions();
  59.          this.selection = ss;
  60.       }
  61.  
  62.       public PromptSelectionResult EditSelection()
  63.       {
  64.          if( this.selection.Any() )
  65.          {
  66.             ed.SelectionAdded += selectionAdded;
  67.             /// A kludge that triggers the firing of the SelectionAdded event:
  68.             ed.Document.SendStringToExecute( "_L\n", true, false, false );
  69.          }
  70.          return ed.GetSelection( options );
  71.       }
  72.  
  73.       /// Removes the "Last" object added by the SendStringToExecute()
  74.       /// kludge, and then adds the selection set to be edited:
  75.       void selectionAdded( object sender, SelectionAddedEventArgs e )
  76.       {
  77.          ed.SelectionAdded -= selectionAdded;
  78.          if( e.AddedObjects.Count > 0 )
  79.             e.Remove( 0 );
  80.          foreach( ObjectId id in this.selection )
  81.             e.Add( new SelectedObject( id, SelectionMethod.NonGraphical, IntPtr.Zero ) );
  82.       }
  83.    }
  84.  
  85.    public static class SSEditExampleCommands
  86.    {
  87.       [CommandMethod( "SSEDIT" )]
  88.       public static void SSEdit()
  89.       {
  90.          try
  91.          {
  92.             Document doc = Application.DocumentManager.MdiActiveDocument;
  93.  
  94.             /// Get an initial selection set that we
  95.             /// will then allow the user to edit:
  96.  
  97.             Editor ed = doc.Editor;
  98.             var psr = ed.GetSelection();
  99.             if( psr.Status != PromptStatus.OK )
  100.                return;
  101.  
  102.             /// Allow the user to edit the selection set
  103.             /// acquired above:
  104.            
  105.             PromptSelectionResult result = ed.EditSelection( psr.Value );
  106.  
  107.             if( result != null && result.Value != null )
  108.                doc.Editor.WriteMessage(
  109.                   "\nEdited selection Count = {0}",
  110.                   result.Value.Count );
  111.          }
  112.          catch( System.Exception ex )
  113.          {
  114.             throw ex;
  115.          }
  116.       }
  117.    }
  118. }
  119.  
  120.  
« Last Edit: February 20, 2013, 12:17:53 AM by TT »