<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: AutoLisp get attribute value in Visual LISP, AutoLISP and General Customization Forum</title>
    <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/autolisp-get-attribute-value/m-p/12164223#M25802</link>
    <description>&lt;P&gt;Did you use CHATGP to write the code ? There are numerous problems with the code. To mention 1,&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;(setq blkref (entlast))&lt;/PRE&gt;&lt;P&gt;This is what was ever created last not necessarily your block, could be a line.&lt;/P&gt;</description>
    <pubDate>Fri, 11 Aug 2023 08:40:38 GMT</pubDate>
    <dc:creator>Sea-Haven</dc:creator>
    <dc:date>2023-08-11T08:40:38Z</dc:date>
    <item>
      <title>AutoLisp get attribute value</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/autolisp-get-attribute-value/m-p/12164080#M25800</link>
      <description>&lt;P&gt;Hello, dear colleagues, could you help me with writing an auto-lisp, a part of which should accept an attribute value from a block.&lt;/P&gt;&lt;P&gt;After running this lisp I get an error - "REVISION attribute not found in DX_A3-A0_w_Titleblock_DE block".&lt;/P&gt;&lt;P&gt;But this attribute is definitely there!&lt;BR /&gt;Could you please help me to fix this. Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;(defun c:getRevisionAttribute ()
  (setq block-name "DX_A3-A0_w_Titleblock_DE") ; Name of the block
  (setq attribute-name "REVISION") ; Name of the attribute
  
  (if (tblsearch "BLOCK" block-name)
    (progn
      (setq blkref (entlast))
      (setq attval (cdr (assoc attribute-name (entget blkref))))
      (if attval
        (progn
          (princ (strcat "Value of " attribute-name " attribute: " attval))
        )
        (princ (strcat "Attribute " attribute-name " not found in block " block-name))
      )
    )
    (princ (strcat "Block " block-name " not found"))
  )
  
  (princ)
)

(c:getRevisionAttribute)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2023 07:05:27 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/autolisp-get-attribute-value/m-p/12164080#M25800</guid>
      <dc:creator>i.shkola</dc:creator>
      <dc:date>2023-08-11T07:05:27Z</dc:date>
    </item>
    <item>
      <title>Re: AutoLisp get attribute value</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/autolisp-get-attribute-value/m-p/12164157#M25801</link>
      <description>&lt;P&gt;hi,&lt;/P&gt;&lt;P&gt;maybe this one if rev block is not dynamic.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="lisp"&gt;(defun c:getRevisionAttribute ()
	(if (and 
			(setq rev_block_sset (ssget "_x" '((0 . "insert") (2 . "DX_A3-A0_w_Titleblock_DE"))))
			(= 1 (sslength rev_block_sset))
		)
			(if (vl-some '(lambda (attribute) (= "REVISION" (vla-get-tagstring (setq target_attribute attribute))))
  						  (vlax-invoke (vlax-ename-&amp;gt;vla-object (ssname rev_block_sset 0)) 'getattributes)
				)
				(princ (strcat "\nValue of \"REVISION\" attribute: " (vla-get-textstring target_attribute)))
				(princ "\nAttribute \"REVISION\" not found in block \"DX_A3-A0_w_Titleblock_DE\"")
			)
			(princ "\nMultiple revision blocks or not found")
	)
	(princ)
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2023 09:56:03 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/autolisp-get-attribute-value/m-p/12164157#M25801</guid>
      <dc:creator>komondormrex</dc:creator>
      <dc:date>2023-08-11T09:56:03Z</dc:date>
    </item>
    <item>
      <title>Re: AutoLisp get attribute value</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/autolisp-get-attribute-value/m-p/12164223#M25802</link>
      <description>&lt;P&gt;Did you use CHATGP to write the code ? There are numerous problems with the code. To mention 1,&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;(setq blkref (entlast))&lt;/PRE&gt;&lt;P&gt;This is what was ever created last not necessarily your block, could be a line.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2023 08:40:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/autolisp-get-attribute-value/m-p/12164223#M25802</guid>
      <dc:creator>Sea-Haven</dc:creator>
      <dc:date>2023-08-11T08:40:38Z</dc:date>
    </item>
    <item>
      <title>Re: AutoLisp get attribute value</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/autolisp-get-attribute-value/m-p/12164299#M25803</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;If &lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/6254908"&gt;@Sea-Haven&lt;/a&gt; is right, please, &lt;STRONG&gt;do not post code generated by ChatGPT&lt;/STRONG&gt;!&lt;/P&gt;
&lt;P&gt;This expression also is a LISP non-sense:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="lisp"&gt;(setq attval (cdr (assoc attribute-name (entget blkref))))&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;IMO, the simplest way to get an attribute value since AutoCAD 2012 is to use the getpropertyvalue which consider an attribute tag as a property name (unlike the DXF list returned by entget).&lt;/P&gt;
&lt;P&gt;You can wrap the getpropertyvalue expression in a vl-catch-all-apply one to catch the case the attribute does not exists.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="lisp"&gt;;; getAttributeValue
;; Returns the attribute value or nil, if not found.
;;
;; Arguments
;; block : block ename
;; tag: attribute tag
(defun getAttributeValue (block tag / value)
  (vl-catch-all-apply
    '(lambda () (setq value (getpropertyvalue br tag)))
  )
  value
)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/13423916"&gt;@komondormrex&lt;/a&gt; to get all references of a block definition, it is much more efficient to use the following method which directly gets the references from the block table record than calling (ssget "_X" ...) which iterates through all entities in all spaces.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="lisp"&gt;;; gc:GetReferences
;; Returns the list of all non erased block references.
;;
;; Arguments
;; bname : block name
;; flag : sum of the following binary codes
;;        1 = nested in blocks
;;        2 = inserted in model space
;;        4 = inserted in paper space
(defun gc:GetReferences	(bname flag / blk refs elst)
  (if
    (and
      (setq blk (tblobjname "BLOCK" bname))
      (setq refs
	     (vl-remove-if-not
	       'cdr
	       (mapcar
		 (function
		   (lambda (x)
		     (if (setq elst (entget x))
		       (cons x (entget (cdr (assoc 330 elst))))
		     )
		   )
		 )
		 (gc:massoc 331 (entget (cdr (assoc 330 (entget blk)))))
	       )
	     )
      )
    )
     (if (= 7 flag)
       (mapcar 'car refs)
       (if (&amp;lt; 0 flag 7)
	 (mapcar 'car
		 (vl-remove-if
		   (function
		     (lambda (x)
		       (wcmatch
			 (strcase (cdr (assoc 2 (cdr x))))
			 (cond
			   ((= 1 flag) "`**_SPACE*")
			   ((= 2 flag) "~`*MODEL_SPACE")
			   ((= 3 flag) "`*PAPER_SPACE*")
			   ((= 4 flag) "~`*PAPER_SPACE*")
			   ((= 5 flag) "`*MODEL_SPACE*")
			   ((= 6 flag) "~`**_SPACE*")
			 )
		       )
		     )
		   )
		   refs
		 )
	 )
       )
     )
  )
)


;; gc:massoc
;; Returns the list of all values for the supplied key in an association list.
;;
;; Arguments
;; key : searched key (group code for DXF lists)
;; alst : association list
(defun gc:massoc (key alst)
  (if (setq alst (member (assoc key alst) alst))
    (cons (cdar alst) (gc:massoc key (cdr alst)))
  )
)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then, your command could be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="lisp"&gt;(defun c:getRevisionAttribute (/ refs val)
  (if (= 1
	 (length
	   (setq refs (gc:GetReferences "DX_A3-A0_w_Titleblock_DE" 6))
	 )
      )
    (if	(setq val (getAttributeValue (car refs) "REVISION"))
      (princ (strcat "\nValue of \"REVISION\" attribute: " val))
      (princ "\nAttribute \"REVISION\" not found in block \"DX_A3-A0_w_Titleblock_DE\"")
    )
    (princ "\nMultiple revision blocks or not found")
  )
)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2023 10:49:57 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/autolisp-get-attribute-value/m-p/12164299#M25803</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2023-08-11T10:49:57Z</dc:date>
    </item>
    <item>
      <title>Re: AutoLisp get attribute value</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/autolisp-get-attribute-value/m-p/12164308#M25804</link>
      <description>&lt;P&gt;Yes, you are right, unfortunately I am completely new to this and thought it would work. But I see that chatgpt has a lot of problems with it.&lt;BR /&gt;Thanks for the reply&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2023 09:17:04 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/autolisp-get-attribute-value/m-p/12164308#M25804</guid>
      <dc:creator>i.shkola</dc:creator>
      <dc:date>2023-08-11T09:17:04Z</dc:date>
    </item>
    <item>
      <title>Re: AutoLisp get attribute value</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/autolisp-get-attribute-value/m-p/12164314#M25805</link>
      <description>&lt;P&gt;Hi. Yes, I realized that this is not the best way to solve such problems &lt;span class="lia-unicode-emoji" title=":grinning_face_with_big_eyes:"&gt;😃&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Thanks for such a detailed answer! I will try to do as you said.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2023 09:19:57 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/autolisp-get-attribute-value/m-p/12164314#M25805</guid>
      <dc:creator>i.shkola</dc:creator>
      <dc:date>2023-08-11T09:19:57Z</dc:date>
    </item>
  </channel>
</rss>

