Force load of HatchPatternTable

Hello,

For a plugIn I’m writing right now I’m relying on the default Hatchpatterns of R6 for some visualization purposes.

I’m getting the hatchpatterns from the Rhinodoc with the
FindName Method.
My problem is, that in a new document, the HatchPatternTable does not get loaded at startup, so FindName always returns null until i go to the optionsPage of the HatchPatterns or actually run the Hatch command.

Is There a way to force Rhino to load its standard HatchPatternTable in the doc?

You can test the behaviour im talking about with this simple python code in a new RhinoDoc:

import scriptcontext as sc

print sc.doc.HatchPatterns.FindName("Solid")

Many Thanks, Lando

Hi @lando.schumpich,

How about this?

import Rhino
import scriptcontext as sc

hatch_pattern = sc.doc.HatchPatterns.FindName("Solid")
if not hatch_pattern:
    hatch_pattern_index = sc.doc.HatchPatterns.Add(Rhino.DocObjects.HatchPattern.Defaults.Solid)
    hatch_pattern = sc.doc.HatchPatterns.FindIndex(hatch_pattern_index)
print hatch_pattern

– Dale

Hi @dale,

Thank you very much, it works nicely, i have a follow up question if you don’t mind:

What would be a good way of doing this from a class structure?
I have my own class in my plugin “HatchSettings” which in its simplest form looks like this:

public class HatchSettings
    {
        public string Name {get; set;}

        public HatchSettings(string name)
           {
                 Name = name;
            }

        public int Index => GetIndex();

        private int GetIndex()
        {
            var pattern = doc.HatchPatterns.FindName(Name);
            if (pattern is null)
            {
               // here i have to use reflection i guess?
            }

            return pattern.Index
        }
    }

Would you mind giving an example how to retrieve the properties from HtachPattern.Defaults?

i tried with:

var propInfo = typeof(HatchPattern.Defaults).GetProperty(name);
var constant = propInfo.GetConstantValue();

Which raises a System.InvalidOperationException.
I’m sure the real problem here is me not knowing enough about reflection…

Many Thanks, Lando

Hi @lando.schumpich,

I’m a fan of extension methods. Thus, you could so something like this:

public static class HatchPatternExtensionMethods
{
  /// <summary>
  /// Returns the Solid hatch pattern
  /// </summary>
  public static HatchPattern SolidPattern(this HatchPatternTable table)
  {
    var pattern = table.FindName(HatchPattern.Defaults.Solid.Name);
    var index = (null == pattern) ? table.Add(HatchPattern.Defaults.Solid) : pattern.Index;
    return table[index];
  }

  /// <summary>
  /// Returns the Dash hatch pattern
  /// </summary>
  /// <param name="table"></param>
  /// <returns></returns>
  public static HatchPattern DashPattern(this HatchPatternTable table)
  {
    var pattern = table.FindName(HatchPattern.Defaults.Dash.Name);
    var index = (null == pattern) ? table.Add(HatchPattern.Defaults.Dash) : pattern.Index;
    return table[index];
  }

  // TODO: add extension methods for other default hatch patterns
}

Then, you can use the above like this:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  var circle = new Circle(Plane.WorldXY, 5.0);
  var curve = circle.ToNurbsCurve();
  var pattern = doc.HatchPatterns.DashPattern();
  var hatch = Hatch.Create(curve, pattern.Index, 0.0, 1.0, doc.ModelAbsoluteTolerance);
  doc.Objects.AddHatch(hatch[0]);
  doc.Views.Redraw();
  return Result.Success;
}

– Dale

1 Like

Thanks very much @dale!

I like this way of implementing it!

Hi
Is this supposed to work in Rhino 7 too? I’m doing a plug-in and I’m not sure what scriptcontext represents here. I’ve assumed it represents RhinoDoc.ActiveDoc.
Thanks!

No.

scriptcontext provides the active Rhino document (Rhino.RhinoDoc in RhinoCommon) while a script is executing. This variable is set by Rhino before the execution of every script.

Keep in mind that Rhino for Mac is a multi-document application.

scriptcontext also provides access to a sticky dictionary.

– Dale

I’m actually on Windows. So how could I force load the default hatch patterns with C#?

Thanks.

I’m not aware of a way @Not_Necessarily, but there is a hint above in Dales answer, rather than looking in the HatchPattern table you can look to a the DocObjects for some Hatch Pattern defaults which are trustworthy;

Rhino.DocObjects.HatchPattern.Defaults.Solid;

See this part of the RN7 API

The only way I’m aware of loading the Default Hatch Patterns is by opening the Hatches in options.

Thanks. I was aware of the HatchPattern.Defaults, but I was hoping there was an easier way.

There’s not. The hatch panels in Rhino look at the documents hatch table to see if each of the defaults are there and adds them if they are not when the panels load.

Thanks.