Author Topic: Rename Layout Tab to include dwg size  (Read 2304 times)

0 Members and 1 Guest are viewing this topic.

KimbleC

  • Mosquito
  • Posts: 3
Rename Layout Tab to include dwg size
« on: September 14, 2017, 08:29:37 PM »
New user and first time poster.

So far, and with many thanks I have used a routine that was found https://www.theswamp.org/index.php?topic=1466.0.

I have a minor problem in so far as I wish to add the drawing size to the Tab name.
We have several drawings....some with an A3 border/title block and others with an A1 border/title block.
Each drawing has either A3 or A1 but not both inserted in the paperspace layout tab.
In our case these blocks either start with A3_xxxx or A1_xxxx.

So is there an easy way to check for the name of the XX_title.dwg or XX_Border.dwg and add this XX information to layout  tab name.

I hope that this all makes sense.

Regards
Kim Williams
Long time AutoCAD user (v2.6)
Current version 2018

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Rename Layout Tab to include dwg size
« Reply #1 on: September 15, 2017, 03:01:06 PM »
Welcome to the Swamp!

Try:
Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun KGA_Conv_Collection_To_List (coll / ret)
  3.     (vlax-for a coll
  4.       (setq ret (cons a ret))
  5.     )
  6.   )
  7. )
  8.  
  9. (defun KGA_Sys_Apply (expr varLst / ret)
  10.   (if (not (vl-catch-all-error-p (setq ret (vl-catch-all-apply expr varLst))))
  11.     ret
  12.   )
  13. )
  14.  
  15. ; Returns name of first block reference in block definition defObj that matches nmePat.
  16. ; (FindRefInDef (vla-get-block (vla-item (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))) "Layout1")) "A3_*,A1_*") => "A3_Test"
  17. (defun FindRefInDef (defObj nmePat)
  18.     '(lambda (obj / nme)
  19.       (if
  20.         (and
  21.           (= "AcDbBlockReference" (vla-get-objectname obj))
  22.           (wcmatch
  23.             (strcase
  24.               (setq nme
  25.                 (
  26.                   (if (vlax-property-available-p obj 'effectivename)
  27.                     vla-get-effectivename
  28.                     vla-get-name
  29.                   )
  30.                   obj
  31.                 )
  32.               )
  33.             )
  34.             (strcase nmePat)
  35.           )
  36.         )
  37.         nme
  38.       )
  39.     )
  40.     (KGA_Conv_Collection_To_List defObj)
  41.   )
  42. )
  43.  
  44. (defun c:Test ( / doc fnd)
  45.     (if
  46.       (and
  47.         (= :vlax-false (vla-get-modeltype lyt))
  48.         (not (wcmatch (vla-get-name lyt) "* A3,* A1"))
  49.         (setq fnd (FindRefInDef (vla-get-block lyt) "A3_*,A1_*"))
  50.       )
  51.       (KGA_Sys_Apply
  52.         'vla-put-name
  53.         (list lyt (strcat (vla-get-name lyt) " " (substr fnd 1 2)))
  54.       )
  55.     )
  56.   )
  57.   (princ)
  58. )

EDIT: Added (vl-load-com).
« Last Edit: September 16, 2017, 04:13:15 AM by roy_043 »

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Rename Layout Tab to include dwg size
« Reply #2 on: September 15, 2017, 03:45:54 PM »
Another for fun. Welcome to TheSwamp!

Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ a s sz tb)
  2.   (if (setq s (ssget "_x" '((0 . "insert") (2 . "A#_*") (410 . "~Model"))))
  3.     (foreach b (mapcar 'cadr (ssnamex s))
  4.       (setq a  (cdr (assoc 330 (entget b)))
  5.             a  (cdr (assoc 340 (entget a)))
  6.             a  (vlax-ename->vla-object a)
  7.             tb (cdr (assoc 410 (entget b)))
  8.             sz (substr (cdr (assoc 2 (entget b))) 1 2)
  9.       )
  10.       (and (not (wcmatch tb "*_A#*")) (vla-put-name a (strcat tb "_" sz)))
  11.     )
  12.   )
  13.   (princ)
  14. )
« Last Edit: September 16, 2017, 10:03:38 AM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Rename Layout Tab to include dwg size
« Reply #3 on: September 15, 2017, 06:42:58 PM »
Nice Ron  :-)

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Rename Layout Tab to include dwg size
« Reply #4 on: September 15, 2017, 09:10:13 PM »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

KimbleC

  • Mosquito
  • Posts: 3
Re: Rename Layout Tab to include dwg size
« Reply #5 on: September 19, 2017, 12:55:57 AM »
Thanks Roy_043.....works well.
ronjonp I like your one better as it is much simpler.
But........
How do I change the order so that Tab reads A#_Layoutx.
I tried to experiment around with line 10 but ended  with extra A# still at the end not the beginning of the string.
I hope to add this one the Ron has come up with to  the one that CAB has
https://www.theswamp.org/index.php?topic=1466.0.

Thanks in advance
Kim

ps......this looks like it works
(and (not (wcmatch tb "*A#_*")) (vla-put-name a (strcat sz "_" tb)))
« Last Edit: September 19, 2017, 01:15:20 AM by KimbleC »
Long time AutoCAD user (v2.6)
Current version 2018

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Rename Layout Tab to include dwg size
« Reply #6 on: September 19, 2017, 04:29:25 AM »
OK FWIW then :-D:
Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun KGA_Conv_Collection_To_List (coll / ret)
  3.     (vlax-for a coll
  4.       (setq ret (cons a ret))
  5.     )
  6.   )
  7. )
  8.  
  9. ; Returns name of first block reference in block definition defObj that matches nmePat.
  10. ; (FindRefInDef (vla-get-block (vla-item (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))) "Layout1")) "A3_*,A1_*") => "A3_Test"
  11. (defun FindRefInDef (defObj nmePat)
  12.     '(lambda (obj / nme)
  13.       (if
  14.         (and
  15.           (= "AcDbBlockReference" (vla-get-objectname obj))
  16.           (wcmatch
  17.             (strcase
  18.               (setq nme
  19.                 (
  20.                   (if (vlax-property-available-p obj 'effectivename)
  21.                     vla-get-effectivename
  22.                     vla-get-name
  23.                   )
  24.                   obj
  25.                 )
  26.               )
  27.             )
  28.             (strcase nmePat)
  29.           )
  30.         )
  31.         nme
  32.       )
  33.     )
  34.     (KGA_Conv_Collection_To_List defObj)
  35.   )
  36. )
  37.  
  38. (defun c:Test ( / doc fnd str)
  39.   (setq str "Layout")
  40.   ;; Make sure the string is unique to avoid name clashes:
  41.   (while
  42.     (vl-some
  43.       '(lambda (nme) (wcmatch (strcase nme) (strcase (strcat "*" str "*"))))
  44.       (layoutlist)
  45.     )
  46.     (setq str (strcat str "X"))
  47.   )
  48.     (if (= :vlax-false (vla-get-modeltype lyt))
  49.       (vla-put-name
  50.         lyt
  51.         (strcat
  52.           (if (setq fnd (FindRefInDef (vla-get-block lyt) "A#_*"))
  53.             (strcat (substr fnd 1 2) "_")
  54.             ""
  55.           )
  56.           str
  57.           (itoa (vla-get-taborder lyt))
  58.         )
  59.       )
  60.     )
  61.   )
  62.   ;; Substitute the string if required:
  63.   (if (/= str "Layout")
  64.     (vlax-for lyt (vla-get-layouts doc)
  65.       (if (= :vlax-false (vla-get-modeltype lyt))
  66.         (vla-put-name
  67.           lyt
  68.           (vl-string-subst "Layout" str (vla-get-name lyt))
  69.         )
  70.       )
  71.     )
  72.   )
  73.   (princ)
  74. )

EDIT: Changed the name format from "Layout1_A3" to "A3_Layout1".

« Last Edit: September 19, 2017, 09:49:13 AM by roy_043 »

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Rename Layout Tab to include dwg size
« Reply #7 on: September 19, 2017, 09:29:25 AM »
...

ps......this looks like it works
(and (not (wcmatch tb "*A#_*")) (vla-put-name a (strcat sz "_" tb)))
That's the ticket :) .. you could also remove the first asterisk in red.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

KimbleC

  • Mosquito
  • Posts: 3
Re: Rename Layout Tab to include dwg size
« Reply #8 on: September 19, 2017, 09:14:43 PM »
Thanks to all   :yay!:
So this is what it now looks like.......

;; CREDIT https://www.theswamp.org/index.php?topic=1466.0
;; This routine will rename all layout tabs in all open drawings
;;  to "Layoutx" where x = the tab count
(defun c:re-name-all-layouts (/ Tab_name doc x cnt)
  (vl-load-com)
  (setq Tab_name "Layout")
  (vlax-for doc (vla-get-documents (vlax-get-acad-object))
    (setq cnt 0)
    (vlax-for x (vla-get-layouts doc)
      (if (not (= (vla-get-name x) "Model"))
        (vla-put-name x
          (strcat Tab_name (itoa (setq cnt (1+ cnt))))
        )
      )
    )
  )
)

;; CREDIT https://www.theswamp.org/index.php?topic=53447.new#new

(defun c:foo (/ a s sz tb)
      (if (setq s (ssget "_x" '((0 . "insert") (2 . "A#_*") (410 . "~Model"))))
        (foreach b (mapcar 'cadr (ssnamex s))
          (setq a  (cdr (assoc 330 (entget b)))
           a  (cdr (assoc 340 (entget a)))
           a  (vlax-ename->vla-object a)
           tb (cdr (assoc 410 (entget b)))
           sz (substr (cdr (assoc 2 (entget b))) 1 2)
          )
          (and (not (wcmatch tb "A#_*")) (vla-put-name a (strcat sz "_" tb)))
        )
      )
      (princ)
    )
    (vl-load-com)


(defun C:LayTab ()
   (C:re-name-all-layouts)
   (c:foo)

)
(princ "\n\tre-name-all-layouts Loaded...........................Start command with LayTab.")
(princ)
(princ)
Long time AutoCAD user (v2.6)
Current version 2018

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Rename Layout Tab to include dwg size
« Reply #9 on: September 19, 2017, 09:21:14 PM »
Nice job putting them together :) .. I''l have some time in the morning to consolidate them for you. Have a nice eve.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Rename Layout Tab to include dwg size
« Reply #10 on: September 20, 2017, 07:59:22 AM »
Sorry Ron - I couldn't resist the urge to LISP  :-P

Code - Auto/Visual Lisp: [Select]
  1. (defun c:laytab ( / i n o s x )
  2.     (if (setq s (ssget "_x" '((0 . "insert") (2 . "A#_*") (410 . "~Model"))))
  3.         (repeat (setq i (sslength s))
  4.             (or (wcmatch (setq n (cdr (assoc 410 (setq x (entget (ssname s (setq i (1- i)))))))) "A#_*")
  5.                 (vla-put-name
  6.                     (setq o (vlax-ename->vla-object (cdr (assoc 340 (entget (cdr (assoc 330 x)))))))
  7.                     (strcat (substr (cdr (assoc 2 x)) 1 2) "_" n (itoa (vla-get-taborder o)))
  8.                 )
  9.             )
  10.         )
  11.     )
  12.     (princ)
  13. )

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Rename Layout Tab to include dwg size
« Reply #11 on: September 20, 2017, 08:47:52 AM »
Sorry Ron - I couldn't resist the urge to LISP  :P

Code - Auto/Visual Lisp: [Select]
  1. (defun c:laytab ( / i n o s x )
  2.     (if (setq s (ssget "_x" '((0 . "insert") (2 . "A#_*") (410 . "~Model"))))
  3.         (repeat (setq i (sslength s))
  4.             (or (wcmatch (setq n (cdr (assoc 410 (setq x (entget (ssname s (setq i (1- i)))))))) "A#_*")
  5.                 (vla-put-name
  6.                     (setq o (vlax-ename->vla-object (cdr (assoc 340 (entget (cdr (assoc 330 x)))))))
  7.                     (strcat (substr (cdr (assoc 2 x)) 1 2) "_" n (itoa (vla-get-taborder o)))
  8.                 )
  9.             )
  10.        )
  11.     )
  12.     (princ)
  13. )
No worries .. it is a disease you know.  ;D


Here's mine:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ a s sz tb to)
  2.   (if (setq s (ssget "_x" '((0 . "insert") (2 . "A#_*") (410 . "~Model"))))
  3.     (foreach b (mapcar 'cadr (ssnamex s))
  4.       (setq a  (cdr (assoc 330 (entget b)))
  5.             a  (cdr (assoc 340 (entget a)))
  6.             to (cdr (assoc 71 (entget a)))
  7.             a  (vlax-ename->vla-object a)
  8.             tb (cdr (assoc 410 (entget b)))
  9.             sz (substr (cdr (assoc 2 (entget b))) 1 2)
  10.       )
  11.       (and (not (wcmatch tb "A#_*")) (vla-put-name a (strcat sz "_Layout" (itoa to))))
  12.     )
  13.   )
  14.   (princ)
  15. )
« Last Edit: September 20, 2017, 08:58:04 AM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC