CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

AutoCAD 2022 开发者帮助

放大到 AutoCAD 实体的步骤

2024-5-31 20:10| 发布者: admin| 查看: 111| 评论: 0|原作者: admin|来自: AutoCAD

放大到 AutoCAD 实体的步骤

放大到 AutoCAD 实体的步骤

此 AutoCAD JavaScript 教程演示了如何注册新的透明命令并缩放到所选对象的范围。

在本教程中,您将:

  • 使用函数注册自定义命令addCommand

  • 提示用户选择对象

  • 使用函数获取所选对象的范围getExtents

  • 使用函数缩放到所选对象的范围zoomExtents

    1. 使用以下 JavaScript 代码创建命令以缩放到所选实体的范围:

      // 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. 将文件另存为 ZoomEntity.js

    3. 将要加载 JavaScript 文件的域添加到 TRUSTEDDOMAINS 系统变量的现有域中。

      下面是如何使用 AutoLISP 将 mywesbite.com 域添加到受信任域列表中的示例:

      (setvar "TRUSTEDDOMAINS" (strcat (getvar "TRUSTEDDOMAINS") ";mywesbite.com"))
    4. 在 AutoCAD 命令提示符下,输入 webload,然后输入 ZoomEntity.js 文件的 URI。

      "_.WEBLOAD" "http://mywebsite.com/files/ZoomEntity.js"
    5. 在 AutoCAD 命令提示符下,输入 zen

    6. 选择要缩放到其范围的对象。


路过

雷人

握手

鲜花

鸡蛋

最新评论

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1   苏公网安备32011402011833)

GMT+8, 2025-3-5 17:50

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部