Move Point with RhinoCommon

Any hint why this doesn’t work?

#################################
### TEST Move point by vector ###
#################################
import System
import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino



def TST():
    pt0_id = rs.GetObject("select point: ",rs.filter.point)
    pt0 = rs.coerce3dpoint(pt0_id)
    
    pt_move_transform = Rhino.Geometry.Transform.Translation.Overloads[Rhino.Geometry.Vector3d](Rhino.Geometry.Vector3d.XAxis*100)
    print pt_move_transform
    """
    Help on method-descriptor Transform

 |  Transform(...)
 |          Transform(self: Point3d, xform: Transform)
 |              Transforms the present point in place. The transformation matrix acts on the 
 |               left of the point. i.e.,
 |                      result = transformation*point
 |          
 |          
 |              xform: Transformation to apply.

    """
    pt1 = pt0.Transform(pt_move_transform) # THIS DOESN'T SEEM TO WORK
    print "PT0: ", pt0
    print "PT1: ", pt1
    #
    if pt1 is not None:
        sc.doc.Objects.AddPoint(pt1)

if __name__ == "__main__":
    TST()

Thanks.

I think we had this discussion in the opposite direction recently.

pt1 = pt0.Transform(pt_move_transform)

doesn’t work because the _.Transform() methods transform the original object - they do not make a copy. The return value is just a Boolean True or False (success/fail).

Try adding pt0 to the document after the transform…

Pffff, damn static methods

Yep, this is not as easier as I imagined.

A simple move of a point by vector… :zipper_mouth_face:

Just add the vector to the point… (yes, it works) :stuck_out_tongue_winking_eye:

import Rhino
import scriptcontext as sc

ptA=Rhino.Geometry.Point3d(0,0,0)
move_vec=Rhino.Geometry.Vector3d(1,1,0)
move_vec*=10
ptB=ptA+move_vec
sc.doc.Objects.AddPoint(ptB)
sc.doc.Views.Redraw()

I know, i wanted to do this with RhinoCommon and it seems a bit overcomplicated to me.

I even forgot why I started reading about Transform.Translation in the first place. :crazy_face:

That is RhinoCommon. All the operators with points and vectors are simply part of the object’s methods…

Dunno, Transform (in place) seems pretty simple to me. The main thing to remember is if you do need to keep the original, you need to make a copy of the object first and transform the copy.

1 Like

it is simple, but you have to take care of GUID replacing and all that poop

Well, yeah, if you are going to work in RhinoCommon and want to deal with objects in the document, you are always going to need to be pulling them in to RC and then pushing them back out to the doc. That’s what rhinoscriptsyntax does for you automatically…

I found out most of my scripts are mixing rhinocommon and rhinoscriptsyntax and I end up confusing myself reading them.

I need to establish a better approach, either just RC or more comments.

Solution:

#################################
### TEST Move point by vector ###
#################################
import System
import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino


def gimme_it():
    go = Rhino.Input.Custom.GetObject()
    go.SetCommandPrompt("Select objects to group")
    go.GroupSelect = True
    go.GetMultiple(1, 0)
    
    if go.CommandResult()!=Rhino.Commands.Result.Success:
        return go.CommandResult()
    
    ids = [go.Object(i).ObjectId for i in range(go.ObjectCount)]
    #geos = [go.Object(i).Geometry for i in range(go.ObjectCount)]
    if len(ids) > 1:
        return ids
    if len(ids) == 1:
        return ids[0]


def TST_RC():
    get_object = gimme_it()
    #pt0_id = rs.GetObject("select point: ",rs.filter.point)
    pt0_id = get_object#[0][0]
    
    pt_move_transform = Rhino.Geometry.Transform.Translation.Overloads[Rhino.Geometry.Vector3d](Rhino.Geometry.Vector3d.XAxis*10000)
    pt0_obj = sc.doc.Objects.Find(pt0_id)
    
    pt1_obj = pt0_obj.DuplicateGeometry()
    pt1 = pt1_obj
    pt1.Transform(pt_move_transform)
    if pt1 is not None:
        sc.doc.Objects.Replace(pt0_id,pt1)

if __name__ == "__main__":
    TST_RC()

UPDATE:

OMG a better simpler solution
https://developer.rhino3d.com/5/api/RhinoCommon/html/M_Rhino_Geometry_Point3d_Add_1.htm

Point3D.Add()

Umm, that’s what I was trying to tell you… Except you can also just use pt + vec

1 Like

Well, yes, it can get confusing if you are not careful - and comments are good. I approach it more or less this “hybridized” way:

  1. Anything that deals with getting objects from the Rhino document, manipulating them and immediately returning them to the document, I stick to 100% rhinoscriptsyntax if I can.

  2. For user interaction such as picking stuff, I almost always use rhinoscriptsyntax as well, except if I really need to create command line options.

  3. For anything that needs intermediate geometry that I will not add to the document, or for functions not available in rhinoscriptsyntax I use RC.

  4. The above means I often have the two together in a script (like you). To separate them, I try to name my variables in such a way as to distinguish between RC geometry objects and document object GUIDS. So anything that will be a GUID is named something like obj_id, whereas anything that is geometry will be named just obj something. That way you can tell at a glance what is supposed to be what.

2 Likes

to clarify what I said, I know the mathematical way of moving the point by vector, just didn’t know there was a dedicated method for that :smiley:, hence me struggling with the transforms.