CAB 发表于 2022-7-6 15:25:49

好的,这是我的一键例行程序,比如GetKword。
我让它有点通用。
;;GetKeyPress.lsp
;;CABversion 1.003/26/09
;;Get one key press from user similar to GetKword
;;keys = list of key char & return value'(("Y" "Yes")("N" "No"))
;;def = result if ENTER is pressednil or "Yes" or "No" etc
;;      if nil then Enter is dissallowed
;;msg = the prompt nil = "Press a key"
;;emsg = the error message nil = "Incorrect keypress."
(defun GetKeyPress (keys def msg emsg / input result)
(or msg (setq msg "Press a key"))
(or emsg (setq emsg "Incorrect keypress."))
(princ (strcat "\n" msg))
(while (null result)
   (and (= (car (setq input (grread))) 2) ; keyboard entry
      (< 31 (setq input (cadr input)) 255) ; a key was pressed
   )
   (cond
   ((listp input)) ; not a keypress
   ((setq result (assoc (strcase (chr input)) keys))
      (setq result (cadr result))
   )
   ((and (= input 13) def)
      (setq result def)
   )
   ((princ (strcat "\n" emsg "\n" msg)))
   )
)
result
)
 
(defun c:test(/ ans)
(setq ans (GetKeyPress
             '(("Y" "Yes")("N" "No")); keys allowed, not case sensitive, & return value
             "Yes" ; default when ENTER is pressed
             "Do you need an extra leader? (Y/N) :" ; message prompt
             nil ; use default error message
             ))
(princ ans)
(princ)
)

Lee Mac 发表于 2022-7-6 15:28:07

啊。。。很漂亮的出租车,没想到格雷德

Ham007 发表于 2022-7-6 15:32:42

谢谢你,出租车!!那么,在代码中如何调用该函数呢?我现在很困惑
 
这是我的完整代码:
 
(定义c:bom()
(命令“-osnap”“”)
(命令“ortho”“Off”)
 
;-- 获取插入BOM表块所需的参数
(setq ip(getpoint“\n插入点:”)
(setq int1(getint“\n项目编号:”))
(setq st1(getstring“\n左文本:”))
(setq st2(getstring“\n右文本:”))
 
;-- 将图层设置为文字
(命令“-layer”“set”“TEXT”“”)
 
;--插入具有给定参数的块
(命令“_insert”“bom”ip”“”“int1 st1 st2)
(setq ldr2“Y”)
(同时(=ldr2“Y”)
 
;-- 获取引导点并绘制它
(setq pt1(获取点ip“\n连接到:”)
(setq pt2(极性ip(角度ip pt1)0.1625))--这是BOM表圆上的pt
(命令“_line”pt2 pt1”)
 
我想我必须在这里调用你的函数,但我现在不知道如何调用它
;-- 请另一位领导(原始代码)
(initget“Y”)
(setq ldr2(getkword“\N您需要额外的领导者吗?(是/否):”)
 
)
 
(命令“-layer“T”0“ON“0”set“0”)
(普林斯)
)
 
 
我会感谢你的帮助。
 
非常感谢。

CAB 发表于 2022-7-6 15:34:59

试试这个:
(defun c:bom ()
(command "-osnap" "")
(command "ortho" "Off")

;;-- Get the parameter reqd to insert the BOM block
(setq ip (getpoint "\nInsertion Point: "))
(setq int1 (getint "\nItem Number: "))
(setq st1 (getstring "\nLeft Text: "))
(setq st2 (getstring "\nRight Text: "))

;;-- Set layer to TEXT
(command "-layer" "set" "TEXT" "")

;;--Insert the block with the given params
(command "_insert" "bom" ip "" "" "" int1 st1 st2)
(setq ldr2 "Y")
(while (= ldr2 "Y")

   ;;-- Get the leader point & draw it
   (setq pt1 (getpoint ip "\nConnect to: "))
   (setq pt2 (polar ip (angle ip pt1) 0.1625))
   ;;--This is the pt on the BOM circle
   (command "_line" pt2 pt1 "")

   ;;-- Ask for another leader (original code)
   (setq ldr2 (GetKeyPress
                '(("Y" "Yes") ("N" "No")); keys allowed, not case sensitive, & return value
                "Yes"                  ; default when ENTER is pressed
                "Do you need an extra leader? (Y/N) :" ; message prompt
                nil                  ; use default error message
            )
   )
)

(command "-layer" "T" "0" "ON" "0" "set" "0" "")
(princ)
)

;;GetKeyPress.lsp
;;CABversion 1.003/26/09
;;Get one key press from user similar to GetKword
;;keys = list of key char & return value'(("Y" "Yes")("N" "No"))
;;def = result if ENTER is pressednil or "Yes" or "No" etc
;;      if nil then Enter is dissallowed
;;msg = the prompt nil = "Press a key"
;;emsg = the error message nil = "Incorrect keypress."
(defun GetKeyPress (keys def msg emsg / input result)
(or msg (setq msg "Press a key"))
(or emsg (setq emsg "Incorrect keypress."))
(princ (strcat "\n" msg))
(while (null result)
   (and (= (car (setq input (grread))) 2) ; keyboard entry
      (< 31 (setq input (cadr input)) 255) ; a key was pressed
   )
   (cond
   ((listp input))                   ; not a keypress
   ((setq result (assoc (strcase (chr input)) keys))
      (setq result (cadr result))
   )
   ((and (= input 13) def)
      (setq result def)
   )
   ((princ (strcat "\n" emsg "\n" msg)))
   )
)
result
)

Ham007 发表于 2022-7-6 15:38:56

谢谢你,出租车,
 
我加载了代码,但每当函数要求我输入另一个前导时,我仍然无法使其运行。它会按“Y”或“N”或“enter”结束函数。我找不到我做错了什么。

Lee Mac 发表于 2022-7-6 15:40:38

试试这个火腿,
 
我已经包括了一个错误处理程序,并更新了变量设置和编码,以适应用户错误。
 

(defun c:bom(/ *error ovar vlst ip int1 st1 st2 ldr2 pt2)

(defun *error*(msg)
   (if    ovar
   (mapcar 'setvar vlst ovar))
   (princ (strcat "\nError: " (strcase msg)))
   (princ))

(setq    vlst '("CMDECHO" "ORTHOMODE" "OSMODE" "ATTREQ" "CLAYER")
   ovar (mapcar 'getvar vlst))

(mapcar 'setvar (cdr (reverse vlst)) '(1 0 0 0))
(if (not (tblsearch "LAYER" "TEXT"))
   (command "-layer" "m" "TEXT" "")
   (setvar "CLAYER" "TEXT"))

(if (findfile "bom.dwg")
   (progn
   (if (and (setq ip (getpoint "\nInsertion Point: "))
          (not (initget 5))
          (setq int1 (getint "\nItem Number: ")
            st1(getstring "\nLeft Text: ")
            st2(getstring "\nRight Text: ")))
   (progn
   (command "-insert" "bom" ip "" "" "" int1 st1 st2)
   (setq ldr2 "Yes")
   (while (and (= ldr2 "Yes")
             (setq pt1 (getpoint ip "\nConnect to: ")))
       (setq pt2 (polar ip (angle ip pt1) 0.1625))
       (command "_line" pt2 pt1 "")

       (setq ldr2 (GetKeyPress
            '(("Y" "Yes") ("N" "No")) ; keys allowed, not case sensitive, & return value
            "Yes" ; default when ENTER is pressed
            "Do you need an extra leader? (Y/N) :" ; message prompt
            nil ; use default error message
            ))))
   (princ "\n<!> Block Information Not Correct <!>")))
   (princ "\n<!> Block Not Found <!>"))
(mapcar 'setvar vlst ovar)
(princ))

;;GetKeyPress.lsp
;;CABversion 1.003/26/09
;;Get one key press from user similar to GetKword
;;keys = list of key char & return value'(("Y" "Yes")("N" "No"))
;;def = result if ENTER is pressednil or "Yes" or "No" etc
;;      if nil then Enter is dissallowed
;;msg = the prompt nil = "Press a key"
;;emsg = the error message nil = "Incorrect keypress."
(defun GetKeyPress(keys def msg emsg / input result)
(or msg (setq msg "Press a key"))
(or emsg (setq emsg "Incorrect keypress."))
(princ (strcat "\n" msg))
(while (null result)
   (and (= (car (setq input (grread))) 2) ; keyboard entry
    (< 31 (setq input (cadr input)) 255) ; a key was pressed
    )
   (cond
   ((listp input)) ; not a keypress
   ((setq result (assoc (strcase (chr input)) keys))
      (setq result (cadr result))
      )
   ((and (= input 13) def)
      (setq result def)
      )
   ((princ (strcat "\n" emsg "\n" msg)))
   )
   )
result
)

CAB 发表于 2022-7-6 15:44:32

哎呀,我省略了代码以防止区分大小写。
修订版本:
10

Ham007 发表于 2022-7-6 15:47:45

哇!你是了不起的出租车!!!代码看起来有点复杂,但我已经看过了,现在我了解了很多。非常感谢。
 
祝你有美好的一天!!!

Ham007 发表于 2022-7-6 15:51:16

谢谢李,谢谢你的帮助!!!你们太棒了!!!

Lee Mac 发表于 2022-7-6 15:53:46

 
 
没问题-我确实需要更详细地研究grread函数-我自己只使用过一两次,但当你知道如何正确使用它时,调用它是一个非常强大的命令
 
干杯伙计们,
 
页: 1 [2]
查看完整版本: 如何在ty之后强制进入