Hatch list in Ghpython

Hello, i create a script to add hatch but the problem it can’t detect list hatch automatically unless i open options and select hatch than click ok.
This happen also with other hatch components from other plugins

One “work around” is to use rs.HatchPatternNames() as the first thing in your code.
That will load the default patterns without going into options.

1 Like

This don’t work

don’t forget to use scriptcontext when using rhinoscriptsyntax.

sc.doc = Rhino.RhinoDoc.ActiveDoc
hNames = rs.HatchPatternNames()
sc.doc = ghdoc
1 Like

Thank this work fine, is this line needed? sc.doc = ghdoc

As far as I know, it is best practice to return the scriptcontext focus back to the grasshopper document.
In most cases, I subscribe to the “always use rhinocommon when working in ghpython” school of thought. (which eliminates the need for scriptcontext stuff in the first place). But, rhinoscriptsyntax certainly does have it’s place sometimes.
Perhaps something has changed in Rhino, but as a rule, (unless someone knows otherwise?), I would say you should always set the focus back to the ghdoc when your done with your rhinoscript operation.

1 Like

I don’t want open a new thread since it is about the same component but a different problem;
when i click bake 4 duplicate hatches created, i don’t know why?
Can anyone please check the script and you are free to modify to make it more simple and better

add hatch.gh (9.8 KB)

from ghpythonlib.componentbase import executingcomponent as component
import Grasshopper, GhPython
import System
import rhinoscriptsyntax as rs
import Rhino as rh
import scriptcontext as sc
import Rhino.Geometry as rg
import math

class CreateHatch(component):

def RunScript(self,Curve,Hatch,Angle,Scale,Hatch_Color,Boundary_Color,Bake):
    
    sc.doc = rh.RhinoDoc.ActiveDoc
    rs.HatchPatternNames()
    sc.doc = ghdoc
    self.c1 = Hatch_Color
    self.c2 = Boundary_Color
    if not self.c1 : self.c1 = System.Drawing.Color.Black
    else : self.c1
    if not self.c2: self.c2 = System.Drawing.Color.Black
    else : self.c2
    self.c = Curve
    self.n = Hatch
    if not self.n : self.n = 0
    else : self.n
    self.r = Angle
    if not self.r : self.r = 0
    else : self.r
    self.s = Scale
    if not self.s : self.s = 1
    else : self.s
    self.bake = Bake
    
    if not self.c: return
        
def DrawViewportMeshes(self,arg):
    
    if not self.c: return
    doc = rh.RhinoDoc.ActiveDoc
    H = doc.ActiveDoc.HatchPatterns
    hatch  = rg.Hatch.Create(self.c,self.n,math.radians(self.r),self.s) 
    pip = arg.Display
    rh.Display.DisplayPipeline.DrawHatch(pip,hatch[0],self.c1,self.c2)
    if self.bake == True:
        doc.Objects.AddHatch(hatch[0])
        doc.Views.Redraw()

Problem solved; i use this
image

instead of

if self.bake == True:
    doc.Objects.AddHatch(hatch[0])
    doc.Views.Redraw()

Another problem :slight_smile:
DisplayPipeline.DrawHatch show hatch of one curve; but rs.AddHatch bake them all with auto-detection of region

After baking:

The same problem with : Hatch.CreateDisplayGeometry

add hatch3.gh (7.1 KB)

Fixed; both solutions work , the first one work even hatch preview off and the second one need hatch preview on

image

1 Like