Make previous layer active

Goodmorning scripting friends, is there a way to create a script/ or macro (if it can be done that simply) to make whatever layer was previously current, current again?

I have aliases that put things in certain layers, for example, when I hit hh, I create a hatching and put it in particular layer that has it’s own particular properties, but – rather than stay in the hatch layer, I would like to go back to the layer I was on before I ran this alias. I am hoping to hit another alias that would make the layer that was current before, current again.

Any help would be really appreciated.

Thank you.

If you are changing layers inside a script, it’s certainly possible to record which layer was previously current ad restore it… The question I have is, if you are simply making a hatch and putting it onto some other layer, you should not need to change the current layer… So maybe I’m not understanding the real problem.

–Mitch

Hi Mitch,

Good question. What I have is several aliases that are used for specific tasks that involve changing layers:

  1. ! _-Layer _Current “detail” _Enter _Detail _Add
  2. !_-Layer _Current “hatch” _Enter _Hatch
  3. ! _-Layer _Current “annot” _Enter _DIM _dimvertical _enter

In each case, what happens is that I type in two letters like “hh-return” and the program makes the “hatch” layer current and puts me in to hatch mode. I create the hatch which is placed in its layer, but then remain in this new layer. This is the point where I wish I could type in a second alias that would make my previous layer current so that I can go back to what I was doing before in the layer that I was working in before — or if possible rewrite the above macros so they pause for me to do the task but then once done make the previous layer current.

Makes sense?

Thank you.

Well, I don’t think you will be able to do what you want with a macro - one of the limitations of macros is they do not have the ability to store information such as a particular layer to restore current. (there is no “PrevLayer” command as there is with viewports or cplanes)

So, you’d either have to have a script to do this, or try to reverse the order, that is to make the hatch first on the current layer and then use ChangeLayer to change it to the “hatch” layer. Depending on what you are doing inside the Hatch command though, this might not be all that reliable…

So, perhaps something like the following might work for you:

import rhinoscriptsyntax as rs

c_layer=rs.CurrentLayer()
h_layer="hatch"
if not rs.IsLayer(h_layer): rs.AddLayer(h_layer)
rs.CurrentLayer(h_layer)
rs.Command("_Hatch")
rs.CurrentLayer(c_layer)

Beware, python is case sensitive… including the layer names.

–Mitch

Thank you, Mitch. That’s awesome.

Here’s perhaps a cleaner way to do it - it allows you to bail from the Hatch command without the current layer being changes or a new “hatch” layer added if it isn’t there already.

import rhinoscriptsyntax as rs

rs.Command("_Hatch")
lco=rs.LastCreatedObjects()
if lco:
    h_layer="hatch"
    if not rs.IsLayer(h_layer): rs.AddLayer(h_layer)
    rs.ObjectLayer(lco,h_layer)

–Mitch

Very cool, Mitch. I will try it. Thank you. I really appreciate it.