How to know/ figure out what functions I need (Search & replace names)


#1

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.


#2

Hi Peter,

Is this what you need:

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”)

replacePartOfLayerName.py (658 Bytes)


#3

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

#4

Hello @djordje

I think the both scripts looking at the code, do about the same thing? I’m going to try using the tools-python script option now : )


#5

The difference is that Dale’s code is in vb rhinoscript; won’t run on a Mac.

–Mitch


#6

Hello @djordje,

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


OK


#7

I am using a Windows PC, yet after adding the rsv script, I can not seem to run it in Rhino by typing in the command line:
“LayerRename”


#8

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.


#9

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…

–Mitch


#10

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?

Cheers,

Peter


#11

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:

Rhino.AddStartUpScript Rhino.LastLoadedScriptFile Rhino.AddAlias "LayerRename", "! _NoEcho _-Runscript (LayerRename.rvb)

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.

–Mitch


#12

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

[ OK ]


#13

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.


#14

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.

–Mitch


#15

Ah i see, so this Python script will not be able to get the layer correctly.

The layers are all unique in name, Example:
33.100.202
-> 33.100.202_2D
-> 33.100.202_3D
etc.

I finaly got the RVB script loaded and working this seems so far to do have the proper effect!


#16

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

I want to replace the the second “x” with “~”


#17

I can imagine this not working because:

Fiets;x;~;
Fiets;x;x;

The first result would find this:
Fiets;x;~;
Fiets;x;x;

But if it checks on a PER layer name basis, this problem should not arise though.


#18

Hi Peter,

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.

Happy New Year!!!

layerNames_Make2D.rui (12.3 KB)

renameLayerNamesAsteriks.py (6.1 KB)


#19

@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 : )


#20

@djordje

Hello Djorde,

Thanks again for the work, I’ve read trough the script, and tried it out, yet when I input the follwing:

Replace:
*22.151*;[BLOCK]*

With:
*22.151*;*

On:
210 (all layers)|

Tried also:

Replace:
    *;[BLOCK]*
        
With:
    *;*
        
On:
210 (all layers)|

It does not seem to have the desired effect, it actually seems to replace some regular ";"s and the first letter after it by “;”