Author Topic: Renaming Layout Tabs...  (Read 2726 times)

0 Members and 1 Guest are viewing this topic.

stusic

  • Guest
Renaming Layout Tabs...
« on: September 10, 2015, 04:46:20 PM »
Hello!

I've got a routine I use to rename layout tabs to the number of the position they're in, but I'm having trouble modifying it to a new standard.

I have layout tabs named something like "CB-05-Overview ISO" or "SS-10-Counter". I'd like to rename the layout tabs so each layout that starts with "CB" has the following number in order, counting up from 01. So if there were only 1 layout that starts with CB, then my above example would turn it into "CB-01-Overview ISO". Then I'd like to repeat this with other formats (i.e., "CB", "SS", "SI"...) (hence the COND statement).

This is the code I've been fiddling with, but I can't seem to get it to work (or see what's exactly wrong with it). Would someone be kind enough to give me some direction?

Code: [Select]
(defun RenameLayouts (/ acDoc)

  (defun *error* (msg)
    (if acDoc
      (vla-endundomark acDoc)
    )
    (cond ((not msg))                                                   ; Normal exit
          ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
          ((princ (strcat "\n** Error: " msg " ** ")))                  ; Fatal error, display it
    )
    (princ)
  )

  (vla-startundomark
    (setq acDoc (vla-get-activedocument (vlax-get-acad-object)))
  )

  (vlax-for x (vla-get-layouts acDoc)
    (cond
      (
       (and
       (/= (vla-get-name x) "Model") ; not model tab
       (= (vla-get-name x) "CB-*") ; construction/build tab
       )
(vla-put-name x (strcat "CB-" (itoa (vla-get-taborder x)) (itoa (vl-string-subst x 5))))
      )
      (t (princ "Nope."))
      )
  )

  (*error* nil)
)

Thanks for the help :)

Dave M

  • Newt
  • Posts: 196
Re: Renaming Layout Tabs...
« Reply #1 on: September 10, 2015, 05:38:23 PM »
Check out Lee Mac's TabSort, it's great!  http://lee-mac.com/tabsort.html
Civil 3D 2018 - Microstation SS4 - Windows 10 - Dropbox

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Renaming Layout Tabs...
« Reply #2 on: September 10, 2015, 06:25:53 PM »

In passing:
The *error* function name SHOULD be included in the local variables list

ie
Code - Auto/Visual Lisp: [Select]
  1. (defun RenameLayouts (/ *error* acDoc)
  2. ;;...

If you fail to do this the *error* definition will replace the default global definition , possibly to your detriment.


//------------------

Can you confirm your numbering scheme.

Will each tab name reflect it's taborder index ?
or
Will the number reflect the 'count' for that particular prefix ?

//------------------


kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

stusic

  • Guest
Re: Renaming Layout Tabs...
« Reply #3 on: September 10, 2015, 06:37:54 PM »

In passing:
The *error* function name SHOULD be included in the local variables list

ie
Code - Auto/Visual Lisp: [Select]
  1. (defun RenameLayouts (/ *error* acDoc)
  2. ;;...

If you fail to do this the *error* definition will replace the default global definition , possibly to your detriment.

Thanks, I'll def do this. Thanks for the heads up.


Can you confirm your numbering scheme.

Will the number reflect the 'count' for that particular prefix ?


Yes, the number would reflect the count for that particular prefix. The first occurrence of "CB" would be "CB-01", the second occurrence would be "CB-02", etc.

Thanks for the help.

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Renaming Layout Tabs...
« Reply #4 on: September 10, 2015, 07:09:25 PM »
For these kinds of tasks, you're probably going to need to temporarily rename all layouts to a unique name which differs from the numbering format to be used, otherwise you will likely get duplicate key errors if the layouts have been previously renumbered and have subsequently changed tab order.

However, to get you started, I would suggest the following as a starting point:

Code - Auto/Visual Lisp: [Select]
  1. (defun renamelayouts ( doc / idx itm lay lst num obj prf tmp )
  2.     (vlax-for lay (vla-get-layouts doc)
  3.         (if (wcmatch (vla-get-name lay) "??-*")
  4.             (setq lst (cons lay lst)
  5.                   idx (cons (vla-get-taborder lay) idx)
  6.             )
  7.         )
  8.     )
  9.     (foreach  idx (vl-sort-i idx '<)
  10.         (setq obj (nth idx lst)
  11.               lay (vla-get-name obj)
  12.               prf (substr lay 1 2)
  13.         )
  14.         (if (setq itm (assoc prf tmp))
  15.             (setq num (1+ (cdr itm))
  16.                   tmp (subst (cons prf num) itm tmp)
  17.             )
  18.             (setq tmp (cons  (cons prf 1) tmp)
  19.                   num 1
  20.             )
  21.         )
  22.         (vla-put-name obj
  23.             (strcat prf "-" (if (< num 10) (strcat "0" (itoa num)) (itoa num))
  24.                 (substr lay (if (wcmatch lay "??-##-*") 6 3))
  25.             )
  26.         )
  27.     )
  28.     (princ)
  29. )


stusic

  • Guest
Re: Renaming Layout Tabs...
« Reply #5 on: September 11, 2015, 10:39:44 AM »
For these kinds of tasks, you're probably going to need to temporarily rename all layouts to a unique name which differs from the numbering format to be used, otherwise you will likely get duplicate key errors if the layouts have been previously renumbered and have subsequently changed tab order.

However, to get you started, I would suggest the following as a starting point:

Code - Auto/Visual Lisp: [Select]
  1. (defun renamelayouts...
  2. )

This is very cool. And it's actually taking care of a lot of things I was concerned about, but was going to put off til later (double-digit vs. single-digit numbers, for example).

I am getting "; error: too few arguments", and since everything else seems correct afaik, this if statement is the only thing that seemed a bit odd. Can you verify (debug wasn't a big help)?

Code: [Select]
(substr lay (if (wcmatch lay "??-##-*") 6 3))
Thanks for getting me off on the right foot, Lee. You constantly amaze.

stusic

  • Guest
Re: Renaming Layout Tabs...
« Reply #6 on: September 11, 2015, 11:44:55 AM »
Check out Lee Mac's TabSort, it's great!  http://lee-mac.com/tabsort.html

That is a great one :)

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Renaming Layout Tabs...
« Reply #7 on: September 11, 2015, 12:00:51 PM »
You're welcome stusic  :-)

I am getting "; error: too few arguments"...

Did you supply the function with the document object argument? e.g.:

Code - Auto/Visual Lisp: [Select]

stusic

  • Guest
Re: Renaming Layout Tabs...
« Reply #8 on: September 11, 2015, 02:29:09 PM »
You're welcome stusic  :-)

I am getting "; error: too few arguments"...

Did you supply the function with the document object argument? e.g.:

Code - Auto/Visual Lisp: [Select]

No, i didn't... nevermind. Thanks again Lee.

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Renaming Layout Tabs...
« Reply #9 on: September 12, 2015, 06:45:49 AM »
No worries stusic  :-)