Deleting layers

I am running a script that reads an machine event log and I create a layer (with 2 sub layers) for each event, it is possible to create over 100 layers or more. When I run the script a second time, I delete all the layers. This is a very slow operation that can take 60 seconds or more. Interestingly, if I select the layers manually, they are deleted immediately.

This is my current code:

		arrObjects = Rhino.AllObjects

		If IsArray(arrObjects) Then Rhino.DeleteObjects arrObjects

		arrLayers = Rhino.LayerNames
		If IsArray(arrLayers) Then
			For Each strLayer In arrLayers
				Rhino.PurgeLayer strLayer
			Rhino.AddLayer "00000 Default"
		End If

Is there a quicker way to do this?

Another solution would be to programmatically create a new empty document, but I can’t see any RhinoScript method that lets me do this.

Thanks for your help.

Hi Keith,

since you delete all objects in your script beforehand, there is no purpose to use Rhino.PurgeLayer, you could use Rhino.DeleteLayer instead if no blocks are involved. Using a for each loop is generally slower compared to using a indexed for next loop.

You can set the current document modified flag to false, then open a new document using Rhino.Command like this

Option Explicit

Call Main()
Sub Main()

    Dim cmd
    Rhino.DocumentModified False
    cmd = "_-New _None"
    Rhino.Command cmd, False
End Sub

Note that “None” in the cmd line stands for the template name. You can replace this with the path of the template to open. If the path contains spaces, enclose it witin double quotes eg.

CHR(34) & strPath & CHR(34)


Thanks for the reply, I modified the loop by removing the PurgeLayer and using a counter but it was still slow. I had 1929 layers and it took 197s to delete them all! Looks like Rhino needs a Rhino.DeleteAllLayers (except one or more).

I then replaced the code with the Rhino.Command method and it is much faster.

Thanks for your help.

Just to check: did you cut the redraw using Rhino.EnableRedraw(False)? Every time the layer box has something happen in it, the screen is refreshed. Here 1000 layers are instantaneously created or deleted with the redraw disabled, and it takes about 30-40 seconds to do the same with redraw enabled. This in either Python or VB. The Rhino command probably has this built-in.


Do this:

Call Rhino.EnableRedraw(False)

' Delete layers here...

Call Rhino.EnableRedraw(True)

Dale, & Helvetsaur,

You fixed it! I am using EnableRedraw to speed up the loading of the features read from the log file, but I was deleting the layers before I set it to False, just a bad location and I didn’t make the connection that the layer problem was the same issue, i.e. Rhino will appear to be slow when drawing entities dynamically!

Thanks again…