Visual LISP, AutoLISP and General Customization
cancel
Showing results forย 
Showย ย onlyย  | Search instead forย 
Did you mean:ย 

Add same value to text, mtext, leader, block attribute

17 REPLIES 17
SOLVED
Reply
Message 1 of 18
mruPRQUJ
885 Views, 17 Replies

Add same value to text, mtext, leader, block attribute

Hi, I wonder if one lisp can be created, the requirements are below,

 

1. to add same value to text, mtext, leader, block attribute,

for example, add "1" to the following 5, 10, 15, 50, 100, they will be changed to 6, 11, 16 , 51, 101

2. to minus same value to text, mtext, leader, block attribute,

for example, minus "2" to the following 5, 10, 15, 50, 100, they will be changed to 3, 8, 13 , 48, 98.

 

Thank you very much in advance. ๐Ÿ™‚

Labels (4)
17 REPLIES 17
Message 2 of 18
komondormrex
in reply to: mruPRQUJ

hi,

that's it

 

 

(defun c:add_value (/ value add_value check_list)
  	(setq add_value +1)	;	to add 1
;;; (setq add_value -2)	;	to substract 2
  	(setq check_list '("5" "10" "15" "50" "100"))
	(vlax-map-collection (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object)))
		'(lambda (object)
		   	(cond
				(
					(and
					  	(member (vla-get-objectname object) '("AcDbText" "AcDbMText" "AcDbMLeader"))
						(member (setq value (vla-get-textstring object)) check_list)
					)
				 		(vla-put-textstring object (+ add_value (atoi value)))
				)
				(
				 	(and
					  	(= "AcDbBlockReference" (vla-get-objectname object))
						(minusp (vlax-get object 'hasattributes))
					)
				 		(foreach attribute (vlax-invoke object 'getattributes)
							(if (member (setq value (vla-get-textstring attribute)) check_list)
							  	(vla-put-textstring attribute (+ add_value (atoi value)))
							)
						)
				)
				(
				 	t
				)
			 )
		)
	)
  	(vla-regen (vla-get-activedocument (vlax-get-acad-object)) acactiveviewport) 
  	(princ)
)

 

 

Message 3 of 18
calderg1000
in reply to: mruPRQUJ

Regards @mruPRQUJ 

Try this code

(defun c:edt (/ n as opc s tx atb)
    (setq n (getreal "\nEnter Numeric Value: "))
    (Initget 1 "A S")
    (setq as (getkword "Select Adiction/Substraction [A/S]:<A>"))
    (if (= as "A")
      (setq n n)
      (setq n (* -1 n))
    )
    (while t
      (Initget 1 "T M B")
      (setq opc
             (getkword "Select Mtext/Mleader/Bloc/Escape key to Exit [T/M/B]:<T>"
             )
      )
      (if (= opc "T")
        (progn
          (setq s  (vlax-ename->vla-object (car (entsel "\nSelect Mtext: ")))
                tx
                   (vla-get-textstring
                     s
                   )
                tx
                   (+ (atof tx) n)
          )
          (vla-put-textstring s tx)
        )
      )
      (if (= opc "M")
        (progn
          (setq s  (vlax-ename->vla-object (car (entsel "\nSelect Mleader: ")))
                tx
                   (vla-get-textstring
                     s
                   )
                tx
                   (+ (atof tx) n)
          )
          (vla-put-textstring s tx)
        )
      )
      (if (= opc "B")
        (progn
          (setq s   (vlax-ename->vla-object (car (entsel "\nSelect Block: ")))
                atb (nth
                      0
                      (vlax-safearray->list
                        (vlax-variant-value (vla-getattributes s))
                      )
                    )
                tx  (vla-get-textstring
                      atb
                    )
                tx  (+ (atof tx) n)
          )
          (vla-put-textstring atb tx)
        )
      )
    )
  )

Carlos Calderon G
EESignature
>Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

Message 4 of 18
mruPRQUJ
in reply to: calderg1000

It works perfect, great job! Thank you very much all of you, have a wonderful weekend. ๐Ÿ™‚

Message 5 of 18
Netelaana
in reply to: calderg1000

This is a great lisp, but it seems to only work on blocks with only one attribute - my block has two attributes that both need to get adjusted by the same amount - is it possible to adjust all the attributes by the specified amount? Selection window would be great, like in the code below. I've  used this one before which does exactly what I need, but it only works on text objects, not attributes buried in blocks:

 

(defun c:addn ( / ss)
  (vl-load-com)
  (if (and (setq ss (ssget "P" (list (cons 0 "*text"))))
       (setq amt (getreal "\nPlease type the amount you would like to add: ")))
    (progn
      (mapcar '(lambda (z) (vla-put-textstring z (rtos (+ (atof (vla-get-textstring z)) amt) 2 2)))
          (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))))
      )
    )
  (princ)
  )

 

Message 6 of 18
mruPRQUJ
in reply to: Netelaana

Hi there,

 

I have a question for you. After load the lisp, type in "addn", is it correct? It looks like it did not work for me. Could you please provide some advice to me? thank you very much in advance. ๐Ÿ™‚

Message 7 of 18
Netelaana
in reply to: mruPRQUJ

that is the correct command - it is confusing because if you do not have a selection set already in queue it does not prompt you for it. after typing "addn" you need to do a selection window and then hit enter, and then it will prompt for the value change. Be careful though, because if you already have a selection set in memory it will go ahead and make the change to those objects.

 

If anyone has ideas on how to improve this lisp (or the thread's previous solution lisp) to work with multiple attributes/blocks, it would be greatly appreciated!

Message 8 of 18
mruPRQUJ
in reply to: Netelaana

could you please explain more about it? It looks like it did not work. 

Message 9 of 18
calderg1000
in reply to: Netelaana

Regards @Netelaana 

try this code, for blocks with multiple attributes...

 

 

;;;___
(defun c:addn_b ( / ss n)
(vl-load-com)
(setvar 'Dimzin 0)
  (if (and (setq ss (ssget"_+.:E:S"(list (cons 0 "insert"))))
       (setq n (getreal "\nPlease type the amount you would like to add: ")))
    (progn
      (mapcar '(lambda (z) (vla-put-textstring z (rtos (+ (atof (vla-get-textstring z)) n)2 2)))
          (vlax-safearray->list(vlax-variant-value(vla-getattributes(car(mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))))))))
      )
    )
  (princ)
  )

 

 

 

 


Carlos Calderon G
EESignature
>Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

Message 10 of 18
Netelaana
in reply to: calderg1000

thank you! this works great - just one hiccup, it looses precision if it sums to a whole number  (11.55+0.45 = 12.00 displays as "12"). rtos precision is set to "2" correctly as far as I can tell - what am I missing?

It would be great if it could lookup whatever precision the attribute had to begin with and keep that precision, as often the precision varies (and matters) even within the same block.

Message 11 of 18
Netelaana
in reply to: mruPRQUJ

here is my workflow:

type "ADDN"

do a selection window

Netelaana_0-1721914093747.png

hit enter

Netelaana_1-1721914187069.png

type amount to add, hit enter

Netelaana_2-1721914346160.png

all values in selection set are changed

 

Message 12 of 18
calderg1000
in reply to: Netelaana

Regards @Netelaana 

You have 2 alternatives:
1. configure in the command bar, Dimzin=0
2.Enter this line in the routine: (setvar 'Dimzin 0)


Carlos Calderon G
EESignature
>Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

Message 13 of 18
Netelaana
in reply to: calderg1000

learn something new every day....
thanks!
Message 14 of 18
mruPRQUJ
in reply to: Netelaana

It works now. There is only block attribute in the drawings, so it did not work. Thank you very much! ๐Ÿ™‚

Message 15 of 18
mruPRQUJ
in reply to: calderg1000

Hi there,

 

Could you please advise me where to add "setvar 'Dimzin 0" in the lisp? Also, it looks like it only modifies one block at a time. Is it possible to modify multiple blocks at one time? Thank you very much in advance! ๐Ÿ™‚

Message 16 of 18
calderg1000
in reply to: mruPRQUJ

Regards @mruPRQUJ 

Check message 6, where the routine has already been edited...


Carlos Calderon G
EESignature
>Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

Message 17 of 18
mruPRQUJ
in reply to: calderg1000

Hi there,

 

Great!

One more question for you. It looks like it only modifies one block at a time. Is it possible to modify multiple blocks at one time? Thanks.

Message 18 of 18
calderg1000
in reply to: mruPRQUJ

Regards @mruPRQUJ 

Try this code...

 

 

;;;___
(defun c:add_n ( / sn n i j)
  (vl-load-com)
  (setvar 'Dimzin 0)
  (if (and (setq sn(vl-remove-if 'listp (mapcar 'cadr (ssnamex (ssget'((0 . "insert"))))))
        n (getreal "\nPlease type the amount you would like to add to the attribute: "))
           )
      (foreach i sn
      (mapcar '(lambda (j) (vla-put-textstring j (rtos (+ (atof (vla-get-textstring j)) n)2 2)))
          (vlax-safearray->list(vlax-variant-value(vla-getattributes(vlax-ename->vla-object i)))))
      )
    )
  (princ)
  )

 

 


Carlos Calderon G
EESignature
>Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report