Share

To Zoom in to an AutoCAD Entity

This AutoCAD JavaScript tutorial demonstrates how to register a new transparent command and zoom to the extents of a selected object.

In this tutorial you will,

  • Register a custom command using the addCommand function

  • Prompt the user to select an object

  • Get the extents of the selected object using the getExtents function

  • Zoom to the extents of the selected object with zoomExtents function

    1. Use the following JavaScript code to create a command to zoom to the extents of a selected entity:

      // Define the callback function for the custom command
      function zoomEntity() {
        try {
      
           // Set the options and prompt to use
           var peo = new Acad.PromptEntityOptions();
           peo.setMessageAndKeywords("\nSelect an object: ", "");
           peo.allowObjectOnLockedLayer = true;
      
           // Prompt the user to select an object
           Acad.Editor.getEntity(peo).then(onComplete, onError);
        }
        catch(e) {
           console.log(e.message);
        }
      }
      
      // If the getEntity function was successful, not necessarily that they selected an object
      function onComplete(jsonPromptResult) {
        try {
      
           // Parse the JSON string containing the prompt result
           var resultObj = jsonPromptResult;
           if (resultObj && resultObj.status == 5100) {
      
              // If an object was successful selected, get the selected entity...
              var entity = new Acad.DBEntity(resultObj.objectId);
      
              // Get the extents of the entity
              entity.getExtents().then(ext,onError);
           }
        }
        catch(e) {
           console.log(e.message);
        }
      }
      
      // Zoom to the extents of the entity, choosing to animate the
      // view transition (if possible to do so)
      function ext(arg) {
         try {
             var minPoint3d =  new Acad.Point3d(arg.minPoint3d.x,arg.minPoint3d.y,arg.minPoint3d.z);
             var maxPoint3d =  new Acad.Point3d(arg.maxPoint3d.x,arg.maxPoint3d.y,arg.maxPoint3d.z);
             Acad.Editor.CurrentViewport.zoomExtents(minPoint3d, maxPoint3d, true);
            }
        catch(e) {
           console.log(e.message);
         }
      }
      
      // General message to display if an error occurred during object selection
      function onError(jsonPromptResult) {
        console.log("\nProblem encountered.");
      }
      
      // Register the custom command, as transparent
      Acad.Editor.addCommand(
        "ZOOM_CMDS",
        "ZEN",
        "ZEN",
        Acad.CommandFlag.TRANSPARENT,
        zoomEntity
      );
      
      // Display a message in the AutoCAD Command line window
      console.log("\nRegistered ZEN command.\n");
    2. Save the file as ZoomEntity.js.

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

    4. At the Enter javascript URL to load: prompt, enter the URI to the ZoomEntity.js file.

    5. At the AutoCAD command prompt, enter zen.

    6. Select an object for which you want to zoom to its extents.

Was this information helpful?