I have a script that I have been using to search text-tags that have been rounded to either 0.0 or 0.00 decimal places. I am trying to find a way to manipulate it to enable it to find text-tags that are greater than a decimal value eg; select all text-tags greater than 0.150.
I have tried to edit a few things, but have not got the result I am after…
Is there a way to simply modify the ‘pattern =’.[0-9]{2}$’ line to enable this function to operate?
You might try finding all of the numbers in a string. Then compare those numbers to some value that’s important.
import re
def ExtractNumbers(str):
values = re.findall("\d+\.\d+", str)
if values:
return [float(v) for v in values]
return None
def TestExtractNumbers():
max_value = 1.50
strings = ["Area = abc", "Area = 1.49", "Area = 1.50", "Area = 1.51"]
for s in strings:
values = ExtractNumbers(s)
if not values:
continue
rc = [v for v in values if v < max_value]
if rc:
print("String: {0}, Value = {1}".format(s, rc))
if __name__ == "__main__":
TestExtractNumbers()
Hi @dale
thanks to the pointers!
I have given it a try but unable to get it to operate…
I have very basic knowledge of coding and I am guessing there is something I am missing. Any further advice would be much appreciated.
import rhinoscriptsyntax as rs
import re
def _extract_numbers(str):
# Fancy number finder
regex = "\d+\.\d+"
values = re.findall(regex, str)
if values:
return [float(v) for v in values]
return None
def test_mwilson():
# Select text objects with numeric values greather than this
max_val = 1.50
# Select some text objects
ids = rs.GetObjects("Select text", filter = 512, preselect=False)
if not ids:
return
# Process each text object
for id in ids:
# Get the text object's text
str = rs.TextObjectText(id)
# Try getting numbers out of the string
vals = _extract_numbers(str)
if vals:
# Find the numbers that exceed something
ok = [v for v in vals if v >= max_val]
if len(ok) > 0:
# Select the object
rs.SelectObject(id, True)
if __name__ == "__main__":
test_mwilson()
Are you on Rhino 7? The script runs on 7.28.23041.13001, 2023-02-10 here.
Although there are some changes you could make:
The regex expression on line 6 might be better as
regex = "\d*\.\d+"
as that will also capture numbers without a leading zero, e.g. .259 (not relevant if your threshold is 1.5 of course, but pertinent should you reduce it).
If you want to capture numbers without decimal points, e.g. integers like 2, you can change it further:
regex = "\d*\.?\d+"
Also, the comparison coded is ‘greater than or equal to’ rather than ‘greater than’ so it will select values of 1.5. If you only want ‘greater than’ then change line 29 to:
ok = [v for v in vals if v > max_val]
These changes will only affect what the script does, not make it start running. Let us know which version of Rhino you are on.
Arr, I see. It is a version issue then. No, unfortunately I am still running RHv5…
Many thanks for the input, I will be looking into possible work around(s) etc. I do have the same thing working in Grasshopper at present, so it is not an urgent task to figure out.
Just some people within my team prefer to click a button with an embedded script, than playing around with all the wires/components etc. in Grasshopper.
Many thanks again @dale & @jeremy5 for the help!
Cheers, Michael