Grrr 发表于 2022-7-5 17:56:24

(做这个)或(做那个)

大家好,
又是我!
 
我对此感到好奇和鼓舞:

 
有没有人有过类似的经验?
我的意思是:
[执行此操作]或选择在做这件事的时候。
 
以下是我对getstring和getkword的尝试:
;(Do this) or (do that)
;Type string or choose keyword:
;Similar: getdist or getreal

(defun c:test ( / ans )

(setq ans (getstring t "\nType a string or : "))

   (cond

      ( (= ans "KWORD") ( (initget 1 "KW1 KW2 KW3") (setq ans (getkword "\nChoose keyword : ")) (princ (strcat "\nFinal answer is " ans " ")) )

;          (cond
;            ( (= ans "KW1" ) (princ "\nFinal answer is keyword1 ") )
;            ( (= ans "KW2" ) (princ "\nFinal answer is keyword2 ") )
;            ( (= ans "KW3" ) (princ "\nFinal answer is keyword3 ") )
;            (t (princ (strcat "\nFinal answer is " ans " ")) )
;          );cond

       ) ; = ans "KWORD"

       (t (princ (strcat "\nFinal answer is " ans " "))
       ); t

   );cond

;   (princ (strcat "\nFinal answer is " ans " "))

   (princ)

);defun
(princ)
 
然而,我遇到的问题是:
Choose keyword : KW1
Invalid option keyword.
我不理解为什么指定的选项无效,因为它包含在initget函数中。
 
无论如何,我喜欢这种方法,因为对于getdist和getreal函数,值可以用图形和分析方式表示(无需额外提示选择图形或分析方法)。

marko_ribar 发表于 2022-7-5 18:17:19

在第一个cond语句中有额外的()。。。
 

;(Do this) or (do that)
;Type string or choose keyword:
;Similar: getdist or getreal

(defun c:test ( / ans )
(setq ans (getstring t "\nType a string or : "))
(cond
    ( (= ans "KWORD")
      (initget 1 "KW1 KW2 KW3")
      (setq ans (getkword "\nChoose keyword : "))
      (princ (strcat "\nFinal answer is " ans " "))
    ) ; = ans "KWORD"
    (t (princ (strcat "\nFinal answer is " ans " "))
    ); t
);cond
(princ)
);defun

 
这是修订版。。。

Lee Mac 发表于 2022-7-5 18:25:55

除非您打算在通用程序中将getstring与关键字结合使用,否则不需要额外的提示,例如:
(defun c:test ( / ans )
   (initget "Alpha Beta Gamma")
   (cond
       (   (not (setq ans (getdist "\nEnter a distance or : ")))
         (princ "\nUser exited.")
       )
       (   (numberp ans)
         (princ "\nUser entered a distance: ")
         (princ ans)
       )
       (   (princ "\nUser selected a keyword: ")
         (princ ans)
       )
   )
   (princ)
)

Grrr 发表于 2022-7-5 18:37:02

你好,marko_ribar,
谢谢你清理这个!我可能需要开始使用VLIDE,以使我的语法更具可读性(我习惯于在记事本中编写那些lisp代码)
 
 
李,你好,
我试图从您的代码中理解这一部分,因为它缺少谓词:
      (   (princ "\nUser selected a keyword: ")
         (princ ans)
       )
 
那部分是你的代码(否则做这个)部分吗
或者(然后做第三件事)优先级最低?
 
从条件教程:
(cond ((predicate1) (then do something 1))    ;if this predicate is true,
                                             ;do something1

   ((predicate2) (then do something 2))    ;if this predicate is true,
                                             ;do something2,
                                             ;each predicate and action
                                             ;following the
                                             ;first one is optional

   (t (else do this))                      ;else, if none of the
                                             ;predicates returns
                                             ;T, do this
)

Lee Mac 发表于 2022-7-5 18:44:45

 
第一个princ表达式[(princ“\n用户选择的关键字:”)]构成cond条件的谓词。由于AutoLISP中的任何非nil值都将验证条件语句,因此princ表达式返回的字符串将验证测试表达式,并导致计算第二个princ表达式。
 
这基本上等同于使用:
然而,由于princ将始终返回提供的字符串(即非零值),因此不需要使用“t”强制计算条件,因为princ表达式可用于获得相同的结果。

Grrr 发表于 2022-7-5 18:58:26

李,
谢谢你的解释!
你对细节的了解太棒了!
页: [1]
查看完整版本: (做这个)或(做那个)