Create new Dimension Style

Hi everyone,

I am trying to create a new Dimension Style from scratch and set it as the current one in Grasshopper.

I tried to build the code but something is wrong since the new style is created but not set as the current one. I t should be something easy but I am not able to solve the problem. I just compared the index of new and old styles and they are different.

 private void RunScript(ref object New_Style, ref object Current_Style)
  {

    var style_name = "IMT-Style";

    Rhino.DocObjects.DimensionStyle dimStyle = new Rhino.DocObjects.DimensionStyle(){
        Name = style_name,
        Id = Guid.NewGuid(),
        DimensionScale = 0.15,
        LengthResolution = 0,
        ArrowLength = 2.5,
        TextGap = 1,
        TextHeight = 3,
        FixedExtensionOn = true,
        FixedExtensionLength = 5
        };

    // Add the newly created text style to the document
    Rhino.RhinoDoc.ActiveDoc.DimStyles.Add(dimStyle, false);

    // Set the newly created text style as the current style
    Rhino.RhinoDoc.ActiveDoc.DimStyles.SetCurrent(dimStyle.Index, false);

    New_Style = dimStyle.Index;
    Current_Style = this.RhinoDocument.DimStyles.CurrentIndex;
  }

Additionally, each time I run GH new Dimension styles are created: IMT-Style-01, IMT-Style-02, IMT-Style-03…

Is it possible to adapt the code so in case of having stored the same name do nothing? Would you mind taking a look at that @AndersDeleuran @magicteddy ?

Thank you in advance!

DIMStyle.gh (5.0 KB)

I think you need to search the style again after it’s inserted in Rhino.
By the way, I noticed if the Annotation Styles window is opened and the component is launched, it doesn’t refresh the table. Insertion works but you have to restart the window to see it.

private void RunScript(string x, ref object New_Style, ref object Current_Style)
  {
    var style_name = "IMT-Style";

    //find it again
    Rhino.DocObjects.DimensionStyle ds = Rhino.RhinoDoc.ActiveDoc.DimStyles.FindName(style_name);

    if (ds == null)
    {
      Rhino.DocObjects.DimensionStyle dimStyle = new Rhino.DocObjects.DimensionStyle(){
          Name = style_name,
          Id = Guid.NewGuid(),
          DimensionScale = 0.15,
          LengthResolution = 0,
          ArrowLength = 2.5,
          TextGap = 1,
          TextHeight = 3,
          FixedExtensionOn = true,
          FixedExtensionLength = 5
          };

      // Add the newly created text style to the document
      Rhino.RhinoDoc.ActiveDoc.DimStyles.Add(dimStyle, false);

      //find it again
      ds = Rhino.RhinoDoc.ActiveDoc.DimStyles.FindName(style_name);
    }

    // Set the newly created text style as the current style
    Rhino.RhinoDoc.ActiveDoc.DimStyles.SetCurrent(ds.Index, false);

    New_Style = ds.Index;
    Current_Style = this.RhinoDocument.DimStyles.CurrentIndex;
  }
2 Likes

Thank you very much @magicteddy it works perfectly :slight_smile:

Sorry to bother you again. I’ve just added the arrowType1 and arrowType2 but I think nothing happens :frowning:

I think It may be caused since I have more components in the same grasshopper and everything runs at the same time so the Dim Style is not uploaded.

Can the Redaw method solve this situation? Is there any way of changing the priorities in order to run this component first?

  private void RunScript(string Name)
  {
    var style_name = Name;

    //Find if the dimension style exists
    Rhino.DocObjects.DimensionStyle ds = Rhino.RhinoDoc.ActiveDoc.DimStyles.FindName(style_name);

    if (ds == null)
    {
      Rhino.DocObjects.DimensionStyle dimStyle = new Rhino.DocObjects.DimensionStyle(){
          Name = style_name,
          Id = Guid.NewGuid(),
          DimensionScale = 0.15,
          LengthResolution = 0,
          ArrowLength = 2.5,
          ArrowType1 = Rhino.DocObjects.DimensionStyle.ArrowType.Arrow,
          ArrowType2 = Rhino.DocObjects.DimensionStyle.ArrowType.Arrow,
          TextGap = 1,
          TextHeight = 3,
          FixedExtensionOn = true,
          FixedExtensionLength = 5
          };

      // Add the newly created text style to the document
      Rhino.RhinoDoc.ActiveDoc.DimStyles.Add(dimStyle, false);

      //find it again
      ds = Rhino.RhinoDoc.ActiveDoc.DimStyles.FindName(style_name);
    }

    // Set the newly created text style as the current style
    Rhino.RhinoDoc.ActiveDoc.DimStyles.SetCurrent(ds.Index, false);

    var New_Style = ds.Index;
    var Current_Style = this.RhinoDocument.DimStyles.CurrentIndex;

  }

I encontered the same problem before and unfortunately the solution is still to be found. See this thread :

Still don’t know if it’s a bug or there is another method to use.
@AndyPayne maybe ? - edit, seems to work fine on this file.

1 Like

BTW this is the file.

Dim_Style.gh (4.1 KB)

When opening it by double clicking the GH file, the Dimension Style is not uploaded. When I Recalculate the GH file the Dimension Style is uploaded.

Regards :slight_smile:

Indeed, you have a problem of the order the components are computed. And the arrows do work as intended.

You should add an output to your C# component that returns the Dimension Style, and use that as an input to your Python component, so it triggers in the correct order.

You are right!

Anyway, I am capable of getting and output with the dimension style name for the c# component but I am not able to modify the python component and make it works since I do not know how to change the code in a right way. I would appreciate any help on this :slight_smile:

Like this ?

Dim_Style.gh (6.0 KB)

2 Likes

That’s it, thank you very much @magicteddy :slight_smile: