How to check if a DimStyles exist or not


#1

Hello all

I want add a dimStyle and it work well. If the dim Style name not exist

int dim_style_index = doc.DimStyles.Add(“RH_3_00”);
Rhino.DocObjects.DimensionStyle RH_3_00 = doc.DimStyles[dim_style_index];
RH_3_00.ArrowLength = 15;
RH_3_00.TextHeight = 15;
RH_3_00.ExtensionLineOffset = 0;
RH_3_00.ExtensionLineExtension = 3;
RH_3_00.CommitChanges();

but if i start the plug-in the second time in the same doc it stopped

so I try to check by

ds = doc.DimStyles.Find(name, true);

but now it break if he not exist

do somebody have a idea how to do?

Thanks Robert


(Dale Fugier) #2

Hi Robert,

This block of code seems to work between file sessions:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  string dimstyle_name = "RH_3_00";
  Rhino.DocObjects.DimensionStyle dimstyle = doc.DimStyles.Find(dimstyle_name, true);
  if (null != dimstyle)
  {
    Rhino.RhinoApp.WriteLine(string.Format("Dimstyle \"{0}\" found.", dimstyle_name));
  }
  else
  {
    int dimstyle_index = doc.DimStyles.Add("RH_3_00");
    if (dimstyle_index >= 0)
    {
      dimstyle = doc.DimStyles[dimstyle_index];
      if (null != dimstyle)
      {
        dimstyle.ArrowLength = 15;
        dimstyle.TextHeight = 15;
        dimstyle.ExtensionLineOffset = 0;
        dimstyle.ExtensionLineExtension = 3;
        dimstyle.CommitChanges();
        Rhino.RhinoApp.WriteLine(string.Format("Dimstyle \"{0}\" added.", dimstyle_name));
      }
    }
    else
    {
      Rhino.RhinoApp.WriteLine(string.Format("Dimstyle \"{0}\" not added.", dimstyle_name));
    }
  }
  return Result.Success;
}

Let me know if you find otherwise.

– Dale