Continuous Transformation in a loop

I have a with Transformation. I need to transform a selected object continuously in a loop as seen in the attached code. I am running the loop for 18 times and in each loop I am rotating the selected object by 10 degree around z axis ( 0,0,1). But even after doing lots of attempt in different permutation combination, I see only one transformation of 10 degree ( don’t know the first or the last ) where as I expect the selected object to be finally rotated by 180 degree when the loop ends.

What might be issue ?

The code is as below.

public class TransFormTest : Command
{

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
    Rhino.DocObjects.ObjRef rhobj1;
    var rc1 = RhinoGet.GetOneObject("Select Link One", true, Rhino.DocObjects.ObjectType.InstanceReference, out rhobj1);
    if (rc1 != Rhino.Commands.Result.Success)
        return rc1;

    currentTransform = Transform.Identity;

    for(int i = 1; i <= 18; i++)
    {
        TransformBrep(ref  doc, rhobj1);
        //Thread.Sleep(100);
    }
    
    return Result.Success;
}

public Rhino.Commands.Result TransformBrep(ref Rhino.RhinoDoc doc, Rhino.DocObjects.ObjRef rhobj1)
{

    double deg = 10.0;
    double rad = (22.0 / 7.0) / 180.0 * deg;
    Point3d cen = new Point3d(0,0,0);

    Transform xform = Rhino.Geometry.Transform.Rotation(rad, cen);

    currentTransform = currentTransform * xform ;
    //currentTransform = xform * currentTransform; // tried both

    doc.Objects.Transform(rhobj1, currentTransform, true);

    doc.Views.Redraw();  

    return Rhino.Commands.Result.Success;
}

Transform currentTransform;

}

I dont know exactly but can you try to add:

RhinoApp.wait() before the Redraw?

Just tested this quickly

 For Each selObj In doc.Objects.GetSelectedObjects(False, False)
                Dim obj As New ObjRef(selObj.Id)
                Dim g As Guid = obj.ObjectId
                Dim br As Brep = obj.Brep
                For i = 0 To 18 - 1
                    br.Translate(10, 0, 0)
                    doc.Objects.Replace(g, br)
                    RhinoApp.Wait()
                    doc.Views.Redraw()
                Next
            Next

and this seems to work.

I found somewhere that probably I need to call doc.Objects.Replace.

I have actually imported step files and the geometries I have are Inserts ( Block Reference/Instances ).

But no overload of Replace takes Insert as 2nd parameter.

By the way, I have already tested RhinoApp.wait(). But no good.

Can you check if the Guid of the objects change after the oc.Objects.Transform(rhobj1, currentTransform, true);?

Maybe you lose your reference while you do this…

Seems I have the same id after transformation.

Oke I found it.

This works:

   For Each selObj In doc.Objects.GetSelectedObjects(False, False)
                Dim obj As New ObjRef(selObj.Id)
                Dim g As Guid = obj.ObjectId
                Dim br As Brep = obj.Brep
                For i = 0 To 18 - 1
                    Dim xform As Transform = Rhino.Geometry.Transform.Translation(10, 0, 0)
                    doc.Objects.Transform(g, xform, True)
                    RhinoApp.Wait()
                    doc.Views.Redraw()
                Next
            Next

Thid doesn’t

   For Each selObj In doc.Objects.GetSelectedObjects(False, False)
                Dim obj As New ObjRef(selObj.Id)
                Dim g As Guid = obj.ObjectId
                Dim br As Brep = obj.Brep
                For i = 0 To 18 - 1
                    Dim xform As Transform = Rhino.Geometry.Transform.Translation(10, 0, 0)
                    doc.Objects.Transform(selObj, xform, True)
                    RhinoApp.Wait()
                    doc.Views.Redraw()
                Next
            Next

Difference?
doc.Objects.Transform(g, xform, True)
doc.Objects.Transform(selObj, xform, True)

After the first transform the reference to selObj/rhobj1 is gone! Use the guid instead and you are ready to go.

I am able to make my block instance to mesh.

Hope now I can go ahead.

But could you give me the c# equivalent of the line below

Dim obj As New ObjRef(selObj.Id)

1 Like

So your “animation” works?

I think something like this:

ObjRef obj = new ObjRef(selObj.Id);

Yes, now its working perfectly.

Final working code is as below.

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
Rhino.DocObjects.ObjRef rhobj1;

        var rc1 = RhinoGet.GetOneObject("Select Link 1", true, Rhino.DocObjects.ObjectType.Mesh, out rhobj1);
        if (rc1 != Rhino.Commands.Result.Success)
            return rc1;

        for (int i = 1; i <= 90; i++)
        {
            TransformBrep(ref  doc, rhobj1);
        }
        
        return Result.Success;
    }

    public Rhino.Commands.Result TransformBrep(ref  Rhino.RhinoDoc doc, Rhino.DocObjects.ObjRef rhobj1)
    {

        double deg = 1.0;
        double rad = (22.0 / 7.0) / 180.0 * deg;
        Point3d cen = new Point3d(0,0,0);

        Transform xform = Rhino.Geometry.Transform.Rotation(rad, cen);

        Guid g = rhobj1.ObjectId;

        Guid guid = doc.Objects.Transform(g, xform, true);

        doc.Views.Redraw();  

        return Rhino.Commands.Result.Success;
    }

But I have another problem.

I had selected one object. Now I want to select two objects as below.

        var rc1 = RhinoGet.GetOneObject("Select Link 1", true, Rhino.DocObjects.ObjectType.Mesh, out rhobj1);
        if (rc1 != Rhino.Commands.Result.Success)
            return rc1;

        var rc2 = RhinoGet.GetOneObject("Select Link 2", true, Rhino.DocObjects.ObjectType.Mesh, out rhobj1);
        if (rc2 != Rhino.Commands.Result.Success)
            return rc2;

Strangely its not selecting the 2nd object.

I tried to debug but it just skips those lines.

Any idea ?

Rhino.DocObjects.ObjRef rhobj1;

        var rc1 = RhinoGet.GetOneObject("Select Link 1", true, Rhino.DocObjects.ObjectType.Mesh, out rhobj1);
        if (rc1 != Rhino.Commands.Result.Success)
            return rc1;

        Rhino.DocObjects.ObjRef rhobj2;

        var rc2 = RhinoGet.GetOneObject("Select Link 2", true, Rhino.DocObjects.ObjectType.Mesh, out rhobj2);
        if (rc2 != Rhino.Commands.Result.Success)
            return rc2;

Does not work either.

Just the 2nd selection is skipped. Why ?

Because for the 2nd object select you got an object preselected (the 1st object one).

Place doc.objects.unselectall() in between.