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

Find and replace unique attributes using script & LISP?

10 REPLIES 10
SOLVED
Reply
Message 1 of 11
Anonymous
5897 Views, 10 Replies

Find and replace unique attributes using script & LISP?

I have a “PART” block with attributes tags “PARTMARK” and “SHEAREDFROM” inserted 5 to 10 times in 10 to 50 drawings.

The “PARTMARK” value is always a unique number entered in autocad.

The “SHEAREDFROM” value is calculated in Excel and exported to a “SHEARED.txt” file.

In each “PART” block I need to replace the “SHEAREDFROM” value based on the unique “PARTMARK” value, using a script that uses (or incorporates) the values from the “SHEARED.txt” file.

 

So far the only solution I’ve come up with is having a unique value for the “SHEAREDFROM” and –attedit to change the values in a script. However if the script doesn’t encounter a "K20-1S" in the first drawing it errors out and doesn’t proceed to the next. Below is an example of my rudimentary script.

 

(command "-attedit" "n" "n" "PART" "SHEAREDFROM" "K20-1S" "K20-1S" "8-60x192")

(command "-attedit" "n" "n" "PART" "SHEAREDFROM" "K20-2S" "K20-2S" "2-60x144")

(command "-attedit" "n" "n" "PART" "SHEAREDFROM" "K20-3S" "K20-3S" "2-60x132")

(command "-attedit" "n" "n" "PART" "SHEAREDFROM" "K20-4S" "K20-4S" "4-60x120")

(command "-attedit" "n" "n" "PART" "SHEAREDFROM" "K20-5S" "K20-5S" "0.7-60x120")

 

I’m assuming I need LISP to do this, but my LISP skills are limited. I think I would need a setq for each unique shearedfrom value (setq K20-1S (“8-60x192”))? Then somehow search each drawing for the unique shearedfrom value to replace.?

10 REPLIES 10
Message 2 of 11
Anonymous
in reply to: Anonymous

Hi,

 

Your problem doesn't seem too complicated, although it is not very clear (at least not for me) what do you want to do. For example I don't see why do you need to export Excel to TXT, when you could just save the Excel file as CSV and so it will be as easy to read as the TXT file!

 

Maybe you can find another way to explain what you want to achieve and then maybe I could help you.

 

I think also that scripts are not suitable for opening and closing files and of course you can not have error trapping in scripts, so if something is missing, the script will stop.

 

 

 

Message 3 of 11
stevor
in reply to: Anonymous

My guess is that you first find, modify, or make, a lsp routine to search for the presence if INSERTs of the BLOCK named 'PART' and then issue your edit commands.

 

If that fails, then you can change the ATT values explicitly: there are example in this forum.

S
Message 4 of 11
Anonymous
in reply to: Anonymous

I need to batch modify multiple attributes in multiple drawings using scriptpro.

I extract all the attributes from all my PART blocks to Excel to calculate the SHEAREDFROM value. I now have about 50 SHEAREDFROM values, in excel, that I need to import back into each autocad drawing to the proper PART block.

 

 

Message 5 of 11
Anonymous
in reply to: stevor

stevor,

Correct. I'm looking to find the presence of the block PART. Then find the unique partmark value and change the shearedfrom value based on the partmark match. All lisp examples I found dont have the option to add 50 or so shearedfrom values to it for evaluation.

 

 

 

 

Message 6 of 11
Anonymous
in reply to: stevor

Below is the closest LISP I could find to solve my problem. However, I need it to search for a specific attribute VALUE in addition to the TAGNAME. Then change that VALUE to a new one. ei......."40-60x120".

Any suggestions on how to change this LISP to search for a specific value?

 

 

 

;; T_Force_Attributes.lsp
;; New : 03-Apr-2009
;; Revision: 2.0
;; Function:
;; Place 'constants' into certain attributes..in certain blocks.
;; Revised: 15-June-2009, added Attribute Level (for multiple Tags, with 'same' Tagname)
;;

 (vl-load-com)

;; ******************************************** TUNING REQUIRED HERE *********************************

;; BLOCKNAME TO SEARCH FOR:
 (setq BLKNAME "XXXX"); block to find

;; ATTRIBUTE TAGNAME TO SEARCH FOR:
 (setq TAGNAME "XXXX"); Attribute Tagname to find
 (setq TAGNAME2 "")

;; NEW VALUE TO PLACE INTO ATTRIBUTE/TAG (treated as a String in all cases)
 (setq VALUE "XXXX"); value to store into attribute
 (setq VALUE2 "")

;; IF you need to 'dig' into attribute tagname, AND there are more than (1) attribute by the
;;   SAME Tagname, change the next line .. to use 'which' attribute. Values = 1 thru 5
 (setq WHICH-TAGNAME 1); IF = 2, use 2nd Attribute by the 'same' Tagname


;; **************** END OF TUNING *****************


 (defun stuff_atts () ; place data into title block attributes..
  (setq ss nil TagFlag 0)
  (setq ss (ssget "X" (list (cons 2 BLKNAME))))
  (if ss
   (progn
    (setq blk (vlax-ename->vla-object (ssname ss 0)))
     (if (safearray-value (setq atts (vlax-variant-value (vla-getattributes blk))))
      (progn
       (setq atts (vlax-safearray->list (vlax-variant-value (vla-getattributes blk))))
        (foreach att atts
         (setq Tag (vla-get-tagstring att))
           (if (= Tag TAGNAME2)
               (vla-put-textstring att VALUE2)
           ); if
           (if (= Tag TAGNAME)
            (progn
             (setq TagFlag (+ TagFlag 1))
             (if (and (= TagFlag 1)(= WHICH-TAGNAME 1))
               (vla-put-textstring att VALUE)
             ); if
             (if (and (= TagFlag 2)(= WHICH-TAGNAME 2))
               (vla-put-textstring att VALUE)
             ); if
             (if (and (= TagFlag 3)(= WHICH-TAGNAME 3))
               (vla-put-textstring att VALUE)
             ); if
             (if (and (= TagFlag 4)(= WHICH-TAGNAME 4))
               (vla-put-textstring att VALUE)
             ); if
             (if (and (= TagFlag 5)(= WHICH-TAGNAME 5))
               (vla-put-textstring att VALUE)
             ); if
            ); progn
           ); if
        ); foreach
      ); progn
     ); if
    ); end progn
   ); end if
 ); function

;; MAIN
; (command "_zoom" "E")
; (setvar "clayer" "0")
 (stuff_atts) ; place data into title block
 (setq ss nil)
 (princ)

Message 7 of 11
Kent1Cooper
in reply to: Anonymous

An approach you might take....  Consider this list of paired values:

 

(setq sublist '("A" 1 "B" 2 "C" 3 "D" 4 "E" 5))

 

You can get the second value paired with a given first value this way:

 

(nth (1+ (vl-position "A" sublist)) sublist)


returns 1

 

(nth (1+ (vl-position "D" sublist)) sublist)


returns 4

 

In your case, if I'm interpreting your original post correctly, you could have a list of paired old values and new values to substitute, like this:

 

(setq sublist
  '(
    "K20-1S" "8-60x192"
    "K20-2S" "2-60x144"
    "K20-3S" "2-60x132"
    "K20-4S" "4-60x120"
    "K20-5S" "0.7-60x120"
  ); end list
); end setq

Then you could have an all-encompassing Attribute-value substitution portion something like this:

 

....
  (setq Tag (vla-get-tagstring att))
  (if (member Tag sublist)
    (vla-put-textstring
      att

(nth

(1+

(vl-position Tag sublist)

); end 1+

sublist

); end nth

); end vla-put ); end if ....

That should replace any old Attribute value that's in the list with the appropriate new value; if the Attribute's value isn't in the list, it wouldn't do anything with it.  You wouldn't need to build separate lines of code for every possibility, or separate variables for all possible new and old values.  And as you enlarge or reduce the number of possibilities, you can just change the contents of the substitution list, without having to do anything else to the code like add or remove lines for different possibilities.


@Anonymous wrote:

.... I need it to search for a specific attribute VALUE in addition to the TAGNAME. Then change that VALUE to a new one. ei......."40-60x120".

....


 

[Edited 06/09/10 10:00-ish EDST US, to simplify code above]

Kent Cooper, AIA
Message 8 of 11
Anonymous
in reply to: Kent1Cooper

Thanks Kent. I think the sublist is exactly what i need since a know the unique value I want to replace. Let me work with what you gave me and see what I can come up with. My programming skills are very bad and I know just enough to be dangerous, so it may take me awhile, but I'll post back if I figure it out. Thanks again........

Message 9 of 11
Anonymous
in reply to: Anonymous

I solved it by purchasing ToolPac 13.0 from Dotsoft. It has "Attribute Value Populate" which is a perfect tool for changing other attributes in a block based on finding a "KEY"  tagname/column that occurs in autocad and excel, respectively.

So I change my values in excel and import them back into autocad. Its a perfect tool with the exception that it cannot be scripted to run on multiple drawings. 

Message 10 of 11
kameron1967
in reply to: Kent1Cooper

If we wanted to replace an attribute value (123) based on the value of another attribute (JANUARY) within the same ABC.DWG block to 456 - how do you write that in your code?  Any suggestion is appreciated.

Message 11 of 11
stevor
in reply to: Anonymous

As long as you took that route, keep watching, as the Dotsoft program probably can be 'scripted' to do multiple dwgs.

S

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

Post to forums  

Autodesk Design & Make Report

”Boost