Replacing attribute value

Discussion in 'AutoCAD' started by Brett Harbourne, Aug 9, 2004.

  1. I have a created a routine, (with some help from 4d-technologies), that will
    replace a value in an attribute with the current date. This is for use in
    titleblocks. The name of the block is "issuenotes" and the tag name is
    "date". The code below is what I have so far, but I cannot work out why it
    will not work. I'm sure it used to work.
    Could someone please cast their eye over this code and let me know why it is
    not working as intended.
    Also, is it possible to specify the exact tag value to change instead of the
    tag name? If so, how could this be achieved?

    Thanks
    Brett

    (defun c:rawd ()
    (setq ss (ssget "x" '((0 . "insert") (2 . "issuenotes"))))
    (setq ename (ssname ss 0))
    (setq tag "date")
    (setq isdate (rtos (getvar "cdate") 2 4)
    day (substr isdate 7 2)
    mo (substr isdate 5 2)
    yr (substr isdate 1 4))
    (setq value (strcat day "." mo "." yr))
    (bl_writeattr)
    )

    (defun BL_WriteAttr ( / aname entl )
    (setq aname (BL_FindAttr ename tag)) ; Search for attribute
    (if aname
    (progn (setq entl (entget aname)
    entl (subst (cons 1 value) (assoc 1 entl) entl))
    (entmod entl)
    (entupd ename)
    )
    (setq ename nil)
    )
    ename
    )

    (defun BL_FindAttr( ename tag / entl en cnt More _tag aname More )
    (setq en "ATTRIB"
    aname nil
    tag (strcase tag)
    More T)
    (while More (setq ename (entnext ename)
    entl (entget ename)
    en (LI_item 0 entl))
    (if (= en "ATTRIB")
    (progn (setq _tag (strcase (LI_item 2 entl)))
    (if (= tag _tag)
    (setq aname ename
    More nil)
    )
    )
    (setq More nil)
    )
    )
    aname
    )

    (defun LI_item (n alist)
    (cdr (assoc n alist))
    )
     
    Brett Harbourne, Aug 9, 2004
    #1
  2. Brett,

    The code looks fine. Do you have multiple "issuenotes" inserts in your file - you are only modifying the first one returned from the ssget function (i.e. - (ssname ss 0)). You may have additional titleblocks in other paperspaces - the (ssget "x" ...) returns all matching entities in the drawing, not just the current space.

    Other possibilities - your block is no longer named "issuenotes", you have renamed your tag (i.e. - no longer "date"), the attribute entity is on a locked layer - or possibly, you have multiple tags with the name "date", the first of which is invisible or on a frozen/offed layer.

    You can modify your BL_FindAttr to return only the first attribute that matches a value by comparing the "to find" value to the text value of the attribute rather than the tag value - that is, use 1 instead of 2.

    (defun BL_FindAttr_byval (ename val / entl en cnt More _val aname More)
    (setq en "ATTRIB"
    aname nil
    val (strcase val)
    More T
    )
    (while More
    (setq ename (entnext ename)
    entl (entget ename)
    en (LI_item 0 entl)
    )
    (if (= en "ATTRIB")
    (progn (setq _val (strcase (LI_item 1 entl)))
    (if (= val _val)
    (setq aname ename
    More nil
    )
    )
    )
    (setq More nil)
    )
    )
    aname
    )

    I haven't tested this - also this (and your BL_FindAttr) function will return the first attribute that it finds that matches the string (or tag) value. You could also use wcmatch rather than = (wcmatch _val val) to add some flexibility.

    Peter
     
    petersciganek, Aug 9, 2004
    #2
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.