TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: ChrisCarlson on October 20, 2014, 09:24:44 AM

Title: Selecting attribute within a block
Post by: ChrisCarlson on October 20, 2014, 09:24:44 AM
Selecting attribute within a block

Code - Auto/Visual Lisp: [Select]
  1.    
  2.          (setq key_ida (entget key_id))
  3.          (= (cdr (assoc 0 key_ida)) "ATTRIB")
  4.          (= (cdr (assoc 2 key_ida)) "KEY_ID")
  5.                 ) ; end check

It's then updated via

Code - Auto/Visual Lisp: [Select]
  1. (vla-put-TextString(vlax-ename->vla-object key_ida) key_id)

So I have a snippet which only allows the user to select the attribute area of a block, in this instance a key note block. Is there a relatively easy way to allow the user to select the block (less error prone, larger surface area to click) and then pull attribute from it and allow updating? If you have a link to something similar I'd love to read it.
Title: Re: Selecting attribute within a block
Post by: ronjonp on October 20, 2014, 09:49:24 AM
Maybe this will give you ideas:
Code - Auto/Visual Lisp: [Select]
  1. ;; Returns a list of tagnames and values
  2. (defun _getattributevalues (block / att out)
  3.        (if (= (type block) 'ename)
  4.          (vlax-ename->vla-object block)
  5.          block
  6.        )
  7.        'getattributes
  8.           )
  9.     (setq out (cons (cons (vla-get-tagstring att) (vla-get-textstring att)) out))
  10.   )
  11. )
  12. ;; (66 . 1) Are blocks that have attributes
  13. (setq ss (ssget '((0 . "insert") (66 . 1))))
  14. (setq e (ssname ss 0))
  15. (_getattributevalues e)
Title: Re: Selecting attribute within a block
Post by: Lee Mac on October 20, 2014, 09:56:07 AM
This set of functions may also help: Attribute Functions (http://www.lee-mac.com/attributefunctions.html)
Title: Re: Selecting attribute within a block
Post by: kpblc on October 20, 2014, 09:57:51 AM
Another one :)
Code: [Select]
(defun set-value-to-attr (block-ref att-tag value / lst err _kpblc-conv-vla-to-list)
                         ;|
*    settings value to attribute
*    Call params:
  block-ref       Block reference pointer
  att-tag         Attribute tag
  value           Value to be setted

*    Call sample:

(if (setq blk (ssname (ssget "_+.:s:e:l" '((0 . "insert") (66 . 1))) 0))
  (set-value-to-attr blk "viewnumber" "qwer")
  ) ;_ end of if

|;

  (defun _kpblc-conv-vla-to-list (value / res)
    (cond
      ((listp value)
       (mapcar (function _kpblc-conv-vla-to-list) value)
       )
      ((= (type value) 'variant)
       (_kpblc-conv-vla-to-list (vlax-variant-value value))
       )
      ((= (type value) 'safearray)
       (if (>= (vlax-safearray-get-u-bound value 1) 0)
         (_kpblc-conv-vla-to-list (vlax-safearray->list value))
         ) ;_ end of if
       )
      (t value)
      ) ;_ end of cond
    ) ;_ end of defun

  (if (= (type block-ref) 'ename)
    (setq block-ref (vlax-ename->vla-object block-ref))
    ) ;_ end of if
  (foreach att (_kpblc-conv-vla-to-list (vla-getattributes block-ref))
    (if (wcmatch (strcase (vla-get-tagstring att)) (strcase att-tag))
      (if (vl-catch-all-error-p
            (setq err (vl-catch-all-apply
                        (function
                          (lambda ()
                            (vla-put-textstring att value)
                            ) ;_ end of lambda
                          ) ;_ end of function
                        ) ;_ end of vl-catch-all-apply
                  ) ;_ end of setq
            ) ;_ end of vl-catch-all-error-p
        (princ "\nError setting value \""
               value
               "\" to Attribute \""
               att-tag
               "\" : "
               (vl-catch-all-error-p err)
               ) ;_ end of princ
        ) ;_ end of if
      ) ;_ end of if
    ) ;_ end of foreach
  ) ;_ end of defun
Title: Re: Selecting attribute within a block
Post by: ronjonp on October 21, 2014, 10:37:20 AM
Did you figure this out ?
Title: Re: Selecting attribute within a block
Post by: ChrisCarlson on October 21, 2014, 12:54:04 PM
It was on the calender for today, but will have to wait until tomorrow. A cursory look at the functions kind of make sense.