TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: dubb on June 20, 2017, 12:33:49 PM

Title: How to write attribute functions?
Post by: dubb on June 20, 2017, 12:33:49 PM
I am trying to learn how to manipulate attributes. They are a bit weird for me to grasp. I am also looking for some tutorials. I think I am having trouble identifying the entity handles.

My block attribute has a single tag and attribute text set to invisible.
Code: [Select]
(entget(car(entsel)))
Code: [Select]
(entget(car(nentsel)))
If the text was not set as invisible I could see the the dotted pair (1 . "attribute-info")



http://www.lee-mac.com/attributefunctions.html
Thanks for the info Lee Mac

So while looking into how they can be manipulated, how do I test this?
Code: [Select]
Command: (test "pole-j" "pole-no")
; error: no function definition: TEST
Code: [Select]
;; Get Attribute Value  -  Lee Mac
;; Returns the value held by the specified tag within the supplied block, if present.
;; blk - [ent] Block (Insert) Entity Name
;; tag - [str] Attribute TagString
;; Returns: [str] Attribute value, else nil if tag is not found.

(defun c:test ( blk tag / enx )
    (if (= "ATTRIB" (cdr (assoc 0 (setq enx (entget (setq blk (entnext blk)))))))
        (if (= (strcase tag) (strcase (cdr (assoc 2 enx))))
            (cdr (assoc 1 enx))
            (c:test blk tag)
        )
    )
)
Title: Re: How to write attribute functions?
Post by: ronjonp on June 20, 2017, 12:48:31 PM
You need to take the C: off of the test function and remove  (c:test blk tag) then feed it:
Code - Auto/Visual Lisp: [Select]
  1. (defun test (blk tag / enx)
  2.   (if (= "ATTRIB" (cdr (assoc 0 (setq enx (entget (setq blk (entnext blk)))))))
  3.     (if   (= (strcase tag) (strcase (cdr (assoc 2 enx))))
  4.       (cdr (assoc 1 enx))
  5.     )
  6.   )
  7. )
  8. ;; (test (car (entsel)) "pole-no")


*EDIT .. after looking at Lee's original function, you need the recursive call:
Code - Auto/Visual Lisp: [Select]
  1. ;; Get Attribute Value  -  Lee Mac
  2. ;; Returns the value held by the specified tag within the supplied block, if present.
  3. ;; blk - [ent] Block (Insert) Entity Name
  4. ;; tag - [str] Attribute TagString
  5. ;; Returns: [str] Attribute value, else nil if tag is not found.
  6.  
  7.  
  8. (defun LM:getattributevalue ( blk tag / enx )
  9.     (if (= "ATTRIB" (cdr (assoc 0 (setq enx (entget (setq blk (entnext blk)))))))
  10.         (if (= (strcase tag) (strcase (cdr (assoc 2 enx))))
  11.             (cdr (assoc 1 enx))
  12.             (LM:getattributevalue blk tag)
  13.         )
  14.     )
  15. )
  16. ;; (LM:getattributevalue  (car (entsel)) "pole-no")
Title: Re: How to write attribute functions?
Post by: dubb on June 20, 2017, 12:57:52 PM
Awesome Thanks Ron!
Title: Re: How to write attribute functions?
Post by: BIGAL on June 22, 2017, 11:24:03 PM
If you look into VL Lisp you can get at object properties 'Attributes being one of them, the advantage I see is that you work more in property names rather than using dxf codes.

Code: [Select]
(foreach att (vlax-invoke (vlax-ename->vla-object (car (entsel "pick an attributed block"))) 'getattributes)
(Princ (strcat "\ntagname is " (strcase (vla-get-tagstring att))))
)