/// ssedit.cs (c) 2012 Tony Tanzillo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
namespace Autodesk.AutoCAD.EditorInput.MyExtensions
{
public static class EditorExtensionMethods
{
/// Edit existing SelectionSet
public static PromptSelectionResult EditSelection( this Editor ed,
SelectionSet ss,
PromptSelectionOptions options = null )
{
return new SelectionSetEditor
( ed, ss, options
).EditSelection(); }
/// Edit a sequence or array of ObjectId[]
public static PromptSelectionResult EditSelection( this Editor ed,
IEnumerable<ObjectId> ids,
PromptSelectionOptions options = null )
{
return new SelectionSetEditor
( ed, ids, options
).EditSelection(); }
}
public class SelectionSetEditor
{
Editor ed = null;
PromptSelectionOptions options = null;
IEnumerable<ObjectId> selection = null;
public SelectionSetEditor( Editor editor, SelectionSet ss,
PromptSelectionOptions options = null )
{
if( editor == null )
throw new ArgumentNullException
( "editor" ); if( ss == null )
throw new ArgumentNullException
( "ss" ); this.ed = editor;
this.options = options
?? new PromptSelectionOptions
(); this.selection = ss.GetObjectIds();
}
public SelectionSetEditor( Editor editor, IEnumerable<ObjectId> ss,
PromptSelectionOptions options = null )
{
if( editor == null )
throw new ArgumentNullException
( "editor" ); if( ss == null )
throw new ArgumentNullException
( "ss" ); this.ed = editor;
this.options = options
?? new PromptSelectionOptions
(); this.selection = ss;
}
public PromptSelectionResult EditSelection()
{
if( this.selection.Any() )
{
ed.SelectionAdded += selectionAdded;
/// A kludge that triggers the firing of the SelectionAdded event:
ed.Document.SendStringToExecute( "_L\n", true, false, false );
}
return ed.GetSelection( options );
}
/// Removes the "Last" object added by the SendStringToExecute()
/// kludge, and then adds the selection set to be edited:
void selectionAdded( object sender, SelectionAddedEventArgs e )
{
ed.SelectionAdded -= selectionAdded;
if( e.AddedObjects.Count > 0 )
e.Remove( 0 );
foreach( ObjectId id in this.selection )
e
.Add( new SelectedObject
( id, SelectionMethod
.NonGraphical, IntPtr
.Zero ) ); }
}
public static class SSEditExampleCommands
{
[CommandMethod( "SSEDIT" )]
public static void SSEdit()
{
try
{
Document doc = Application.DocumentManager.MdiActiveDocument;
/// Get an initial selection set that we
/// will then allow the user to edit:
Editor ed = doc.Editor;
var psr = ed.GetSelection();
if( psr.Status != PromptStatus.OK )
return;
/// Allow the user to edit the selection set
/// acquired above:
PromptSelectionResult result = ed.EditSelection( psr.Value );
if( result != null && result.Value != null )
doc.Editor.WriteMessage(
"\nEdited selection Count = {0}",
result.Value.Count );
}
catch( System.Exception ex )
{
throw ex;
}
}
}
}