I am looking for a way to create blocks from layers and name the blocks with the layer name automatically for all layers. I found a Rhino script (see https://www.modelical.com/en/pack-rhino-objects-one-block-per-layer-scripting/ 3). This script does the transformation, but it gives a syntax error at a certain line (line 41, as far as I remember). How can I correct the Rhino script, so that everything works without a syntax error?
Dunno, works here on a couple of test files - can you post a file where it errors out?
Model08Joined02Cut08.3dm (2.3 MB)
Dear Helvetosaur, yes, sure. Please look also at the Error.jpg.
What is wrong in line 42?
The script doesn’t check for bad objects - you have one in there. Use SelBadObjects to find it. If you isolate that one and fix it, the script runs fine.
Model08-fixed.3dm (2.6 MB)
What’s actually happening on line 42 is that the block is not created for the bad object, so strBlock is Null and InsertBlock fails. There is no error checking in the script, so instead of exiting politely and telling you there is a problem with an object, it just errors out.
Perfect, thanks!
Dear Helvetosaur,
Something is still not working. I have a model (see uploaded model), that does not have any bad objects (“SelBadObjects”), and that gives again the error in line 42. The blocks are correcly created. How can I get rid of this nasty error message?Model08Joined02Cut09.3dm (2.4 MB)
It doesn’t error out here with your file…
The thing is, this is not my script. As written, it does not do any error checking at all - which is usually a bad idea if you are going to release a script to the public. So even if there are no bad objects, if something does go wrong with the process of creating a block, it will error out.
Here is a slightly cleaned up version.
Option Explicit
'Script written by Roberto Molinos @robertomolinos
'Script copylefted by Modelical // free to use, improve and share
'Script version Friday, 21 March 2014 19:58:18
'Modified by Mitch Heynick 29.11.19 - added error checking and some cleanup
Call Layers2Blocks()
Sub Layers2Blocks()
Dim strFile
Dim arrLayers, strLayer, strBlock
Dim arrSelected, arrInsertPoint
Dim intBadCount : intBadCount = 0
'Create insertion point at 0,0,0
arrInsertPoint = Array(0, 0, 0)
' Get names of all layers
arrLayers = Rhino.LayerNames
' Disable redrawing
Rhino.EnableRedraw False
' Process each layer
For Each strLayer In arrLayers
' Unselect all
Rhino.UnselectAllObjects
' Select all objects on layer.
Rhino.ObjectsByLayer strLayer, True
' Make sure some objects were selected
arrSelected = Rhino.SelectedObjects
If IsArray(arrSelected) Then
'Set current layer so block is created on the same one
Call Rhino.CurrentLayer(strLayer)
'Create block
strBlock = Rhino.AddBlock(arrSelected, arrInsertPoint, strLayer, True)
'Check to see if a block actually got made
If Not IsNull(strBlock) Then
'Delete objects as they are now in the block
Call Rhino.DeleteObjects(arrSelected)
'Insert the block
Call Rhino.InsertBlock(strBlock, arrInsertPoint)
Else
intBadCount = intBadCount + 1
End If
End If
' Unselect all
Rhino.UnselectAllObjects
Next
' Unselect all
Rhino.UnselectAllObjects
' Enable redrawing
Rhino.EnableRedraw True
'reporting
If intBadCount > 0 Then
Dim strErrorMsg
strErrorMsg = " layers unable to be converted to blocks"
Call Rhino.Print("Warning: " & Cstr(intBadCount) & strErrorMsg)
End If
End Sub
Thank you. The scripts creates the 56 blocks, however the warning “Warning: 56 layers unable to be converted to blocks” comes anyway. Do I simply neglect it? The situation is similar to the previous: I get the blocks, however I get also an error or, with your script, a warning, which should not come.
There is definitely something different in your Rhino than mine - I get no errors. intBadCount which starts at 0 should only get incremented if the result of adding a block is Null, and the error message only prints at the end if intBadCount is not 0 (i.e. at least one operation of block creation returned Null). Looks like the block creation might actually happen, but still return Null.
Since I cannot reproduce it here, I don’t really know where/why it’s going wrong on your end.
Dear Helvetosaur, is it possible for you to convert this script to python version, as i am using rhino for mac?
Thank you for the quick reply. It works, changes objects into blocks divided into layers. However, after the action, all changed items disappear and you can retrieve them from the block list. It’s best if that doesn’t happen. Additionally, can you change two things. Only selected (not visible) elements turn into a block. The second thing is that the block name will be created from the combination of the layer name with sublayers. In my case, I have two more levels of sublayers from the main layer.
So the name of the block would be: “4 PLYTA SKL F/F CZARNA 6.5 mm”
Thanks again for your reply.
Sorry, that was an oversight, left out one line, it is corrected above (re-download).
The above was a basic script for layer by layer conversion with no sublayers, and not acting on anything that is not selectable. What you want is a bit more specialized, acting on parent layers and including all objects on any sublayers of the parent - plus the naming convention. Certainly possible with some work.
What I don’t understand is this phrase:
Does that mean you want objects that can be seen but are not selectable (i.e. locked or on locked layers) to be included? But not including anything that might be invisible (hidden or on off layers)?
Sorry for not being clear. I would like to create blocks only from selected objects. Others visible, invisible, locked or on a locked layer objects did not turn into a block.