Attribute writes to non-opened drawings

  • Oh no! An error has occurred!
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • You need to be logged in to do that.
  • Coinciding with the acquisition of VIA Development, Nate joined Autodesk in March of 2003 after a decade stint as an entrepreneur following a two-decade stint as a controls engineer and software applications developer at Owens-Corning. Nate is now the lead product architect for AutoCAD Electrical. He loves this stuff.

    About Nate

Latest Post

  • Attribute writes to non-opened drawings
    July 5, 2009, 08:57 PM Nate Holt

    This dovetails with the last posting... instead of Xdata writes, this one illustrates attribute writes... find/replace block insert attribute values on non-opened drawings.

    There is at least one other posting from a couple years ago ( ! ) that deals with this subject, but here we'll pull it together a bit more completely. We'll illustrate two little AutoLisp utilities... one that works on the active drawing only and employs straight, generic AutoLisp calls. The second will be a version that is meant to work on non-opened drawing files. It seems to test okay using AutoCAD Electrical 2008 through 2010. I did not test on other ACE versions, so exercise caution.

    The Scenario

    The example will be this... we want to post-process our electrical schematic drawings and look for block insert instances with attribute tag "COLOR". We examine the current value on the attribute. If it matches up with a short list of possible values, we map a new value to it and push this out to the attribute.

    For example, if we find attribute "COLOR" with a value of "R" (for "red"), we want to map a new value of "RD" and push it out to the block insert's COLOR attribute. If we find a value of "G" (for "green"), we want to map a new value of "GN" and push it out.

    The Straight AutoLISP version - active drawing only

    Here is the first version of the utility (below). It is designed to process the active drawing only. You APPLOAD the utility, launch it at the "Command:" prompt. It runs, makes the changes, and exits.

    (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)
    )        

    The above can be downloaded here:

    files/29401_29500/29471/file_29471.lsp - rename to remap_color_value.lsp. Appload it. Type remap_color_attrval [Enter] at the "Command:" prompt.
           

    The second version - meant to operate on non-opened drawing files

    This version parallels the one above but uses calls that are specific to AutoCAD Electrical (tested in ACE2008-2010 only). It is a bit longer and not reproduced here. Download a copy at this link:

    files/29401_29500/29481/file_29481.lsp - rename to remap_color_value_notactive.lsp. Appload it. Type remap_color_attrval_projectwide [Enter] at the "Command:" prompt.

    The advantage of this second version is that the processing can be done on multiple drawings without having to open each in turn.

     

    0 Comment | Add Comment Controlling the Machine >

Comments



You must be logged in to post a comment.

Subscribe to Blog

Want to keep up with the latest? Subscribe to the RSS feed today.

RSS

Tags

You must be logged in to add a tag.

Send to a Peer

You must login to share pages.

Feedback

Tell us what you think of the site.

Send Feedback