Author Topic: Delete All Layouts Except List  (Read 1401 times)

0 Members and 1 Guest are viewing this topic.

BHenry85

  • Mosquito
  • Posts: 4
Delete All Layouts Except List
« on: October 25, 2022, 08:16:04 PM »
Hello Swamp,

I am trying to delete all layouts except ones that either start with "C" or end with "_AL". I found a page that should have done the trick, but I am getting an error with both examples. One by LeeMac and another by RonJonp, both legends in arena in my opinion. I'm still learning, but I tried to incorporate comparing the list to not include my list using the wcmatch method, and something I am doing there is where it all breaks down. I would greatly appreciate any insight on where I went wrong.

The examples listed below are located here: https://www.cadtutor.net/forum/topic/10119-delete-all-layouts-except-current/

Example 1 (Lee Mac):
Code: [Select]
(defun c:DeleteAllLayoutsBut ( / exceptions )
 (vl-load-com)

 (setq exceptions '("Site Plan"))

 (vlax-for l
   (vla-get-Layouts
     (vla-get-ActiveDocument
       (vlax-get-acad-object)
     )
   )
   (if (not
         (vl-position (vla-get-name l)
           (cons "Model" (cons (getvar 'ctab) exceptions))
         )
       )
     (if (vl-catch-all-error-p
           (vl-catch-all-apply 'vla-delete (list l))
         )
       (princ (strcat "\n** Error deleting layout: " (vla-get-name l) " **"))
     )
   )
 )

 (princ)
)

I removed (cons "Model" (cons (getvar 'ctab) exceptions) because I set the layout as part of a larger project to "C1" before this would run and the current layout could be one that should be removed and then I added "Model" to the exceptions list. But, when I run this example, I get a "Too few arguments" error.

Example 1 Test:
Code: [Select]
(defun c:Example1Test ( / exceptions )
 (vl-load-com)

 (setq exceptions (wcmatch "Name" "Model, C*, ~*_AL"))

 (vlax-for l
   (vla-get-Layouts
     (vla-get-ActiveDocument
       (vlax-get-acad-object)
     )
   )
   (if (not
         (vl-position (vla-get-name l)
         )
       )
     (if (vl-catch-all-error-p
           (vl-catch-all-apply 'vla-delete (list l))
         )
       (princ (strcat "\n** Error deleting layout: " (vla-get-name l) " **"))
     )
   )
 )

 (princ)
)

Example 2 (RonJonp):
Code: [Select]
(defun c:deleteallbutcurrentlayout (/)
 (vl-load-com)
 (vlax-map-collection
   (vla-get-layouts
     (vla-get-activedocument (vlax-get-acad-object))
   )
   '(lambda (lay)
      (if (/= (vla-get-name lay) (getvar 'ctab))
    (vl-catch-all-apply 'vla-delete (list lay))
      )
    )
 )
)

For this example, I replaced (vla-get-name lay) (getvar 'ctab) on line 8 with (wcmatch "Name" "Model, C*, ~*_AL") in hopes that this would produce the list of layouts NOT to remove, but it removes ALL layouts when I run the command.

Example 2 Test:
Code: [Select]
(defun c:Example2Test (/)
 (vl-load-com)
 (vlax-map-collection
   (vla-get-layouts
     (vla-get-activedocument (vlax-get-acad-object))
   )
   '(lambda (lay)
      (if (/= (wcmatch "Name" "Model, C*, ~*_AL"))
    (vl-catch-all-apply 'vla-delete (list lay))
      )
    )
 )
)
« Last Edit: October 26, 2022, 11:23:47 AM by BHenry85 »

tombu

  • Bull Frog
  • Posts: 289
  • ByLayer=>Not0
Re: Delete All Layouts Except List
« Reply #1 on: October 25, 2022, 09:44:57 PM »
Have you looked at Lee Mac's TabSort: http://www.lee-mac.com/tabsort.html
A program designed with the intention to aid in the organisation of layout tabs in a drawing, the program enables the user to organise each layout tab using intuitively engineered buttons to move selected tabs up/down or to the top/bottom of the tab order.

The program also offers the ability to rename a layout tab by double-clicking on its entry in the list displayed, and furthermore add a prefix and/or suffix to selected/every layout tab.

A facility is also provided to add & delete layout tabs, and also sort the tabs into alphabetical, numerical, or architectural order; or reverse the order currently displayed.

A Find & Replace functionality is also offered to allow the user to quickly replace a text string in multiple layout tabs.

The user may also copy selected layout tabs, or set the selected entry as the current drawing layout.

A Help Dialog may be shown upon pressing H at the Main Dialog interface.
Tom Beauford P.S.M.
Leon County FL Public Works - Windows 7 64 bit AutoCAD Civil 3D

mhupp

  • Bull Frog
  • Posts: 250
Re: Delete All Layouts Except List
« Reply #2 on: October 26, 2022, 12:35:22 AM »
Code - Auto/Visual Lisp: [Select]
  1. (wcmatch "Name" "Model,C*,*_AL")

Remove the spaces.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Delete All Layouts Except List
« Reply #3 on: October 26, 2022, 10:48:24 AM »
Try this:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:example2test (/)
  2.     (or ;; No need to check for model when using vl-catch-all-apply
  3.         (wcmatch (strcase (vla-get-name lay)) "C*,*_AL")
  4.         (vl-catch-all-apply 'vla-delete (list lay))
  5.     )
  6.   )
  7. )

One issue you had was you're checking against "Name" rather than getting the name of the layout:
Code - Auto/Visual Lisp: [Select]
  1. (/= (wcmatch "Name" "Model, C*, ~*_AL"))

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

BHenry85

  • Mosquito
  • Posts: 4
Re: Delete All Layouts Except List
« Reply #4 on: October 26, 2022, 11:21:48 AM »
Have you looked at Lee Mac's TabSort: http://www.lee-mac.com/tabsort.html

Yeah, I typically go to his website first and then look for his advice on other websites so I can learn what I can. I see on lines 249-256 where he is getting the layouts, but he uses the eq function to remove the Model tab and that would need to be changed to allow filtering, which is why I opted for the wcmatch method.

Remove the spaces.

Tried that but got the same results for both examples; Example 1 by Lee Mac produces the "too few arguments" error and Example 2 by ronjonp still deletes all of the tabs.

Try this:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:example2test (/)
  2.     (or ;; No need to check for model when using vl-catch-all-apply
  3.         (wcmatch (strcase (vla-get-name lay)) "C*,*_AL")
  4.         (vl-catch-all-apply 'vla-delete (list lay))
  5.     )
  6.   )
  7. )

One issue you had was you're checking against "Name" rather than getting the name of the layout:
Code - Auto/Visual Lisp: [Select]
  1. (/= (wcmatch "Name" "Model, C*, ~*_AL"))

Speak of the devil and he shall appear. Lol. That did the trick sir. Works perfectly and as desired. I was assuming that the example of yours that I had found was using the (vlax-map-collection (vla-get-layouts... to get all of the layout names and then (lambda.. was starting the evaluation of what was not listed in the (if (/= (wcmatch "Name" "Model,C*,*_AL") bit of code.

Thank you all so much for your insight and this learning opportunity. Cheers!

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Delete All Layouts Except List
« Reply #5 on: October 26, 2022, 02:46:31 PM »
Glad to help  :-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC