Author Topic: AU 2005 code - Part 5 "Selection Sets"  (Read 2688 times)

0 Members and 1 Guest are viewing this topic.

Bobby C. Jones

  • Swamp Rat
  • Posts: 516
  • Cry havoc and let loose the dogs of war.
AU 2005 code - Part 5 "Selection Sets"
« on: December 28, 2005, 02:45:11 PM »
This example shows how you can use the Editor objects SelectionAdded event to filter selection sets.  The command "Unselectablecircles" shows how you can filter not only your own selection sets, but selections sets of standard AutoCAD commands!

Code: [Select]

using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace AU2005.AcadApi
{
//Page 11
public class SelectionSets
{
[CommandMethod("ssExample")]
public void SelectSetExample()
{
Editor currentEditor = acadApp.DocumentManager.MdiActiveDocument.Editor;

//Set up an event handler to filter the selection set to dissallow circles
SelectionAddedEventHandler selectionAdded = new SelectionAddedEventHandler(OnSelectionAdded);
currentEditor.SelectionAdded += selectionAdded;

//Create a new selection set options object
PromptSelectionOptions ssOptions = new PromptSelectionOptions();
ssOptions.AllowDuplicates = true;
ssOptions.MessageForAdding = "\nSelect anything but circles: ";

//Start the selection process
currentEditor.GetSelection(ssOptions);

//Remove the event handler
currentEditor.SelectionAdded -= selectionAdded;
}

//Not in the handout
[CommandMethod("UnselectableCircles")]
public void AlwaysRemoveCircles ()
{
Editor currentEditor = acadApp.DocumentManager.MdiActiveDocument.Editor;
currentEditor.SelectionAdded += new SelectionAddedEventHandler(OnSelectionAdded);
}

//Not in the handout
[CommandMethod("SelectableCircles")]
public void AllowCircles ()
{
Editor currentEditor = acadApp.DocumentManager.MdiActiveDocument.Editor;
currentEditor.SelectionAdded -= new SelectionAddedEventHandler(OnSelectionAdded);
}


private void OnSelectionAdded(object sender, SelectionAddedEventArgs e)
{
using (Transaction ssTrans = acadApp.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
{
ObjectId[] selectedEntityIds = e.AddedObjects.GetObjectIds();
Entity selectedEntity;

//Loop through the selected objects
for (int entityIndex = 0; entityIndex < e.AddedObjects.Count; entityIndex++)
{
ObjectId selectedEntityId = selectedEntityIds[entityIndex];
selectedEntity = (Entity)ssTrans.GetObject(selectedEntityId, OpenMode.ForRead);

//Remove the circles
if (selectedEntity is Circle)
{
e.Remove(entityIndex);
}
}
}
}
}
}

Bobby C. Jones