I am trying to write a command to add new layout for the selected object. With all the selected object, one of the object has to be a Text entity such that the new layout will be named as the string in the TextEntity.
I run into this problem where the Viewport.ZoomExtentsSelected(); function does not work the first time, however it works after I run the same function again on the same selected objects.
Stuck at this for few hours and do not have any ideas on what caused this.
To test the code, you will need to create some objects with one of the object as a Text entity. Ignore the code that uses ZoomBoundingBox, as I cannot find a way to get the ZoomBoundingBox to work.
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
// Get all selected Objects
GetObject go = new GetObject();
go.GroupSelect = true;
go.SubObjectSelect = false;
go.EnableClearObjectsOnEntry(false);
go.EnableUnselectObjectsOnExit(false);
go.DeselectAllBeforePostSelect = false;
go.EnableSelPrevious(true);
go.EnablePreSelect(true, false);
go.EnablePressEnterWhenDonePrompt(false);
go.SetCommandPrompt("Select items for the new layout:");
// Disable the scaling
RhinoApp.RunScript("_-DocumentProperties Annotation ScaleAnnotation=No ScaleHatches=No TextScale=1.0 HatchScale=2.0 _Enter _Enter", true);
GetResult result = go.GetMultiple(1, -1);
if (go.CommandResult() != Rhino.Commands.Result.Success)
{
return go.CommandResult();
}
RhinoApp.WriteLine("Total Objects Selected: {0}", go.ObjectCount);
string labelName = null;
Point2d minPoint = new Point2d(0, 0);
Point2d maxPoint = new Point2d(0, 0);
// Loop through all the objects to find Text
for (int i = 0; i < go.ObjectCount; i++)
{
RhinoObject rhinoObject = go.Object(i).Object();
if (rhinoObject.ObjectType == ObjectType.Annotation)
{
TextEntity textEntity = rhinoObject.Geometry as TextEntity;
if (textEntity != null)
{
labelName = textEntity.Text;
}
}
BoundingBox bBox = go.Object(i).Object().Geometry.GetBoundingBox(true);
if (bBox.Min.X < minPoint.X)
{
minPoint.X = bBox.Min.X;
}
if (bBox.Min.Y < minPoint.Y)
{
minPoint.Y = bBox.Min.Y;
}
if (bBox.Max.X > maxPoint.X)
{
maxPoint.X = bBox.Max.X;
}
if (bBox.Max.Y > maxPoint.Y)
{
maxPoint.Y = bBox.Max.Y;
}
}
if (labelName == null)
{
return Rhino.Commands.Result.Failure;
}
// Add layout
doc.PageUnitSystem = Rhino.UnitSystem.Millimeters;
var page_views = doc.Views.GetPageViews();
var pageview = doc.Views.AddPageView(string.Format("{0}", labelName), 210, 297);
Point2d bottomLeft = new Point2d(10, 40);
Point2d topRight = new Point2d(200, 287);
if (pageview != null)
{
var detail = pageview.AddDetailView(labelName, bottomLeft, topRight, Rhino.Display.DefinedViewportProjection.Top);
if (detail != null)
{
pageview.SetActiveDetail(detail.Id);
// detail.Viewport.Zoom
detail.Viewport.ZoomExtentsSelected();
// detail.Viewport.ZoomBoundingBox(new BoundingBox(minPoint.X, minPoint.Y, 0, maxPoint.X, maxPoint.Y, 0));
detail.DetailGeometry.IsProjectionLocked = false;
// 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.CommitViewportChanges();
detail.CommitChanges();
pageview.SetPageAsActive();
doc.Views.ActiveView = pageview;
doc.Views.Redraw();
return Rhino.Commands.Result.Success;
}
}
return Result.Failure;
}