What do "coerce3dpoint" "coerce3dvector" stand for?


#1

I have been checking the VectorScale rhinoscriptsyntax method and it’s RhinoCommon code, and went onto a “coerce3dvector” function:

import utility as rhutil

def VectorScale(vector, scale):
    vector = rhutil.coerce3dvector(vector, True)
    return vector*scale

I remember that I have seen the “coerce3dpoint” function too.

What do these two functions do?

Thank you.


Move objects
#2

Python is a bit different than vb Rhinoscript, it is an object-oriented language. Whereas in vb Rhinoscript a point is represented by a simple array of three numbers, in Python, a 3dpoint is a type of object. If you simply define a point in Python by a list or tuple [x,y,z] or (x,y,z) it is not recognized as a 3dpoint object by RhinoCommon. rs.coerce3dpoint will take a list or tuple of three numbers - or even the object ID of an existing point object - and return a real Python 3dpoint object.

Most of the Python rhinoscriptsyntax methods actually use coerce3dpoint and other “coerce” methods behind the scenes to make it easier for the user to use simple scripting methods similar to vb Rhinoscript without having to worry too much about the complexities of OOP in Python.

–Mitch


(Menno Deij - van Rijswijk) #3

Some Googling turns up the code below. Basically it tries to convert a Python tuple or list or a Point3d struct to a Vector3d struct.
Even though Python is dynamically typed, it still has types. The correct type needs to be used to ensure that the right code is executed, especially when interacting with strongly typed code (e.g. RhinoCommon).

def coerce3dvector(vector):
  """
  Convert input into a Rhino.Geometry.Vector3d if possible.
  If not possible, return None
  """
  if( type(vector) is Rhino.Geometry.Vector3d): return vector
  if( type(vector) is list or type(vector) is tuple ):
    length = len(vector)
    if( length == 3 and type(vector[0]) is not list ):
      rc = Rhino.Geometry.Vector3d(vector[0], vector[1], vector[2])
      return rc
    return None
  if( (type(vector) is Rhino.Geometry.Point3d) or (type(vector) is Rhino.Geometry.Point3f) ):
    rc = Rhino.Geometry.Vector3d(vector.X, vector.Y, vector.Z)
    return rc
  return None

#4

Thank you guys.