calude写的
 - ;; AutoLISP 命令等待示例
- ;; 演示如何等待命令完成后再执行下一句
- (defun c:wait-demo ()
- (princ "\n=== 命令等待演示 ===\n")
-
- ;; 方法1: 使用 command 函数 (推荐)
- ;; command 函数会自动等待命令完成
- (princ "\n方法1: 使用 command 函数")
- (command "circle" "0,0" "50") ; 画圆,会等待完成
- (princ "\n圆已绘制完成,继续下一步...")
-
- ;; 方法2: 使用 vl-cmdf 函数 (更现代的方式)
- (princ "\n方法2: 使用 vl-cmdf 函数")
- (vl-cmdf "line" "100,0" "200,100" "") ; 画线,会等待完成
- (princ "\n线已绘制完成...")
-
- ;; 方法3: 检查系统变量 CMDACTIVE
- (princ "\n方法3: 监控 CMDACTIVE 系统变量")
- (command "arc" "300,0" "400,50" "350,100")
- (while (> (getvar "CMDACTIVE") 0) ; 等待命令完成
- (princ ".") ; 显示等待过程
- )
- (princ "\n弧线已绘制完成...")
-
- (princ "\n=== 演示完成 ===")
- (princ)
- )
- ;; 高级等待函数示例
- (defun wait-for-command-complete ()
- "等待当前命令完成的通用函数"
- (while (> (getvar "CMDACTIVE") 0)
- (command "") ; 发送空命令继续
- )
- )
- ;; 带超时的等待函数
- (defun wait-with-timeout (timeout-seconds)
- "带超时的命令等待函数"
- (setq start-time (getvar "DATE"))
- (while (and (> (getvar "CMDACTIVE") 0)
- (< (- (getvar "DATE") start-time) (/ timeout-seconds 86400.0)))
- (command "")
- )
- (if (> (getvar "CMDACTIVE") 0)
- (progn
- (command "^C^C") ; 取消命令
- (princ "\n命令超时,已取消")
- )
- (princ "\n命令正常完成")
- )
- )
- ;; 实际应用示例:批量绘图
- (defun c:batch-draw ()
- "批量绘图示例 - 确保每个命令完成后再执行下一个"
- (princ "\n开始批量绘图...")
-
- ;; 设置图层
- (command "layer" "m" "图形层" "c" "red" "图形层" "")
- (wait-for-command-complete)
-
- ;; 绘制多个图形
- (setq shapes (list
- '("circle" "0,0" "25")
- '("rectangle" "50,50" "100,100")
- '("line" "150,0" "200,50" "")
- ))
-
- (foreach shape shapes
- (princ (strcat "\n正在绘制: " (car shape)))
- (apply 'command shape) ; 执行绘图命令
- (wait-for-command-complete) ; 等待完成
- (princ " 完成!")
- )
-
- (princ "\n批量绘图完成!")
- (princ)
- )
- ;; 用户交互等待示例
- (defun c:interactive-demo ()
- "交互式命令等待演示"
- (princ "\n请选择一个点...")
-
- ;; 等待用户输入
- (setq pt (getpoint "\n选择起点: "))
- (if pt
- (progn
- (princ "\n开始绘制...")
- (command "circle" pt "30")
- (wait-for-command-complete)
- (princ "\n绘制完成!")
- )
- (princ "\n用户取消操作")
- )
- (princ)
- )
- ;; 错误处理的等待示例
- (defun c:safe-command ()
- "带错误处理的命令等待"
- (setq old-error *error*) ; 保存原错误处理
- (setq *error* my-error-handler) ; 设置自定义错误处理
-
- (princ "\n执行可能出错的命令...")
- (command "unknown-command") ; 故意的错误命令
-
- ;; 恢复错误处理
- (setq *error* old-error)
- (princ)
- )
- (defun my-error-handler (msg)
- "自定义错误处理函数"
- (princ (strcat "\n发生错误: " msg))
- (command "^C^C") ; 取消当前命令
- (setq *error* old-error) ; 恢复原错误处理
- (princ)
- )
- ;; 调试辅助函数
- (defun debug-command-state ()
- "显示当前命令状态信息"
- (princ (strcat "\nCMDACTIVE: " (itoa (getvar "CMDACTIVE"))))
- (princ (strcat "\nCMDNAME: " (getvar "CMDNAMES")))
- (princ (strcat "\nCMDECHO: " (itoa (getvar "CMDECHO"))))
- (princ)
- )
- ;; 使用说明:
- ;; 1. 加载此文件到AutoCAD
- ;; 2. 运行 wait-demo 查看基本等待方法
- ;; 3. 运行 batch-draw 查看批量操作示例
- ;; 4. 运行 interactive-demo 查看交互式等待
- ;; 5. 运行 debug-command-state 查看当前命令状态
|