Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Active Contributor
dglenn9000
Posts: 48
Registered: ‎07-10-2007
Message 1 of 6 (289 Views)
Accepted Solution

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

289 Views, 5 Replies
10-03-2012 04:37 PM

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!

*Expert Elite*
pbejse
Posts: 2,406
Registered: ‎11-24-2009
Message 2 of 6 (271 Views)

Re: Rename Blocks up to a character. Delete text before "_". abcd_123

10-03-2012 07:40 PM 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

 

Active Contributor
dglenn9000
Posts: 48
Registered: ‎07-10-2007
Message 3 of 6 (237 Views)

Re: Rename Blocks up to a character. Delete text before "_". abcd_123

10-04-2012 02:49 PM 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.

*Expert Elite*
Kent1Cooper
Posts: 5,243
Registered: ‎09-13-2004
Message 4 of 6 (223 Views)

Re: Rename Blocks up to a character. Delete text before "_". abcd_123

10-04-2012 05:35 PM 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
*Expert Elite*
pbejse
Posts: 2,406
Registered: ‎11-24-2009
Message 5 of 6 (212 Views)

Re: Rename Blocks up to a character. Delete text before "_". abcd_123

10-04-2012 08:31 PM 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

 

 

*Expert Elite*
Kent1Cooper
Posts: 5,243
Registered: ‎09-13-2004
Message 6 of 6 (192 Views)

Re: Rename Blocks up to a character. Delete text before "_". abcd_123

10-05-2012 06:31 AM 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

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community