How do I create a layer with the same name as a user-deleted layer?


#1

If a user deletes a layer, the layer isn’t deleted enough that a RhinoCommon plugin can re-add a layer with that name. (LayerTable.Find will also still find the old index.) Is there a way to do this add successfully? Retrieving the old, deleted layer index and calling Delete or Purge don’t work.


(Alain Cormier) #2

Hi.

I just did a quick test and if the layer is marked deleted then

doc.Layers.Add("same name as deleted layer", System.Color.Black)

works as expected.


#3

Maybe, but if you create a new Layer object (with a new Id) and attempt to add it that way, the Id of the added layer doesn’t equal the Id of the layer you passed it. It instead equals the Id of the old, deleted layer. A weird thing I’ve discovered is that the layer colors match, though:

var newLayer = new Rhino.DocObjects.Layer()
{
    Name = "test",
    Id = Guid.NewGuid(),
    Color = System.Drawing.Color.Purple
};
var addedLayer = doc.Layers[doc.Layers.Add(newLayer)];
RhinoApp.WriteLine("ids {0}", addedLayer.Id == newLayer.Id ? "match" : "don't match!");
RhinoApp.WriteLine("colors {0}", addedLayer.Color == newLayer.Color ? "match" : "don't match!");

The reason I discovered this is that I’m attempting to add a layer hierarchy, and since layer parentage is determined by Id, I wasn’t able to do it if the parent layer shared a name with an old, deleted layer.


#4

Hi,
Maybe if you add a layer check if it is already deleted and use this instead:

Dim oldguid, newguid

For Each layer In doc.Layers

If layer.IsDeleted Then

oldguid = layer.Id
Dim newlayer As New DocObjects.Layer
newlayer.Id = layer.Id
newlayer.Name = layer.Name
newlayer.Color = layer.Color
doc.Layers.Add(newlayer)
newguid = newlayer.Id
MsgBox(oldguid.ToString & vbNewLine & newguid.ToString)

End If

Next

So if it is not deleted add it the normal way. Else use old id? :slight_smile:

Msgbox was to test if the Guid’s were the same