protected override Result RunCommand(RhinoDoc doc, RunMode mode)
    {
      Rhino.DocObjects.ObjRef objref;
      var rc = RhinoGet.GetOneObject("Select curve", true, ObjectType.Curve,out objref);
      if(rc!= Result.Success)
        return rc;
      var curve = objref.Curve();
      if( curve==null )
        return Result.Failure;

      var gp = new GetPoint();
      gp.SetCommandPrompt("Pick a location on the curve");
      gp.Constrain(curve, false);
      gp.Get();
      if (gp.CommandResult() != Result.Success)
        return gp.CommandResult();

      var point = gp.Point();
      double closest_point_param;
      if (curve.ClosestPoint(point, out closest_point_param))
      {
        RhinoApp.WriteLine("point: ({0}), parameter: {1}", point, closest_point_param);
        doc.Objects.AddPoint(point);
        doc.Views.Redraw();
      }
      return Result.Success;
    }
Ejemplo n.º 2
0
  public static Rhino.Commands.Result InsertKnot(Rhino.RhinoDoc doc)
  {
    const ObjectType filter = Rhino.DocObjects.ObjectType.Curve;
    Rhino.DocObjects.ObjRef objref;
    Result rc = Rhino.Input.RhinoGet.GetOneObject("Select curve for knot insertion", false, filter, out objref);
    if (rc != Rhino.Commands.Result.Success)
      return rc;
    Rhino.Geometry.Curve curve = objref.Curve();
    if (null == curve)
      return Rhino.Commands.Result.Failure;
    Rhino.Geometry.NurbsCurve nurb = curve.ToNurbsCurve();
    if (null == nurb)
      return Rhino.Commands.Result.Failure;

    Rhino.Input.Custom.GetPoint gp = new Rhino.Input.Custom.GetPoint();
    gp.SetCommandPrompt("Point on curve to add knot");
    gp.Constrain(nurb, false);
    gp.Get();
    if (gp.CommandResult() == Rhino.Commands.Result.Success)
    {
      double t;
      Rhino.Geometry.Curve crv = gp.PointOnCurve(out t);
      if( crv!=null && nurb.Knots.InsertKnot(t) )
      {
        doc.Objects.Replace(objref, nurb);
        doc.Views.Redraw();
      }
    }
    return Rhino.Commands.Result.Success;  
  }
    public static Result PrincipalCurvature(RhinoDoc doc)
    {
        ObjRef obj_ref;
        var rc = RhinoGet.GetOneObject("Select surface for curvature measurement", true,
          ObjectType.Surface, out obj_ref);
        if (rc != Result.Success)
          return rc;
        var surface = obj_ref.Surface();

        var gp = new Rhino.Input.Custom.GetPoint();
        gp.SetCommandPrompt("Select point on surface for curvature measurement");
        gp.Constrain(surface, false);
        gp.Get();
        if (gp.CommandResult() != Result.Success)
          return gp.CommandResult();
        var point_on_surface = gp.Point();

        double u, v;
        if (!surface.ClosestPoint(point_on_surface, out u, out v))
          return Result.Failure;

        var surface_curvature = surface.CurvatureAt(u, v);
        if (surface_curvature == null)
          return Result.Failure;

        RhinoApp.WriteLine("Surface curvature evaluation at parameter: ({0}, {1})", u, v);

        RhinoApp.WriteLine("  3-D Point: ({0}, {1}, {2})",
          surface_curvature.Point.X,
          surface_curvature.Point.Y,
          surface_curvature.Point.Z);

        RhinoApp.WriteLine("  3-D Normal: ({0}, {1}, {2})",
          surface_curvature.Normal.X,
          surface_curvature.Normal.Y,
          surface_curvature.Normal.Z);

        RhinoApp.WriteLine(string.Format("  Maximum principal curvature: {0} ({1}, {2}, {3})",
          surface_curvature.Kappa(0),
          surface_curvature.Direction(0).X,
          surface_curvature.Direction(0).Y,
          surface_curvature.Direction(0).Z));

        RhinoApp.WriteLine(string.Format("  Minimum principal curvature: {0} ({1}, {2}, {3})",
          surface_curvature.Kappa(1),
          surface_curvature.Direction(1).X,
          surface_curvature.Direction(1).Y,
          surface_curvature.Direction(1).Z));

        RhinoApp.WriteLine("  Gaussian curvature: {0}", surface_curvature.Gaussian);
        RhinoApp.WriteLine("  Mean curvature: {0}", surface_curvature.Mean);

        return Result.Success;
    }
    public static Rhino.Commands.Result ConstrainedCopy(Rhino.RhinoDoc doc)
    {
        // Get a single planar closed curve
        var go = new Rhino.Input.Custom.GetObject();
        go.SetCommandPrompt("Select curve");
        go.GeometryFilter = Rhino.DocObjects.ObjectType.Curve;
        go.GeometryAttributeFilter = Rhino.Input.Custom.GeometryAttributeFilter.ClosedCurve;
        go.Get();
        if( go.CommandResult() != Rhino.Commands.Result.Success )
          return go.CommandResult();
        var objref = go.Object(0);
        var base_curve = objref.Curve();
        var first_point = objref.SelectionPoint();
        if( base_curve==null || !first_point.IsValid )
          return Rhino.Commands.Result.Cancel;

        Rhino.Geometry.Plane plane;
        if( !base_curve.TryGetPlane(out plane) )
          return Rhino.Commands.Result.Cancel;

        // Get a point constrained to a line passing through the initial selection
        // point and parallel to the plane's normal
        var gp = new Rhino.Input.Custom.GetPoint();
        gp.SetCommandPrompt("Offset point");
        gp.DrawLineFromPoint(first_point, true);
        var line = new Rhino.Geometry.Line(first_point, first_point+plane.Normal);
        gp.Constrain(line);
        gp.Get();
        if( gp.CommandResult() != Rhino.Commands.Result.Success )
          return gp.CommandResult();
        var second_point = gp.Point();
        Rhino.Geometry.Vector3d vec = second_point - first_point;
        if( vec.Length > 0.001 )
        {
          var xf = Rhino.Geometry.Transform.Translation(vec);
          Guid id = doc.Objects.Transform(objref, xf, false);
          if( id!=Guid.Empty )
          {
        doc.Views.Redraw();
        return Rhino.Commands.Result.Success;
          }
        }
        return Rhino.Commands.Result.Cancel;
    }
    protected override Result RunCommand(RhinoDoc doc, RunMode mode)
    {
      ObjRef obj_ref;
      var rc = RhinoGet.GetOneObject("Select surface", false, ObjectType.Surface, out obj_ref);
      if (rc != Result.Success || obj_ref == null)
        return rc;
      var surface = obj_ref.Surface();

      var gp = new GetPoint();
      gp.SetCommandPrompt("Point on surface");
      gp.Constrain(surface, false);
      var option_toggle = new OptionToggle(false, "U", "V");
      gp.AddOptionToggle("Direction", ref option_toggle);
      Point3d point = Point3d.Unset;
      while (true)
      {
        var grc = gp.Get();
        if (grc == GetResult.Option)
          continue;
        else if (grc == GetResult.Point)
        {
          point = gp.Point();
          break;
        }
        else
          return Result.Nothing;
      }
      if (point == Point3d.Unset)
        return Result.Nothing;

      int direction = option_toggle.CurrentValue ? 1 : 0; // V : U
      double u_parameter, v_parameter;
      if (!surface.ClosestPoint(point, out u_parameter, out v_parameter)) return Result.Failure;

      var iso_curve = surface.IsoCurve(direction, direction == 1 ? u_parameter : v_parameter);
      if (iso_curve == null) return Result.Failure;

      doc.Objects.AddCurve(iso_curve);
      doc.Views.Redraw();
      return Result.Success;
    }
Ejemplo n.º 6
0
    protected override Result RunCommand(RhinoDoc doc, RunMode mode)
    {
      // select a surface
      var gs = new GetObject();
      gs.SetCommandPrompt("select surface");
      gs.GeometryFilter = ObjectType.Surface;
      gs.DisablePreSelect();
      gs.SubObjectSelect = false;
      gs.Get();
      if (gs.CommandResult() != Result.Success)
        return gs.CommandResult();
      // get the selected face
      var face = gs.Object(0).Face();
      if (face == null)
        return Result.Failure;

      // pick a point on the surface.  Constain
      // picking to the face.
      var gp = new GetPoint();
      gp.SetCommandPrompt("select point on surface");
      gp.Constrain(face, false);
      gp.Get();
      if (gp.CommandResult() != Result.Success)
        return gp.CommandResult();

      // get the parameters of the point on the
      // surface that is clesest to gp.Point()
      double u, v;
      if (face.ClosestPoint(gp.Point(), out u, out v))
      {
        var direction = face.NormalAt(u, v);
        if (face.OrientationIsReversed)
          direction.Reverse();
        RhinoApp.WriteLine(
          string.Format(
            "Surface normal at uv({0:f},{1:f}) = ({2:f},{3:f},{4:f})", 
            u, v, direction.X, direction.Y, direction.Z));
      }
      return Result.Success;
    }
Ejemplo n.º 7
0
  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
        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)
  {
    Rhino.DocObjects.ObjRef objref;
    var rc = Rhino.Input.RhinoGet.GetOneObject("Select object", true, Rhino.DocObjects.ObjectType.AnyObject, out objref);
    if (rc != Rhino.Commands.Result.Success)
      return rc;

    rc = Rhino.Input.RhinoGet.GetPoint("Start point", false, out m_point_start);
    if (rc != Rhino.Commands.Result.Success)
      return rc;

    var obj = objref.Object();
    if (obj == null)
      return Rhino.Commands.Result.Failure;

    // create an instance of a GetPoint class and add a delegate
    // for the DynamicDraw event
    var gp = new Rhino.Input.Custom.GetPoint();
    gp.DrawLineFromPoint(m_point_start, true);
    var optdouble = new Rhino.Input.Custom.OptionDouble(m_distance);
    bool constrain = false;
    var optconstrain = new Rhino.Input.Custom.OptionToggle(constrain, "Off", "On");
    gp.AddOptionDouble("Distance", ref optdouble);
    gp.AddOptionToggle("Constrain", ref optconstrain);
    gp.DynamicDraw += ArrayByDistanceDraw;
    gp.Tag = obj;
    while (gp.Get() == Rhino.Input.GetResult.Option)
    {
      m_distance = optdouble.CurrentValue;
      if (constrain != optconstrain.CurrentValue)
      {
        constrain = optconstrain.CurrentValue;
        if (constrain)
        {
          var gp2 = new Rhino.Input.Custom.GetPoint();
          gp2.DrawLineFromPoint(m_point_start, true);
          gp2.SetCommandPrompt("Second point on constraint line");
          if (gp2.Get() == Rhino.Input.GetResult.Point)
            gp.Constrain(m_point_start, gp2.Point());
          else
          {
            gp.ClearCommandOptions();
            optconstrain.CurrentValue = false;
            constrain = false;
            gp.AddOptionDouble("Distance", ref optdouble);
            gp.AddOptionToggle("Constrain", ref optconstrain);
          }
        }
        else
        {
          gp.ClearConstraints();
        }
      }
    }

    if (gp.CommandResult() == Rhino.Commands.Result.Success)
    {
      m_distance = optdouble.CurrentValue;
      var pt = gp.Point();
      var vec = pt - m_point_start;
      double length = vec.Length;
      vec.Unitize();
      int count = (int)(length / m_distance);
      for (int i = 1; i < count; i++)
      {
        var translate = vec * (i * m_distance);
        var xf = Rhino.Geometry.Transform.Translation(translate);
        doc.Objects.Transform(obj, xf, false);
      }
      doc.Views.Redraw();
    }

    return gp.CommandResult();
  }
Ejemplo n.º 9
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            //pick surface to orient to or block instance to relocate
            GetObject gs = new Rhino.Input.Custom.GetObject();

            gs.SetCommandPrompt("Surface to orient new object on or BlockInstance to move");
            gs.GeometryFilter              = Rhino.DocObjects.ObjectType.Surface | Rhino.DocObjects.ObjectType.InstanceReference;
            gs.SubObjectSelect             = true;
            gs.DeselectAllBeforePostSelect = false;
            gs.OneByOnePostSelect          = true;
            gs.Get();
            if (gs.CommandResult() != Result.Success)
            {
                return(gs.CommandResult());
            }

            Rhino.DocObjects.ObjRef      objref = gs.Object(0);
            Rhino.DocObjects.RhinoObject obj    = objref.Object();
            if (obj == null)
            {
                return(Result.Failure);
            }
            surface = objref.Surface();


            //relocate block instance
            if (surface == null)
            {
                Rhino.DocObjects.InstanceObject instance1 = objref.Object() as Rhino.DocObjects.InstanceObject;

                instancePoint = instance1.InsertionPoint;
                double g, h;
                surface2.ClosestPoint(instancePoint, out g, out h);
                var instanceDirection = surface2.NormalAt(g, h);
                instancePlane = new Plane(instancePoint, instanceDirection);

                Rhino.Input.Custom.GetPoint gpss = new Rhino.Input.Custom.GetPoint();
                gpss.SetCommandPrompt("Point on surface to orient to");
                gpss.Constrain(surface2, false);

                gpss.DynamicDraw += RefObjDraw;
                gpss.Tag          = instance1;

                gpss.Get();
                if (gpss.CommandResult() != Rhino.Commands.Result.Success)
                {
                    return(gpss.CommandResult());
                }
                Point3d ptss = gpss.Point();
                surface2.ClosestPoint(ptss, out g, out h);
                var       direction1 = surface2.NormalAt(g, h);
                Plane     pl11       = new Plane(ptss, direction1);
                Transform iform      = Rhino.Geometry.Transform.PlaneToPlane(instancePlane, pl11);
                doc.Objects.Transform(instance1, iform, true);

                return(Result.Success);
            }

            obj.Select(false);

            //pick objekt to orient
            var copy = new Rhino.Input.Custom.OptionToggle(false, "No", "Yes");

            GetObject go = new GetObject();

            go.SetCommandPrompt("Select object to orient.");
            go.AddOptionToggle("Copy", ref copy);
            go.SubObjectSelect             = true;
            go.DeselectAllBeforePostSelect = false;
            go.GroupSelect = true;

            for (; ;)
            {
                var res = go.GetMultiple(1, -1);
                if (gs.CommandResult() != Result.Success)
                {
                    return(gs.CommandResult());
                }
                if (res == GetResult.Option)
                {
                    copyBol = copy.CurrentValue;
                    continue;
                }
                if (gs.CommandResult() != Result.Success)
                {
                    return(gs.CommandResult());
                }

                break;
            }



            int    obCount      = go.ObjectCount;
            string instDefCount = DateTime.Now.ToString("ddMMyyyyHHmmss");

            //create block instance and plane for instance
            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());
            }

            Vector3d vt1 = new Vector3d(0, 0, 1);
            Point3d  pt1 = gp.Point();

            sourcePlane = new Plane(pt1, vt1);
            Plane     originPlane = new Plane(Point3d.Origin, vt1);
            Transform bform       = Rhino.Geometry.Transform.PlaneToPlane(sourcePlane, originPlane);

            //block instance
            GeometryBase[] obj1List = new GeometryBase[obCount];
            List <Brep>    opList   = new List <Brep>();

            for (int igo = 0; igo < obCount; igo++)
            {
                Rhino.DocObjects.ObjRef      objref1 = go.Object(igo);
                Rhino.DocObjects.RhinoObject obj1    = objref1.Object();
                if (obj == null)
                {
                    return(Result.Failure);
                }
                obj.Select(false);


                Rhino.Geometry.Brep opItem = objref1.Brep();
                opList.Add(opItem);

                GeometryBase obj1Base = obj1.Geometry;
                obj1Base.Transform(bform);

                obj1List[igo] = obj1Base;
            }

            var orientBlock = doc.InstanceDefinitions.Add("Block" + instDefCount, "OrientBlock", Point3d.Origin, obj1List);

            //get all go.Objects to .Tag
            Brep[] op = new Brep[obCount];
            op = Brep.CreateBooleanUnion(opList, 0.01);
            Brep od = new Brep();

            od = op[0];
            var odGuid = doc.Objects.AddBrep(od);

            Rhino.DocObjects.ObjRef      objref2 = new Rhino.DocObjects.ObjRef(odGuid);
            Rhino.DocObjects.RhinoObject objDrw  = objref2.Object();

            //orient plane to surface
            if (copyBol)
            {
                while (true)
                {
                    Rhino.Input.Custom.GetPoint gps = new Rhino.Input.Custom.GetPoint();
                    gps.SetCommandPrompt("Point on surface to orient to. Press enter when done.");
                    gps.Constrain(surface, false);
                    gps.AcceptNothing(true);
                    gps.DynamicDraw += RefObjDraw;
                    gps.Tag          = objDrw;

                    var res = gps.Get();

                    if (res == GetResult.Nothing)
                    {
                        break;
                    }
                    //else if (gps.CommandResult() != Rhino.Commands.Result.Success)
                    //    return gps.CommandResult();


                    Point3d pts = gps.Point();
                    double  u, v;
                    surface.ClosestPoint(pts, out u, out v);
                    Vector3d direction = surface.NormalAt(u, v);
                    Plane    pl1       = new Plane(pts, direction);

                    Rhino.Geometry.Transform xform = Rhino.Geometry.Transform.PlaneToPlane(originPlane, pl1);

                    doc.Objects.AddInstanceObject(orientBlock, xform);

                    doc.Objects.Delete(objDrw);
                }
                copyBol = false;
            }
            else
            {
                Rhino.Input.Custom.GetPoint gps = new Rhino.Input.Custom.GetPoint();
                gps.SetCommandPrompt("Point on surface to orient to");
                gps.Constrain(surface, false);

                gps.DynamicDraw += RefObjDraw;
                gps.Tag          = objDrw;

                gps.Get();
                if (gps.CommandResult() != Rhino.Commands.Result.Success)
                {
                    return(gps.CommandResult());
                }
                Point3d pts = gps.Point();
                double  u, v;
                surface.ClosestPoint(pts, out u, out v);
                Vector3d direction = surface.NormalAt(u, v);
                Plane    pl1       = new Plane(pts, direction);

                Rhino.Geometry.Transform xform = Rhino.Geometry.Transform.PlaneToPlane(originPlane, pl1);

                doc.Objects.AddInstanceObject(orientBlock, xform);

                doc.Objects.Delete(objDrw);
            }

            surface2 = surface;

            return(Result.Success);
        }