CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

AutoLISP 开发指南

教程:创建新的自定义命令并使用系统变量进行控制(AutoLISP)

2023-1-5 08:45| 发布者: admin| 查看: 579| 评论: 0|来自: AutoCAD

摘要: 使用 AutoLISP 编程语言,可以通过创建自己的类似命令的函数和设置系统变量的值来控制绘制环境。

使用 AutoLISP 编程语言,可以通过创建自己的类似命令的函数和设置系统变量的值来控制绘制环境。

使用 AutoLISP,您可以创建可从命令提示符访问的新命令,就像与标准 AutoCAD 命令或第三方实用程序定义的命令进行交互一样。自定义命令可以将标准 AutoCAD 命令与函数一起使用,也可以使用 AutoLISP 函数直接操作对象。许多开发人员创建 AutoLISP 函数,这些函数按特定顺序执行多个标准 AutoCAD 命令。command

AutoLISP 程序还可以查询和更改系统变量的值。系统变量会影响命令的行为和 AutoCAD 环境。

创建新函数

使用 AutoLISP 函数定义一个新函数。意思是“定义函数”。defundefun

这是函数的签名:defun

(defun function_name ([arguments] [/ local_variables ...]) expr ...)

如您所见,第一个参数是要定义的函数的名称。

该函数还允许您定义可传递给函数的参数列表以及函数“局部”的用户定义变量列表。当您声明仅在函数处于活动状态时对函数可用的用户定义变量时,请确保将它们作为表达式的一部分添加到局部变量列表中。defundefun

下面是将消息输出到命令提示符或消息框中的自定义函数的示例:

(defun display-msg (msg mode / )
  (if (= mode 0)
    (prompt (strcat "\n" msg))
    (alert msg)
  )
 (princ)
)

自定义 display-msg 函数需要两个值,一个消息作为文本字符串,一个模式作为整数值 0 或 1。可以通过在 AutoCAD 命令提示下输入代码,然后输入以下内容之一来执行函数来测试函数:

  • (display-msg "Hello from AutoLISP!" 0)
  • (display-msg "Hello from AutoLISP!" 1)

创建自定义命令

自定义命令是使用 thefunction 定义的函数,但使用特殊的命名约定:它们使用字符作为前缀。这使它们与其他功能区分开来。defunc:

您可以定义接受参数的函数,但切勿定义将用作接受参数的自定义命令的函数。相反,定义为自定义命令的函数应提示用户输入 ,,, 和函数。getXXXentselnentselssget

为函数提供函数名称后,可以输入在命令提示下输入自定义命令时应执行的 AutoLISP 表达式。defun

以下步骤说明如何定义名为 HELLO 的自定义命令。此命令将提示用户输入字符串值,然后显示在消息框中输入的字符串:

  1. 在命令提示符下,输入 !msg并按 Enter 键。

    应返回该值,因为当前未为用户定义的变量赋值。这是一个用户定义的变量,将定义为 HELLO 命令的一部分,并且不应用于其他程序。nilmsgmsg

  2. 在命令提示符下,一次输入一行以下代码。在每行后按回车键。输入最后一行后,您将看到命令窗口历史记录中显示的值 C:HELLO。

    (defun c:hello ( / msg)
      (setq msg (getstring T "\nEnter a message: "))
      (alert msg)
    )

现在,HELLO 命令已定义,您可以通过在命令提示符下输入其名称来执行它。使用以下步骤执行 HELLO 命令:

  1. 在命令提示下,输入hello

  2. “输入消息:”提示符下,输入这是我的第一个 AutoLISP 命令!

    将显示一个消息框,其中显示您输入的消息。

    Message Box - Windows

    Message Box - Mac OS

  3. Click OK to dismiss the message box.

  4. At the Command prompt, enter !msg and press Enter.

    The value should be returned, and is as expected. Even though you used the function to assign the value entered at the Enter a message: prompt to the variable, the value of the variable is not maintained because it was defined as a local variable to the C:HELLO function. If you remove from the local variable list in the expression for the C:HELLO function, the variable would be defined as a global variable then and the value would be retained after the function has finished executing. nilsetqmsgmsgdefunmsg

Entering expressions directly at the Command prompt makes it easy to learn and work with AutoLISP, but there is a disadvantage to this convenience. Any functions and user-defined variables you define in a drawing are accessible only from that drawing until it is closed. You can see this by doing the following:

  1. Define the HELLO function shown earlier in the current drawing, if you did not previously do so.

  2. Create a new drawing.

  3. At the Command prompt, enter hello.

    The following message is displayed:

    Unknown command "HELLO". Press F1 for help.

You can save your AutoLISP expressions to a file with the LSP file extension so they can be reused and loaded into other drawings. For information on creating and loading AutoLISP Source (LSP) files, see Tutorial: Creating, Loading, and Opening an AutoLISP File.

Accessing and Setting System Variable Values

System variables control the behavior of commands, change the settings of the drawing environment, and specify the default property values of new objects and much more. You can query and set the value for a system variable using the following functions:

  • getvar - Returns the current value of a system variable
  • setvar - Assigns a new value to a system variable

The following explain how to get and set the value of the OSMODE (Object Snap mode) system variable:

  1. At the Command prompt, enter (setq cur_osmode (getvar "osmode")).

    The current value of the OSMODE system variable is returned and assigned to the user-defined variable of . While OSMODE returns an integer value, the value is the sum of multiple "bit-code" values. For example, the value 35 indicates that the Endpoint (1), Midpoint (2), and Intersection (32) running object snaps are enabled. cur_osmode

  2. At the Command prompt, enter osnap.

    The Drafting Settings dialog box is displayed with the Object Snap tab current. This tab allows you to see which object snaps are enabled. The following image shows what the Drafting Settings dialog box looks like when the OSMODE system variable is set to a value of 35.

    Drafting Settings dialog box - Windows

    “绘图设置”对话框 - Mac OS

  3. 在“草图设置”对话框中,更改当前对象捕捉,然后单击“确定”。

  4. 在命令提示符下,输入(getvar “osmode”)。

    返回 OSMODE 系统变量的当前值。

  5. 在命令提示符下,输入(setvar “osmode” cur_osmode)。

    OSMODE 系统变量的值将恢复为先前分配给用户定义变量的值。cur_osmode

  6. 再次显示“绘图设置”对话框。您应该看到对象捕捉设置已恢复。

注意:在对绘图环境进行更改之前,最好存储任何系统变量的值,然后在程序结束之前恢复它们。

路过

雷人

握手

鲜花

鸡蛋

最新评论

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

GMT+8, 2024-5-19 12:45

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部