I’m trying to apply hatches to closed geometries through RhinoInside “headless” in an external Python code, but I can’t make it work.
I have a code working when used with the Python editor in the Rhino GUI, but what I think is the equivalent with RhinoInside running headless doesn’t. Both codes define the hatching pattern, import the dxf, add the hatch pattern to the hatch table, select the curves and create a hatch. This works in both cases it generates no error, but when looking at the created hatch, in the GUI it contains 759 lines and in the rhinoinside version it contains 0 line.
Please find attached the code that works in the GUI, the one that doesn’t with RhinoInside and the dxf that is used in these codes. Can anyone observe the same behaviour as I do? Is there something wrong with the rhinoinside code?
Thanks for the clear report and the sample files, that made this easy to track down.
The problem isn’t Hatch.Create — that part works headless. It’s Hatch.Explode(). A hatch only stores a pattern index, and to turn that index into actual pattern geometry Explode() looks it up in Rhino’s active document. A headless document isn’t the active one, so the lookup fails and you get back an empty array instead of an error. In the GUI the active document happens to be the same one you added “Hatch1” to, which is why the identical code works there.
Since you’re running in your own Python process with a single document, you can work around it with one line right after you create the document:
The rest of your script can stay exactly as it is. With your Square_test.dxf that gives me 759 exploded lines headless — the same number you’re seeing in the GUI.
This is worth doing regardless: other geometry that reads from document tables (text and dimensions, for example) hits the same issue headless, so setting the active document up front saves you from tripping over it again.