Creating Custom Commands
Creating custom command objects and registering them globally within the DWGdirect runtime environment allow a custom DWGdirect DRX application to create commands that clients of the DRX application can use.
DWGdirect maintains a global command stack of type OdEdCommandStack that stores custom commands the client applications create. The odedRegCmds function can access the global stack.
An OdEdCommandStack stores CFxCommand objects. A CFxCommand has the following attributes:
Global name
The global name is the non-localized version of the command name.Local name
The local name represents the localized version of the command name.Group name
The group name provides a mechanism to group sets of commands together within the stack, where a group can access them using the OdEdCommand::newGroupIterator() function.Help-ID
The help-ID is used as a mechanism to provide a command specific help.
CFxCommand also contains exception handling and a virtual Execute function, which is called to execute the command.
When a command is executed, an instance of CFxCommandContext is passed to the command Execute() function, thus providing access to the current database and to an I/O interface that can be used to implement interactive commands.
Example of Creating a Custom Command
Here is an example of a simple CFxCommand Subclass. When executed, this object retrieves a string value from the passed in I/O object and writes this string back out.
class SampleCmd : public OdStaticRxObject < CFxCommand >
{
public:
const OdString localName() const { return globalName(); }
const OdString groupName() const { return "ODA Example Commands"; }
const OdString globalName() const { return OdString("SampleCommand"); }
// Body of the custom command.
int Execute( CFxCommandContext* pCmdCtx )
{
CFxUserIO* pUserIO = pCmdCtx->GetFxDocument()->GetFxUserIO(); // User IO object from command context
CFxDatabase* pDB = pCmdCtx->GetFxDocument()->GetFxDatabase(); // Current database
CFxString sKey = L"< default >";
CFxString sPrompt = pUserIO->GetPromptString( L"Enter a value: ", &sKey );
pUserIO->GetString( true, sPrompt, sKey, &sKey );
pUserIO->Write( sKey + " was entered" );
}
};
Custom commands can peform database operations, such as creating new objects, query for input, and operations based on that input.
Registering a Custom Command
A custom command is registered using the OdEdCommandStack::addCommand function on the global command stack:
SampleCmd command; // must not be destroyed while the command exists in the global stack
odedRegCmds()->addCommand(& command);
Since addCommand takes a pointer, the passed in command object must not be destroyed until the command is removed from the global stack at some later point.