Many AutoCAD commands that work on sets of entities support two styles of working: verb-noun or noun-verb. This basically means that if the user has pre-selected a set of entities (the "noun") and then launch the command (the "verb") then the command will not need to request the user select them.
This is enabled using something called the pickfirst or implied selection set inside AutoCAD. To take advantage of this feature inside your commands the first thing to do is to use a special command-flag called "UsePickSet": this tells the AutoCAD editor not to clear the pickfirst set when the command is invoked. Next the command implementation will use the SelectImplied() method on the editor object to retrieve the pickfirst set, should one be available. At this stage it's also good practice to clear the pickfirst set with SetImpliedSelection().
The following C# sample shows a few extras, such as how to fall back on asking the user to select the objects if there isn't a pickfirst set. As for what we do with the selected entities... in this simple example each entity is opened for read and has its list() method called to dump out its contents to the command-line (just as the LIST command does). You could, of course, do something much more interesting with the object at this point (I'll try to show more examples of getting specific entity properties in a later post).
Here's the code:
public class PickfirstTestCmds
// Must have UsePickSet specified
[CommandMethod("PFT", CommandFlags.UsePickSet |
static public void PickFirstTest()
Document doc =
Editor ed = doc.Editor;
PromptSelectionResult selectionRes =
// If there's no pickfirst set available...
if (selectionRes.Status == PromptStatus.Error)
// ... ask the user to select entities
PromptSelectionOptions selectionOpts =
"\nSelect objects to list: ";
// If there was a pickfirst set, clear it
// If the user has not cancelled...
if (selectionRes.Status == PromptStatus.OK)
// ... take the selected objects one by one
Transaction tr =
ObjectId objIds = selectionRes.Value.GetObjectIds();
foreach (ObjectId objId in objIds)
Entity ent =
// In this simple case, just dump their properties
// to the command-line using list
// Although no changes were made, use Commit()
// as this is much quicker than rolling back
catch (Autodesk.AutoCAD.Runtime.Exception ex)