How do I update a sphere radius with RhinoCommon?

I’m completely new to the Rhino SDK and trying to figure out how things work. I want to change the radius of an existing sphere and see the change on the display. This adds the sphere:

doc.Objects.AddSphere[sphere];
doc.Views.Redraw[];

Now I want to change its radius. This naive attempt doesn’t work, I suspect because the object in the document is not the sphere I supplied as an argument to AddSphere:

sphere.Radius = 2;
doc.Views.Redraw[];

AddSphere returns a Guid, which I’d guess I can use to get at the object whose radius I want to change. Can anyone fill me in or point me to an example?

Chris

Rhino does not have a spherical surface object. When you add a Rhino.Geometry.Sphere to the document, a surface of revolution is created from the Sphere - Sphere.ToRevSurface() - and that surface is added to the document in for form of a single-faced Rhino.Geometry.Brep object.

To modify Rhino objects, you must:

  1. Get the object
  2. Get the object’s geometry
  3. Make a copy of the geometry
  4. Replace the object’s geometry by calling doc.Objects.Replace

By following these rules, undo and redo work for the end user.

Does this help?

1 Like

Yes, thanks, that helps a lot. Just one question: how do I retrieve the object from the Guid returned by AddSphere?

Try this:

Rhino.Geometry.Plane plane = Rhino.Geometry.Plane.WorldXY;
double radius = 5.0;
Rhino.Geometry.Sphere sphere = new Sphere(plane, radius);
System.Guid objectId = doc.Objects.AddSphere(sphere);
doc.Views.Redraw();

Thanks again, Dale. I’ve gotten that far, but I don’t know how to get from objectId to the object itself in step 1 in your first response. If I’m being very dense, don’t worry about it–I’ll figure it out.

Here is a full blown example you might want to review:

https://github.com/dalefugier/SampleCsCommands/blob/master/SampleCsModifySphereRadius.cs

Thanks for the detailed example! The geometry updating code will be very helpful. I had seen several examples of that sort, where the object to be updated is selected interactively. But I just wanted to update the radius of the sphere that I had just created via AddSphere—without any interaction. So my question remains, is there no way to get at the object that corresponds to a given Guid?

If you have an object’s GUID and want to retrieve the object that owns it, then you can do something like this:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  Guid guid = <some GUID...>;

  RhinoObject rhino_object = doc.Objects.Find(guid);
  if (null != rhino_object)
  {
    // TODO...
  }
  return Result.Success;
}

Does this help?

or

RhinoObject rhino_object = new docobject.objref(Guid)

Yes! That’s what I’ve been looking for. Thanks so much for your help, Dale and Jordy.

I think I must be a bit slower in comprehension than Mr. Carlson.

What are the detailed differences between

RhinoObject rhino_object = doc.Objects.Find(guid);
and
RhinoObject rhino_object = new docobject.objref(Guid) ?

Thanks.

As in most cases, there is more than one way of doing things…

I would not suggest to use this one, as it makes the assumption that the document is always only one, which might not be the case (especially in the future). The first one is a much better option. This latter one does not work well with GhPython either, for example, because GhPython uses its own document, and I hope in general we will discourage its use.

Thanks,

Giulio

Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com