Jump to content

Function (Lambda...??


chulse

Recommended Posts

Can someone help me understand the purpose of the Function - Lambda combination and what it does? :oops:

 

example from 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))

 

I believe this is to set a block attribute, but I do not understand how it works...

 

Thanks

Link to comment
Share on other sites

The function vl-remove-if-not takes a predicate function as an argument, along with a list argument.

 

vl-remove-if-not will then iterate through each member of the list argument and process each item though the predicate function, if the predicate function returns nil, the item will be removed (vice-versa for vl-remove-if).

 

The 'function' is to declare the lambda expression as a predicate function argument, and hence not to be evaluated. It has a few performance advantages over the use of an apostrophe.

 

See the VLIDE help file for more info.

Link to comment
Share on other sites

Ok, Please speak slowly and use small words :oops:

 

Can you walk me through that bit of code to show me how it actually sets the attribite value? I guess I still don't quite get what it's doing

with the string.

 

The reason for all this is I am trying to use this to set 2 attributes (tags ID and BOTANICAL) in a revised version of your code.

I think I have the various values set to variables with the following:

 

(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*)))

 

I am not sure if I have the second attribute set correctly (since I don't really inderstand how it does it...)

 

Thanks

Link to comment
Share on other sites

Step by Step:

 

(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>)

 

The GetAttributes method will returns the AttributeObjects (in a list if used with vlax-invoke), and this list is then iterated using vl-remove-if-not.

 

Each Attribute Object is tested within the predicate function, and the resultant list is returned.

Link to comment
Share on other sites

Cary, please note that it is coding etiquette to note your revisions to someone else's code - else their work is tarnished.

 

Very sorry:oops:

I am still new to coding

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...