Select a Hatch by Pattern - Python & RhinoCommon

Hey Guys

to improve my scripting/coding skills (I’m a programming-beginner ^^), I try to develop basic scripts to make my daily rhinowork faster.

Currently, I’m trying to develop a kind of “Select a Hatch by Pattern” Script, that selects all hatches with the same pattern in Python.

I get stuck at the point, where my script has to select the objects that are stored in a list.
I’ve tried to use RhinoCommon (RhinoCommon Documentation for this issue, but i got this error:
“Message: expected ObjectTable, got Guid”

(As far as I know, I did everything, that the common-documentation asks for^^)
It sounds like I missunderstood some basic RhinoCommon/Python stuff ^^

Any Ideas to fix this?

My Python Code so far:

import rhinoscriptsyntax as rs
import Rhino
import scriptcontext
import System

hatchbypattern = []
hatch = rs.GetObject("Select Hatch to select by Pattern", rs.filter.hatch, True, True)

if hatch:
    hatchpattern = rs.HatchPattern(hatch)
    print hatchpattern

allhatches = rs.ObjectsByType(65536)
for i in allhatches:
    if rs.HatchPattern(i) == hatchpattern:
        hatchbypattern.append(i)

for j in hatchbypattern:
    Rhino.DocObjects.Tables.ObjectTable.Select(j, True)

Thanks,
Rivex

1 Like

@rivex,

to select the results, you might use RhinoScript syntax too. Below is a slightly shorter example:

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs
    
def SelByHatchPattern():
    
    msg = "Select Hatch to select by Pattern"
    hatch_id = rs.GetObject(msg, rs.filter.hatch, True, True)
    if not hatch_id: return
    
    hatchpattern = rs.HatchPattern(hatch_id)
    allhatches = rs.ObjectsByType(rs.filter.hatch)
    
    results = []
    for hatch in allhatches:
        if rs.HatchPattern(hatch) == hatchpattern:
            if rs.IsObjectSelectable(hatch):
                results.append(hatch)
    
    if len(results) > 0: rs.SelectObjects(results)
     
if __name__=="__main__":
    SelByHatchPattern()

c.

1 Like

That was fast, thank you very much!
Now I can use the script, yay :smile:

But now I’m interested, how to do this in rhino.common, how to code the Rhino.DocObjects…select-command right?

Anyway, Thanks @clement :slightly_smiling:

If you do not need the list of ids in the results variable, you can do this:

    for hatch in allhatches:
        if rs.HatchPattern(hatch) == hatchpattern:
            if rs.IsObjectSelectable(hatch):
                scriptcontext.doc.Objects.Select(hatch, True, True)

c.