cadman6735 发表于 2022-7-6 09:58:46

写这篇文章的捷径?

我正在寻找一种较短的方法来写我的Lisp程序下面的“不是”部分。
 
我有10个“not”语句排成一行,我一生似乎都无法在谷歌上键入正确的搜索词来获得我想要的答案。
 
 
保持宏的相同逻辑,你们这些经验丰富的程序员如何编写宏的“not”部分?
 
谢谢你的帮助
 
 

(defun C:test ()

(vl-load-com)

(setq
acadObject (vlax-get-acad-object)
acadActiveDocument (vla-get-ActiveDocument acadObject)
acadLayers (vla-get-Layers acadActiveDocument)
)

(vlax-for Layer acadLayers
(if
(progn
(not (eq (vla-get-PlotStyleName Layer) "As Drawn"))
(not (eq (vla-get-PlotStyleName Layer) "10% Screen"))
(not (eq (vla-get-PlotStyleName Layer) "20% Screen"))
(not (eq (vla-get-PlotStyleName Layer) "30% Screen"))
(not (eq (vla-get-PlotStyleName Layer) "40% Screen"))
(not (eq (vla-get-PlotStyleName Layer) "50% Screen"))
(not (eq (vla-get-PlotStyleName Layer) "60% Screen"))
(not (eq (vla-get-PlotStyleName Layer) "70% Screen"))
(not (eq (vla-get-PlotStyleName Layer) "80% Screen"))
(not (eq (vla-get-PlotStyleName Layer) "90% Screen"))
)
(vla-put-PlotStyleName Layer "As Drawn")
)
)

(princ)
)

Lee Mac 发表于 2022-7-6 10:03:53

记住progn函数是如何工作的-只有最后一个not语句会被考虑在内-其余的都是多余的。在代码中,您应该用和替换progn,以确保满足所有条件,或者更好。。。
 
我可能会这样做:
 

(defun c:test nil (vl-load-com)

(vlax-for layer
   (vla-get-layers
   (vla-get-ActiveDocument (vlax-get-acad-object))
   )
   (if (not (wcmatch (vla-get-PlotStyleName layer) "As Drawn,#0% Screen"))
   (vla-put-PlotStyleName layer "As Drawn")
   )
)

(princ)
)

cadman6735 发表于 2022-7-6 10:09:06

谢谢李
 
我知道有一种方法可以把我的书变成一个句子。
 
 
我在每个逗号后面加了空格“如图所示,10%屏幕,20%屏幕”,迫使所有内容都如图所示,这导致了10个“not”语句。
删除空格,它现在就可以工作了。
 
 
感谢(progn)tid位,我没有百分之百意识到这一点,我会记住这一点
 
通配符功能使其更短。
 
问题:
 
我在研究中看到过,但#符号有什么作用#0%屏幕
 
谢谢

karl 发表于 2022-7-6 10:13:13

你好
我建议使用“cond”而不是带有“not”的“if”结构。
 

(cond ((eq (vla-get-PlotStyleName Layer) "As Drawn")   (DO-SOMETHING))
      ((eq (vla-get-PlotStyleName Layer) "10% Screen") (DO-SOMETHING-ELSE))
      (ETCETERA)
      (T(DO-SOMETHING-IF-NONE-OF-ABOVE))
);end-of-conditional

 
卡尔

cadman6735 发表于 2022-7-6 10:19:16

谢谢Karl
 
看到不同的方法来做同一件事很好,它保持了选项的开放性,我可以看到不同的方法来编写代码。

Lee Mac 发表于 2022-7-6 10:22:28

也许可视化progn函数的方法是作为一个“包装器”,允许您向具有单个参数的函数提供许多表达式-progn将仅计算提供给它的表达式,因此单个progn表达式可以提供给其他函数。
 
一些例子:
 
如果
 

(if <test expr>
<do this>
<else this>
)


(if <test expr>
(progn
   <do this>
   <and this>
   <and this>
)
<else this>
)

(if <test expr>
<do this>
(progn
   <else this>
   <and this>
   <and this>
)
)

(if <test expr>
(progn
   <do this>
   <and this>
   <and this>
)
(progn
   <else this>
   <and this>
   <and this>
)
)


(if
(progn
   <do this>
   <and this>
   <test expr>
)
<do this>
<else this>
)
 

(while <test expr>
<do this>
<and this>
<and this>
)


(while
(progn
   <do this>
   <and this>
   <test expr>
)
<do this>
<and this>
<and this>
)
 
使用VLIDE帮助(请参阅此处和此处)查看wcmatch函数的文档以查看完整参考,#匹配任何单个数字字符。

cadman6735 发表于 2022-7-6 10:27:41


 
谢谢你的帮助

Lee Mac 发表于 2022-7-6 10:33:55

 
别担心

cadman6735 发表于 2022-7-6 10:40:08


 
我希望这不是一个“愚蠢的笨蛋”问题
 
您的通配符示例运行良好,而Karls(cond)选项也运行良好
但就我的一生而言,我不知道如何使用(and)函数。帮助菜单给了我一个与我所做的事情无关的例子,我甚至在谷歌或这个论坛上都找不到一个例子
你能帮我举个例子吗?
谢谢

Lee Mac 发表于 2022-7-6 10:43:06

例子:
 

(if
(and
   (setq pt1 (getpoint "\nPoint1: "))
   (setq pt2 (getpoint "\nPoint2: "))
)
<do this>
<else do this>
)
页: [1] 2
查看完整版本: 写这篇文章的捷径?