【问题标题】:Using While Loops inside a Command在命令中使用 While 循环
【发布时间】:2019-03-10 21:28:37
【问题描述】:

一个简单的循环程序可以用作:

(setq y 0)
(while (< q 12) (setq q (1+ q)) 
(print "Hello")
)

但是如何在 Autocad 命令中使用这样的循环呢?

我相信下面的代码可以使用循环来简化,但如何在运行命令中使用它们。

(defun PX (_angle)
  (list (* BaseRadius
       (+ (cos (+ 1.570796 _angle)) (* _angle (cos _angle)))
    )
    (* BaseRadius
       (+ (sin (+ 1.570796 _angle)) (* _angle (sin _angle)))
    )
  )
)

(defun c:cir ()
  (setq BaseRadius 8)
  (command "_pline"
       "qua"
       (PX 0)
       (PX 0.1)
       (PX 0.2)
       (PX 0.3)
       (PX 0.4)
       (PX 0.5)
       (PX 0.6)
       (PX 0.7)
       (PX 0.8)
       (PX 0.9)
       (PX 1)
       (PX 1.1)
       (PX 1.2)
       (PX 1.3)
       (PX 1.4)
       (PX 1.5)
       ""
  )

【问题讨论】:

  • 在你的第一个例子中,我相信(setq y 0)实际上应该是(setq q 0)
  • 是的,必须纠正!

标签: autocad autolisp


【解决方案1】:

您可以通过多种方式完成此操作。

使用repeat

最简单的方法可能是使用repeat 循环,该循环以预定次数计算一组表达式,每次迭代都会增加角度变量:

(defun c:cir ( / ang rad )
    (setq ang 0.0
          rad 8.0
    )
    (command "_.pline")
    (repeat 16
        (command "_non"
            (list
                (* rad (+ (cos (+ (/ pi 2.0) ang)) (* ang (cos ang))))
                (* rad (+ (sin (+ (/ pi 2.0) ang)) (* ang (sin ang))))
            )
        )
        (setq ang (+ ang 0.1))
    )
    (command "")
    (princ)
)

注意事项:

关于上述代码的几点注意事项:

  • 局部变量(即只需要在此函数范围内定义的变量)被声明为 defun 表达式的一部分,位于参数列表中的正斜杠 (/) 之后。这可确保您的函数定义的变量不会干扰 Document 命名空间中定义的同名全局变量。关于这个话题的更多信息,你不妨参考我的教程here

  • 最好始终使用下划线 (_) 和句点 (.) 命令前缀(例如 "_.pline"),因为下划线确保命令的英文版本是始终使用,与评估函数的 AutoCAD 语言版本无关;句点确保使用命令的原始定义,忽略任何可能的重新定义。

  • 在任何点输入之前提供的"_non"(或"_none")对象捕捉修改器可确保在将点提供给命令时忽略任何活动的对象捕捉模式。另一种方法是在提供点输入时临时存储OSMODE 系统变量的值并将其设置为0(从而禁用所有对象捕捉模式),或者临时将OSNAPCOORD 系统变量设置为1,但是这些解决方案中的任何一个都需要使用错误处理程序来重置系统变量,以防出现任何故障。

  • 表达式(cos (+ (/ pi 2.0) ang))(sin (+ (/ pi 2.0) ang)) 可以选择分别替换为(sin (- ang))(cos ang)。但我不想离你的原始代码太远,以便你更好地理解我所做的更改。

使用while

或者,如果您希望迭代由超过给定限制的变量值确定的次数,while 循环可能是更合适的选择:

(defun c:cir2 ( / ang rad )
    (setq ang 0.0
          rad 8.0
    )
    (command "_.pline")
    (while (< ang 1.6)
        (command "_non"
            (list
                (* rad (+ (sin (- ang)) (* ang (cos ang))))
                (* rad (+ (cos    ang)  (* ang (sin ang))))
            )
        )
        (setq ang (+ ang 0.1))
    )
    (command "")
    (princ)
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多