Find certain attribute values and change their color

Find certain attribute values and change their color

Anonymous
Not applicable
1,328 Views
3 Replies
Message 1 of 4

Find certain attribute values and change their color

Anonymous
Not applicable

Hi folks,

 

I need to search for all instances of "xx" , "xxx" and "xxxx" in multiple drawings and change their colors to "255,0,0". I am using follwing code which works fine for *text and *leaders but I want to do the same for attributes as well (I have certain blocks with attributes whose values may contain "xx" , etc.). Can anyone help me with adding that bit to the code? I mean I want to search for attribute values. If it contains the value of "xx" or whatever, then change that certain attribute's text color to "255,0,0".

 

Thanks

 

 

(defun foo (text / ss)
  (sssetfirst
    nil
    (setq ss (ssget "_x"
           (list '(-4 . "<OR")
                 '(-4 . "<AND")
                 '(0 . "*TEXT")
                 (cons 1 (setq text(strcat "*" (strcase text) "*,*" (strcase text T) "*")))
                 '(-4 . "AND>")
                 '(-4 . "<AND")
                 '(0 . "MULTILEADER")
                 (cons 304 text)
                 '(-4 . "AND>")
                 '(-4 . "OR>")
             )
               )
      )
    )
  ss
)
(if (setq ss (foo "xx"))		;;Searching for "xx"
  (command ".CHPROP"  ss "" "Color" "Truecolor" "255,0,0" "")
)
(if (setq ss (foo "xxx"))		;;Searching for "xxx"
  (command ".CHPROP"  ss "" "Color" "Truecolor" "255,0,0" "")
)
(if (setq ss (foo "xxxx"))		;;Searching for "xxxx"
  (command ".CHPROP"  ss "" "Color" "Truecolor" "255,0,0" "")
)

 

0 Likes
Accepted solutions (1)
1,329 Views
3 Replies
Replies (3)
Message 2 of 4

dbroad
Mentor
Mentor
Accepted solution

Attributes cannot be found directly by ssget methods.  Instead you need to search for blocks containing variable attributes and then scan those.  An example follows which should work for you:

;;Translation of help file documentation https://tinyurl.com/kupecwq
;;D. C. Broad, Jr.
;;4/24/2017
(defun truecolor (r g b / tcol)
 (setq tcol (vlax-create-object "AutoCAD.AcCmColor.20"))
 (vla-setRGB tcol r g b)
  tcol
  )
;;Modify truecolor of attributes with a given partial text value matching txt
;;Use: (modattcolor "searchstring" red green blue)
;; red green and blue are integers between 0 and 255.
;;Answer to newsgroup topic https://tinyurl.com/mz7dhzw
;;D. C. Broad, Jr.  4/24/2017
(defun modattcolor(txt r g b / tcol )
  (setq txt (strcase txt))
  (setq tcol (truecolor r g b))

(if
  (ssget "x" '((0 . "insert")(66 . 1)))
  (vlax-for n (vla-get-activeselectionset
		(vla-get-activedocument
		  (vlax-get-acad-object)))
    (foreach m (vlax-invoke a 'getattributes)
      (if (wcmatch (strcase (vla-get-textstring m)) txt)
	(vla-put-truecolor m tcol)
	)
      )
    )
  )
  )
Architect, Registered NC, VA, SC, & GA.
Message 3 of 4

Ranjit_Singh
Advisor
Advisor

just a minor typo here

(vlax-invoke a 'getattributes); should be (vlax-invoke n 'getattributes) 

 

Message 4 of 4

Anonymous
Not applicable

Thanks dboard and Ranjit.Singh both. In addition to that minor typo I had to change "AutoCAD.AcCmColor.20" to "AutoCAD.AcCmColor.21" in order to work in AutoCAD 2017.

 

But other than that, it works fine.

 

Really appreciate it.

0 Likes