Ellipsoid c#

I am struggling to figure out how to create ellipsoids via the C# API. Rhino clearly has a method for making ellipsoid that creates a very clean brep. But I haven’t figured out how to replicate this behavior via code. My current method involves creating surfaces of revolution based on an ellipse curve but that seems pretty inefficient.

Hi @gruedisueli,

Consider this method:

public static NurbsSurface CreateEllipsoid(Ellipse ellipse1, Ellipse ellipse2)
{
  if (!ellipse1.Plane.IsValid || ellipse1.Radius1 == 0 || ellipse1.Radius2 == 0 || ellipse2.Radius2 == 0)
    return null;

  var plane = new Plane(ellipse1.Plane.Origin, ellipse1.Plane.YAxis, ellipse1.Plane.ZAxis);
  var sphere = new Rhino.Geometry.Sphere(plane, 1.0);
  var nurb = sphere.ToNurbsSurface();
  if (!nurb.IsValid)
    return null;

  var xform = Transform.Scale(ellipse1.Plane, ellipse1.Radius1, ellipse1.Radius2, ellipse2.Radius2);
  nurb.Transform(xform);
  if (!nurb.IsValid)
    return null;

  return nurb;
}

You can use it like this:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  var ellipse1 = new Ellipse(Plane.WorldXY, 5.0, 2.0);
  var ellipse2 = new Ellipse(Plane.WorldYZ, 2.0, 3.0);

  var nurb = CreateEllipsoid(ellipse1, ellipse2);
  if (null != nurb)
  {
    doc.Objects.AddSurface(nurb);
    doc.Views.Redraw();
  }

  return Result.Success;
}

– Dale

@dale great, thanks, works! Yes, I was wondering about this transformation approach before I started this thread. I think I was thrown off by the language in the documentation about transforming spheres, which to me implied that the transform of a sphere wouldn’t accept a “non-similarity preserving transformation” input, but I guess all they were saying is that you can certainly do it but to remember that the sphere will become ellipsoidal. Anyways, this should work quite well for my purposes.

This comment applies to a Sphere, proper, not a NURBS surface that looks like a sphere…

– Dale

Ah yes, of course… The sphere has to be a sphere to be called as such!