Looking for a Python script to optimise surface positioning in Rhino

Hi everyone,

I’m working on a project in Rhino where I need to optimize the orientation of multiple planar surface on my C-plane. The idea is to find the best orientation for each surface so that the bounding rectangle has the small possible area, maybe i have to keep a maximum length and width limits.

I’m a bit stuck on how to write the loop logic for this. Should I test every possible angle, degree by degree, or is there a smarter way to do it? If someone has a script or a code example that could help me get started, that would be amazing.

This is for a woodworking project to reduce wastes when cutting woode panels.

Thanks a lot for your help!

Best,

dear @onlyforpeace
not sure if I 100% understood your needs - we are talking about a 2d-problem ?
why don t you use a existing nesting solution ? there is opennest and there are several online services that offer demo or trail accounts.
Do you have a screenshot / sketch that illustrates your challenge ?

kind regards -tom

I try this:```# coding: utf-8
import rhinoscriptsyntax as rs

Sélection des surfaces

objs = rs.GetObjects(“Sélectionnez les surfaces à optimiser”, 8)
if not objs:
print(“Aucun objet sélectionné.”)
else:
try:
rs.EnableRedraw(enable=False)
f=1
for obj in objs:
print(“###################### Traitement de la forme 1 ######################”)
rs.SelectObject(obj)

        # Calcul initial de la boîte englobante
        bbox_start = rs.BoundingBox(obj)
        if not bbox_start:
            print("Impossible de calculer la BoundingBox pour l'objet .")
            continue

        rotate_point = bbox_start[0]
        current_min_width = rs.Distance(bbox_start[3], bbox_start[0])
        optimal_angle = 0  # Angle initial

        # Recherche de l'angle optimal
        for angle in range(0, 180):  # Rotation de 0° à 179°
            rotated_obj = rs.RotateObject(obj, rotate_point, angle, copy=True)
            if not rotated_obj:
                print("Erreur lors de la rotation de l'objet.")
                continue

            bbox = rs.BoundingBox(rotated_obj)
            if bbox:
                # Calcul des dimensions
                current_length = rs.Distance(bbox[1], bbox[0])
                current_width = rs.Distance(bbox[3], bbox[0])

                print("Rotation ",angle,"° : longueur = ",current_length," largeur = ",current_width)

                # Mise à jour de l'angle optimal
                if current_width < current_min_width:
                    current_min_width = current_width
                    optimal_angle = angle
                    print("Nouvelle largeur minimale : ",current_min_width," à l'angle ",optimal_angle'"°")

            # Suppression de l'objet temporaire
            rs.DeleteObject(rotated_obj)

        # Appliquer la rotation optimale
        rs.RotateObject(obj, rotate_point, optimal_angle)
        print("Forme optimisée à l'angle," optimal_angle,"° avec une largeur minimale de ,"current_min_width)
except:
    print ('erreur lors du script')
finally:
    rs.EnableRedraw(enable=True)
 it's not really fast, but it's work!