明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 303|回复: 4

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

[复制链接]
发表于 3 天前 | 显示全部楼层 |阅读模式
(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 "[1-9]*")) ; 非连续字符(如包含空格或字母)
        (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)

回复

使用道具 举报

发表于 3 天前 | 显示全部楼层
本帖最后由 飞雪神光 于 2025-4-6 21:17 编辑

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

评分

参与人数 2明经币 +2 收起 理由
zhoupeng220 + 1 很给力!
ZZXXQQ + 1 赞一个!

查看全部评分

回复 支持 反对

使用道具 举报

发表于 3 天前 | 显示全部楼层

你人还怪好的勒
回复 支持 反对

使用道具 举报

 楼主| 发表于 3 天前 | 显示全部楼层
是的咧,俺不会写,ai写一写了
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2025-4-9 08:54 , Processed in 0.167038 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表