I’m trying to figure out how to make a script/macro
How or where can I find out what functions exist, what they do and the proper syntax/sub options?
Right now i’m trying to figure out a script that does:
Select layers by layer name = Manual input A -> *string1*string2*string3*
For each layer that is true on above search:
Replace in layer name = Manual input B *string6*string8*
So basically I want to be able to do a Search & replace for all my layer names.
For example all springs TH-12-7 need to become TH-12~7
I want to be able to do:
Search layer names for *TH-12-7*
Change for all results layer name part *TH-12-7* for *TH-12~7*
Mainly I would like to know, how I can figure out how to make such a script.
import rhinoscriptsyntax as rs
def replaceLayerNames():
layersL = rs.LayerNames()
find = rs.StringBox("Which part of your Layer's name are you looking for?",None,"Find")
replace = rs.StringBox("Replace with?",None,"Replace")
if find and replace:
newLayersL = []
for layer in layersL:
newLayer = layer.replace(find,replace)
newLayersL.append(newLayer)
for i in range(len(newLayersL)):
new_name = rs.RenameLayer(layersL[i],newLayersL[i])
return
else:
print "You did not enter search or replace word"
return
replaceLayerNames()
Try running the attached python file (Tools -> PythonScript -> Run, and choose the downloaded “replacePartOfLayerName.py”)
I just found this, it seems to be what I want on first glance, but does anyone have any more tips on how to create a script?
I was thinking this was gold, but I can’t figure out how to execute the script, i’ve added it to rhino, but there is no command line option “LayerRename”
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' LayerRename.rvb -- November 2011
' If this code works, it was written by Dale Fugier.
' If not, I don't know who wrote it.
' Works with Rhino 5.0.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit
Sub LayerRename()
Dim strFind, strReplace
Dim arrLayers, strLayer, strName
If Rhino.ExeVersion <> 5 Then
Call Rhino.Print("This RhinoScript requires Rhino 5.0.")
Exit Sub
End If
strFind = Rhino.StringBox("String to find")
If IsNull(strFind) Then Exit Sub
strReplace = Rhino.StringBox("Replace with")
If IsNull(strReplace) Then Exit Sub
arrLayers = Rhino.LayerIds
For Each strLayer In arrLayers
strName = Rhino.LayerName(strLayer, False)
If InStr(strName, strFind) > 0 Then
strName = Replace(strName, strFind, strReplace)
strName = Trim(strName)
Call Rhino.RenameLayer(strLayer, strName)
End If
Next
End Sub
Thank you for your reply. I am getting the following error after choosing the search & replace terms.
Exception Occured
Message: ;33.153.501N;::;33.153.501N_2D;::1;VRN8601800-07;Valve body;M36x1.25-3-G1/2-Ø28.5-Ø45.5-C125;Assembly part;1;[BLOCK] does not exist in LayerTable
Traceback:
line 16, in __getlayer, “C:\Users\peter\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript\layer.py”
line 436, in RenameLayer, “C:\Users\peter\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript\layer.py”
line 14, in replaceLayerNames, “R:\Rhino\Rhino Scripts\replacePartOfLayerName.py”
line 20, in , “R:\Rhino\Rhino Scripts\replacePartOfLayerName.py”
Honestly have no idea what that error message means.
I do not think function would raise a error if you typed some part of the layer name that does not exist in your LayerTable.
And there would be no problem with Norwegian letters either.
No clue.
Steve or Mitch could probably help you.
Since I see the name “block” in there I wonder if you have referenced blocks in the file with layer names that are not necessarily in the layer table? I don’t know how layer names work with referenced blocks…
The given layer is a set of self-made layers with 3 levels. The file never has any problems in renaming layers manually neither self-made nor the layers which hold connection to the inserted blocks.
On the other hand, do you have any clue why the other function would not run in Rhino; the LayerRename.rvb?
Dale’s script (like most of mine) is not made for drag and drop and as such does not add an alias with the command. What is posted above also lacks the Call function that actually runs the script.
So, you can load it into the Rhinoscript editor and run it from there to try it out - in that case you need to add:
Call LayerRename() above the Sub LayerRename()
If you then decide it’s what you want, it can be re-set up to drag and drop and add the alias -
First, remove the Call LayerRename() if you added to test as above.
Then add the following at the top of the script just under Option Explicit:
Then save the script file somewhere where it will “live” permanently (like your “scripts” folder), from there, drag and drop it into a running instance of Rhino (only one open at a time). That should add the alias and the script should run when you type the name.
I believe the error has something to do with the sub-layers, take a look at this change in layer names,
For the first set of sublayers, it did replace the “Ø10.6-Ø8.2” with “Ø10.6~Ø8.2”, but it put the name of the parent layer in front of the renamed sublayer.
Then at the second hit, it put the parent layer infront of it again, and then the following error arose. And a picture of the naming changes:
[ Exception Occured ]
Message: ;33.200.001N;::;33.200.001N_2D;::1;TH-10.6 8.6 0.6 34;Spring;Ø10.6-Ø8.6xØ0.6-L34;Stainless steel;1;[BLOCK] does not exist in LayerTable
Traceback:
line 16, in __getlayer, “C:\Users\peter\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript\layer.py”
line 436, in RenameLayer, “C:\Users\peter\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript\layer.py”
line 14, in replaceLayerNames, “R:\Rhino\Rhino Scripts\replacePartOfLayerName.py”
line 20, in , “R:\Rhino\Rhino Scripts\replacePartOfLayerName.py”
After a test in a separate file; Here I did not receive the error messages, but you can see it still does not work 100%
Replace A;22; -> A;Q33V, as you can see it adds the parent layer to the sublayer, this is unwanted behaviour.
Replacing “Jaap;A;” with “Piet;Q;” while having the “Jaap” layers unparented.
As you can see apparently the script renames sublayers with a parent layer prefix.
The problem is that Rhino stores layers with the full layer path:
ParentLayer::SubLayer::SubSubLayer::SubSubSubLayer... etc.
A sublayer’s full name thus includes the parent layer name. If you start doing replacements it will then get complicated. the best way to uniquely identify layers is to always work with their full names, even better with their ID’s - but rhinoscriptsyntax in python does not yet give you access to those (vb does), so you would have to use some RhinoCommon and start delving into the document layer table, I think.
The addition of the possibility to have same-named sublayers with Rhino 5 considerably complicated layer manipulations by name.
The script is already a blessing now, yet I wonder,
would it be possible to expand the script to achieve the following?
Using the “Filter layers” in rhino itself does use *, would you imagine this is possible?
Search for
*Fiets*x*x*
Replace with
*Fiets*x*~*
So to search with a * (joker).
Scenario:
01;Fiets;Ø20xØ5x8
02;Fiets;Ø12xØ7x2
03;Fiets;Ø16xØ6x4
04;Fiets;Ø20xØ9x8
05;Fiets;Ø5xØ3x3
Regular Expressions are what you need.
Check the attached .py file. It will allow you to use asteriks symbol when replacing layer names, exactly as you wanted.
You could also just download the attached .rui file, and drop into your Rhino toolbar. It will automatically generate a new Toolbar tab with two buttons: this one(replace layer names with/without asteriks symbol), and “make 2d layers” you wanted in here.
Hope that helps.
@djordje Thank you again!! Have a great new year yourself too. I’ll be checking the script out the comming days, after I’ve tried it out I’ll get back here : )