To Create a Custom Command
This AutoCAD JavaScript tutorial demonstrates how to register a custom command and execute an AutoCAD command.
In this tutorial you will,
Register a custom command using the
addCommand
functionDefine a callback function that executes the AutoCAD ERASE command to erase all objects in the canvas
Type the following JavaScript statements in a plain text file.
// Register a custom command Acad.Editor.addCommand("JSCOMMAND", "MYERASE", "MYERASE", Acad.CommandFlag.TRANSPARENT, callback); // Define the callback to execute when the command is started function callback() { Acad.Editor.executeCommand("_erase", "_all", ""); }
Save the file as myerase.js.
Add the path from where you want to load the JavaScript file to the existing paths of the TRUSTEDPATHS system variable.
At the AutoCAD Command prompt, enter webload and then press enter again to use the Load option.
At the
Enter javascript URL to load:
prompt, enter the URI to the myerase.js file.The myerase.js file is loaded into and executed by AutoCAD if it is found in one of the paths listed in the TRUSTEDPATHS system variable.
At the AutoCAD Command prompt, enter myerase.
All of the objects in the current drawing should now be erased.