protected override Rhino.Commands.Result RunCommand(RhinoDoc doc, Rhino.Commands.RunMode mode) { // Select objects to scale var list = new Rhino.Collections.TransformObjectList(); var rc = SelectObjects("Select objects to scale", list); if (rc != Rhino.Commands.Result.Success) { return(rc); } Point3d anchor; Point3d _ref; Plane plane; // Origin point var gp = new Rhino.Input.Custom.GetPoint(); gp.SetCommandPrompt("Origin point"); var copy = new Rhino.Input.Custom.OptionToggle(m_copy, "No", "Yes"); gp.AddOptionToggle("Copy", ref copy); for (; ;) { var res = gp.Get(); if (res == Rhino.Input.GetResult.Point) { var view = gp.View(); if (view == null) { return(Rhino.Commands.Result.Failure); } plane = view.ActiveViewport.ConstructionPlane(); anchor = gp.Point(); break; } if (res == Rhino.Input.GetResult.Option) { continue; } return(Rhino.Commands.Result.Cancel); } bool bAcceptDefault = true; // Scale factor or first reference point gp.SetCommandPrompt("Scale factor or first reference point"); gp.SetBasePoint(anchor, true); gp.DrawLineFromPoint(anchor, true); gp.ConstrainToConstructionPlane(true); for (; ;) { if (bAcceptDefault) { gp.AcceptNumber(true, false); gp.SetDefaultNumber(m_scale); } else { gp.AcceptNothing(true); gp.ClearDefault(); } var res = gp.Get(); if (res == Rhino.Input.GetResult.Point) { _ref = gp.Point(); break; } if (res == Rhino.Input.GetResult.Number) { double scale = Math.Abs(gp.Number()); const double EPS_DIVIDE = 0.000001; if (scale < EPS_DIVIDE) { continue; } m_scale = scale; plane.Origin = anchor; var xform = Transform.Scale(plane, m_scale, m_scale, m_scale); TransformObjects(list, xform, copy.CurrentValue, copy.CurrentValue); m_copy = copy.CurrentValue; if (m_copy) { bAcceptDefault = false; continue; } doc.Views.Redraw(); return(Rhino.Commands.Result.Success); } if (res == Rhino.Input.GetResult.Nothing) { if (bAcceptDefault == false) { return(Rhino.Commands.Result.Success); } plane.Origin = anchor; var xform = Transform.Scale(plane, m_scale, m_scale, m_scale); TransformObjects(list, xform, copy.CurrentValue, copy.CurrentValue); m_copy = copy.CurrentValue; if (m_copy) { bAcceptDefault = false; continue; } doc.Views.Redraw(); return(Rhino.Commands.Result.Success); } if (res == Rhino.Input.GetResult.Option) { continue; } return(Rhino.Commands.Result.Cancel); } // Second reference point var gx = new GetScaleXform(); gx.SetCommandPrompt("Second reference point"); gx.AddOptionToggle("copy", ref copy); gx.AddTransformObjects(list); gx.SetBasePoint(anchor, true); gx.DrawLineFromPoint(anchor, true); gx.ConstrainToConstructionPlane(true); gx.Plane = plane; gx.RefPoint = _ref; gx.AcceptNothing(true); gx.AcceptNumber(true, false); rc = Rhino.Commands.Result.Cancel; for (; ;) { var res = gx.GetXform(); if (res == Rhino.Input.GetResult.Point) { var view = gx.View(); if (view == null) { rc = Rhino.Commands.Result.Failure; break; } var xform = gx.CalculateTransform(view.ActiveViewport, gx.Point()); if (xform.IsValid && xform != Transform.Identity) { TransformObjects(list, xform, copy.CurrentValue, copy.CurrentValue); rc = Rhino.Commands.Result.Success; m_scale = gx.Scale; } m_copy = copy.CurrentValue; if (m_copy) { continue; } break; } if (res == Rhino.Input.GetResult.Number) { var view = gx.View(); if (view == null) { rc = Rhino.Commands.Result.Failure; break; } var xform = gx.CalculateTransform(view.ActiveViewport, gx.Number()); if (xform.IsValid && xform != Transform.Identity) { TransformObjects(list, xform, copy.CurrentValue, copy.CurrentValue); rc = Rhino.Commands.Result.Success; m_scale = gx.Scale; } m_copy = copy.CurrentValue; if (m_copy) { continue; } break; } if (res == Rhino.Input.GetResult.Option) { continue; } break; } doc.Views.Redraw(); return(rc); }
public static Rhino.Commands.Result AddBackgroundBitmap(Rhino.RhinoDoc doc) { // Allow the user to select a bitmap file var fd = new Rhino.UI.OpenFileDialog { Filter = "Image Files (*.bmp;*.png;*.jpg)|*.bmp;*.png;*.jpg" }; if (!fd.ShowOpenDialog()) { return(Rhino.Commands.Result.Cancel); } // Verify the file that was selected System.Drawing.Image image; try { image = System.Drawing.Image.FromFile(fd.FileName); } catch (Exception) { return(Rhino.Commands.Result.Failure); } // Allow the user to pick the bitmap origin var gp = new Rhino.Input.Custom.GetPoint(); gp.SetCommandPrompt("Bitmap Origin"); gp.ConstrainToConstructionPlane(true); gp.Get(); if (gp.CommandResult() != Rhino.Commands.Result.Success) { return(gp.CommandResult()); } // Get the view that the point was picked in. // This will be the view that the bitmap appears in. var view = gp.View(); if (view == null) { view = doc.Views.ActiveView; if (view == null) { return(Rhino.Commands.Result.Failure); } } // Allow the user to specify the bitmap width in model units var gn = new Rhino.Input.Custom.GetNumber(); gn.SetCommandPrompt("Bitmap width"); gn.SetLowerLimit(1.0, false); gn.Get(); if (gn.CommandResult() != Rhino.Commands.Result.Success) { return(gn.CommandResult()); } // Cook up some scale factors var w = gn.Number(); var image_width = image.Width; var image_height = image.Height; var h = w * (image_height / image_width); var plane = view.ActiveViewport.ConstructionPlane(); plane.Origin = gp.Point(); view.ActiveViewport.SetTraceImage(fd.FileName, plane, w, h, false, false); view.Redraw(); return(Rhino.Commands.Result.Success); }
public static Rhino.Commands.Result OrientOnSrf(Rhino.RhinoDoc doc) { // Select objects to orient Rhino.Input.Custom.GetObject go = new Rhino.Input.Custom.GetObject(); go.SetCommandPrompt("Select objects to orient"); go.SubObjectSelect = false; go.GroupSelect = true; go.GetMultiple(1, 0); if (go.CommandResult() != Rhino.Commands.Result.Success) return go.CommandResult(); // Point to orient from Rhino.Input.Custom.GetPoint gp = new Rhino.Input.Custom.GetPoint(); gp.SetCommandPrompt("Point to orient from"); gp.Get(); if (gp.CommandResult() != Rhino.Commands.Result.Success) return gp.CommandResult(); // Define source plane Rhino.Display.RhinoView view = gp.View(); if (view == null) { view = doc.Views.ActiveView; if (view == null) return Rhino.Commands.Result.Failure; } Rhino.Geometry.Plane source_plane = view.ActiveViewport.ConstructionPlane(); source_plane.Origin = gp.Point(); // Surface to orient on Rhino.Input.Custom.GetObject gs = new Rhino.Input.Custom.GetObject(); gs.SetCommandPrompt("Surface to orient on"); gs.GeometryFilter = Rhino.DocObjects.ObjectType.Surface; gs.SubObjectSelect = true; gs.DeselectAllBeforePostSelect = false; gs.OneByOnePostSelect = true; gs.Get(); if (gs.CommandResult() != Rhino.Commands.Result.Success) return gs.CommandResult(); Rhino.DocObjects.ObjRef objref = gs.Object(0); // get selected surface object Rhino.DocObjects.RhinoObject obj = objref.Object(); if (obj == null) return Rhino.Commands.Result.Failure; // get selected surface (face) Rhino.Geometry.Surface surface = objref.Surface(); if (surface == null) return Rhino.Commands.Result.Failure; // Unselect surface obj.Select(false); // Point on surface to orient to gp.SetCommandPrompt("Point on surface to orient to"); gp.Constrain(surface, false); gp.Get(); if (gp.CommandResult() != Rhino.Commands.Result.Success) return gp.CommandResult(); // Do transformation Rhino.Commands.Result rc = Rhino.Commands.Result.Failure; double u, v; if (surface.ClosestPoint(gp.Point(), out u, out v)) { Rhino.Geometry.Plane target_plane; if (surface.FrameAt(u, v, out target_plane)) { // Build transformation Rhino.Geometry.Transform xform = Rhino.Geometry.Transform.PlaneToPlane(source_plane, target_plane); // Do the transformation. In this example, we will copy the original objects const bool delete_original = false; for (int i = 0; i < go.ObjectCount; i++) doc.Objects.Transform(go.Object(i), xform, delete_original); doc.Views.Redraw(); rc = Rhino.Commands.Result.Success; } } return rc; }
public static Rhino.Commands.Result OrientOnSrf(Rhino.RhinoDoc doc) { // Select objects to orient Rhino.Input.Custom.GetObject go = new Rhino.Input.Custom.GetObject(); go.SetCommandPrompt("Select objects to orient"); go.SubObjectSelect = false; go.GroupSelect = true; go.GetMultiple(1, 0); if (go.CommandResult() != Rhino.Commands.Result.Success) { return(go.CommandResult()); } // Point to orient from Rhino.Input.Custom.GetPoint gp = new Rhino.Input.Custom.GetPoint(); gp.SetCommandPrompt("Point to orient from"); gp.Get(); if (gp.CommandResult() != Rhino.Commands.Result.Success) { return(gp.CommandResult()); } // Define source plane Rhino.Display.RhinoView view = gp.View(); if (view == null) { view = doc.Views.ActiveView; if (view == null) { return(Rhino.Commands.Result.Failure); } } Rhino.Geometry.Plane source_plane = view.ActiveViewport.ConstructionPlane(); source_plane.Origin = gp.Point(); // Surface to orient on Rhino.Input.Custom.GetObject gs = new Rhino.Input.Custom.GetObject(); gs.SetCommandPrompt("Surface to orient on"); gs.GeometryFilter = Rhino.DocObjects.ObjectType.Surface; gs.SubObjectSelect = true; gs.DeselectAllBeforePostSelect = false; gs.OneByOnePostSelect = true; gs.Get(); if (gs.CommandResult() != Rhino.Commands.Result.Success) { return(gs.CommandResult()); } Rhino.DocObjects.ObjRef objref = gs.Object(0); // get selected surface object Rhino.DocObjects.RhinoObject obj = objref.Object(); if (obj == null) { return(Rhino.Commands.Result.Failure); } // get selected surface (face) Rhino.Geometry.Surface surface = objref.Surface(); if (surface == null) { return(Rhino.Commands.Result.Failure); } // Unselect surface obj.Select(false); // Point on surface to orient to gp.SetCommandPrompt("Point on surface to orient to"); gp.Constrain(surface, false); gp.Get(); if (gp.CommandResult() != Rhino.Commands.Result.Success) { return(gp.CommandResult()); } // Do transformation Rhino.Commands.Result rc = Rhino.Commands.Result.Failure; double u, v; if (surface.ClosestPoint(gp.Point(), out u, out v)) { Rhino.Geometry.Plane target_plane; if (surface.FrameAt(u, v, out target_plane)) { // Build transformation Rhino.Geometry.Transform xform = Rhino.Geometry.Transform.PlaneToPlane(source_plane, target_plane); // Do the transformation. In this example, we will copy the original objects const bool delete_original = false; for (int i = 0; i < go.ObjectCount; i++) { doc.Objects.Transform(go.Object(i), xform, delete_original); } doc.Views.Redraw(); rc = Rhino.Commands.Result.Success; } } return(rc); }
protected override Rhino.Commands.Result RunCommand(RhinoDoc doc, Rhino.Commands.RunMode mode) { // Select objects to scale var list = new Rhino.Collections.TransformObjectList(); var rc = SelectObjects("Select objects to scale", list); if (rc != Rhino.Commands.Result.Success) return rc; var anchor = new Point3d(); var _ref = new Point3d(); Plane plane = new Plane(); // Origin point var gp = new Rhino.Input.Custom.GetPoint(); gp.SetCommandPrompt("Origin point"); var copy = new Rhino.Input.Custom.OptionToggle(m_copy,"No", "Yes"); gp.AddOptionToggle("Copy", ref copy); for (; ; ) { var res = gp.Get(); if (res == Rhino.Input.GetResult.Point) { var view = gp.View(); if (view == null) return Rhino.Commands.Result.Failure; plane = view.ActiveViewport.ConstructionPlane(); anchor = gp.Point(); break; } if (res == Rhino.Input.GetResult.Option) continue; return Rhino.Commands.Result.Cancel; } bool bAcceptDefault = true; // Scale factor or first reference point gp.SetCommandPrompt("Scale factor or first reference point"); gp.SetBasePoint(anchor, true); gp.DrawLineFromPoint(anchor, true); gp.ConstrainToConstructionPlane(true); for (; ; ) { if (bAcceptDefault) { gp.AcceptNumber(true, false); gp.SetDefaultNumber(m_scale); } else { gp.AcceptNothing(true); gp.ClearDefault(); } var res = gp.Get(); if (res == Rhino.Input.GetResult.Point) { _ref = gp.Point(); break; } if (res == Rhino.Input.GetResult.Number) { double scale = Math.Abs(gp.Number()); const double EPS_DIVIDE = 0.000001; if (scale < EPS_DIVIDE) continue; m_scale = scale; plane.Origin = anchor; var xform = Transform.Scale(plane, m_scale, m_scale, m_scale); TransformObjects(list, xform, copy.CurrentValue, copy.CurrentValue); m_copy = copy.CurrentValue; if (m_copy) { bAcceptDefault = false; continue; } doc.Views.Redraw(); return Rhino.Commands.Result.Success; } if (res == Rhino.Input.GetResult.Nothing) { if (bAcceptDefault == false) return Rhino.Commands.Result.Success; plane.Origin = anchor; var xform = Transform.Scale(plane, m_scale, m_scale, m_scale); TransformObjects(list, xform, copy.CurrentValue, copy.CurrentValue); m_copy = copy.CurrentValue; if (m_copy) { bAcceptDefault = false; continue; } doc.Views.Redraw(); return Rhino.Commands.Result.Success; } if (res == Rhino.Input.GetResult.Option) continue; return Rhino.Commands.Result.Cancel; } // Second reference point var gx = new GetScaleXform(); gx.SetCommandPrompt("Second reference point"); gx.AddOptionToggle("copy", ref copy); gx.AddTransformObjects(list); gx.SetBasePoint(anchor, true); gx.DrawLineFromPoint(anchor, true); gx.ConstrainToConstructionPlane(true); gx.Plane = plane; gx.RefPoint = _ref; gx.AcceptNothing(true); gx.AcceptNumber(true, false); rc = Rhino.Commands.Result.Cancel; for (; ; ) { var res = gx.GetXform(); if (res == Rhino.Input.GetResult.Point) { var view = gx.View(); if (view == null) { rc = Rhino.Commands.Result.Failure; break; } var xform = gx.CalculateTransform(view.ActiveViewport, gx.Point()); if (xform.IsValid && xform != Transform.Identity) { TransformObjects(list, xform, copy.CurrentValue, copy.CurrentValue); rc = Rhino.Commands.Result.Success; m_scale = gx.Scale; } m_copy = copy.CurrentValue; if (m_copy) continue; break; } if (res == Rhino.Input.GetResult.Number) { var view = gx.View(); if (view == null) { rc = Rhino.Commands.Result.Failure; break; } var xform = gx.CalculateTransform(view.ActiveViewport, gx.Number()); if (xform.IsValid && xform != Transform.Identity) { TransformObjects(list, xform, copy.CurrentValue, copy.CurrentValue); rc = Rhino.Commands.Result.Success; m_scale = gx.Scale; } m_copy = copy.CurrentValue; if (m_copy) continue; break; } if (res == Rhino.Input.GetResult.Option) continue; if (res == Rhino.Input.GetResult.Nothing) break; break; } doc.Views.Redraw(); return rc; }