Change an attribute value by a user given amount

Discussion in 'AutoCAD' started by John Scott, Apr 23, 2004.

  1. John Scott

    John Scott Guest

    I have searched the newsgroup and found some help with this, but I am still
    stumped. I have created a lisp routine for inserting grade boxes and it
    works great. It uses an attribute for the box. Now, I would like to write a
    routine that will allow the user to specify an elevation change (positive
    and negative) and have the attribute update by that amount. I am not looking
    for someone to write a routine for me, just to point me in the right
    direction. I thoroughly enjoy writing the routines and want to learn as much
    as I can.

    John
     
    John Scott, Apr 23, 2004
    #1
  2. entsel - user select block
    entget - get block entity and store
    entget, entnext - get attribute entity and store
    atof - change existing value to real number
    getreat - user input elevation change
    add change to existing value
    entmod - change stored attribute entity
    entupd - update block
     
    Alan Henderson, Apr 23, 2004
    #2
  3. John Scott

    John Scott Guest

    Thanks, Alan. I will work on it this morning. I really appreciate your
    assistance. I found some code written for changing attributes by a constant,
    predetermined value and was trying to modify that. However, I did not know
    what some of the commands were until you posted this info. THANKS!

    John
     
    John Scott, Apr 23, 2004
    #3
  4. John Scott

    Murph Guest

    Shouldn't it be getreal instead of getreat (i know it's FRIDAY)
    also getstring for users input.

    Murph
     
    Murph, Apr 23, 2004
    #4
  5. Sorry about the typo.... GetReal is my favorite.... I keep telling myself
    that everytime I write any program.
     
    Alan Henderson, Apr 23, 2004
    #5
  6. John Scott

    Murph Guest

    LOL,
    I'm a getstring guy myself.
    I like too pull the strings whenever possible.

    Murph
     
    Murph, Apr 23, 2004
    #6
  7. John Scott

    John Scott Guest

    This is what I have so far. It seems to work fine until it comes to updating
    the block and then it stops. I get this: Error: bad list in entity tail:
    "99.40" - program aborted.


    (defun C:EC ()

    (setvar "cmdecho" 0)

    (setq elev_change (getreal "\nEnter the elevation change <Enter to exit>:
    "))
    (while (/= elev_change nil)
    (setq bloc (car (entsel "\nSelect box to edit <Enter to exit>: ")))
    (while (/= bloc nil)
    (setq first_att (entnext bloc))
    (setq second_att (entnext first_att))
    (setq first_code (cdr (assoc 1 (entget first_att))))
    (setq second_code (cdr (assoc 1 (entget second_att))))
    (setq first_real (atof first_code))
    (setq second_real (atof second_code))
    (setq new_first (+ first_real elev_change))
    (setq new_second (+ second_real elev_change))
    (setq first_code (rtos new_first 2 2))
    (entmod first_code)
    (setq second_code (rtos new_second 2 2))
    (entmod second_code)
    (entupd bloc)
    (setq bloc (car (entsel "\nSelect box to edit <Enter to exit>: ")))
    )
    (setq elev_change (getreal "\nEnter the elevation change <Enter to exit>:
    "))
    )
    (princ)
    )
     
    John Scott, Apr 23, 2004
    #7
  8. John Scott

    John Scott Guest

    I can not see my first post of this so I am sorry if it is a duplicate.

    This is what I have so far. It seems to work fine until it comes to updating
    the block and then it stops. I get this: Error: bad list in entity tail

    (defun C:EC ()

    (setvar "cmdecho" 0)

    (setq elev_change (getreal "\nEnter the elevation change <Enter to exit>:
    "))
    (while (/= elev_change nil)
    (setq bloc (car (entsel "\nSelect box to edit <Enter to exit>: ")))
    (while (/= bloc nil)
    (setq first_att (entnext bloc))
    (setq second_att (entnext first_att))
    (setq first_code (cdr (assoc 1 (entget first_att))))
    (setq second_code (cdr (assoc 1 (entget second_att))))
    (setq first_real (atof first_code))
    (setq second_real (atof second_code))
    (setq new_first (+ first_real elev_change))
    (setq new_second (+ second_real elev_change))
    (setq first_code (rtos new_first 2 2))
    (entmod first_code)
    (setq second_code (rtos new_second 2 2))
    (entmod second_code)
    (entupd bloc)
    (setq bloc (car (entsel "\nSelect box to edit <Enter to exit>: ")))
    )
    (setq elev_change (getreal "\nEnter the elevation change <Enter to exit>:
    "))
    )
    (princ)
    )
     
    John Scott, Apr 23, 2004
    #8
  9. change (entmod first_code) to
    (setq first_att (subst (cons 1 first_code) (assoc 1 first_att) first_att))
    (entmod first_att)

    repeat for 2nd att
     
    Alan Henderson, Apr 23, 2004
    #9
  10. John Scott

    Jeff Mishler Guest

    Try this....the code I modified and/or added is marked by asterisk's ***.

    Jeff

    (defun C:EC (/ bloc elev_change first_att first_code first_real
    new_first new_second second_att second_code second_real)

    ;(setvar "cmdecho" 0) ; not needed, no "command" calls that would echo

    (setq elev_change (getreal "\nEnter the elevation change <Enter to exit>:
    "))
    (while (/= elev_change nil)
    (setq bloc (car (entsel "\nSelect box to edit <Enter to exit>: ")))
    (while (/= bloc nil)
    (setq first_att (entget (entnext bloc)));***
    (setq second_att (entget (entnext (cdar first_att))));***
    (setq first_code (cdr (assoc 1 first_att)));***
    (setq second_code (cdr (assoc 1 second_att)));***
    (setq first_real (atof first_code))
    (setq second_real (atof second_code))
    (setq new_first (+ first_real elev_change))
    (setq new_second (+ second_real elev_change))
    (setq first_code (rtos new_first 2 2))
    (setq first_att (subst (cons 1 first_code)(assoc 1 first_att)
    first_att));***
    (entmod first_att);***
    (setq second_code (rtos new_second 2 2))
    (setq second_att (subst (cons 1 second_code)(assoc 1 second_att)
    second_att));***
    (entmod second_att);***
    (entupd bloc)
    (setq bloc (car (entsel "\nSelect box to edit <Enter to exit>: ")))
    )
    (setq elev_change (getreal "\nEnter the elevation change <Enter to exit>:
    "))
    )
    (princ)
    )
     
    Jeff Mishler, Apr 23, 2004
    #10
  11. John Scott

    John Scott Guest

    Jeff,
    That works like a charm. Thank you VERY much for the assistance.

    John
     
    John Scott, Apr 23, 2004
    #11
  12. John Scott

    ECCAD Guest

    (defun C:EC ()
    (setvar "cmdecho" 0)
    (setq elev_change (getreal "\nEnter the elevation change <Enter to exit>:"))
    (while (/= elev_change nil)
    (setq bloc (car (entsel "\nSelect box to edit <Enter to exit>: ")))
    (while (/= bloc nil)
    (setq first_att (entnext bloc))
    (setq second_att (entnext first_att))
    (setq first_code (cdr (assoc 1 (entget first_att))))
    (setq second_code (cdr (assoc 1 (entget second_att))))
    (setq first_real (atof first_code))
    (setq second_real (atof second_code))
    (setq new_first (+ first_real elev_change))
    (setq new_second (+ second_real elev_change))
    (setq first_code (rtos new_first 2 2))

    ;;(entmod first_code) - this line a problem, is trying to 'entmod' a string at this point
    (entmod (setq first_att (subst (cons 1 new_first)(cons 1 first_code) first_att))); should fix it
    ;; same for second entity..below

    (setq second_code (rtos new_second 2 2))
    (entmod second_code)
    (entupd bloc)
    (setq bloc (car (entsel "\nSelect box to edit <Enter to exit>: ")))
    )
    (setq elev_change (getreal "\nEnter the elevation change <Enter to exit>:
    "))
    )
    (princ)
    )

    Bob
     
    ECCAD, Apr 23, 2004
    #12
  13. John Scott

    John Scott Guest

    Joe, I apologize. I forgot to thank you for the willingness to help me. I
    really appreciate it.

    John
     
    John Scott, Apr 23, 2004
    #13
  14. John Scott

    John Scott Guest

    Thanks for the help, Alan.

    John
     
    John Scott, Apr 23, 2004
    #14
  15. John Scott

    Joe Burke Guest

    John,

    No problem.

    One suggestion: think positive. This (while bloc ... is the same as (while (/= bloc
    nil).

    Joe Burke
     
    Joe Burke, Apr 23, 2004
    #15
  16. John Scott

    John Scott Guest

    Joe, Alan and Murph,
    I want to thank all of you for the help with this lisp routine. I was
    able to get the routine to work with one exception, we use a mix of single
    and double entry attribute blocks and the routine as written only worked on
    double entry blocks. However, I was able to modify the routine so that it
    works on both blocks. I have used it this morning and it is a BIG time
    saver. Thanks so much for your guidance.

    John
     
    John Scott, Apr 27, 2004
    #16
  17. John Scott

    Friptzap Guest

    I am trying to do something similar so maybe I should watch the magic at work :)
     
    Friptzap, Apr 27, 2004
    #17
  18. John Scott

    John Scott Guest

    Here is what I have. Hopefully you can get it to work for you, too. I tried
    to separate sections to compensate for the poor word wrap in Outlook. Hope
    it is easy to follow.

    John

    (defun C:EC ()

    (setvar "cmdecho" 0)

    (setq elev_change (getreal "\nEnter the elevation change <Enter to exit>:
    "))
    (while (/= elev_change nil)

    (setq bloc (car (entsel "\nSelect box to edit <Enter to exit>: ")))
    (while (/= bloc nil)

    (setq first_att (entget (entnext bloc)))
    (setq second_att (entget (entnext (cdar first_att))))
    (setq first_code (cdr (assoc 1 first_att)))
    (setq first_real (atof first_code))
    (setq new_first (+ first_real elev_change))
    (setq first_code (rtos new_first 2 2))
    (setq first_att (subst (cons 1 first_code)(assoc 1 first_att)first_att))
    (entmod first_att)

    (if (/= (setq second_code (cdr (assoc 1 second_att))) nil)

    (progn
    (setq second_real (atof second_code))
    (setq new_second (+ second_real elev_change))
    (setq second_code (rtos new_second 2 2))
    (setq second_att (subst (cons 1 second_code) (assoc 1 second_att)
    second_att))
    (entmod second_att)
    ))

    (entupd bloc)
    (setq bloc (car (entsel "\nSelect box to edit <Enter to exit>: ")))
    )
    (etq elev_change (getreal "\nEnter the elevation change <Enter to exit>: "))
    )
    (princ)
    )
     
    John Scott, Apr 27, 2004
    #18
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.