Jump to content

Lisp for switching layouts


OMEGA-ThundeR

Recommended Posts

Hi,

 

No... it's not what you think it would ask.

 

I have a large number of layouts, al numbered like '01 - 02 - 03 - ... - 27 - 28 - 29 - etc.'

 

It's for an project that comes back a couple of times a year and i need to switch a lot from layout to layout.

 

I thought i could do that faster via a LISP routine.

 

I can switch from layout in the command bar using the '-LAYOUT' command. Using the 'SET' option to manualy entering the layout i want.

However i just have to go from the current to the next or previous layout.

 

My LISP knowledge is not high enough to make what i need.

 

I guess it requires some variabeles and calculations.

 

In website terms, in php, it would be something like this:

 


$var_next = + 1;
$var_prev = - 1;

$current = 'Find out what the current layout name is (i.e. 21)';

Function next() {

Command: Next
("-Layout" "set" "$var_next $current") #giving ("-Layout" "set" "+ 1 21")
}

Function previous() {

Command: Prev
("-Layout" "set" "$var_prev $current") #giving ("-Layout" "set" "- 1 21")
}

 

So when i enter the command NEXT i go to the layout that is called 22 and when i enter the PREV command i go to the layout called 20.

 

 

Is there a way (sure there is :P) and/or can somebody build that?

 

It would help me a lot.

Link to comment
Share on other sites

I know there's a neat one by Alanjt floating around here somewhere, you might give it a search... Short of that, this was written quickly:

 

(vl-load-com)

(defun c:FOO (/ tab)
 (if (and (setq tab (strcase (getstring T "\nEnter tab name: ")))
          (vl-position
            tab
            (mapcar 'strcase (cons "model" (layoutlist)))
          )
     )
   (if (/= (strcase (getvar 'ctab)) tab)
     (setvar 'ctab tab)
     (prompt
       (strcat "\n** \"" tab "\" tab is already current ** ")
     )
   )
   (prompt (strcat "\n** \"" tab "\" tab not found ** "))
 )
 (princ)
)

Link to comment
Share on other sites

Thats not really what i had in mind, and in compressed form it's the same as what i allready had;

 

(Defun C:FOO ()
(command "-layout" "set" pause)
(princ)
)

 

To not put all you guys to work my main problem is how to find out the current layout name and put that in a variable (setq ??)

 

Then i need to calculate (- or + 1 21); gives me 20 or 22 like in the previous example.

 

Then i need a way to read out that variable so i can put it on the pause location in the above lisp.

Link to comment
Share on other sites

... my main problem is how to find out the current layout name and put that in a variable (setq ??)

 

Consider the CTAB system variable, already noted above.

 

Then i need to calculate (- or + 1 21); gives me 20 or 22 like in the previous example.

 

If the CTAB system variable is say "21", then convert the string to an integer via the ATOI function, then perform your math calculation(s). To convert back to a string from an integer, either of the RTOS, or ITOA functions should work.

 

Then i need a way to read out that variable so i can put it on the pause location in the above lisp.

 

Why not instead test for valid input first, using an IF statement, then supply that valid input in lieu of the PAUSE, else report to the user. Lemon squeezy... And already included in the CTAB command (since you're reinventing the wheel). :thumbsup:

Link to comment
Share on other sites

It kinda needs to be quicker then moving the mouse and clicking on the layout tab i need.

 

Just typing in NEXT or PREV should switch the layout.

 

At the moment i am searching the whole interwebs to get the current layout in a variable.

 

I get the right number with

 

(getvar 'ctab)

 

But i get an error ; error: bad argument type: numberp: "21". 21 is the name of the current layout

 

i'm studying every lisp i can find but i can't seem to find where it goes wrong.

 

Since the script stops there i do not know if the rest is even correct, but this is what i got so far.

 

(Defun C:next(/ current newlayout)

(setq current (getvar 'ctab))

(setq newlayout (+ current 1))

(command "-layout" "set" !newlayout)
(princ)
)

 

And @RenderMan, IFs, WHILEs and things like that are even more a mistery than figuring out how to set an variable. So i try to keep it simple :oops:.

Link to comment
Share on other sites

After reading your subsequent posts more carefully, try the following two commands:

 

([color=BLUE]defun[/color] c:la+ ( [color=BLUE]/[/color] l n )
   ([color=BLUE]setq[/color] l (getorderedlayouts)
         n ([color=BLUE]length[/color] l)
   )
   ([color=BLUE]setvar[/color] 'ctab ([color=BLUE]nth[/color] ([color=BLUE]rem[/color] ([color=BLUE]1+[/color] ([color=BLUE]vl-position[/color] ([color=BLUE]getvar[/color] 'ctab) l)) n) l))
   ([color=BLUE]princ[/color])
)

([color=BLUE]defun[/color] c:la- ( [color=BLUE]/[/color] l n )
   ([color=BLUE]setq[/color] l (getorderedlayouts)
         n ([color=BLUE]length[/color] l)
   )
   ([color=BLUE]setvar[/color] 'ctab ([color=BLUE]nth[/color] ([color=BLUE]rem[/color] ([color=BLUE]+[/color] n -1 ([color=BLUE]vl-position[/color] ([color=BLUE]getvar[/color] 'ctab) l)) n) l))
   ([color=BLUE]princ[/color])
)

([color=BLUE]defun[/color] getorderedlayouts ( [color=BLUE]/[/color] l )
   ([color=BLUE]vlax-for[/color] x (aclayouts)
       ([color=BLUE]setq[/color] l ([color=BLUE]cons[/color] ([color=BLUE]list[/color] ([color=BLUE]vla-get-taborder[/color] x) ([color=BLUE]vla-get-name[/color] x)) l))
   )
   ([color=BLUE]mapcar[/color] '[color=BLUE]cadr[/color] ([color=BLUE]vl-sort[/color] l '([color=BLUE]lambda[/color] ( a b ) ([color=BLUE]<[/color] ([color=BLUE]car[/color] a) ([color=BLUE]car[/color] b)))))
)

([color=BLUE]defun[/color] aclayouts [color=BLUE]nil[/color]
   ([color=BLUE]eval[/color]
       ([color=BLUE]list[/color] '[color=BLUE]defun[/color] 'aclayouts '[color=BLUE]nil[/color]
           ([color=BLUE]vla-get-layouts[/color] ([color=BLUE]vla-get-activedocument[/color] ([color=BLUE]vlax-get-acad-object[/color])))
       )
   )
   (aclayouts)
)

([color=BLUE]vl-load-com[/color]) ([color=BLUE]princ[/color])

Type 'la+' & 'la-' to move between layouts.

Link to comment
Share on other sites

That seems to do exactly what i need. But how its build up is like algebra for me.

 

I kinda feel bad for calling it an hit and run now i got what i need, but i still would like to know what went bad on my error.

 

Why can't i get the 'ctab in a variable?

 

(getvar 'ctab) seems to get me the name set for the current layout, but after

 

(setq current (getvar 'ctab))

 

i get the 'numberp' error :(.

Link to comment
Share on other sites

Why can't i get the 'ctab in a variable?

 

(getvar 'ctab) seems to get me the name set for the current layout, but after

 

(setq current (getvar 'ctab))

i get the 'numberp' error :(.

 

Note that the CTAB System Variable holds a value of string type, and you are attempting to perform a numerical operation on a string, resulting in a bad argument type: numberp error (numberp, since the arithmetical function is testing the data type of the supplied argument to ensure it is numerical).

 

To fix your code, you would need to use either the atoi function or atof to convert the string to an integer of float, before performing the arithmetic operations. However, for this solution to be successful you will need to ensure that all layouts are named numerically, and that the next layout exists before attempting to switch to it.

 

In my example, I use the taborder property of each layout in the Layouts Collection so that the program performs independently of the layout naming convention being used in a drawing; I also use the rem function for modular arithmetic to cycle back to the first layout when the last layout is reached.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...