Author Topic: Increment value from attribute from blocks selected  (Read 3595 times)

0 Members and 1 Guest are viewing this topic.

mobile

  • Guest
Increment value from attribute from blocks selected
« on: December 08, 2015, 12:49:32 PM »
Hi everybody, This is my firs post :)
My question is..It's possible to implement this lisp? Preface: I am just starting out with lisp. I tried this but I did not succeed :-(
I'll explain, I will select some dynamic blocks, then I insert a number "N" (es.1,2,3..) to increment an attribute called "PO", then the lisp add the value that I've inserted "N" at the value contained in the attribute "PO" before...Example: Block1(attribute"PO":34 -->N=2 -->attribute"PO":36), Block2(attribute"PO":25 -->N=1 -->attribute"PO":26)

thank you!

This is the lisp that I would like to modify:
Code: [Select]
;;  Room Number Update by CAB 03/21/2005
;;  Update selected blocks with attribute "ROOM#"
;;  with user entered room number
;;  NOTE: Very little error checking
;;
;;   Modified a subroutine by Jeff Mishler 1/11/05
;;
(defun c:rm#update (/ rm# attname ss count blk atts2 att2 att1 bc)
  (vl-load-com)

  (setq rm# (getstring t "\nEnter new room number: ")
        attname "ROOM#"  ; uppercase only
        bc 0) ; block counter

  (prompt "\n***  Select blocks to change room number.  ***")
  ;;  get only blocks with attributes
  (if (and rm#
           (not (eq rm# ""))
           (setq ss (ssget (list '(0 . "INSERT") '(66 . 1))))
      )
    (progn
      (setq count -1)
      (while (< (setq count (1+ count)) (sslength ss))
        (setq blk   (vlax-ename->vla-object (ssname ss count))
              atts2 (vlax-invoke blk "getattributes")
        )
        (foreach att2 atts2
          (if (= attname (vla-get-tagstring att2))
            (progn
              (vla-put-textstring att2 rm#)
              (vla-update att2)
              (vla-update blk)
              (setq bc (1+ bc))
            )
          )
        )
      )
      (prompt (strcat "\n-=< " (itoa bc) " blocked updated  >=-"))
    ) ; progn
    (prompt "\n---  No blocks Updated  ---")
  ) ; endif
  (princ)
) ; defun
(prompt "\nRoom Number Update Loaded, Enter Rm#Update to run.")
(princ)

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Increment value from attribute from blocks selected
« Reply #1 on: December 08, 2015, 03:18:34 PM »
Here's a quick routine to increment attribute values. Welcome to the swamp :)
Code - Auto/Visual Lisp: [Select]
  1. (defun c:inc (/ _ss2list i str)
  2.   (defun _ss2list (ss / n out)
  3.     (if (= (type ss) 'pickset)
  4.       (repeat (setq n (sslength ss))
  5.         (setq out (cons (vlax-ename->vla-object (ssname ss (setq n (1- n)))) out))
  6.       )
  7.     )
  8.   )
  9.   (if (setq i (getint "\nEnter value to increment by: "))
  10.     (foreach blk (_ss2list (ssget (list '(0 . "INSERT") '(66 . 1))))
  11.       (foreach att (vlax-invoke blk "getattributes")
  12.         (setq str (vla-get-textstring att))
  13.         (if (or (= 'real (type (read str))) (= 'int (type (read str))))
  14.           (vla-put-textstring att (vl-princ-to-string (+ (read str) i)))
  15.         )
  16.       )
  17.     )
  18.   )
  19.   (princ)
  20. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

mobile

  • Guest
Re: Increment value from attribute from blocks selected
« Reply #2 on: December 08, 2015, 04:19:11 PM »
Wow thank you! It works :-) :-)

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Increment value from attribute from blocks selected
« Reply #3 on: December 08, 2015, 11:07:40 PM »
 :-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

mobile

  • Guest
Re: Increment value from attribute from blocks selected
« Reply #4 on: December 09, 2015, 01:14:35 PM »
Hi ronjonp,
I have a problem whit your lisp :cry:...I have a dynamic block, and when I remumerate th attriute "PO" whit your lisp, it renumerate all the attribure, and so will increment all the attribute, and not only the attribute "PO"

See the problem...


I start the lisp, and I apply the increment by 3:


In the correct mode...:


Could you help me another time? Sorry...
Thank you in advance

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Increment value from attribute from blocks selected
« Reply #5 on: December 09, 2015, 01:44:46 PM »
Try this:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:IncPO (/ _ss2list i str)
  2.   (defun _ss2list (ss / n out)
  3.     (if (= (type ss) 'pickset)
  4.       (repeat (setq n (sslength ss))
  5.         (setq out (cons (vlax-ename->vla-object (ssname ss (setq n (1- n)))) out))
  6.       )
  7.     )
  8.   )
  9.   (if (setq i (getint "\nEnter value to increment by: "))
  10.     (foreach blk (_ss2list (ssget (list '(0 . "INSERT") '(66 . 1))))
  11.       (foreach att (vlax-invoke blk 'getattributes)
  12.         (if
  13.           (and
  14.             (= "PO" (strcase (vla-get-tagstring att)))
  15.             (setq str (vla-get-textstring att))
  16.             (numberp (read str))
  17.           )
  18.           (vla-put-textstring att (vl-princ-to-string (+ (read str) i)))
  19.         )
  20.       )
  21.     )
  22.   )
  23.   (princ)
  24. )

mobile

  • Guest
Re: Increment value from attribute from blocks selected
« Reply #6 on: December 10, 2015, 12:48:29 PM »
Thank you! Now It works exactly :)