Fit geometry in detail of layout C# (RhinoViewport.SetCameraLocations?)

Hi everybody,

I am trying to generate a detail in GH and center some geometry.
But it never centers the camera, it stays all the time at the origin.

Maybe someone knows what to do?

Thanks everybody and have a nice weekend!

File:20210501_SetView.gh (3.8 KB)

Hey @Baris, is this what you’re looking for? This is from the RhinoCommon samples: Add Layout with C#, VB

partial class Examples
{

  public static Rhino.Commands.Result AddLayout(Rhino.RhinoDoc doc)
  {
    doc.PageUnitSystem = Rhino.UnitSystem.Millimeters;
    var page_views = doc.Views.GetPageViews();
    int page_number = (page_views==null) ? 1 : page_views.Length + 1;
    var pageview = doc.Views.AddPageView(string.Format("A0_{0}",page_number), 1189, 841);
    if( pageview!=null )
    {
      Rhino.Geometry.Point2d top_left = new Rhino.Geometry.Point2d(20,821);
      Rhino.Geometry.Point2d bottom_right = new Rhino.Geometry.Point2d(1169, 20);
      var detail = pageview.AddDetailView("ModelView", top_left, bottom_right, Rhino.Display.DefinedViewportProjection.Top);
      if (detail != null)
      {
        pageview.SetActiveDetail(detail.Id);
        detail.Viewport.ZoomExtents();
        detail.DetailGeometry.IsProjectionLocked = true;
        detail.DetailGeometry.SetScale(1, doc.ModelUnitSystem, 10, doc.PageUnitSystem);
        // Commit changes tells the document to replace the document's detail object
        // with the modified one that we just adjusted
        detail.CommitChanges();
      }
      pageview.SetPageAsActive();
      doc.Views.ActiveView = pageview;
      doc.Views.Redraw();
      return Rhino.Commands.Result.Success;
    }
    return Rhino.Commands.Result.Failure;
  }
}

In your code I think you’re just needing to add the detail.Viewport.ZoomExtents(); after the pageview.SetActiveDetail(detail.Id);

Hope this helps.

Hi @Lukxf ,

thanks a lot for your answer!
Unfortunately it leads to the same result:

Regards!

I guess you’re looking for CommitViewportChanges:

private void RunScript(Point3d center, double width, double heigth, ref object A)
{
  doc.PageUnitSystem = Rhino.UnitSystem.Millimeters;
  var pageview = doc.Views.AddPageView("01", width, heigth);
  var detail = pageview.AddDetailView("ModelView", new Point2d(0, 0), new Point2d(width, heigth), Rhino.Display.DefinedViewportProjection.Top);
  pageview.SetActiveDetail(detail.Id);
  detail.Viewport.SetCameraLocations(center, center);
  detail.Viewport.SetCameraTarget(center, true);
  detail.CommitViewportChanges();
  detail.DetailGeometry.SetScale(1, doc.ModelUnitSystem, 1, doc.PageUnitSystem);
  detail.DetailGeometry.IsProjectionLocked = true;
  detail.CommitChanges();
  pageview.SetPageAsActive();
}

CommitViewportChanges.gh (5.0 KB)

3 Likes

Fantastic!
That’s it!
Thanks a lot @Mahdiyar !