Share

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 function

  • Define a callback function that executes the AutoCAD ERASE command to erase all objects in the canvas

    1. 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", "");
      }
    2. Save the file as myerase.js.

    3. Add the path from where you want to load the JavaScript file to the existing paths of the TRUSTEDPATHS system variable.

    4. At the AutoCAD Command prompt, enter webload and then press enter again to use the Load option.

    5. 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.

    6. At the AutoCAD Command prompt, enter myerase.

      All of the objects in the current drawing should now be erased.

      Before Executing the MYERASE Command image

      After Executing the MYERASE Command image

Was this information helpful?