AutoCAD Exchange

January 14, 2011

Extending ADDSELECTED to support custom objects

In a post earlier this week, I mentioned that you could extend the ADDSELECTED command to support other object types. Before I get into that, you should be clear on how ADDSELECTED works.

Overview

ADDSELECTED lets you quickly create a new object of the same object type and with the same general properties as an existing object you select. Basically, it looks at the object you select and invokes the appropriate command to create another object of that type. For example: if I select a circle, ADDSELECTED will start the circle command and let me quickly create another circle with the same general properties (color, layer, etc.) as the circle I selected.

The general properties that newly created objects inherit from the selected objects include: color, layer, linetype, linetype scale, plot style, lineweight and thickness. Some object types also inherit other properties, e.g., dimension objects also inherit the dimstyle and dimscale; and text objects also inherit the text style and height.

The command that gets run to create the new object is based on the type of object that you selected and not necessarily on the command you originally used to create that object. If I select a rectangle, ADDSELECTED invokes the PLINE command – not the RECTANG command – because a rectangle is really just an AcDbPolyline. That is its object type.

Object types supported by the ADDSELECTED command include those objects that have a single creation command, e.g., a LINE, ARC or CIRCLE. Object types that are not supported are those that can be created by more than one command. For example, a 3D Solid object can be created using the BOX or SPHERE commands but the end result for both is still just a 3D Solid object. By default, any object type that can be created using multiple commands is not automatically supported by ADDSELECTED. Not surprisingly, 3rd-party custom objects are also not supported however you may be able to add support for specific command/object combinations by manually editing the AcSelCmdMap.xml file in your AutoCAD 2011 installation.

Adding a New Object Type

Warning: What I'm going to describe is not well-documented and any issues that arise from editing this file are not officially supported. Although this file is a pretty basic XML format, you should not edit this file unless you are comfortable doing this kind of thing. One misplaced line or bracket could affect the whole ADDSELECTED command and you could suddenly find that no object types are supported anymore. So … don’t edit this file unless you are confident doing so and always, always, always make a backup of the original file first in case something does happen. You have been duly warned.

To demonstrate this, I'll use the AcDbArcAlignedText object as an example of a custom object that is currently not supported by ADDSELECTED. If I run the command and select a bit of ArcAlignedText (created using the Express Tools ARCTEXT command), I get the following message:

This is the kind of message you’ll see when selecting an unsupported object. Now lets add support for an ArcAlignedText object so that ADDSELECTED will invoke the ARCTEXT command when I select one of these objects.

In your AutoCAD support directory, under your local user profile, there is a new file named AcSelCmdMap.xml (the exact location of this file will vary depending on your installed Autodesk product and your operating system but you can quickly locate it with a bit of AutoLISP by entering (findfile "AcSelCmdMap.xml") at the command line). This file contains the mappings of object types to command names that get used by ADDSELECTED.

When you open this file, you’ll see a comment at the top that describes how to add an additional node to extend support to other object types and their creation commands:

<!--To setup additional properties to a command when ADDSELECTED command is executed, you can add node like this:

The leading comment is pretty much the extent of the explanation on how to do this but you can look through this file and see examples of simple and complex object types and how they were defined. For example, here is a very simple entry for the CIRCLE object:

I’m going to create a new entry for the ARCTEXT object by copying and editing the example node from the top of the file and adding it to the end of the file after the last entry (Pdf). At the most basic level, I need to provide an entry name, a class name, and a command name.

For the entry name, I'll just called it ArcText. This is mostly just a unique identifier within the XML file.

For the class name, you need to know exactly what the object type is and spelling and case counts. One quick way to find out the name of an object type is to use a little bit of AutoLISP. I determine this by entering (entget (car (entsel))) at the command line, selecting an ArcAlignedText object, and inspecting the results:

That (100 . "AcDbArcAlignedText") dotted pair is what I'm looking for and gives me the case and spelling of the ArcAlignedText object.

For the command name, I'll provide the "_arctext" command. This is the command that will be launched when I select an ArcAlignedText object. I don't need the two <Property name> entries so I'll just remove those lines from the example node I started with.

After saving AcSelCmdMap.xml and relaunching AutoCAD, I can run the ADDSELECTED command and select an existing ArcAlignedText object and it immediately runs the ARCTEXT command and prompts me to select an arc for my new text.

This is a very general example of how to add support for an unsupported object type. You may have custom objects that can benefit from the same support. I suspect that how well this works may vary from one custom object to another but it could be worth the time to experiment with it a bit.