Jump to content

Single Attribute Edit LISP


tmelancon

Recommended Posts

Hello guys just need some help editing a routine I found browsing the web and would be very useful.

 

I want my CAD guys to be able to type their initials in the command line and it change the revision initials in a drawing automatically. These initials are located within titleblocks (names are random because of years of amateur CAD managers with no knowledge of "standards") So I would like to treat the block as wildcard. Then I want to search the block for the tag "REVBY" then I want the initials searched to be wildcards. Next I want those values to be replaced with the initials of the CAD operator running the code. I will customize each for their respective initials once I get it running. Check it out below. Im stuck

 

(defun changeAttribValue (ent atttag oldval newval / entl)
 (while (and ent (/= "SEQEND" (cdr (assoc 0 (setq entl (entget ent))))))
   (and (= atttag (cdr (assoc 2 entl)))
        (= oldval (cdr (assoc 1 entl))) ;<- could use WCMATCH instead
        (entmod (subst (cons 1 newval) (assoc 1 entl) entl))
        (entupd ent)
        (mapcar 'princ (list "\n" oldval " -> " newval))
   )
   (setq ent (entnext ent))
 )
)

(defun C:TJM (/ ss a attag bname oldval newval)
 (and (/= "" (setq bname "*"))
      (/= "" (setq attag "REVBY"))
      (/= "" (setq oldval "*"))
      (/= "" (setq newval "T.J.M."))
      (setq a  0
            ss (ssget "X" (list '(0 . "INSERT") '(66 . 1) (cons 2 bname)))
      )
      (repeat (sslength ss)
        (changeAttribValue (ssname ss a) attag oldval newval)
        (setq a (1+ a))
      )
 )
)

Link to comment
Share on other sites

  • Replies 20
  • Created
  • Last Reply

Top Posters In This Topic

  • tmelancon

    11

  • David Bethel

    3

  • Lee Mac

    3

  • marko_ribar

    1

Here is slight mod of your code, but I don't see why and with your version you have problems... I've only added (wcmatch) as you suggested and checking of selections for unlocked layer(s) - otherwise it may not process (changeAttribValue) function correctly with what it's supposed to do...

 

(defun changeAttribValue ( ent atttag oldval newval / entl )
 (while (and ent (/= "SEQEND" (cdr (assoc 0 (setq entl (entget ent))))))
   (and (= atttag (cdr (assoc 2 entl)))
        (wcmatch oldval (cdr (assoc 1 entl)))
        (entmod (subst (cons 1 newval) (assoc 1 entl) entl))
        (entupd ent)
   )
   (setq ent (entnext ent))
 )
)

(defun C:TJM ( / ss a attag bname oldval newval )
 (and (/= "" (setq bname "*"))
      (/= "" (setq attag "REVBY"))
      (/= "" (setq oldval "*"))
      (/= "" (setq newval "T.J.M."))
      (setq a  0
            ss (ssget "_X" (list '(0 . "INSERT") '(66 . 1) (cons 2 bname)))
      )
      (not (equal '(nil nil) (sssetfirst nil ss)))
      (setq ss (ssget "_:L"))
      (sssetfirst nil nil)
      (repeat (sslength ss)
        (changeAttribValue (ssname ss a) attag oldval newval)
        (setq a (1+ a))
      )
 )
 (princ)
)

Link to comment
Share on other sites

Hey Marko thanks for your response, I have tried the code you modified and I get not results. The only thing different is it response back with "1 found" at the command line, No changes get made to the attribute value of the REVBY tag.

 

Hey kpblc thanks for chiming in. The bname variable is wildcard because not every titleblock is named the same in every drawing. Sometimes its TBLOCK, or BLOCKT or ASDFG. This is why I jjust decided to wildcard the blockname then search for attribute tags within that block with a tag name of REVBY. Any other help would be greatly appreciated. I will continue to evaluate.

Link to comment
Share on other sites

Here is the original code that works but it prompts users to enter block name, tag name, old value, new value. This defeats the purpose. I want my guys to be able to type in their initials (im going to copy them each their own respective code) and it automatically update the REVBY initials, regardless of what the block name is, to their initials. Automatically, all in one command, no prompts. :)

 

(defun changeAttribValue (ent atttag oldval newval / entl)
 (while (and ent (/= "SEQEND" (cdr (assoc 0 (setq entl (entget ent))))))
   (and (= atttag (cdr (assoc 2 entl)))
        (= oldval (cdr (assoc 1 entl))) ;<- could use WCMATCH instead
        (entmod (subst (cons 1 newval) (assoc 1 entl) entl))
        (entupd ent)
        (mapcar 'princ (list "\n" oldval " -> " newval))
   )
   (setq ent (entnext ent))
 )
)

(defun C:CHATTRIB (/ ss a attag bname oldval newval)
 (and (/= "" (setq bname (getstring "\nBlock name: ")))
      (/= "" (setq attag (getstring T "\nTag: ")))
      (/= "" (setq oldval (getstring T "\nOld value: ")))
      (/= "" (setq newval (getstring T "\nNew value: ")))
      (setq a  0
            ss (ssget "X" (list '(0 . "INSERT") '(66 . 1) (cons 2 bname)))
      )
      (repeat (sslength ss)
        (changeAttribValue (ssname ss a) attag oldval newval)
        (setq a (1+ a))
      )
 )
)

Link to comment
Share on other sites

Maybe :

 

[b][color=BLACK]([/color][/b]defun c:revby [b][color=FUCHSIA]([/color][/b]/ by ss i en an ad f[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]setq by [color=#2f4f4f]"T.J.M."[/color][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]and [b][color=NAVY]([/color][/b]setq ss [b][color=MAROON]([/color][/b]ssget [color=#2f4f4f]"X"[/color] [b][color=GREEN]([/color][/b]list [b][color=BLUE]([/color][/b]cons 0 [color=#2f4f4f]"INSERT"[/color][b][color=BLUE])[/color][/b][b][color=BLUE]([/color][/b]cons 66 1[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
      [b][color=NAVY]([/color][/b]setq i 0[b][color=NAVY])[/color][/b]
      [b][color=NAVY]([/color][/b]while [b][color=MAROON]([/color][/b]setq en [b][color=GREEN]([/color][/b]ssname ss i[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]setq f nil
                  an [b][color=GREEN]([/color][/b]entnext en[b][color=GREEN])[/color][/b]
                  ad [b][color=GREEN]([/color][/b]entget an[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]while [b][color=GREEN]([/color][/b]= [color=#2f4f4f]"ATTRIB"[/color] [b][color=BLUE]([/color][/b]cdr [b][color=RED]([/color][/b]assoc 0 ad[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                    [b][color=GREEN]([/color][/b]and [b][color=BLUE]([/color][/b]= [color=#2f4f4f]"REVBY"[/color] [b][color=RED]([/color][/b]cdr [b][color=PURPLE]([/color][/b]assoc 2 ad[b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b]
                         [b][color=BLUE]([/color][/b]setq f T[b][color=BLUE])[/color][/b]
                         [b][color=BLUE]([/color][/b]entmod [b][color=RED]([/color][/b]subst [b][color=PURPLE]([/color][/b]cons 1 by[b][color=PURPLE])[/color][/b] [b][color=PURPLE]([/color][/b]assoc 1 ad[b][color=PURPLE])[/color][/b] ad[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                    [b][color=GREEN]([/color][/b]setq an [b][color=BLUE]([/color][/b]entnext an[b][color=BLUE])[/color][/b]
                          ad [b][color=BLUE]([/color][/b]entget an[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]if f [b][color=GREEN]([/color][/b]entupd en[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]setq i [b][color=GREEN]([/color][/b]1+ i[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

 

 

-David

Link to comment
Share on other sites

Maybe add this method for initials then only need 1 version of code.

 

(setq lst (list "tjm" "T.J.M" "tmelancon" "BIGAL")
(AT:ListSelect "Rev update" "pick your name"  12 12 "False" lst)
(setq REVBY (nth (atoi item) lst))

 

  ;; List Select Dialog (Temp DCL list box selection, based on provided list)
 ;; title - list box title
 ;; label - label for list box
 ;; height - height of box
 ;; width - width of box
 ;; multi - selection method ["true": multiple, "false": single]
 ;; lst - list of strings to place in list box
 ;; Alan J. Thompson, 09.23.08 / 05.17.10 (rewrite)
(defun AT:ListSelect (title label height width multi lst / fn fo d f)
 (setq fo (open (setq fn (vl-filename-mktemp "" "" ".dcl")) "w"))
 (foreach x (list (strcat "list_select : dialog { label = \"" title "\"; spacer;")
                  (strcat ": list_box { label = \"" label "\";" "key = \"lst\";")
                  (strcat "allow_accept = true; height = " (vl-princ-to-string height) ";")
                  (strcat "width = " (vl-princ-to-string width) ";")
                  (strcat "multiple_select = " multi "; } spacer; ok_cancel; }")
            )
   (write-line x fo)
 )
 (close fo)
 (new_dialog "list_select" (setq d (load_dialog fn)))
 (start_list "lst")
 (mapcar (function add_list) lst)
 (end_list)
 (setq item (set_tile "lst" "0"))
 (action_tile "lst" "(setq REVBY $value)")
 (setq f (start_dialog))
 (unload_dialog d)
 (vl-file-delete fn)
 (if (= f 1)
   ((lambda (s / i s l)
      (while (setq i (vl-string-search " " s))
        (setq l (cons (nth (atoi (substr s 1 i)) lst) l))
        (setq s (substr s (+ 2 i)))
      )
      (reverse (cons (nth (atoi s) lst) l))
    )
     item
   )
 )
)

Link to comment
Share on other sites

Hey David how would I include another TAG variation but within the same code? So for instance i want the code to look at tags "REVBY" and "REVIBY"... I am seeing different variations now.. Thanks

Link to comment
Share on other sites

1 way would be :

[b][color=BLACK]([/color][/b]defun c:revby [b][color=FUCHSIA]([/color][/b]/ by ss i en an ad f[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]setq by [color=#2f4f4f]"T.J.M."[/color][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]and [b][color=NAVY]([/color][/b]setq ss [b][color=MAROON]([/color][/b]ssget [color=#2f4f4f]"X"[/color] [b][color=GREEN]([/color][/b]list [b][color=BLUE]([/color][/b]cons 0 [color=#2f4f4f]"INSERT"[/color][b][color=BLUE])[/color][/b][b][color=BLUE]([/color][/b]cons 66 1[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
      [b][color=NAVY]([/color][/b]setq i 0[b][color=NAVY])[/color][/b]
      [b][color=NAVY]([/color][/b]while [b][color=MAROON]([/color][/b]setq en [b][color=GREEN]([/color][/b]ssname ss i[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]setq f nil
                  an [b][color=GREEN]([/color][/b]entnext en[b][color=GREEN])[/color][/b]
                  ad [b][color=GREEN]([/color][/b]entget an[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]while [b][color=GREEN]([/color][/b]= [color=#2f4f4f]"ATTRIB"[/color] [b][color=BLUE]([/color][/b]cdr [b][color=RED]([/color][/b]assoc 0 ad[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                    [b][color=GREEN]([/color][/b]and [b][color=BLUE]([/color][/b]member [b][color=RED]([/color][/b]cdr [b][color=PURPLE]([/color][/b]assoc 2 ad[b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b]
                                '[b][color=RED]([/color][/b][color=#2f4f4f]"REVIBY"[/color] [color=#2f4f4f]"REVBY"[/color][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b]
                         [b][color=BLUE]([/color][/b]setq f T[b][color=BLUE])[/color][/b]
                         [b][color=BLUE]([/color][/b]entmod [b][color=RED]([/color][/b]subst [b][color=PURPLE]([/color][/b]cons 1 by[b][color=PURPLE])[/color][/b] [b][color=PURPLE]([/color][/b]assoc 1 ad[b][color=PURPLE])[/color][/b] ad[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                    [b][color=GREEN]([/color][/b]setq an [b][color=BLUE]([/color][/b]entnext an[b][color=BLUE])[/color][/b]
                          ad [b][color=BLUE]([/color][/b]entget an[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]if f [b][color=GREEN]([/color][/b]entupd en[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
             [b][color=MAROON]([/color][/b]setq i [b][color=GREEN]([/color][/b]1+ i[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

 

 

Just keep adding the variations ( all caps ) to the list in the (member) call

 

-David

Link to comment
Share on other sites

Humm another thing I was just thinking about is there a way of editing the code so I can build a master list with windows usernames and the initials I want to be entered automatically in the REVBY attribute? No dialog boxes.. We could call it C:REVBY, then I could piggyback that command inside of our "plot" routine I have coded. That way when they run the plot command which is C;DPA, it will run plot like normal but also have C:REVBY in their prior to plotting so they dont have to manually type another command to update their initials... Just another step I am noticing gets forgotten about. All help is appreciated. Thanks

Link to comment
Share on other sites

Try the following:

(defun c:revby ( / b e i l s v x )
   (setq l
      '(
           ("YourUserName" . "T.J.M")
       )
   )
   (cond
       (   (not (setq v (cdr (assoc (getvar 'loginname) l))))
           (princ (strcat "\nUsername \"" (getvar 'loginname) "\" not found in database."))
       )
       (   (setq s (ssget "_X" '((0 . "INSERT") (66 . 1))))
           (repeat (setq i (sslength s))
               (setq b (ssname  s (setq i (1- i)))
                     e (entnext b)
                     x (entget  e)
               )
               (while (= "ATTRIB" (cdr (assoc 0 x)))
                   (if (wcmatch (cdr (assoc 2 x)) "REVBY,REVIBY")
                       (if (entmod (subst (cons 1 v) (assoc 1 (reverse x)) x))
                           (entupd b)
                       )
                   )
                   (setq e (entnext e)
                         x (entget  e)
                   )
               )
           )
       )
   )
   (princ)
)

Link to comment
Share on other sites

Hey Lee one of my guys just brought up a valid point, which isnt really a big deal, because I prefer initials to be changed regardless, however. Is there a way to write an if statement in there for when a user opens a drawing just to print and doesnt change a thing.. meaning there is no changes then ignore the users initials being changed?

Link to comment
Share on other sites

Humm the above request is least important than this one.. Is there a way to add another statement to cross check the DRAWBY or DRAWNBY attribute for those initials and if the DRAWBY or DRAWNBY initials are the exact same as the current user, then do not enter initials for the REVBY, REVIBY attribute tag value.. Just a thought.

Link to comment
Share on other sites

Hey Lee one of my guys just brought up a valid point, which isnt really a big deal, because I prefer initials to be changed regardless, however. Is there a way to write an if statement in there for when a user opens a drawing just to print and doesnt change a thing.. meaning there is no changes then ignore the users initials being changed?

 

Just don't run the command? :huh:

Link to comment
Share on other sites

That would make total sense and I assure you I gave it some thought, lol, I promise ;) However, I have the command piggybacking with our plot command because most users tend to forget to change their initials so I took the liberty to do it programmatically. Its fine though really no big deal I was just curious.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...