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

Rename Blocks up to a character. Delete text before "_". abcd_123 become 123

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
dglenn9000
698 Views, 5 Replies

Rename Blocks up to a character. Delete text before "_". abcd_123 become 123

I have 30 blocks per drawing about 60 drawings.  Each block starts with the jobname has other block info and then the block type. For example

 

Bldg31-MechEquip_ABC

Bldg33-MechText_DEF

Bldg45-ElecEquip_GHI

 

I want the output to be just ABC, DEF, and GHI.

 

I have a rename script but I dont know where to go from here.  How can I tell it to remove everything up and including  the "_" symbol?

 

 Thanks!

5 REPLIES 5
Message 2 of 6
pbejse
in reply to: dglenn9000


@dglenn9000 wrote:

...

Bldg31-MechEquip_ABC

Bldg33-MechText_DEF

Bldg45-ElecEquip_GHI

 

I want the output to be just ABC, DEF, and GHI.

...

 


(defun c:renb (/ i a m p be)
  (while (setq a (tblnext "block" (not a)))
    (if (and
          (not
            (= 4 (logand 4
                 (cdr (assoc 70
                     (tblsearch "BLOCK" (setq m (cdr (assoc 2 a))))
                     )
                   )
                 )
               )
            )
          (setq p (vl-string-position 95 m))
          )
      (command
        "_.rename" "block"
        m
        (if (tblsearch "BLOCK" (setq b (substr m (+ 2 p))))
          (progn
            (setq i 0)
            (while
              (tblsearch "block" (strcat b (itoa (setq i (1+ i)))))
              )
            (strcat b (itoa i))
            ) b
          )
        )
      )
    )
  (princ)
  )

 

HTH

 

Message 3 of 6
dglenn9000
in reply to: pbejse

Wow,  thanks it works exactly as I need. 

 

What line is it referencing the character "_".  Just out of curiosity, if I wanted to use this script for another application where I wanted to delete everything upto and including a different character like "-" or the series of letters "ab" instead of "_" character, where would I change this in the script.  I was reviewing the code expecting something like "_" that I could just replace with another character if needed but this script is above my understanding.

Message 4 of 6
Kent1Cooper
in reply to: dglenn9000


@dglenn9000 wrote:

.... 

What line is it referencing the character "_".  Just out of curiosity, if I wanted to use ... a different character like "-" or the series of letters "ab" instead of "_" character, where would I change this ....


I don't mean to "steal" the opportunity to reply from pbejse, but here I am with a few minutes....
 

It's this that's finding the _ character:

(vl-string-position 95 m)

 

That function takes an ASCII character code.  You could do the same with a different function, quoting the actual character [or more than one, e.g. your "ab"]:
(vl-string-search "_" m)

 

Look into those two functions, plus (ascii) and (char) if you want to find out what the code is for a given character [e.g. your "-"].

Kent Cooper, AIA
Message 5 of 6
pbejse
in reply to: dglenn9000


@Kent1Cooper wrote:

......I don't mean to "steal" the opportunity to reply from pbejse, but here I am with a few minutes....

.... , quoting the actual character [or more than one, e.g. your "ab"]:

(vl-string-search "_" m)...


 

No offense taken , Your input is always welcome kent.

 

i agree, vl-string-search it is.

 


@dglenn9000 wrote:

Wow,  thanks it works exactly as I need. 

 

Just out of curiosity, if I wanted to use this script for another application where I wanted to delete everything upto and including a different character like "-" or the series of letters "ab" instead of "_" character, ..

 


Try this code:

(defun c:renb (/ i a m p be)
(vl-load-com)  
(setq ch (getstring "\nEnter Character(s) to Find: "))

(if (eq ch "")
  (princ "\Invalid character")
  (while (setq a (tblnext "block" (not a)))
    (if (and
          (not
            (= 4 (logand 4
                 (cdr (assoc 70
                     (tblsearch "BLOCK" (setq m (cdr (assoc 2 a))))
                     )
                   )
                 )
               )
            )
          (setq p (vl-string-search (strcase ch)(strcase m)))
          )
      (command
        "_.rename" "block" m
        (if (tblsearch "BLOCK" (setq b (substr m (+ 1 (strlen ch) p))))
          (progn
            (setq i 0)
            (while
              (tblsearch "block" (strcat b (itoa (setq i (1+ i)))))
              )
            (strcat b (itoa i))
            ) b
          )
        )
      )
    )
  )
  (princ)
  )

 

command: Renb

Enter Character(s) to Find: _

 

command: Renb

Enter Character(s) to Find: ab

 

HTH

 

 

Message 6 of 6
Kent1Cooper
in reply to: Kent1Cooper


@Kent1Cooper wrote:
....

Look into those two functions, plus (ascii) and (char) if you want to find out what the code is for a given character [e.g. your "-"].


Oops -- make that (chr).

Kent Cooper, AIA

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

Post to forums  

Autodesk Design & Make Report

”Boost