public static Rhino.Commands.Result CommandLineOptions(Rhino.RhinoDoc doc)
  {
    // For this example we will use a GetPoint class, but all of the custom
    // "Get" classes support command line options.
    Rhino.Input.Custom.GetPoint gp = new Rhino.Input.Custom.GetPoint();
    gp.SetCommandPrompt("GetPoint with options");

    // set up the options
    Rhino.Input.Custom.OptionInteger intOption = new Rhino.Input.Custom.OptionInteger(1, 1, 99);
    Rhino.Input.Custom.OptionDouble dblOption = new Rhino.Input.Custom.OptionDouble(2.2, 0, 99.9);
    Rhino.Input.Custom.OptionToggle boolOption = new Rhino.Input.Custom.OptionToggle(true, "Off", "On");
    string[] listValues = new string[] { "Item0", "Item1", "Item2", "Item3", "Item4" };

    gp.AddOptionInteger("Integer", ref intOption);
    gp.AddOptionDouble("Double", ref dblOption);
    gp.AddOptionToggle("Boolean", ref boolOption);
    int listIndex = 3;
    int opList = gp.AddOptionList("List", listValues, listIndex);

    while (true)
    {
      // perform the get operation. This will prompt the user to input a point, but also
      // allow for command line options defined above
      Rhino.Input.GetResult get_rc = gp.Get();
      if (gp.CommandResult() != Rhino.Commands.Result.Success)
        return gp.CommandResult();

      if (get_rc == Rhino.Input.GetResult.Point)
      {
        doc.Objects.AddPoint(gp.Point());
        doc.Views.Redraw();
        Rhino.RhinoApp.WriteLine("Command line option values are");
        Rhino.RhinoApp.WriteLine(" Integer = {0}", intOption.CurrentValue);
        Rhino.RhinoApp.WriteLine(" Double = {0}", dblOption.CurrentValue);
        Rhino.RhinoApp.WriteLine(" Boolean = {0}", boolOption.CurrentValue);
        Rhino.RhinoApp.WriteLine(" List = {0}", listValues[listIndex]);
      }
      else if (get_rc == Rhino.Input.GetResult.Option)
      {
        if (gp.OptionIndex() == opList)
          listIndex = gp.Option().CurrentListOptionIndex;
        continue;
      }
      break;
    }
    return Rhino.Commands.Result.Success;
  }
예제 #2
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            var pack_algorithm = PackingAlgorithm.Fast;
              Point3d base_point = new Point3d();
              var option_count = new OptionInteger(100, true, 2);
              var option_min_radius = new OptionDouble(0.1, true, 0.001);
              var option_max_radius = new OptionDouble(1.0, true, 0.001);
              var option_iterations = new OptionInteger(10000, false, 100);

              bool done_looping = false;
              while (!done_looping)
              {
            var gp = new GetPoint();
            gp.SetCommandPrompt("Center of fitting solution");
            gp.AddOptionInteger("Count", ref option_count);
            gp.AddOptionDouble("MinRadius", ref option_min_radius);
            gp.AddOptionDouble("MaxRadius", ref option_max_radius);
            gp.AddOptionInteger("IterationLimit", ref option_iterations);
            int index_option_packing = gp.AddOption("Packing", pack_algorithm.ToString());
            gp.AcceptNumber(true, true);

            switch( gp.Get() )
            {
              case GetResult.Point:
            base_point = gp.Point();
            done_looping = true;
            break;
              case GetResult.Option:
            if (index_option_packing == gp.OptionIndex())
            {
              var get_algorithm = new GetOption();
              get_algorithm.SetCommandPrompt("Packing");
              get_algorithm.SetDefaultString(pack_algorithm.ToString());
              var opts = new string[]{"Fast", "Double", "Random", "Simple"};
              int current_index = 0;
              switch(pack_algorithm)
              {
                case PackingAlgorithm.Fast:
                  current_index = 0;
                  break;
                case PackingAlgorithm.Double:
                  current_index = 1;
                  break;
                case PackingAlgorithm.Random:
                  current_index = 2;
                  break;
                case PackingAlgorithm.Simple:
                  current_index = 3;
                  break;
              }
              int index_list = get_algorithm.AddOptionList("algorithm", opts, current_index);
              get_algorithm.AddOption("Help");
              while( get_algorithm.Get() == GetResult.Option )
              {
                if (index_list == get_algorithm.OptionIndex())
                {
                  int index = get_algorithm.Option().CurrentListOptionIndex;
                  if (0 == index)
                    pack_algorithm = PackingAlgorithm.Fast;
                  if (1 == index)
                    pack_algorithm = PackingAlgorithm.Double;
                  if (2 == index)
                    pack_algorithm = PackingAlgorithm.Simple;
                  if (3 == index)
                    pack_algorithm = PackingAlgorithm.Random;
                  break;
                }
                // if we get here, the user selected help
                const string help =
                  @"Fast: fast packing prevents collisions by moving one
            circle away from all its intersectors. After every collision
            iteration, all circles are moved towards the centre of the
            packing to reduce the amount of wasted space. Collision
            detection proceeds from the center outwards.

            Double: similar to Fast, except that both circles are moved
            in case of a collision.

            Random: similar to Fast, except that collision detection is
            randomized rather than sorted.

            Simple: similar to Fast, but without a contraction pass
            after every collision iteration.";
                Rhino.UI.Dialogs.ShowMessageBox(help, "Packing algorithm description", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
              }
            }
            break;
              default:
            return Result.Cancel;
            }
              }
              int count = option_count.CurrentValue;
              double min_radius = option_min_radius.CurrentValue;
              double max_radius = option_max_radius.CurrentValue;
              int iterations = option_iterations.CurrentValue;

              // TODO: try setting up a background worker thread and
              // communicate with the GetString through messages
              //GetString gs = new GetString();
              //gs.SetCommandPrompt("Press escape to cancel");

              using (var all_circles = new PackCircles(base_point, count, min_radius, max_radius))
              {
            double damping = 0.1;
            for (int i = 1; i <= iterations; i++)
            {
              RhinoApp.SetCommandPrompt(string.Format("Performing circle packing iteration {0}...  (Press Shift+Ctrl to abort)", i));

              if (System.Windows.Forms.Control.ModifierKeys == (System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Shift))
              {
            RhinoApp.WriteLine("Circle fitting process aborted at iteration {0}...", i);
            break;
              }

              if (!all_circles.Pack(pack_algorithm, damping, doc.ModelAbsoluteTolerance))
              {
            RhinoApp.WriteLine("Circle fitting process completed at iteration {0}...", i);
            break;
              }

              damping *= 0.98;
              doc.Views.Redraw();
              RhinoApp.Wait();
            }
            all_circles.Add(doc);
              }
              doc.Views.Redraw();
              return Result.Success;
        }
  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();
  }