How Do I add an Object to Layout Space

Hi,

In Rhino Common, I add an Object (Curve, Text etc.) to the Model Space by:

doc.Objects.AddXXX()

What’s the Equivalent to adding something to the Layout Space? Inserting a 2D Line, A Text, Dimensions and Block instances?

Thanks
Karl

The document takes into account the active view when adding geometry. If you have a layout active, the geometry will be added to that specific layout.

As an alternative, you can create ObjectAttributes and set the ViewportId to that of the layout’s main viewport. Pass this ObjectAttributes to the Add functions.

Steve,

Is the second method working properly in Rhino 6?
For example, if I try to add a line on the “Page 1” layout using the code below inside a grasshopper C# component, this line always appears on the active layout.

Rhino.DocObjects.ObjectAttributes att = new Rhino.DocObjects.ObjectAttributes();
var view = RhinoDocument.Views.Find("Page 1", false);
att.ViewportId = view.MainViewport.Id;
RhinoDocument.Objects.AddLine(new Point3d(0, 0, 0), new Point3d(100, 100, 0), att);

Thanks,
Alan

Hi @atai,

I am not finding the second method working. I’ve logged a bug for that.

https://mcneel.myjetbrains.com/youtrack/issue/RH-49373

This, however, does work (from a plug-in command):

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  var page_views = doc.Views.GetPageViews();
  if (null == page_views || 0 == page_views.Length)
    return Result.Nothing;

  const string page_name = "Page 1";
  var page_view = page_views.FirstOrDefault(pg => pg.PageName.Equals(page_name, StringComparison.OrdinalIgnoreCase));
  if (null == page_view)
  {
    RhinoApp.WriteLine("{0} page view not found.", page_name);
    return Result.Nothing;
  }

  var plane = Plane.WorldXY;
  var circle = new Circle(plane, 5.0);

  doc.Views.RedrawEnabled = false;
  var old_view = doc.Views.ActiveView;
  doc.Views.ActiveView = page_view;

  doc.Objects.AddCircle(circle/*, attributes*/);

  doc.Views.ActiveView = old_view;
  doc.Views.RedrawEnabled = true;
  doc.Views.Redraw();

  return Result.Success;
}

– Dale

Dale,

Thanks. Switching between active layout works.

Alan