Help with script that Exports items on layers to separate file

i’ve got a script here that exports objects by layer, i’m having an issue with it reading layer names with “,” (commas) in them. It’s just skipping over those layers. any help with be appreciated, this will save me tons of hours. thank you.

here’s an example of the layer name:
2020-01-24 12_38_11-comparison-temp-RYAN.3dm (14532 KB) - Rhinoceros 5.0 Commercial - Perspective

Option Explicit

Call ExportLayerObjectsSTEP()
Sub ExportLayerObjectsSTEP()

	' Declare local variables
	Dim strPath, strFile
	Dim arrLayers, strLayer
	Dim arrSelected

	' Get the path to and name of the current document.
	' Surround with double-quotes in case path includes spaces.
	strPath = Chr(34) & Rhino.DocumentPath & Rhino.DocumentName & Chr(34)

	' Get names of all layers
	arrLayers = Rhino.LayerNames

	' Disable redrawing
	Rhino.EnableRedraw False

	' Process each layer
	For Each strLayer In arrLayers

		' Unselect all
		Rhino.Command "_-SelNone", 0

		' Select all objects on layer. Surround layer name
		' with double-quotes in case it includes spaces.
		Rhino.Command "_-SelLayer " & Chr(34) & strLayer & Chr(34), 0

		' Make sure some objects were selected
		arrSelected = Rhino.SelectedObjects
		If IsArray(arrSelected) Then

			' Generate a modified path string
			' that includes the layer name
			strFile = strPath
			strFile = Replace(strFile, ".3dm", "_" & strLayer & ".stp")

			' Export the selected objects
			Rhino.Command "_-Export " & strFile, 0
		End If
	Next

	Rhino.MessageBox("Files Exported")

	' Unselect all
	Rhino.Command "_-SelNone", 0

	' Enable redrawing
	Rhino.EnableRedraw True

End Sub

In the last part of the code where you replace the file name with the layer name you don’t wrap strLayer in the two quotes &Chr(34). My guess is that makes it fail.

i ended up using a different one that works. thanks!

Hello -

Try:

Sub ExportLayerObjectsSTEP()

	Dim strPath, strFile
	Dim arrLayers, strLayer
	Dim arrSelected

	strPath = Chr(34) & Rhino.DocumentPath & Rhino.DocumentName & Chr(34)

	arrLayers = Rhino.LayerNames()

	Rhino.EnableRedraw False

	For Each strLayer In arrLayers

		Rhino.UnselectAllObjects()
	       arrSelected = Rhino.ObjectsByLayer(strLayer, True)

		If IsArray(arrSelected) Then
			strFile = strPath
			strFile = Replace(strFile, ".3dm", "_" & strLayer & ".stp")
			strFile = Replace(strFile, ",", "_")
			Rhino.Command "_-Export " & strFile, 0
		End If
	Next

	Rhino.MessageBox("Files Exported")


	Rhino.Command "_-SelNone", 0

	Rhino.EnableRedraw True

End Sub

-Pascal

1 Like

Hi @pascal I get this message when using it as button with _-RunPythonScript (

image

@DiegoKrause It’s a Rhinoscript

1 Like

thanks. I thought they were the same haha sorry