chulse 发表于 2022-7-6 11:12:04

函数(λ。。。??

有人能帮我理解函数-Lambda组合的目的以及它的作用吗?
 
Lee Mac的示例:
 

(vla-put-TextString
             (car
               (vl-remove-if-not
               (function
                   (lambda (x) (eq "TREE#" (strcase (vla-get-TagString x)))))
               
               (vlax-invoke Obj 'GetAttributes))) (car x))

 
我相信这是为了设置块属性,但我不明白它是如何工作的。。。
 
谢谢

Lee Mac 发表于 2022-7-6 11:26:36

函数vl remove if not将谓词函数与列表参数一起作为参数。
 
vl remove if not将遍历列表参数的每个成员,并通过谓词函数处理每个项,如果谓词函数返回nil,则将删除该项(vl remove if反之亦然)。
 
“函数”是将lambda表达式声明为谓词函数参数,因此不进行计算。与撇号相比,它具有一些性能优势。
 
有关更多信息,请参阅VLIDE帮助文件。

chulse 发表于 2022-7-6 11:33:49

好的,请说慢点,用小词
 
你能带我看一下这段代码,告诉我它实际上是如何设置属性值的吗?我想我还是不太明白它在做什么
用绳子。
 
这一切的原因是我试图用它在代码的修订版本中设置2个属性(标记ID和植物学)。
我想我已经用以下变量设置了各种值:
 
(while (setq LINE(car *lst*))
      
   (setq
       x (atof (nth 0 line))
       y (atof (nth 1 line))
       z (atof (nth 2 line))
       bNme (nth 3 line)
       ;;rot (dtr (atof (nth 4 line)))
       ; xscale (atof (nth 4 line))
       ; yscale (atof (nth 5 line))
       tnum (nth 4 line)
       species (nth 5 line)
       canopy (atof (nth 6 line))
   );end setq
      
      
(if (vl-catch-all-error-p
             (setq OBJ
               (vl-catch-all-apply (function vla-InsertBlock)
               (list spc (vlax-3D-point (list x y z)) bNme 1. 1. 1. *ROT*))))
      
         (princ "\n** Error Inserting Block **")
         (progn
         (vla-put-layer OBJ Lname)
         (vla-put-TextString
            (vl-remove-if-not
               (function
                   (lambda (tnum) (eq "ID" (strcase (vla-get-TagString tnum)))))
                                 (vlax-invoke Obj 'GetAttributes)) tnum)
                                 
         (vla-put-TextString
            (vl-remove-if-not
               (function
                   (lambda (botanical) (eq "botanical" (strcase (vla-get-TagString botanical)))))
                                 (vlax-invoke Obj 'GetAttributes)) botanical)
               
         (if (eq :vlax-true (vla-get-isDynamicBlock obj))
             (progn
             (setq ValLst (mapcar 'cons '("CANOPY")
                  (mapcar
                      (function
                        (lambda (i)
                        (if (equal 0.0 (distof i) 0.0001) "1" i)))
                      (list (cadr LINE) (caddr x) (cadddr x)))))
            
                   (foreach dAtt (vlax-safearray->list
                               (vlax-variant-value
                                 (vla-GetDynamicBlockProperties obj)))
               (if (setq tag (assoc (strcase (vla-get-propertyName dAtt)) ValLst))
                   (vla-put-value dAtt
                     (vlax-make-variant (cdr tag)                        
                     (vlax-variant-type (vla-get-value dAtt))))))))))

         ;;(princ (strcat "Tree# " (car x)))
       (setq *lst* (cdr *lst*)))
 
我不确定是否正确设置了第二个属性(因为我真的不知道它是如何做到的…)
 
谢谢

Lee Mac 发表于 2022-7-6 11:47:25

Cary,请注意,注意你对别人代码的修改是一种编码礼仪,否则他们的工作就会被玷污。

Lee Mac 发表于 2022-7-6 11:54:07

一步一步地:
 

(vlax-invoke <BlockObject> 'GetAttributes)

==>(<AttribObject> <AttribObject> <AttribObject> ... <AttribObject>)


(vla-get-TagString <AttribObject>)

==>"TAG"

(vl-remove-if-not
(function
   (lambda ( AttribObject )
   (eq "TAGSTRING"
       (strcase
         (vla-get-TagString AttribObject)
       )
   )
   )
)
(vlax-invoke <BlockObject> 'GetAttributes)
)

==> (<AttribObject>)
 
GetAttributes方法将返回AttributeObject(如果与vlax invoke一起使用,则在列表中),如果没有,则使用vl remove迭代该列表。
 
在谓词函数中测试每个属性对象,并返回结果列表。

chulse 发表于 2022-7-6 12:08:03

 
非常抱歉:哎呀:
我对编码还是新手

Lee Mac 发表于 2022-7-6 12:16:04

 
不用担心,只要抬起头就行了
页: [1]
查看完整版本: 函数(λ。。。??