Rhinoscript MoveObject Python to C#

1.1k views Asked by At

I'm converting a Python script to C# and I'm running into the occasional issue along the way. This time it's with relocating a point from one location to another. In the python script it's line two of the method that I don't know how to convert. I've reviewed the Rhino documentation but I'm still confused.

def move(self):
    self.trailPts.append(self.pos)
    self.pos = rs.PointCoordinates(rs.MoveObject(self.id, self.vec))

This is where I'm at so far:

Transform trans = new Transform(Transform.Translation(Vec));
Pos = PointID.Transform(Pos, trans, true);

But it's not correct. I'm getting an overload error for Transform on line 2. Any help would be great. Thanks!

Here is my C# constructor as well :

public Agent(Point3d pos, Vector3d vec, Point3d pointID, List<Point3d> listOfAgents, List<Point3d> navPoints, List<Circle> pipeProfiles)
        {
            Pos = pos;
            Vec = vec;
            PointID = pointID;
            ListOfAgents = listOfAgents;
            NavPoints = navPoints;
            PipeProfiles = pipeProfiles;

            TrailPoints.Add(new Point3d(Pos));
        }

And the original python constructor :

 def __init__(self, POS, VEC, POINTID, LISTOFAGENTS, NAVPOINTS, PIPEPROFILES):
        self.pos = POS
        self.vec = VEC
        self.id = POINTID
        self.list = LISTOFAGENTS
        self.nav = NAVPOINTS
        self.trailPts = []
        self.trailPts.append(self.pos)
        self.trailID = "empty"
        self.pipeProfiles = PIPEPROFILES
        print("made an agent")
1

There are 1 answers

11
Martijn Pieters On

MoveObject() is a wrapper for MoveObjects where the first result value is returned rather than a list. Looking at the RhinoScript implementation for MoveObjects we see:

xf = Rhino.Geometry.Transform.Translation(translation)
rc = TransformObjects(object_ids, xf)
return rc

where translation is a Vector3d object.

Then looking at TransformObjects the call comes down to

scriptcontext.doc.Objects.Transform(id, xf, True)

The PointCoordinates() function takes the GUID that MoveObject() returned and finds you the object again, then gives you the location for the geometry of that object (via coercegeometry() and provided .Geometry is a Point instance); skipping tests and functions to convert possible other acceptable types this comes down to:

scriptcontext.doc.Objects.Find(id).Geometry.Location

Translating these to RhinoCommon objects would be:

using Rhino.Geometry;
using System;

Transform xf = Transform.Translation(vec);
id = doc.Objects.Transform(id, xf, true);
Point pos = doc.Objects.Find(id).Geometry as Point
Point3d pos3d = pos.Location;

where vec is a Rhino.Geometry.Vector3d instance, and id an object reference.

Also see the Rhino.Geometry.Transform.Translation() documentation, which include a C# example, and the ObjectTable.Transform methods.

Note that the Rhino.Geometry.Transform.Translation() static method already returns a Transform instance, no need to use new Transform() here. And there is no PointID type; perhaps you were looking for Rhino.Geometry.Point3D? The Point3D.Transform() method would operate on that point, however, not on an object with a given id.