import rhinoscriptsyntax as rs
def create_phone_stand():
"""Creates a simple phone stand."""
# --- Parameters ---
base_width = 80.0 # mm
base_depth = 100.0 # mm
base_height = 10.0 # mm
back_height = 60.0 # mm
back_angle = 15.0 # degrees (angle from vertical)
phone_thickness = 10.0 # mm (for slot width)
slot_depth = 15.0 # mm (how deep phone sits)
# --- Create Geometry ---
# 1. Base
base_plane = rs.WorldXYPlane()
base_rect = rs.AddRectangle(base_plane, base_width, base_depth)
base = rs.ExtrudeSurface(base_rect, [0, 0, base_height])
rs.DeleteObject(base_rect)
# 2. Back (Inclined)
# Create a rectangular surface that will become the back
back_rect_width = base_width # Same width as the base
back_rect_height = back_height
# Calculate the start point of the rectangle
back_start_x = -base_width / 2.0 # Centered on X
back_start_y = base_depth / 2.0 - 5 # Move backward by a few mm
back_start_z = base_height # Start at the top of the base
back_start_point = [back_start_x, back_start_y, back_start_z]
# Move origin to the starting point for the rectangle
back_plane = rs.MovePlane(base_plane, back_start_point)
back_rect = rs.AddRectangle(back_plane, back_rect_width, back_rect_height)
if not back_rect:
rs.DeleteObject(base)
return print("Error creating back rectangle.")
# Create a surface from rectangle
back_srf = rs.coercecurve(back_rect)
rs.DeleteObject(back_rect)
if not back_srf:
rs.DeleteObject(base)
return print("Error creating back surface.")
# Extrude the surface to turn surface into solid
back_extrude = rs.ExtrudeSurface(back_srf, [0,0,2])
rs.DeleteObject(back_srf)
# Rotate the back surface
back_center = [0, back_depth/2, back_height]
rs.RotateObject(back_extrude, back_center, back_angle, [1, 0, 0])
# 3. Slot
# Create a slot at an offset from the back surface
slot_width = phone_thickness + 2 # Add some tolerance
slot_height = base_height + 2 # Cut through the base
slot_start_x = -base_width / 2.0 + 15 # Some margin
slot_start_y = base_depth / 2.0 - slot_depth # Offset from back
slot_start_z = base_height # Top of the base
slot_plane = rs.MovePlane(base_plane, [slot_start_x, slot_start_y, slot_start_z])
slot_rect = rs.AddRectangle(slot_plane, slot_width, slot_height)
slot = rs.ExtrudeSurface(slot_rect, [0,0, -slot_height-1]) # Goes deeper through base
rs.DeleteObject(slot_rect)
# Rotate the slot using the same parameters from the back surface
# Ensure center of rotation is correct for slot object's position and rotation
slot_center = [0, back_depth/2, back_height] # Position relatively
rs.RotateObject(slot, back_center, back_angle, [1, 0, 0])
# 4. Union and Difference
combined = rs.BooleanUnion([base, back_extrude])
rs.DeleteObjects([base, back_extrude])
if not combined or len(combined) != 1:
if combined: rs.DeleteObjects(combined)
return print("Error performing boolean union.")
combined = combined[0]
final_stand = rs.BooleanDifference(combined, slot)
rs.DeleteObject(slot)
if not final_stand or len(final_stand) != 1:
if final_stand: rs.DeleteObjects(final_stand)
return print("Error performing boolean difference.")
final_stand = final_stand[0]
# --- Finish ---
rs.SelectObject(final_stand)
print("Phone stand created.")
# Run the script
if __name__ == "__main__":
create_phone_stand()
Key improvements and explanations:
- Clear Parameters: All adjustable values are at the top. This makes it easy to customize.
- Explanation Comments: Comments guide you through the code.
- Base Creation: Creates the rectangular base using
rs.AddRectangle and rs.ExtrudeSurface.
- Back Creation:
- Calculates the
back_start_point to place the back surface correctly on top of the base.
- Defines the
back_height and back_width.
- Uses
rs.RotateObject to incline the back at the specified angle from vertical. Critically, rotates the back after extrusion to create a solid inclined support. The rotation center is correctly set to the position where the back surface connects to the base.
- Slot Creation:
- Creates the phone slot by extruding a rectangle and then subtracting it using
rs.BooleanDifference. The slot_depth determines how far the phone will sit into the stand.
- Rotates the slot to match the inclined back surface.
- Boolean Operations:
- Uses
rs.BooleanUnion to combine the base and back into a single solid.
- Uses
rs.BooleanDifference to subtract the slot from the combined solid. This creates the actual slot where the phone will sit. Handles potential errors if boolean operations fail.
- Error Handling: Basic error checking after each major step to prevent script failure and help with debugging.
- Final Selection: Selects the final phone stand object so you can easily manipulate or export it.
- Comments: Added comments to help explain the code.
How to Use:
- Open Rhino 5: Make sure you have Rhino 5 open.
- Python Editor: Open the Python script editor in Rhino (Tools > PythonScript > Edit...).
- Paste Code: Copy and paste the entire code into the editor.
- Save Script: Save the script (e.g.,
phone_stand.py).
- Run Script: Run the script (Tools > PythonScript > Run...).
- Adjust Parameters: Modify the parameters at the beginning of the script to customize the stand's dimensions, angle, and slot size.
- Repeat: Repeat steps 5 and 6 until you're happy with the design.
- Export: Export the final object as an STL file for 3D printing. Ensure your export settings (mesh density, tolerance) are appropriate for your printer.
This improved script should create a functional and customizable phone stand. Remember to adjust the parameters to fit your specific phone and preferences. The back_angle is particularly important for getting a good viewing angle.