How to load the default Hatches

The default Grasshopper component 'Pattern Hatch ’ (PHatch) allows for generating hatches previews, but you cannot use the curves or set the color. The Human plugin has the component ‘CreateHatch’ which does give the curves as output. However, this requires a ‘Hatch Pattern Name’ which must have been created in the Rhino document already.

My problem is that a new Rhino document has an empty HatchTable. I get this error when trying to list the available hatches with ‘rs.HatchPatternNames’:
" ‘GrasshopperDocument’ object has no attribute ‘HatchPatterns’ "

Somehow there is a default list of hatches available as soon as you open ‘File > Properties’ and go to ‘Document Properties > Hatch’. Press ‘OK’ there and Grasshopper will tell me, through the Human component ‘Hatch Table’, that there are now 9 hatches available.

So how can I activate that default list of hatches through Grasshopper? Is there a C# or Python command that can load in that default hatch list?

You need add something like this to load up the default hatches if they haven’t been called upon yet.

  private void AddDefaultHatchesToHatchTable()
    {
      //Check to make sure the system defaults are in the hatch table
      //If they are not, let's add them back


      //Solid
      var hatch_id = Document.HatchPatterns.FindName(HatchPattern.Defaults.Solid.Name);
      if (hatch_id == null)
        Document.HatchPatterns.Add(HatchPattern.Defaults.Solid);

      
      //Hatch1
      hatch_id = Document.HatchPatterns.FindName(HatchPattern.Defaults.Hatch1.Name);
      if (hatch_id == null)
        Document.HatchPatterns.Add(HatchPattern.Defaults.Hatch1);

      //Hatch2
      hatch_id = Document.HatchPatterns.FindName(HatchPattern.Defaults.Hatch2.Name);
      if (hatch_id == null)
        Document.HatchPatterns.Add(HatchPattern.Defaults.Hatch2);
      
      //Hatch3
      hatch_id = Document.HatchPatterns.FindName(HatchPattern.Defaults.Hatch3.Name);
      if (hatch_id == null)
        Document.HatchPatterns.Add(HatchPattern.Defaults.Hatch3);

      //Dash
      hatch_id = Document.HatchPatterns.FindName(HatchPattern.Defaults.Dash.Name);
      if (hatch_id == null)
        Document.HatchPatterns.Add(HatchPattern.Defaults.Dash);
      
      //Grid
      hatch_id = Document.HatchPatterns.FindName(HatchPattern.Defaults.Grid.Name);
      if (hatch_id == null)
        Document.HatchPatterns.Add(HatchPattern.Defaults.Grid);
     
      //Grid60
      hatch_id = Document.HatchPatterns.FindName(HatchPattern.Defaults.Grid60.Name);
      if (hatch_id == null)
        Document.HatchPatterns.Add(HatchPattern.Defaults.Grid60);

      //Plus
      hatch_id = Document.HatchPatterns.FindName(HatchPattern.Defaults.Plus.Name);
      if (hatch_id == null)
        Document.HatchPatterns.Add(HatchPattern.Defaults.Plus);


      //Squares
      hatch_id = Document.HatchPatterns.FindName(HatchPattern.Defaults.Squares.Name);
      if (hatch_id == null)
        Document.HatchPatterns.Add(HatchPattern.Defaults.Squares);

    }
1 Like

Thank you so much! I think this is it, but I haven’t got it working yet:

Error (CS0103): The name ‘Document’ does not exist in the current context
Error (CS0103): The name ‘HatchPattern’ does not exist in the current context

I guess I need something like ‘Document = Rhino.DocObjects.ActiveDoc();’?

EDIT:

So I got it working with

using Rhino.DocObjects;

//Hatch1
var hatch_id = doc.HatchPatterns.FindName(HatchPattern.Defaults.Hatch1.Name);
if (hatch_id == null)
doc.HatchPatterns.Add(HatchPattern.Defaults.Hatch1);

Thanks Travis!

1 Like