Search code examples
autocadautolisp

Why won't this match text work by copying not only the text but the color as well?


I have been modifying this code but somehow it only works to copy the source text but ignores the source text's color.

(defun c:matchTextAndProp (/ cEnt ss textColor)
(vl-load-com)
(if (and (setq cEnt (car (nentsel "\nSelect Source Text: ")))
         (member (cdr (assoc 0 (entget cEnt)))
               '("TEXT" "MTEXT" "ATTRIB")))
(progn
  (setq textColor (cdr (assoc 62 (entget cEnt))))
  (redraw cEnt 3)
  (if (setq ss (ssget '((0 . "TEXT,MTEXT"))))
    (foreach x  (vl-remove-if 'listp
                  (mapcar 'cadr (ssnamex ss)))
      (entmod (subst (assoc 1 (entget cEnt))
                     (assoc 1 (entget x))
                     (entmod (subst (cons 62 textColor) (assoc 62 (entget x)) (entget x)))))))
)
(command "_regenall")
(princ))

Solution

  • If the color of an entity is ByLayer (256), the 62 group code may be omitted.

    (defun c:matchTextAndProp (/ cEnt elst text color ss i)
      (if (and (setq cEnt (car (nentsel "\nSelect Source Text: ")))
               (member (cdr (assoc 0 (entget cEnt)))
                       '("TEXT" "MTEXT" "ATTRIB")
               )
          )
        (progn
          (setq elst  (entget cEnt)
                text  (assoc 1 elst)
                color (cond ((assoc 62 elst))
                            (T '(62 . 256))
                      )
          )
          (redraw cEnt 3)
          (if (setq ss (ssget '((0 . "TEXT,MTEXT"))))
            (repeat (setq i (sslength ss))
              (setq elst (entget (ssname ss (setq i (1- i)))))
              (entmod
                (subst text
                   (assoc 1 elst)
                   (if (assoc 62 elst)
                     (entmod (subst color (assoc 62 elst) elst))
                     (append elst (list color))
                  )
                )
              )
            )
          )
        )
      )
      (command "_regenall")
      (princ)
    )