Author Topic: Selecting attribute within a block  (Read 2264 times)

0 Members and 1 Guest are viewing this topic.

ChrisCarlson

  • Guest
Selecting attribute within a block
« 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.

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Selecting attribute within a block
« Reply #1 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)
« Last Edit: October 20, 2014, 11:13:47 AM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Selecting attribute within a block
« Reply #2 on: October 20, 2014, 09:56:07 AM »
This set of functions may also help: Attribute Functions

kpblc

  • Bull Frog
  • Posts: 396
Re: Selecting attribute within a block
« Reply #3 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
Sorry for my English.

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Selecting attribute within a block
« Reply #4 on: October 21, 2014, 10:37:20 AM »
Did you figure this out ?

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ChrisCarlson

  • Guest
Re: Selecting attribute within a block
« Reply #5 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.