Commandline options problem in C#

I am trying to make a simple command in rhino to array rectangles in a curve, the problem is the commandline options does not work properly as an input. This is just a test I am trying to learn C# in VS.

        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
            {
                int i;
                var selectedcurves = new CurveList();
                Point3d[] divptarray;


                //Step 1 - Select curves---------------------------------------------------

                GetObject gp = new GetObject();
                gp.SetCommandPrompt("Select some curves");
                gp.GeometryFilter = ObjectType.Curve;
                RhinoApp.WriteLine("Now Choose the Options");

                // set up the options
                OptionInteger intOption = new OptionInteger(1, 1, 99);
                OptionDouble dblOption = new OptionDouble(2.2, 0, 99.9);
                OptionDouble dblOption2 = new OptionDouble(2.2, 0, 99.9);

                gp.AddOptionInteger("Division", ref intOption);
                gp.AddOptionDouble("x", ref dblOption);
                gp.AddOptionDouble("y", ref dblOption2);

                int divisions = intOption.CurrentValue;
                double Xrec = dblOption.CurrentValue;
                double Yrec = dblOption2.CurrentValue;


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

                    if (get_rc == 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}", dblOption2.CurrentValue);
                    }
                    else if (get_rc == GetResult.Option)
                    {
                        continue;
                    }
                    break;
                }

                var curve = gp.Object(0).Curve();

                double[] parameters = curve.DivideByCount(divisions, true, out divptarray);

                myFunctions.XDrawPoints(divptarray, doc);

                myFunctions.XDrawRectanglesInCurve(divptarray, curve, Xrec, Yrec, doc);

                doc.Views.Redraw();

                return Result.Success;
            }

One thing that I notice is that you get the values from the options before the while(true) loop, so these variables (divisions, Xrec and Yrec) are never changed due to the user changing the option values.

My approach would be something like this:

{
  int i;
  var selectedcurves = new CurveList();
  Point3d[] divptarray;
  //Step 1 - Select curves---------------------------------------------------

  // default values
  int divisions = 1;
  double Xrec = 2.2;
  double Yrec = 2.2;

  GetObject gp = new GetObject();
  gp.SetCommandPrompt("Select some curves");
  gp.GeometryFilter = ObjectType.Curve;
  RhinoApp.WriteLine("Now Choose the Options");


  while (true)
  {
    // clear and re-build command options
    gp.ClearCommandOptions();
    
    // set up the options
    OptionInteger intOption = new OptionInteger(divisions, 1, 99);
    OptionDouble dblOption  = new OptionDouble(Xrec, 0, 99.9);
    OptionDouble dblOption2 = new OptionDouble(Yrec, 0, 99.9);

    gp.AddOptionInteger("Division", ref intOption);
    gp.AddOptionDouble("x", ref dblOption);
    gp.AddOptionDouble("y", ref dblOption2);

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

    if (get_rc == 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}", dblOption2.CurrentValue);
    }
    else if (get_rc == GetResult.Option)
    {
      divisions = intOption.CurrentValue;
      Xrec = dblOption.CurrentValue;
      Yrec = dblOption2.CurrentValue;
      continue;
    }
    break;
  }
  
  var curve = gp.Object(0).Curve();

  double[] parameters = curve.DivideByCount(divisions, true, out divptarray);

  myFunctions.XDrawPoints(divptarray, doc);

  myFunctions.XDrawRectanglesInCurve(divptarray, curve, Xrec, Yrec, doc);

  doc.Views.Redraw();

  return Result.Success;
}

It works :slight_smile: , many thanks !!