Executing External Commands/ Utilities From Inside AutoCAD

In an earlier post I shared the BROWSER command that opened a webpage of your choice from inside AutoCAD. This time we will see how we can execute some external command from inside AutoCAD.

What if you are working on your important drawing and you need to see the properties of a steel section?

What we do is, Minimize AutoCAD window go to the location of our steel table in explorer and double click it or fireup Microsoft Excel and open the steel table's soft copy.

What if we could open the steel table file from AutoCAD command line.

Yes, You can.

You can run external programs from AutoCAD command line.
While AutoCAD is running, you can start other programs or utilities like following :
  1. Windows system commands and utilities, such as start, type, dir or copy.
  2. Application such as text editors (notepad), word processor (Microsoft Word)
  3. Database managers, spreadsheets (Microsoft Excel) etc.
  4. Your batch files or VBA macros.
How it works :

To run an external command on command we you need to define it in acad.pgp.
The syntax to define the command is is as follows :

command, [executable],flags[,[*]prompt[,returncode]]  


Command : Whenever a command is entered on command line, AutoCAD looks for the command in acad.pgp. The first section of acad.pgp defines external commands.If  the command you enter on the command line is an internal AutoCAD command, name it is ignored .

Executable : The constant string sent to the operating system when you enter a command name. It can be any command that you can execute at the operating-system prompt. The string can include switches or parameters. The case-sensitivity of this string depends on the application you are running.

flags :

A required bitcoded parameter. Add these integer values in any combination to achieve the result you want.
0 Start the application and wait for it to finish.
1 Don't wait for the application to finish.
2 Run the application in Minimized mode.
4 Run the application “hidden.”
8 Put the argument string in quotes.
Bit values 2 and 4 are mutually exclusive; if both are specified only the 2 bit is used. Using value 2 or 4 without value 1 should be avoided, because AutoCAD becomes unavailable until the application has completed.
Bit value 8 allows commands like del to work properly with file names that have embedded spaces. This eliminates the possibility of passing a space-delimited list of file names to these commands. If you prefer multiple file support, do not use the bit value 8.
prompt :
An optional field. It specifies the prompt to display on the AutoCAD command line or for the dynamic input tooltip. The response to this prompt is appended to the string supplied in the executable field. If the first character of the prompt field is an asterisk (*), the response can contain spaces and the user must press ENTER to terminate it. Otherwise, the response is terminated by either SPACEBAR or ENTER. If no prompt is specified, no input is requested; however, you must add a comma if a return code is to be supplied or if you want the prompt to have a trailing space.
return_code :
An optional bitcoded parameter. You can add these integer values together in any combination to achieve the result you want. For example, if values 1 and2 are required, you use 3 as the return code. The values are defined as follows (codes 0 and 4 are meaningless in a windowed environment and are therefore not included):
1 Loads a DXB file. AutoCAD loads the DXB file named $cmd.dxb into the drawing after the command is terminated. After the DXB file is loaded, the$cmd.dxb file is deleted. This action produces the same result as the DXBIN command.
2 Constructs a block definition from a DXB file. AutoCAD creates a block definition from the DXB file named $cmd.dxb. The response to the prompt field is used as the block name. This name must be a valid block name that does not currently exist in the drawing; therefore, this mode cannot redefine a previously defined block. After AutoCAD loads the DXB file, the $cmd.dxbfile is deleted. The default name for the INSERT command is set to the newly defined block.
The file can also contain comment lines preceded by a semicolon (;).
Examples :
  • For DOS Commands
        DEL,       DEL,            8,File to delete: ,
        DEL is the command to be typed on your command line.
        DEL is the command to be executed.
        8 is a flag.
        File specification: is the command prompt after issuing the command on the command line.
  • Windows System Commands
        NOTEPAD,   START NOTEPAD,  1,*File to edit: ,
        EXPLORER,  START EXPLORER,  1,,

        NOTEPAD is the command to be typed on your command line.

        START is the command to be executed.
        NOTEPAD is application name.
        EXPLORER is application name.
        1 is flag, this will let you switch between notepad and AutoCAD.        
        File to edit: is a command linr prompt
        
In place of START you can use cmd to RUN a batch file also.
               
        RUN, cmd /c,0,*Batch file to run: ,

          /c is a switch which carries out the specific command and then stops (the window closed).    

    
The cmd command has many command line switches that affect the display of the new   window. /c is one of them. You can use /k to keep the window open after the command is executed.
  • Open files directly from command line
        STTB,START C:\Location of your document,2,,

        STTB is the command to be typed on command line.

        START is the command to be executed.
        Next to start is the path to the document to open.
        2 is a flag to let us continue working inside AutoCAD.

Hope the post was useful and you ejoyed it.


Thanking You,

- Abhay Chauhan

 PS : Don't forget to comment and share the post with your friends.

No comments:

Post a Comment