Instead, if i bake a layer, get it from table, Delete() it from layerTable, set new Id and finally bake again, I end up with a baked layer with an ID choosen by me.
Is this behavior new in WIP? A would say that I have done this much easier in the past, as I did in my previous post
Edit: Well… baking, deleting, setting, rebaking is not much work, but figuring it out get me hours… Is this the only way to bake a layer with a prefixed ID??
import System
import Rhino
import scriptcontext as sc
def dump_layer(layer):
if layer:
print 'Name: {0}'.format(layer.Name)
print 'Index: {0}'.format(layer.Index)
print 'ID: {0}'.format(layer.Id)
print 'ID Locked: {0}'.format(layer.IdIsLocked)
def test_layer_id(name):
# Try finding the layer in the document
index = sc.doc.Layers.FindByFullPath(name, Rhino.RhinoMath.UnsetIntIndex)
if index == Rhino.RhinoMath.UnsetIntIndex: return
layer = sc.doc.Layers[index]
if not layer: return
# dump
dump_layer(layer)
# Cannot delete current layer
if index == sc.doc.Layers.CurrentLayerIndex: return
# Delete layer
sc.doc.Layers.Delete(layer)
# Since the document layer has been deleted, our reference it
# no longer valid. So, set it to None so we don't try to use it.
layer = None
# Create a new layer and set its properties
new_layer = Rhino.DocObjects.Layer()
new_layer.Name = name
new_layer.Id = System.Guid.NewGuid()
# dump
dump_layer(new_layer)
# Add the new layer to the document
index = sc.doc.Layers.Add(new_layer)
# Get the layer from the document
layer = sc.doc.Layers[index]
# dump
dump_layer(layer)
# Test the above
test_layer_id('test')
Thank Dale, please read my comment inserted in your code:
import System
import Rhino
import scriptcontext as sc
def dump_layer(layer):
if layer:
print 'Name: {0}'.format(layer.Name)
print 'Index: {0}'.format(layer.Index)
print 'ID: {0}'.format(layer.Id)
print 'ID Locked: {0}'.format(layer.IdIsLocked)
def test_layer_id(name):
# Try finding the layer in the document
index = sc.doc.Layers.FindByFullPath(name, Rhino.RhinoMath.UnsetIntIndex)
if index == Rhino.RhinoMath.UnsetIntIndex: return
layer = sc.doc.Layers[index]
if not layer: return
# dump
dump_layer(layer)
# Cannot delete current layer
if index == sc.doc.Layers.CurrentLayerIndex: return
# Delete layer
sc.doc.Layers.Delete(layer)
# Since the document layer has been deleted, our reference it
# no longer valid. So, set it to None so we don't try to use it.
layer = None
# Create a new layer and set its properties
new_layer = Rhino.DocObjects.Layer()
new_layer.Name = name
# AITOR'S COMMENT
new_layer.Id = System.Guid.NewGuid() # AITOR'S COMMENT: What I need is to set the old Layers id
# AITOR'S COMMENT: Not a new random one. Is this possible?
# dump
dump_layer(new_layer)
# Add the new layer to the document
index = sc.doc.Layers.Add(new_layer)
# Get the layer from the document
layer = sc.doc.Layers[index]
# dump
dump_layer(layer)
# Test the above
test_layer_id('test')
Thank Dale. I finally discovered the piece of info that I was missing: The id of the layers that are present in the layer table (including the deleted layers) is not possible to be reused with other layer. This was not obvious in the case of deleted layers… The only way (afaik) to use that ID is using the Undelete() method of layerTable class, supposing that the layerId we want to use belong to a deleted layer. (otherwise layer+layerId would were already present in the document)
phiu, this has taken a couple of days, but finally working!