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

Looking for a Lisp to swap Attribute values

16 REPLIES 16
Reply
Message 1 of 17
tsbrown
1311 Views, 16 Replies

Looking for a Lisp to swap Attribute values

I would like to swap the attribute value with a preset value

 

Example

attribute value  NAME

replace with BROWN

 

I know that the find command would work for this, but I'm trying to save time by not entering that info all the time.

 

 

 

 

 

16 REPLIES 16
Message 2 of 17
pbejse
in reply to: tsbrown


@tsbrown wrote:

I would like to swap the attribute value with a preset value

 

Example

attribute value  NAME

replace with BROWN

 

I know that the find command would work for this, but I'm trying to save time by not entering that info all the time.

 


Are you refering to  an exact  string value match?

It wil be faster search if you are targeting a specific block name/Attribute tag otherwise a code will search the entire drawing database to find the target. 

 

 

 

 

Message 3 of 17
pbejse
in reply to: pbejse

Have time to kill

 

Specific Block name/TAG/String to replace

(defun repn (bn tg os ns / ss i e )
      	(if (setq ss (ssget "_X" (list '(0 . "INSERT")'(66 . 1)
                              (cons 2 (strcat bn ",`*U*")))))
      				(repeat (setq i (sslength ss))
            		(setq e (vlax-ename->vla-object (ssname ss (setq i (1- i)) )))
              		(cond ((and
                            (Eq (strcase (vla-get-effectivename e)) (strcase bn))
                            (foreach itm (vlax-invoke e 'GetAttributes)
                                  (if (and (eq (vla-get-tagstring itm) (strcase tg))
                                           (eq (strcase (vla-get-textstring itm)) (strcase os)))
                                      	(vla-put-textstring itm ns))))))
                    )
          )(princ)
      )

 

(repn "Blockname" "TAG" "NAME" "BROWN")

 

(repn "Blockname" <----- Block name

          "TAG" <-------------- Attribute TAG

          "NAME" <------------ String to replace

          "BROWN" <------- New string

 

OR for multiple block names and TAG

 

(defun repnL (bnl tgL os ns / ss i e )
      	(if   (and (listp bnl)(listp tgl)
                   (setq ss (ssget "_X" (list '(0 . "INSERT")'(66 . 1)
                              (cons 2 (apply 'strcat (cons "`*U*,"
														(mapcar '(lambda (k) (strcat k ",")) bnl))))))))
      				(repeat (setq i (sslength ss))
            		(setq e (vlax-ename->vla-object (ssname ss (setq i (1- i)) )))
              		(cond ((and
                            (member  (strcase (vla-get-effectivename e)) (mapcar 'strcase bnl))
                            (foreach itm (vlax-invoke e 'GetAttributes)
                                  (if (and (member (vla-get-tagstring itm) (mapcar 'strcase tgl))
                                           (eq (strcase (vla-get-textstring itm)) (strcase os)))
                                      	(vla-put-textstring itm ns))))))
                    )
          )(princ)
      )

 

(repnL '("Blockname1" "Blockname2") '("TAG" "OTHERTAG") "NAME" "BROWN")

 

(repnL '("Blockname1" "Blockname2")  <----- block name list

          '("TAG" "OTHERTAG") <-------------- Attribute TAG

          "NAME" <------------ String to replace

          "BROWN" <------- New string

 

 

And Brute Force method, regardless of blockname and attribute TAG. case senstive input for string to find

 

(defun c:repnbf (/ ns os ss i e )
     (if (and
               (setq os (getstring T "\nEnter attribute string to replace: "))(not (eq os ""))
      				 (setq Ns (getstring "\nEnter new string: "))(not (eq ns ""))
     					 (setq ss (ssget "_X" '((0 . "INSERT")(66 . 1))))
               )
      			(repeat (setq i (sslength ss))
            		(setq e (vlax-ename->vla-object (ssname ss (setq i (1- i)) )))
	                            (foreach itm (vlax-invoke e 'GetAttributes)
                                  (if (eq (strcase (vla-get-textstring itm)) os)
                                      	(vla-put-textstring itm ns)))
                  )
          )(princ)
      
      )

 

Command: REPNBF

Enter attribute string to replace: NAME

Enter new string: BROWN

 

HTH

 

Message 4 of 17
tsbrown
in reply to: pbejse

Thanks for the help

 

I copy and loaded the first command and I get a error:

 

Command: _appload BROWN.lsp successfully loaded.

Command: too many arguments

Command:

 

I have attach the lisp file

 

I'm running 2012 if that makes a difference.

Message 5 of 17
pbejse
in reply to: tsbrown


@tsbrown wrote:

Thanks for the help

 

I copy and loaded the first command and I get a error:

 

Command: _appload BROWN.lsp successfully loaded.

Command: too many arguments

Command:

 

I have attach the lisp file

 

I'm running 2012 if that makes a difference.


 

tsbrown, 

Do not save the code as i had posted it. I'll save a copy for you and attach it here. but first i need to ask you a couple of questions:

 

What is the name of the block that shows the NAME string?

What is the TAG string of that particular attribute?

 

Of the three snippets i posted, the first and second one requires an argument for Block name/Tag name / string to replace/ new string

 

If you are not sure as to what im refering to. use the 3rd code, that will ask you for "what is" and "what will", but the whole point of your posts is saving time by not typing the info.

 

We can include the info on the 3rd code [hard-coded] , or use the 1st or 2nd code and supply the info on the sub-function and it will run upon loading.

 

Hence i need the info from you or you can type it yourself if its a confidentiality issue 🙂

 

the last line of the "saved" brown.lsp file should be in this format

 

(brown "Blockname" "TAG" "NAME" "BROWN") 

 

Granting the info are correct. or like i've said, hard-coded onto the routine. You can post an example of the block, sans "confidential" info. or provide me the block name and/or TAG 


What say you tsbrown?

 

Message 6 of 17
pbejse
in reply to: pbejse

Here's another option:

 

Batch find and replace by Lee Mac  or   Batch Attribute Editor by Lee Mac

 

Download the file and save. You may need to type in the info, YES, but one time only. the programs on those link will processed multiple drawing in one go.

 

HTH

 

Message 7 of 17
tsbrown
in reply to: pbejse

What is the name of the block that shows the NAME string?  AVI_TB_INFO_B

What is the TAG string of that particular attribute? ENGINEERED-BY

 

Thanks again for the help

Message 8 of 17
tsbrown
in reply to: tsbrown

"AVI_TB_INFO_B" <----- Block name

"ENGINEERED-BY" <-------------- Attribute TAG

"ENGINEER NAME" <------------ String to replace

"BROWN" <------- New string

 

 

Message 9 of 17
pbejse
in reply to: tsbrown

This time save the file as you see it here within the code tags. run upon loading but maintaining the sub-function for you to able use in thefuture.

 

(defun brown (bn tg os ns / ss i e )
      	(if (setq ss (ssget "_X" (list '(0 . "INSERT")'(66 . 1)
                              (cons 2 (strcat bn ",`*U*")))))
      				(repeat (setq i (sslength ss))
            		(setq e (vlax-ename->vla-object (ssname ss (setq i (1- i)) )))
              		(cond ((and
                            (Eq (strcase (vla-get-effectivename e)) (strcase bn))
                            (foreach itm (vlax-invoke e 'GetAttributes)
                                  (if (and (eq (vla-get-tagstring itm) (strcase tg))
                                           (eq (strcase (vla-get-textstring itm)) (strcase os)))
                                      	(vla-put-textstring itm ns))))))
                    )
          )(princ)
      )

(brown "AVI_TB_INFO_B" "ENGINEERED-BY" "ENGINEER NAME" "BROWN")

 BUT if you are going to use this exclusively for the info you provided [HARD-CODED] this will run upon loading as well [save this as another file if you will.

 

(if (setq ss (ssget "_X" '((0 . "INSERT")(66 . 1)
                              (2 . "AVI_TB_INFO_B,`*U*"))))
      		(repeat (setq i (sslength ss))
            		(setq e (vlax-ename->vla-object (ssname ss (setq i (1- i)) )))
              		(cond ((and
                            (Eq (strcase (vla-get-effectivename e)) "AVI_TB_INFO_B")
                            (foreach itm (vlax-invoke e 'GetAttributes)
                                  (if (and (eq (vla-get-tagstring itm) "ENGINEERED-BY")
                                           (eq (strcase (vla-get-textstring itm)) "ENGINEER NAME"))
                                      	(vla-put-textstring itm "BROWN"))))))
                    )
          )

 

 Question: if the string is not "ENGINEER NAME" what then? leave it as it is?

 

HTH

Message 10 of 17
tsbrown
in reply to: pbejse

could you make it so what ever value it will change to the new value.

 

 

Message 11 of 17
tsbrown
in reply to: tsbrown

How do I make this into a lisp so I can just type brown into the command line

Message 12 of 17
pbejse
in reply to: tsbrown


@tsbrown wrote:
.....could you make it so what ever value it will change to the new value....

.....How do I make this into a lisp so I can just type brown into the command line....


 

(defun c:brown (/ ss i e )
  (vl-load-com)
      	(if (setq ss (ssget "_X" '((0 . "INSERT")(66 . 1)
                              (2 . "AVI_TB_INFO_B,`*U*"))))
      		(repeat (setq i (sslength ss))
            		(setq e (vlax-ename->vla-object (ssname ss (setq i (1- i)) )))
              		(if (Eq (strcase (vla-get-effectivename e)) "AVI_TB_INFO_B")
                            (foreach itm (vlax-invoke e 'GetAttributes)
                                  (if (eq (vla-get-tagstring itm) "ENGINEERED-BY")
                                      	(vla-put-textstring itm "BROWN")))
                          )
                  )
          (princ "\n<<<No AVI_TB_INFO_B blocks found>>>>")
                 )(princ)
      )

 HTH

 

Message 13 of 17
tsbrown
in reply to: pbejse

It works great Smiley Very Happy

Message 14 of 17
pbejse
in reply to: tsbrown


@tsbrown wrote:

It works great Smiley Very Happy


Glad we hat that settled. I over did it on my previous posts. 🙂

 

Cheers

 

Message 15 of 17
devitg
in reply to: pbejse

Why not to use ATTOUT , fix the file as need, and then attin ??

Message 16 of 17
pbejse
in reply to: devitg


@devitg wrote:

Why not to use ATTOUT , fix the file as need, and then attin ??


 

Why not? simply put. one too many user interaction.  besides i assume the datas on the block are not always the same for every drawing file.

 

If i had known from the get-go the exact needs of the OP i wouldnt have posted all those codes and give the OP what he wants from the start  :D, serves me right over thinking the condition for a simple task. 

 

Anyhow,  someone else might find a use for those snippets

 

Cheers

Message 17 of 17
devitg
in reply to: pbejse

It was a simple "Why"  not a "DO".

Your answer fullfill my question.

 

 

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

Post to forums  

Autodesk Design & Make Report

”Boost