forestgxc 发表于 2025-4-6 18:49:57

这一段代码不接受输入数字+空格继续运行,只接受回车,如何解决?

(DEFUN C:hh (/ hatchstyle input)
(setvar "CMDECHO" 0)

;;; 显示选项提示
(princ "\n填充类型 [混凝土(1)/钢筋混凝土(2)/钢型材(3)/dots(4)/cross(5)/grass(6)/玻璃(7)/玻璃2(9)/ansi34/加气混凝土(10)/结构胶(11)/ansi31(12)] <默认>: ")

;;; 获取用户输入
(setq input (getstring T)) ; 允许用户输入任意字符串,并支持回车
(if (or (not input) (= input ""))
    (setq hatchstyle 1) ; 默认选择 1
    (progn
      ;;; 解析输入,检查是否为连续数字
      (while (not (and (numberp (setq hatchstyle (atoi input))) (<= 1 hatchstyle 12)))
      (cond
          ((not (numberp (atoi input))) (princ "\n错误:请输入数字。"))
          ((not (<= 1 (atoi input) 12)) (princ "\n错误:请输入 1-12 之间的数字。"))
      )
      (setq input (getstring T))
      )
      ;;; 检查输入是否为连续字符
      (if (not (wcmatch input "*")) ; 非连续字符(如包含空格或字母)
      (progn
          (princ "\n输入不连续,视为回车确认。")
          (setq hatchstyle (atoi input)) ; 使用首数字
      )
      (setq hatchstyle (atoi input)) ; 连续数字,直接确认
      )
    )
)

;;; 根据选择执行填充
(cond
    ((= hatchstyle 1) (h1))
    ((= hatchstyle 2) (h2))
    ((= hatchstyle 3) (h3))
    ((= hatchstyle 4) (h4))
    ((= hatchstyle 5) (h5))
    ((= hatchstyle 6) (h6))
    ((= hatchstyle 7) (h7))
    ((= hatchstyle 8) (h8))
    ((= hatchstyle 9) (h9))
    ((= hatchstyle 10) (h10))
    ((= hatchstyle 11) (h11))
    ((= hatchstyle 12) (h12))
    (T (princ "\n无效选择,默认为混凝土。") (h1))
)

(princ)
)


;;; 保持原有填充函数不变
(defun h1 ()
(command "-HATCH" "p" "混凝土" "10" "0" "")
(prompt "当前填充改为混凝土")
(princ)
)


(defun h2 ()
(command "-HATCH" "p" "钢筋混凝土" "10" "0" "")
(prompt "当前填充改为钢筋混凝土")
(princ)
)


(defun h3 ()
(command "-HATCH" "p" "STEEL" "10" "0" "")
(prompt "当前填充改为钢型材")
(princ)
)


(defun h4 ()
(command "-HATCH" "p" "dots" "10" "0" "")
(prompt "当前填充改为dots")
(princ)
)


(defun h5 ()
(command "-HATCH" "p" "cross" "10" "0" "")
(prompt "当前填充改为cross")
(princ)
)


(defun h6 ()
(command "-HATCH" "p" "grass" "10" "0" "")
(prompt "当前填充改为grass")
(princ)
)


(defun h7 ()
(command "-HATCH" "p" "玻璃1" "10" "0" "")
(prompt "当前填充改为玻璃1")
(princ)
)


(defun h8 ()
(command "-HATCH" "p" "玻璃2" "3" "0" "")
(prompt "当前填充改为玻璃2")
(princ)
)


(defun h9 ()
(command "-HATCH" "p" "ansi34" "3" "0" "")
(prompt "当前填充改为ansi34")
(princ)
)


(defun h10 ()
(command "-HATCH" "p" "加气混凝土" "10" "0" "")
(prompt "当前填充改为加气混凝土")
(princ)
)


(defun h11 ()
(command "-HATCH" "p" "ANSI37" "1" "45" "")
(prompt "当前填充改为结构胶")
(princ)
)


(defun h12 ()
(command "-HATCH" "p" "ansi31" "10" "0" "")
(prompt "当前填充改为ansi31")
(princ)
)


;;; 结束程序
(princ)

飞雪神光 发表于 2025-4-6 21:14:21

本帖最后由 飞雪神光 于 2025-4-6 21:17 编辑

又是AI写的破代码
(defun c:hh (/ input)
(setvar "cmdecho" 0)
(initget "1 2 3 4 5 6 7 9 10 11 12")
(setq input (getkword "\n填充类型 [混凝土(1)/钢筋混凝土(2)/钢型材(3)/dots(4)/cross(5)/grass(6)/玻璃(7)/玻璃2(8)/ansi34(9)/加气混凝土(10)/结构胶(11)/ansi31(12)] <1>: "))
(if (or (not input) (= input ""))
    (setq input "1")
)
(eval (read (strcat "(h" input ")")))
(princ)
)

MZ_li 发表于 2025-4-6 21:23:49

飞雪神光 发表于 2025-4-6 21:14
又是AI写的破代码

你人还怪好的勒

forestgxc 发表于 2025-4-6 22:36:54

是的咧,俺不会写,ai写一写了

forestgxc 发表于 2025-4-6 22:42:20

谢谢飞雪
页: [1]
查看完整版本: 这一段代码不接受输入数字+空格继续运行,只接受回车,如何解决?