Author Topic: Changing attributes  (Read 2826 times)

0 Members and 1 Guest are viewing this topic.

hudster

  • Gator
  • Posts: 2848
Changing attributes
« on: October 06, 2005, 03:56:59 AM »
If i have a block with 5 attributes

1, 2, 3 ,4 & 5 how do I extract the information for attribute 3 only?
Revit BDS 2017, 2016, 2015, 2014, AutoCAD 2017, 2016, Navisworks 2017, 2016, BIM360 Glue

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Changing attributes
« Reply #1 on: October 06, 2005, 05:28:36 AM »
What is the TagName for Attribute 3 ??

You could extract all attributes with a generic extractor, then test the return list for the value you want.

The return from getattributelist will be something like this :
(("LABEL" . "VIEW ") ("DET" . "A") ("DWG" . "-") ("N1" . " 32424") ("N2" . " 234534") ("N3" . " 3246463"))

Say your attribute tag is "N1"

Try this :

Code: [Select]
(if (and (setq blockref (car (entsel)))
         (setq attributelist (getattributelist blockref))
         (setq tagvalue (getass "N1" attributelist))
    )
  (Prompt tagvalue)
)
With this loaded :
Code: [Select]
(defun getass (key data /) (cdr (assoc key data)))

(defun GetAttributeList (blockref / catchit returnval)
;;
;; kwb@theswamp

  (if (= (type blockref) 'ename)
    (setq blockref (vlax-ename->vla-object blockref))
  )
  (if (and (= (vla-get-objectname blockref) "AcDbBlockReference")
           (= (vla-get-hasattributes blockref) :vlax-true)
      )
    (if (vl-catch-all-error-p
          (setq catchit (vl-catch-all-apply 'vlax-invoke
                                            (list blockref 'getattributes)
                        )
          )
        )
      (alert (vl-catch-all-error-message catchit))
      ;; else should be OK
      (setq returnval (mapcar '(lambda (attref)
                                 (cons (vla-get-tagstring attref)
                                       (vla-get-textstring attref)
                                 )
                               )
                              catchit
                      )
      )
    )
  )
  returnval
)
« Last Edit: October 06, 2005, 05:33:02 AM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

hudster

  • Gator
  • Posts: 2848
Re: Changing attributes
« Reply #2 on: October 06, 2005, 05:36:27 AM »
Call it tag3.

This was just a general question.  I couldn't find anything in the help menu about extracting the value of 1 specific attribute, so I thought I'd ask here.
Revit BDS 2017, 2016, 2015, 2014, AutoCAD 2017, 2016, Navisworks 2017, 2016, BIM360 Glue

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Changing attributes
« Reply #3 on: October 06, 2005, 05:42:36 AM »
Hi Hudster.

You can step through the AcDbBlockReference but it's a pain.

... and what happens when you want 1, 3 and 5 .. you will have to pen a NEW routine, or run yours three times.

Using the method I've posted, you have ALL attributes to deal with at your leisure, and the routine is generic enough to throw into a library file and use regularly without having to think about it.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

hudster

  • Gator
  • Posts: 2848
Re: Changing attributes
« Reply #4 on: October 06, 2005, 05:49:34 AM »
cheers.

pity there isn't an option like vl-get-attrib or something similar, this would make it easier for newbs like me.
Revit BDS 2017, 2016, 2015, 2014, AutoCAD 2017, 2016, Navisworks 2017, 2016, BIM360 Glue

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Changing attributes
« Reply #5 on: October 06, 2005, 05:58:52 AM »
This will probably work for you.
Code: [Select]
(get-attribute-value (car (entsel)) "N1")
Code: [Select]
(defun get-attribute-value (blockref tag / ent data)
  ;;
  ;; kwb@theswamp
  ;;
  (setq ent blockref)
  (while (and (setq ent (entnext ent))
              (setq data (entget ent))
              (eq (cdr (assoc 0 data)) "ATTRIB")
              (/= (strcase (cdr (assoc 2 data))) tag)
         )
  )
  (if (eq (cdr (assoc 0 data)) "ATTRIB")
    (cdr (assoc 1 data))
  )
)


[modified] edit formatting [/modified]
« Last Edit: October 06, 2005, 06:16:41 AM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Changing attributes
« Reply #6 on: October 06, 2005, 06:01:29 AM »
Safe code (making sure a block with attributes) not included ...

Code: [Select]
(if (setq ename (car (entsel)))
    (mapcar
       '(lambda (attribute)
            (cons
                (vla-get-textstring attribute)
                (vla-get-tagstring attribute)
            )
        )
        (vlax-invoke
            (vlax-ename->vla-object ename)
           'GetAttributes
        )
    )
)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Changing attributes
« Reply #7 on: October 07, 2005, 08:37:49 AM »
or this for an ActiveX and Error Trapped TagValue getter ...

Code: [Select]
(if (and (setq blockref (car (entsel)))
         (setq tagvalue (getattributeval blockref "N1"))
    )
  (prompt tagvalue)
)



Code: [Select]
(defun getattributeVal (blockref tag / catchit returnval)
  ;;
  ;; kwb@theswamp
  (if (= (type blockref) 'ename)
    (setq blockref (vlax-ename->vla-object blockref))
  )
  (if (and (= (vla-get-objectname blockref) "AcDbBlockReference")
           (= (vla-get-hasattributes blockref) :vlax-true)
      )
    (if (vl-catch-all-error-p
          (setq catchit (vl-catch-all-apply 'vlax-invoke
                                            (list blockref 'getattributes)
                        )
          )
        )
      (alert (vl-catch-all-error-message catchit))
      ;; else should be OK
      (foreach attref catchit
        (if (= (vla-get-tagstring attref) tag)
          (setq returnval (vla-get-textstring attref))
        )
      )
    )
  )
  returnval
)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Peter Jamtgaard

  • Guest
Re: Changing attributes
« Reply #8 on: October 07, 2005, 08:47:56 AM »
Here is another way

Code: [Select]

(defun GetAttributeValue (objSelection strTagstring / dprPair)
 (if (setq lstAttributePairs (getAttributePairs objSelection))
  (if (setq dprPair (assoc (strcase strTagString) lstAttributePairs))
   (vla-get-textstring (cdr dprPair))
  )
 )
)

(defun GetAttributePairs (objSelection / lstAttributes)
 (if (= (type objSelection) 'ename)
  (setq objSelection (vlax-ename->vla-object objSelection))
 )
 (if (and
      (vlax-property-available-p objSelection "hasattributes")
      (setq lstAttributes (vlax-invoke objSelection "GetAttributes"))
     )
  (mapcar '(lambda (objAttribute) (cons (strcase (vla-get-tagstring  objAttribute))
                                        objAttribute
                                  )
           )
           lstAttributes
  )
 )
)