; 05-Jul-09 NEHolt created for blog example ; (defun c:remap_color_attrval ( / ss en ix str target_attrnam maplst slen ben blknam enn edd oldval newval newedd hitattr hdl ed x) ; Process all block inserts on active drawing. Look for attribute ; named "COLOR". If found, check existing value and map new color ; value to it. Also adjust the attribute's text size a bit to ; accommodate the longer mapped attribute value (ex: change "R" to "RD" ; or change "B" to "BU") ; ; -- list mapping here -- (setq target_attrnam "COLOR") (setq maplst (list (list "R" "RD") ; red (list "B" "BL") ; blue (list "A" "AM") ; amber (list "G" "GN") ; green (list "C" "CL") ; clear (list "W" "WT") ; white ) ) ; -- main program starts here -- (setq ss (ssget "_X" '((0 . "INSERT")))) (if (/= ss nil) (progn (setq slen (sslength ss)) ; number of entities in selection set (setq ix 0) ; use to index through the selection set list (while (< ix slen) (setq ben (ssname ss ix)) ; get next block insert instance ; from the selection set (setq ix (1+ ix)) ; increment index for next time ; Now look for target attribute tag. Can do this in either ; of two way... can use a call to look for the attribute on ; this block insert, or can do it the hard way and cycle ; through all stand-alone subentities of this block insert ; instance and look for the target attribute. Let's do it ; the hard way. ; Now cycle through its stand-alone subentities like attributes ; (this is not the same as cycling through the entities of the ; block definition itself) (setq hitattr nil) (if (setq enn (entnext ben)) (setq edd (entget enn))) (while (AND enn (not hitattr) (/= (cdr (assoc 0 edd)) "SEQEND") ; end of this entity (/= (cdr (assoc 0 edd)) "INSERT") ) ; beginning of next! (if (= (cdr (assoc 2 edd)) target_attrnam) (progn ; yes, found target attribute tag (setq oldval (cdr (assoc 1 edd))) (setq hitattr 1) ; remember that we've found target attribute ; so we can exit out of the "while" loop ; sooner rather than later. ; Look for this target value in the map list (setq newval nil) ; use as flag to remember if mapped value hit (foreach x maplst (if (= (car x) oldval) (progn ; match on old attribute value (setq newval (cadr x)) ; get the new mapped value ) ) ) (if newval (progn ; yes, found a new mapped value for this attribute ; Prepare to push new value back out to the attribute (if (setq newedd (subst (cons 1 newval)(assoc 1 edd) edd)) (progn (entmod newedd) ; push new attribute value out ; Display something to the command window (setq ed (entget ben)) ; open the block insert instance ; Get block name (setq blknam (cdr (assoc 2 ed))) ; Get handle (setq hdl (cdr (assoc 5 ed))) ; Display to command window (princ "\nHDL=")(princ hdl) (princ " BLKNAM=")(princ blknam) (princ ", ")(princ target_attrnam) (princ " old=")(princ oldval) (princ ", new=")(princ newval) ) ) ) ) ) ) (if (setq enn (entnext enn)) ; advance to next sub-entity of this (setq edd (entget enn))) ; block insert instance ) ) (setq ss nil) ; release the selection set ) ) (princ) )