Hi everyone,
I wanted to create a C# script component that adds a “print layout” (probably not the right Rhino word…) based on a rectangle provided.
I followed this snippet but I got stuck at updating the camera position to the center of the provided rectangle.
Here is my code:
private void RunScript(Rectangle3d PageRect, bool PageRectFlipped, double PageWidth, bool Metric, string LayoutName, bool Enabled, ref object A)
{
if (Enabled) {
var doc = Rhino.RhinoDoc.ActiveDoc;
if (Metric){
doc.PageUnitSystem = UnitSystem.Millimeters;
} else {
doc.PageUnitSystem = UnitSystem.Inches;
}
DeleteLayoutByName(LayoutName);
// sometimes the width/height of a rectangle are not what you think..
double ActualRectWidth = PageRectFlipped ? PageRect.Height : PageRect.Width;
double ActualRectHeight = PageRectFlipped ? PageRect.Width : PageRect.Height;
double PageHeight = ActualRectHeight * (PageWidth / ActualRectWidth);
var pageview = doc.Views.AddPageView(LayoutName, PageWidth, PageHeight);
if( pageview != null )
{
Rhino.Geometry.Point2d top_left = new Rhino.Geometry.Point2d(0, 0);
Rhino.Geometry.Point2d bottom_right = new Rhino.Geometry.Point2d(PageWidth, PageHeight);
var detail = pageview.AddDetailView("ModelView", top_left, bottom_right, Rhino.Display.DefinedViewportProjection.Top);
if (detail != null)
{
// center the detail view onto rectangle
//Print(PageRect.Center.ToString());
pageview.SetActiveDetail(detail.Id);
detail.DetailGeometry.SetScale(ActualRectWidth, doc.ModelUnitSystem, PageWidth, doc.PageUnitSystem);
Print("before:");
Print(detail.Viewport.CameraLocation.ToString());
Print(detail.Viewport.CameraTarget.ToString());
//update camera position
detail.Viewport.SetCameraTarget(PageRect.Center, true);
//detail.Viewport.SetCameraLocation(PageRect.Center, true);
Print("after:");
Print(detail.Viewport.CameraLocation.ToString());
Print(detail.Viewport.CameraTarget.ToString());
//detail.DetailGeometry.IsProjectionLocked = true;
// 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();
}
}
The output of Print() is this:
before:
0,0,100
0,0,-400.00005
after:
303.143800850039,-2.82409994843501,500.00005
303.143800850039,-2.82409994843501,0
Which indicates that the camera position was moved correctly.
But the reality looks different:
Any clues?
