Response to Gumball transform

Hi, all!

I am new to Rhino development. I am trying to write a test plug-in that maintains a rectangle’s area when the user scales the rectangle using gumball. First, I want to test transforming the rectangle again immediately after the user drags the gumball’s scaling handle.

In order to do this, I gather information of a transform in the event handler of RhinoDoc.BeforeTransformObjects. Then I call a method SecondTransform() inside the event handler of RhinoDoc.ReplaceRhinoObject. However SecondTransform() runs twice and the object is not correctly deformed.

What’s wrong? Is there a better way to do it?

Thanks a lot!

Peng

Here is the code.

class EventHandlers    {
…
Transform CurrentTransform;
System.Collections.Generic.IEnumerable<RhinoObject> CurrentObj;

    void OnBeforeTransformObjects(object sender, Rhino.DocObjects.RhinoTransformObjectsEventArgs e)
    {
        TryTraceTransform(sender, e);
    }
    public void TryTraceTransform(object sender,RhinoTransformObjectsEventArgs e)
    {
        CurrentTransform = e.Transform;
        CurrentObj = RhinoDoc.ActiveDoc.Objects.GetSelectedObjects(true, true);
    }

    void OnReplaceRhinoObject(object sender, Rhino.DocObjects.RhinoReplaceObjectEventArgs e)
    {
        SecondTransform();
    }

    public void SecondTransform()
    {
        if (CurrentTransform.SimilarityType == TransformSimilarityType.NotSimilarity)
        {
            Transform t;
            CurrentTransform.TryGetInverse(out t);// Arbitrary transform as a test
            List<Guid> guids = new List<Guid>();
            foreach (var o in CurrentObj)
            {
                Guid g = o.Id;
                guids.Add(g);
            }
            foreach (var g in guids)
            {
                RhinoDoc.ActiveDoc.Objects.Transform(g, t, true);
            }
        }
    }
}
1 Like

If it runs twice, the event handler may be subscribed multiple times. Also, TryGetInverse returns a boolean, so test if it actually returned true.

Oh, and also: in your last line of code you will again replace an object! So, you need to test if you are not already replacing it like so:

bool _isHandlingReplacement = false;
    void OnReplaceRhinoObject(object sender, Rhino.DocObjects.RhinoReplaceObjectEventArgs e)
    {
        if (_isHandlingReplacement) return;
        _isHandlingReplacement = true;
        SecondTransform();
        _isHandlingReplacement = false;
    }

menno,

Thank you very much!
You are right, the last line of my code did that, and your solution works well. However, Rhino still says “Unable to transform 1 object.” when the object is correctly transformed. I also don’t quite get why it ran just twice instead of running endlessly. (I guess it doesn’t matter but if it is easy to see why, please point out.)