Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Valued Contributor
dglenn9000
Posts: 52
Registered: ‎07-10-2007
Message 1 of 6 (318 Views)
Accepted Solution

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

318 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!


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

 

*Expert Elite*
pbejse
Posts: 2,502
Registered: ‎11-24-2009
Message 2 of 6 (300 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

 

Valued Contributor
dglenn9000
Posts: 52
Registered: ‎07-10-2007
Message 3 of 6 (266 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,796
Registered: ‎09-13-2004
Message 4 of 6 (252 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,502
Registered: ‎11-24-2009
Message 5 of 6 (241 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,796
Registered: ‎09-13-2004
Message 6 of 6 (221 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
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!