Drawing a Dashed Line (C#)

Hi all,

I am creating a layer called folds and inside the layer am drawing a straight line. I want the line to be dashed, however, the line is getting drawn as a continuous line. I can change the continuous line to a dotted line manually but I want to do it through the code itself.
Below is my code (Where I add the line )

            if (folds == 4)
               {
                 panel.Perimeter = doc.Objects.AddLine(new Point3d(panelX0 + panel.KFactor, panelY0 + 
                 panel.LeftFirstFoldSetbackBottom, 0), new Point3d(panelX0 + panel.KFactor, panelY1 - 
                     panel.LeftFirstFoldSetbackTop, 0));
              }
              else
              {
                 panel.Perimeter = doc.Objects.AddLine(new Point3d(panelX0 + panel.KFactor, panelY0 + 
               panel.LeftFirstFoldSetbackBottom, 0), new Point3d(panelX0 + panel.KFactor, panelY1, 0));
               }

The line is getting added inside a layer called “Folds”.
I checked the rhino-api documentation and found the linetypes document however, there is no clue on how to implement the different line types through code.
I am new to rhino, so I believe I might have missed something.

Thank you for your time.

Hi @shehanwilfred,

See if this code sample helps.

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  const string linetype_name = "Dashed";
  var linetype_index = doc.Linetypes.Find(linetype_name);
  if (linetype_index < 0)
  {
    RhinoApp.WriteLine("\"{0}\" linetype not found.", linetype_name);
    return Result.Nothing;
  }

  const string layer_name = "Folds";
  var layer_index = doc.Layers.FindByFullPath(layer_name, -1);
  if (layer_index < 0)
  {
    // Layer does not exist, so add one...
    var layer = new Layer { Name = layer_name, LinetypeIndex = linetype_index };
    layer_index = doc.Layers.Add(layer);
    if (layer_index < 0)
    {
      RhinoApp.WriteLine("Unable to add \"{0}\" layer.", layer_name);
      return Result.Failure;
    }
  }
  else
  {
    // Layer exists, so verify linetype index
    if (doc.Layers[layer_index].LinetypeIndex != linetype_index)
      doc.Layers[layer_index].LinetypeIndex = linetype_index;
  }

  doc.Views.Redraw();

  return Result.Success;
}

– Dale

1 Like

Hi Dale,

Thank you for your answer. However even after I implemented it using your way, it does not make the line dashed. The line type index returns 1 so, the line type dashed is present. The line type of the “folds” layer is -1, however even if I try to set it to 1 using your code, it stays -1.
Since my “Folds” layer is a sublayer, (there is a parent layer called “layers for approval” I forgot to mention it in the first place, sorry!) I believe that’s what causing the issue

Edit:
Got it solved, i just set the line type before setting the parent layer and it worked. Thanks again Dale.