I am working on a script which will transform multiple polysurfaces to the face of another polysurface.
This almost works, in that the objects are transformed to the plane of the face, but they lose two dimensions. So if a polysurface started at x=2.5, y=120, z=0.75, once transformed it is x=0, y=120, z=0. Clearly there is something I don’t understand about how to use PlaneToPlane transformation.
If anyone can help guide me in the right direction that would much appreciated.
Here is the script and attached is a sample file. In this sample the objective is to transform the group of objects on the wall_framing and wall_skin layers to the selected face of the object on the 02_sketch layer.
#! python 3
import Rhino
import scriptcontext as sc
import rhinoscriptsyntax as rs
def get_objects_plane(objects):
objects_bbox = None
for obj in objects:
obj = rs.coercerhinoobject(obj)
obj_bbox = obj.Geometry.GetBoundingBox(True)
if obj_bbox.IsValid:
if objects_bbox is None:
objects_bbox = obj_bbox
else:
objects_bbox.Union(obj_bbox)
if not objects_bbox or not objects_bbox.IsValid:
print("Could not compute bounding box for the group.")
return
# Compute the center points of both bounding boxes
objects_center = objects_bbox.Center
# Define the coordinate system of the group
objects_min = objects_bbox.Min
objects_x_axis = Rhino.Geometry.Vector3d(objects_bbox.Corner(True, True, True) - objects_min)
objects_y_axis = Rhino.Geometry.Vector3d(objects_bbox.Corner(False, True, True) - objects_min)
objects_z_axis = Rhino.Geometry.Vector3d(objects_bbox.Corner(True, True, False) - objects_min)
# Normalize the vectors
if objects_x_axis.Length > 0:
objects_x_axis.Unitize()
if objects_y_axis.Length > 0:
objects_y_axis.Unitize()
if objects_z_axis.Length > 0:
objects_z_axis.Unitize()
# Create planes for orientation
objects_plane = Rhino.Geometry.Plane(objects_center, objects_x_axis, objects_y_axis)
return objects_plane
go = Rhino.Input.Custom.GetObject()
go.SubObjectSelect = True
go.GeometryFilter = Rhino.DocObjects.ObjectType.Surface
go.SetCommandPrompt("Select face of object to transform objects to.")
go.Get()
res = go.Result()
objref = go.Object(0)
geo = sc.doc.Objects.Find(objref.ObjectId)
face = objref.Geometry()
if face.IsPlanar:
res, plane = face.TryGetPlane()
if res and plane.IsValid:
objects = rs.GetObjects("Select objects to transform", filter = rs.filter.polysurface)
objects_plane = get_objects_plane(objects)
print(f"objects_plane is {objects_plane}")
print(f"plane is {plane}")
# Compute the transformation
xform = Rhino.Geometry.Transform.PlaneToPlane(objects_plane, plane)
# Apply the transformation to all group members
for obj in objects:
sc.doc.Objects.Transform(obj, xform, True)
transform_test.3dm (2.5 MB)