[C++] custom object transform/morph - how?

customobject

(Menno Deij - van Rijswijk) #1

Yesterday I started to make a custom object (inherited from CRhinoSurfaceObject) that is a wrapper for a surface spline object from another library. Works perfectly, except that when I move or otherwise transform the object, the wrapped object needs to be updated too.

Now, in the C# implementation of a custom object, there are the OnTransform and OnSpaceMorph methods, and these allow me to do just that: hook into a transform/morph operation. I’m sure that this is possible in C++ too, but I can’t find how.


(Dale Fugier) #2

Hi Menno,

You’ve got a few options for hooking transformations. First are these event watchers:

CRhinoOnTransformObject
CRhinoAfterTransformObject

See rhinoSdkEventWatcher.h for the declarations.

Also, if you are attaching user data , then t he ON_UserData::Transform virtual function can be used.

Also, ON_Geometry::Transform and ON_Geometry::Morph`` are virtuals. This is the approach taken by RhinoCommon’s custom objects.

Does this help?

– Dale


(Menno Deij - van Rijswijk) #3

Yes, I started to wrap the object not in a CRhinoSurfaceObject but in an object derived from ON_Surface. Are there any examples that show how to do this? So far, my surface reports that it is invalid without any text in the log, and can’t be added to the Rhino document (as the geometry of a plain old CRhinoSurfaceObject)… :-/


(Menno Deij - van Rijswijk) #4

Another approach I can also try is to attach the wrapped geometry in the user data, good suggestion, thanks :smile:


(Dale Fugier) #5

You’d need to derive from a non-abstract class, such as ON_NurbsSurface. I don’t have any examples of this.


(Menno Deij - van Rijswijk) #6

Deriving from ON_NurbsSurface works in so far that I can add the wrapped geometry to the document.
Unfortunately, the Transform function is not called on the wrapped geometry either :angry:

I’ll try to follow the user data approach, that should work I guess.


#7

I’m looking for a method of the kind void OnTransform(ON_Xform& xform) for an event watcher, just as you do with events like OnAddObject in your SampleObjectEventWatcher example.

But I don’t manage to handle CRhinoOnTransformObject nor a CRhinoEventWatcherEx. It would be very helpful if you provided a small example of this.

I got the data member m_userdata_xform to transform user data. The only problem is that I don’t know where in the plugin to apply the transformation.

(In my case, I need to transform user data attached to a custom grips object. I tried to apply the transform inside the CreateGrips method, but it’s not very clever as the grips get constantly transformed when dragged and the whole thing blows up after some interaction)

This takes me to the question above about the event watcher…

Pablo


(Dale Fugier) #8

Hi Pablo,

If you want your user data to be transformed when the owning object is transformed, then simply override the ON_UserData::Transform virtual function and apply the transformation, provided as input to the function, to your data.

See the comments in opennurbs_userdata.h for details.

– Dale


#9

Hi Dale,

It works!! That’s exactly what I was looking for. Many thanks! :heart_eyes:

Pablo


(Menno Deij - van Rijswijk) #10

Yes, in the end I also implemented it this way. There was some fiddling to get the order right, but in the end it works well.