Layers control in Python


#1

The layers commands in Python seem to have some problems in the latest Mac Rhino5 WIP as of 6/22/17.

  1. ExpandLayer does not work, either for expanding or contracting layers.

  2. I have a sort routine for sub-layers that creates a new working layer (at the bottom of the layers), changes one of the existing layers to be a sub layer of the new layer, then changes the layer hierarchy back down. In Windows Rhino 5, the result is that the layer needing to move becomes a sub-layer of the new layer, then drops back to the original hierarchy level at the end of the layer list. In Mac, the layer is placed in the sub-layer of the new layer (end of layers position) but the layer does not get expanded as in Windows. The expand function will not show the sub-layer and when the new sub-layer is placed back to its normal level, it gets put back to its previous location in the list, not at the end where I want it.

A better solution would be to have a MoveLayer or SortLayer function built-in, but this method should work like the Windows Rhino does.

Here is the SubLayer sort routine:

import rhinoscriptsyntax as rs
from scriptcontext import doc
from System import Guid
import re   # for sort

# sort sublayers
def test_sort():
    SortSubLayers('BoltGen')
#
#---------------------------------------
# SubLayer Sort Routine
def SortSubLayers(ParentLayer):
    ParentLayerID=0
    sorted_layer_names = []
    
    for layer in doc.Layers:
        if layer.Name == ParentLayer:
            if not layer.IsDeleted:
                ParentLayerID = layer.Id
                break
    if ParentLayerID == 0: return False 
    # Parent layer ID found
    for layer in doc.Layers:
        if not layer.IsDeleted:
            if layer.ParentLayerId == ParentLayerID:
                sorted_layer_names.append (layer.FullPath)
                
    # We have list. Set up working layer.
    tmp_layer_name = rs.AddLayer(Guid.NewGuid().ToString(),parent=ParentLayer)
    tmp = doc.Layers[doc.Layers.FindByFullPath(tmp_layer_name, False)]
    
    rs.EnableRedraw(True) # must be active to work, but the parent layer may be contracted.
    
    # sort for alpha numeric
    sorted_layer_names = sort_alphanum(sorted_layer_names)
    for Full_Path in reversed(sorted_layer_names):
        Wk_Layer = doc.Layers[doc.Layers.FindByFullPath(Full_Path, False)]
        parLayer = Wk_Layer.ParentLayerId # save parent
        Wk_Layer.ParentLayerId = tmp.Id # set temp as parent layer for Full_Path to shift index location
        Wk_Layer.CommitChanges() # force completion
        Wk_Layer.ParentLayerId = parLayer #position work layer layer back to parent layer (Hopefully at end of layers list)
        Wk_Layer.CommitChanges() # force completion
    
    doc.Layers.Delete(tmp.LayerIndex, True) # remove working layer
    #rs.MessageBox('Finished test') # test list
    return True

def sort_alphanum(LayerList):
  convert = lambda text: float(text) if text.isdigit() else text
  alphanum = lambda key: [ convert(c) for c in re.split('([-+]?[0-99]*\.?[0-99]*)', key) ]
  LayerList.sort( key=alphanum )
  return LayerList

#----------------------------------------------------------------------

if( __name__ == "__main__" ):
    test_sort()

#2

I found the layer problem in Mac WIP. The Layer Order index does not change when moving layers to another layer as a sub layer. In Windows, each time the layer is moved, the LayerOrder changes to the new position in the Layers list. In Mac, that number remains the same even thought the display shows the layer as a sub layer the alternate layer correctly. When changing the layer position back, it drops in where it was located before which makes sorting impossible (in this method of sorting).


(Dan Belcher) #3

@pascal Can you discern what Ray is talking about well enough to log an issue?


(Pascal Golay) #5

Yep -

https://mcneel.myjetbrains.com/youtrack/issue/MR-3145

-Pascal