Small modification needed in lisp code for color changing of text as per value

Small modification needed in lisp code for color changing of text as per value

Anonymous
Not applicable
989 Views
4 Replies
Message 1 of 5

Small modification needed in lisp code for color changing of text as per value

Anonymous
Not applicable

Dear All,

 

This is my first trail in Lisp with my own code. I have stucked at the code to update the text colours as per text value. Below is my code. Please help

(defun C:chcolor ()
  (setq cmdold (getvar "cmdecho"))
  (setvar "cmdecho" 0)
  
  (setq sel1 (ssget))
  (setq n (sslength sel1))
  (setq count 0)
  
  (repeat n
    (setq sel2 (entget (ssname sel1 count)))
    (setq COLOR (cdr (assoc 1 sel2)))
    (command "CHPROP" (ssname sel1 count) "" "C" COLOR "")
  )
  
  (setq count (1+ count))
  (setvar "cmdecho" cmdold)
  (princ)
)
0 Likes
Accepted solutions (1)
990 Views
4 Replies
Replies (4)
Message 2 of 5

dbhunia
Advisor
Advisor
Accepted solution

Try this

 

(defun C:chcolor ()
  (setq cmdold (getvar "cmdecho"))
  (setvar "cmdecho" 0)
  
  (setq sel1 (ssget))
  (setq n (sslength sel1))
  ;(setq count 0)
  
  (repeat n
    (setq sel2 (entget (ssname sel1 (setq n (- n 1)))))
    (setq COLOR (cdr (assoc 1 sel2)))
    (command "CHPROP" (ssname sel1 n) "" "C" COLOR "")
  )
  
  ;(setq count (1+ count))
  (setvar "cmdecho" cmdold)
  (princ)
)

Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
Message 3 of 5

Anonymous
Not applicable

Thank you Sir

0 Likes
Message 4 of 5

ВeekeeCZ
Consultant
Consultant

'I think that you deserve an explanation. The only issue was that the counter was outside the loop. Put it inside by moving the red parenthesis. 

 

(defun C:chcolor ( / *error* cmdold)
  
  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
      (princ (strcat "\nError: " errmsg)))
    (if cmdold (setvar "cmdecho" cmdold))
    (princ))
  
  (setq cmdold (getvar "cmdecho"))
  (setvar "cmdecho" 0)
  
  (setq sel1 (ssget))
  (setq n (sslength sel1))
  (setq count 0)
  
  (repeat n
    (setq sel2 (entget (ssname sel1 count)))
    (setq COLOR (cdr (assoc 1 sel2)))
    (command "CHPROP" (ssname sel1 count) "" "C" COLOR "")
    (setq count (1+ count))
    )
  
  (setvar "cmdecho" cmdold)
  (princ)
  )

 

Though, the most popular way around here is similar what @dbhunia used - reversed repeat method. I like it too because all the counting stuff you do at the beginning of the loop and then you don't need to think about it anymore.

(repeat (setq n (sslength ss))
  (setq en (ssname ss (setq n (1- n))))
  ;...
  )

 

Some more suggestions. You also should localize your variables. That way you prevent the unwanted interferences with your potential other routines. 

And since you're changing some system variable, you should add the *error* function, also localized. Now, in case that some error occurs within the process, it will break and you won't get the echo back.

0 Likes
Message 5 of 5

Kent1Cooper
Consultant
Consultant

Curiously, though when you "dump" the VLA properties of an object, it doesn't list  one called Color, things actually have such a property, and it's not necessary to deal with the cryptically-formatted TrueColor property that is  listed.  That means this can be done rather simply, without having to turn off command echoing, and therefore no need for an *error* handler to reset that.  [This also restricts selection to appropriate object types and on unlocked Layers, and localizes variables.]

(vl-load-com); [if needed]
(defun C:TCTC (/ ss n obj); = Text Color To Contents (if (setq ss (ssget ":L" '((0 . "*TEXT")))) (repeat (setq n (sslength ss)) (vla-put-Color (setq obj (vlax-ename->vla-object (ssname ss (setq n (1- n))))) (atoi (vla-get-TextString obj)) ) ) ) (princ) )

It works on both Text and Mtext objects whose contents are only numerical characters [no Mtext internal formatting, etc.].  But it does require string values representing integer numbers, whereas the CHPROP approach can also work with color names  [e.g. "red"].

 

It could  also be made to check whether the number represented is in the right range, and could have all the usual enhancements added [Undo begin/end wrapping, and if  doing that, error handling to do the end part if something goes wrong, etc.].

Kent Cooper, AIA
0 Likes