Author Topic: Searching for Layout1 in a list  (Read 10729 times)

0 Members and 1 Guest are viewing this topic.

Hangman

  • Swamp Rat
  • Posts: 566
Searching for Layout1 in a list
« on: February 20, 2007, 12:15:14 PM »
Hey guys, how can this piece of code be manipulated to search for a named layout ??
Code: [Select]
    (progn
      (setq Layout
        (vlax-map-collection (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
          '(lambda (x) (setq tab-list (cons x tab-list)))))
      (setq tab-list (vl-sort tab-list '(lambda (x y) (< (vla-get-taborder x) (vla-get-taborder y)))))
      (setq tab-1 (vl-remove "Layout1" (mapcar '(lambda (x)(vla-get-name x))tab-list)))
      (setvar "ctab" (car tab-1)))

I need to do a search & destroy,
Quote
  If "Layout1" exists, then remove
  If "8.5 x 11" exists, then remove
  Else leave list in sorted order

Thanks a bunch.
Hangman  8)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Drafting Board, Mechanical Arm, KOH-I-NOOR 0.7mm
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Searching for Layout1 in a list
« Reply #1 on: February 20, 2007, 12:37:23 PM »
Code: [Select]
(vl-catch-all-apply
 'vla-Item
 (list
  (vla-get-Layouts
   (vla-get-ActiveDocument
    (vlax-get-Acad-Object)
   )
  )
  "Layout1"
 )
)
You can set this to something, then check to see if it's an error, if so, then the layout doesn't exist, if it's not an error, then it will return the layout object.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Searching for Layout1 in a list
« Reply #2 on: February 20, 2007, 01:55:06 PM »
I don't know who the author is.
Code: [Select]
(defun i:deletelayout (layoutname)
  (vl-load-com)
  (cond ((not
           (vl-catch-all-error-p
             (vl-catch-all-apply
               (function
                 (lambda ()
                   (vla-delete
                     (vla-item
                       (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
                       layoutname
                     )
                   )
                 )
               )
             )
           )
         )
        )
  )
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Searching for Layout1 in a list
« Reply #3 on: February 20, 2007, 02:24:23 PM »
>CAB

I have a little reduced a code.
In a code there were superfluous letters...

Code: [Select]
(defun i:deletelayout (layoutname)
 (vl-load-com)
 (not
  (vl-catch-all-error-p
   (vl-catch-all-apply
    (function
     (lambda ()
      (vla-delete
       (vla-item
        (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
        layoutname
       ) ;_  vla-item
      ) ;_  vla-delete
     ) ;_  lambda
    ) ;_  function
   ) ;_  vl-catch-all-apply
  ) ;_  vl-catch-all-error-p
 ) ;_  not
)

Hangman

  • Swamp Rat
  • Posts: 566
Re: Searching for Layout1 in a list
« Reply #4 on: February 20, 2007, 02:35:16 PM »
Well, I'm at a bit of a loss here.
Maybe I'm trying too hard.
OK, so this is what I've tried.
Quote
(vl-catch-all-apply (vla-delete 'vla-Item (list
(((_> (vla-get-layouts (vla-get-activedocument (vlax-get-Acad-object))
"Layout1"))))
; error: Too many actual parameters
Quote
(vla-delete (vl-catch-all-apply 'vla-Item (list
(((_> (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))
"Layout1"))))
; error: Too many actual parameters
Quote
(setq tab (vl-catch-all-apply 'vla-item (list (vla-get-layouts
((((_> (vla-get-activedocument (vlax-get-acad-object)) "Layout1"))))
; error: Too many actual parameters
Quote
  (setq Layout
    (progn
      (vlax-map-collection (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
          '(lambda (x) (setq tab-list (cons x tab-list)))))
      (setq tab-1 (vla-delete "Layout1" (mapcar '(lambda (x)(vla-get-name x))tab-list)))
      (setvar "ctab" (car tab-1)))

I know I already have the "...get-layouts", the "... get-activedocument" & the "...get-acad-object".  I wouldn't think I would need them twice.
What I'm not understanding from CAB's post, is the (function (lambda () (vla-delete ... ))).  Does the (function (lambda ... )) allow the tab to be deleted ??  Tims post gets the "Layout1" name from the list.  Much like the earlier code mearly removes it from the list, it doesn't delete it from the drawing, right ??

So how do I make an IF statement that will delete the "Layout1" tab from the drawing if it is there without writing all the "get-Layouts", "get-activedocument", & "get-acad-object" two or three times ??
I have that in my code so far,
Code: [Select]
  (setq Layout
    (progn
      (vlax-map-collection (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
        '(lambda (x) (setq tab-list (cons x tab-list)))))
    (setq tab-1 (vl-remove "Layout1" (mapcar '(lambda (x)(vla-get-name x))tab-list)))
I don't need to remove the "Layout1" from the list, but if it is in the list, then delete the tab from the drawing.
Am I getting warmer by putting it this way ??
Code: [Select]
  (setq Layout
    (progn
      (vlax-map-collection (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
        '(lambda (x) (setq tab-list (cons x tab-list)))))
    (if (setq tab-1 (vla-item "Layout1" (mapcar '(lambda (x)(vla-get-name x))tab-list)))
      (function (lambda ()(vla-delete (vla-item "Layout1")))))

Thanks.
Hangman  8)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Drafting Board, Mechanical Arm, KOH-I-NOOR 0.7mm
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Searching for Layout1 in a list
« Reply #5 on: February 20, 2007, 02:42:55 PM »
the code above is the ticket ... a slower method, but shorter code is ...

Code: [Select]
(if (member layoutname (layoutlist))
    (vl-cmdf "_.layout" "d" layoutname)
)

Of course capitalization is a factor ...
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Searching for Layout1 in a list
« Reply #6 on: February 20, 2007, 02:59:42 PM »
Quote
What I'm not understanding from CAB's post, is the (function (lambda () (vla-delete ... ))).  Does the (function (lambda ... )) allow the tab to be deleted ??

You don't need to check if it exists.
The stuff in the (function (lambda ... )) TRIES to delete the layout assuming it exists.
The   (Vl-catch-all-error-p   (vl-catch-all-apply .... )) wrapper is just in case it Doesn't exist.

Quote
I know I already have the "...get-layouts", the "... get-activedocument" & the "...get-acad-object".  I wouldn't think I would need them twice.

Not sure what you mean by this ..

but ..
If you already have variables holding the name of the layouts collection you can use it instead because the variable points to the collection object irrespective of what it contains. In theory you only need to set the variable once per document instance and re-use the same variable for all code. 





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.

Hangman

  • Swamp Rat
  • Posts: 566
Re: Searching for Layout1 in a list
« Reply #7 on: February 20, 2007, 03:29:39 PM »
the code above is the ticket ... a slower method, but shorter code is ...
Code: [Select]
(if (member layoutname (layoutlist))
    (vl-cmdf "_.layout" "d" layoutname)
)
Of course capitalization is a factor ...
Ahhh, so this is also case sensitive ?.  Interesting.  Thank you Keith, I didn't know that.


Quote
What I'm not understanding from CAB's post, is the (function (lambda () (vla-delete ... ))).  Does the (function (lambda ... )) allow the tab to be deleted ??

You don't need to check if it exists.
The stuff in the (function (lambda ... )) TRIES to delete the layout assuming it exists.
The   (Vl-catch-all-error-p   (vl-catch-all-apply .... )) wrapper is just in case it Doesn't exist.

That is what I was thinking, the current code I have is
Code: [Select]
(setq Layout
  (progn
    (vlax-map-collection (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
      '(lambda (x) (setq tab-list (cons x tab-list)))))
    (setq tab-1 (vl-remove "Model" (mapcar '(lambda (x)(vla-get-name x))tab-list)))
    (setvar "ctab" (car tab-1)))
I need this to get to a layout "8.5 x 11" out of two or three layouts in the drawing.  I don't need a "Layout1" if it exists.
But when I try to input the
          (vl-catch-all-error-p
             (vl-catch-all-apply
               (function
                 (lambda ()
                   (vla-delete
                     (vla-item
                       (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
                       layoutname ...
It keeps erroring on me.  I'm not putting it correctly, but I don't know how it's suppose to read yet.



Quote
I know I already have the "...get-layouts", the "... get-activedocument" & the "...get-acad-object".  I wouldn't think I would need them twice.

Not sure what you mean by this ..

but ..
If you already have variables holding the name of the layouts collection you can use it instead because the variable points to the collection object irrespective of what it contains. In theory you only need to set the variable once per document instance and re-use the same variable for all code. 

Basically, just what you said there.  I already have
Code: [Select]
  (progn
    (vlax-map-collection (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
Why would I need it again.

I need to go try Keith's example, see I can get it to work.  BRB
Hangman  8)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Drafting Board, Mechanical Arm, KOH-I-NOOR 0.7mm
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Hangman

  • Swamp Rat
  • Posts: 566
Re: Searching for Layout1 in a list
« Reply #8 on: February 20, 2007, 03:49:00 PM »
*** Grrrrrr ***   :realmad:
I just don't know what I'm doing.

Here's what I put in, and got out.
Quote
Command: (setq Layout (progn (vlax-map-collection (vla-get-layouts
(vla-get-activedocument (vlax-get-acad-object)))
(((_> '(lambda (x) (setq tab-list (cons x tab-list))))))
#<VLA-OBJECT IAcadLayouts 061326b4>    So it's getting the object, or the List.

Command: (if (member "Layout1" (tab-list))
(_> (vl-cmdf "_.layout" "d" "Layout1"))
; error: bad function: (#<VLA-OBJECT IAcadLayout 06133fb4> #<VLA-OBJECT
IAcadLayout 06132044> #<VLA-OBJECT IAcadLayout 06132424>)

Command: (if (member Layout1 (tab-list))(vl-cmdf ".LAYOUT" "D" Layout1))
; error: bad function: (#<VLA-OBJECT IAcadLayout 06133fb4> #<VLA-OBJECT
IAcadLayout 06132044> #<VLA-OBJECT IAcadLayout 06132424>)

Command: (if (member "Layout1" (Layout))(vl-cmdf ".LAYOUT" "D" "Layout1"))
; error: bad function: #<VLA-OBJECT IAcadLayouts 061326b4>

Command: (if (member Layout1 (Layout))(vl-cmdf ".LAYOUT" "D" "Layout1"))
; error: bad function: #<VLA-OBJECT IAcadLayouts 061326b4>

Command: (if (member Layout1 (tab-list))(vl-cmdf ".LAYOUT" "D" "Layout1"))
; error: bad function: (#<VLA-OBJECT IAcadLayout 06133fb4> #<VLA-OBJECT
IAcadLayout 06132044> #<VLA-OBJECT IAcadLayout 06132424>)

I don't understand what format or order this code has to be in to read correctly.
I'm understanding the concept behind it I think.
Quote
If "Layout1" is a member of (Tab-List)
vl-command ".Layout" "Delete" "Layout1"
I would think it would be a straight forward command.
Hangman  8)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Drafting Board, Mechanical Arm, KOH-I-NOOR 0.7mm
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Searching for Layout1 in a list
« Reply #9 on: February 20, 2007, 03:52:45 PM »
Just use the code provide by Alan or Evgeniy.  All you need to do is supply the name to the call.  So call like
Code: [Select]
(i:deletelayout "Layout1")
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Searching for Layout1 in a list
« Reply #10 on: February 20, 2007, 04:53:54 PM »
the one I provided is also where you supply the name of the layout ..

But if you already have the reference to the layout as a vla-object, all you need to do is simply delete it ...

Code: [Select]

(vla-delete MyLayoutObject)

Or you could cycle through the entire list of layouts ...
Code: [Select]
(defun dellayouts (MyLayoutListToDelete / *layouts* *layout*)
  (vl-load-com)
  (setq *layouts* (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))))
  (vlax-for *layout* *layouts*
    (if (member (strcase (vlax-get-property *layout* 'Name)) MyLayoutListToDelete)
      (vla-delete *layout*)
    ) ;_ end of if
  ) ;_ end of vlax-for
) ;_ end of defun

You would use the above like so ...
Code: [Select]
(dellayouts '("LAYOUT1" "11 X 17" "ANOTHER LAYOUT"))

Good Luck
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Searching for Layout1 in a list
« Reply #11 on: February 20, 2007, 06:09:35 PM »
Hangman,

You could try this...it's not much different that what been posted above, but uses wcmatch so wildcards can be used.

Code: [Select]
(defun elayout (filter)

  (vlax-map-collection
    (vla-get-layouts
      (vla-get-activedocument (vlax-get-acad-object))
    )
    '(lambda (x)
       (if (wcmatch (strcase (vla-get-name x)) (strcase filter))
(vla-delete x)
       )
     )
  )
 
)

Use like so: (elayout "*")

Take a look at wcmatch filters in the  developer help file for more info. Once you get your filter setup, deleting tabs will be a cinch.  :-)

Code: [Select]
# (pound)
 Matches any single numeric digit.
 
@ (at)
 Matches any single alphabetic character.
 
. (period)
 Matches any single nonalphanumeric character.
 
* (asterisk)
 Matches any character sequence, including an empty one, and it can be used anywhere in the search pattern: at the beginning, middle, or end.
 
? (question mark)
 Matches any single character.
 
~ (tilde)
 If it is the first character in the pattern, it matches anything except the pattern.
 
[...]
 Matches any one of the characters enclosed.
 
[~...]
 Matches any single character not enclosed.
 
- (hyphen)
 Used inside brackets to specify a range for a single character.
 
, (comma)
 Separates two patterns.
 
` (reverse quote)
 Escapes special characters (reads next character literally).
 


Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Hangman

  • Swamp Rat
  • Posts: 566
Re: Searching for Layout1 in a list
« Reply #12 on: February 20, 2007, 06:59:18 PM »
Hangman,
You could try this...it's not much different that what been posted above, but uses wcmatch so wildcards can be used.
Code: [Select]
(defun elayout (filter)
  (vlax-map-collection
    (vla-get-layouts
      (vla-get-activedocument (vlax-get-acad-object))
    )
    '(lambda (x)
       (if (wcmatch (strcase (vla-get-name x)) (strcase filter))
(vla-delete x)
       )
     )
  )
)

Use like so: (elayout "*")

Ron, this looks really close to what I'm after.  Sorry, I just don't know how to put the VL stuff together yet.  I'm learn'n, but I'm slow.
Basically, I want to take this,
Code: [Select]
(not
  (vl-catch-all-error-p
   (vl-catch-all-apply
    (function
     (lambda ()
      (vla-delete
       (vla-item
        (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
        layoutname
       ) ;_  vla-item
      ) ;_  vla-delete
     ) ;_  lambda
    ) ;_  function
   ) ;_  vl-catch-all-apply
  ) ;_  vl-catch-all-error-p
 ) ;_  not

And incorporate it into this.
Code: [Select]
    (setq Layout
      (progn
        (vlax-map-collection (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object)))
          '(lambda (x) (setq tab-list (cons x tab-list)))))
      (setq tab-list (vl-sort tab-list '(lambda (x y) (< (vla-get-taborder x) (vla-get-taborder y)))))
      (setq tab-1 (vl-remove "Layout1" (mapcar '(lambda (x)(vla-get-name x))tab-list)))
      (setvar "ctab" (car tab-1)))

As you can see from all my posts, I've been trying, but I keep messing it up.  Perhaps I am asking something that can't be done the way I am looking to do it.
But I have enough examples here, I SHOULD be able to do something with it.  (right)   :lmao:

Thank you, I'll keep working on it.
Hangman  8)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Drafting Board, Mechanical Arm, KOH-I-NOOR 0.7mm
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Searching for Layout1 in a list
« Reply #13 on: February 20, 2007, 07:17:32 PM »
Why do you want to make a list of all the layouts?  If you only want to erase the layout, then there is no need to go about making a list of them.  If you have a reason for making a list, then we could code something up that will erase it if it's there, and if it's not (meaning doesn't match the name) when we will add it to the list.

I guess it would help us to understand what the whole program is going to be doing.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Hangman

  • Swamp Rat
  • Posts: 566
Re: Searching for Layout1 in a list
« Reply #14 on: February 20, 2007, 07:46:51 PM »
Why do you want to make a list of all the layouts?  If you only want to erase the layout, then there is no need to go about making a list of them.  If you have a reason for making a list, then we could code something up that will erase it if it's there, and if it's not (meaning doesn't match the name) when we will add it to the list.

I guess it would help us to understand what the whole program is going to be doing.

I need a list of the layouts so I can get the layout I need for plotting.  There are two to three layouts, depending on the detail.
Some of these details have not been updated in a while and have no layout, just "Layout1".  There is nothing set in it, not even a viewport.  Some of these details have two or three layouts, "Layout1", "8.5 x 11", &/or "Detail1".
I'm doing a mass cleanup of these details so I can plot a DWF of them for the engineers.  I have a proto called "8.5 x 11" I am pulling into the drawing.  But I need to delete the old "8.5 x 11" and the "Layout1", then import the new "8.5 x 11" and set it as current, leaving the other layouts alone.  Once I have the new "8.5 x 11" layout as current, I can use the variable 'cvport' to go in & zoom to its proper scale.

I'm just about there if I can figure out how to delete the 1, 2, or 3 layouts while keeping any others and setting a new imported layout as the current layout so I can plot it.

I hope this made sense, otherwise it's a really, really, really, really, really, really, really, really, really long story.   :|
Hangman  8)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Drafting Board, Mechanical Arm, KOH-I-NOOR 0.7mm
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~