; 05-Jul-09 NEHolt created for blog example ; (defun c:remap_color_attrval_projectwide ( / d_lst dwgnam picked_lst ) ; Display active project's drawing list. Prompt user to pick one or more ; drawings to process. (setq d_lst (nth 6 (c:wd_proj_wdp_data))) ; dwg list for active project (setq picked_lst (car (c:wd_pdwgs d_lst "Pick drawings for remap of the COLOR attribute" "" nil))) (if picked_lst (progn ; one or more drawings selected. Process each. (load "wdcom1b.fas") ; make sure this is initialized / in memory (foreach dwgnam picked_lst (princ "\nProcessing ")(princ dwgnam) (remap_color_attrval_notactive dwgnam) ) ) ) (princ) ) ; -- (defun remap_color_attrval_notactive ( dwgfilename / en ix str target_attrnam maplst slen dwg_hdl ben_hdl change_cnt ed edd fullfilename hitattr hdl enn_hdl blknam newedd newval oldval ss_hdl_list x) ; Process all block inserts on target drawing "dwgfilename". 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 dwg_hdl nil) (setq change_cnt 0) ; track number of changes to this drawing file (if (not dwgfilename) (progn ; assume processing the active drawing (setq dwg_hdl (wd_dbx_open nil "w")) ; open for read/write ) ; ELSE (progn ; processing some drawing other than active drawing ; Make sure that passed file name is valid (if (not (setq fullfilename (findfile dwgfilename))) (alert (strcat "Cannot find this drawing file:\n" dwgfilename)) ; ELSE (setq dwg_hdl (wd_dbx_open fullfilename "w")) ) ) ) (if dwg_hdl (progn ; target drawing file is "open". Okay to proceed. (setq ss_hdl_list (wd_dbx_ssget dwg_hdl "_X" '((0 . "INSERT")))) (if (/= ss_hdl_list nil) (progn (setq slen (length ss_hdl_list)) ; number of entities in selection set (setq ix 0) ; use to index through the selection set list (while (< ix slen) (setq ben_hdl (nth ix ss_hdl_list)) ; 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 like this: ; (wd_dbx_entattr ben_hdl dwg_hdl (list "COLOR")) ; or (c:ace_get_pnlval_dbx fullfilename ben_hdl "COLOR" nil) ; ... 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_hdl (wd_dbx_entnext ben_hdl dwg_hdl)) (setq edd (wd_dbx_entget enn_hdl dwg_hdl (list "*")))) (while (AND enn_hdl (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 (wd_dbx_entmod newedd dwg_hdl) ; push new attribute value out (setq change_cnt (1+ change_cnt)) ; track number of changes ; Display something to the command window (setq ed (wd_dbx_entget ben_hdl dwg_hdl (list "*"))) ; 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_hdl (wd_dbx_entnext enn_hdl dwg_hdl)) ; advance to next sub-entity of this (setq edd (wd_dbx_entget enn_hdl dwg_hdl (list "*")))) ; block insert instance ) ) ) ) (if (> change_cnt 0) (wd_dbx_close dwg_hdl T) ; close and save changes ; ELSE (wd_dbx_close dwg_hdl nil) ; close with no save ) ) ) (princ) )