from vpython import *
scene.width = 800
scene.height = 600
scene.background = color.white
end_flag = True
set_flag = False
program_running = True # Initially set to True to run the program
# Workpiece parameters
workpiece_length = 10
workpiece_width = 6
workpiece_height = 1.5
target_depth = 0.8 # Target milling depth
workpiece = box(pos=vector(0, workpiece_height/2, 0), size=vector(workpiece_length, workpiece_height, workpiece_width), color=color.gray(0.6), opacity=.5)
# Cutter parameters
cutter_diameter = 2
cutter_height = 2
cutter = cylinder(pos=vector(-workpiece_length/2, workpiece_height + cutter_height, -workpiece_width/2), axis=vector(0, -1, 0), radius=cutter_diameter/2, color=color.blue, length=cutter_height)
# Milling path parameters
milling_speed = 0.1
step_move = cutter_diameter / 2 # Move step along the Z-axis by half the cutter diameter
depth_step = 0.4 # Depth of cut per pass
clearance_point = workpiece_height + 0.1
# Creating milling path trail
milling_trail = curve(color=color.red, radius=0.05)
# Milling process simulation
current_depth = workpiece_height - depth_step
cutter.pos.y = current_depth + cutter_height
while end_flag:
move_x = True # Flag for moving the cutter left and right
cutter.pos.x = -workpiece_length / 2 # Move back to the left side
cutter.pos.z = -workpiece_width / 2 # Move back to the start of the Z-axis
milling_trail.append(pos=vector(cutter.pos.x, clearance_point, cutter.pos.z))
# Single pass from side to side
while cutter.pos.z <= workpiece_width / 2:
rate(100)
# Move cutter along the X-axis
if move_x:
cutter.pos.x += milling_speed
if cutter.pos.x >= workpiece_length / 2: # End at the right side
cutter.pos.x -= milling_speed
move_x = False
if cutter.pos.z + step_move > workpiece_width / 2:
cutter.pos.z += 0.05
else:
cutter.pos.z += step_move
else:
cutter.pos.x -= milling_speed
if cutter.pos.x <= -workpiece_length / 2: # End at the left side
cutter.pos.x += milling_speed
move_x = True
if cutter.pos.z + step_move > workpiece_width / 2:
cutter.pos.z += 0.05
else:
cutter.pos.z += step_move
# Add point to milling path at the cutter's center
milling_trail.append(pos=vector(cutter.pos.x, cutter.pos.y - cutter_height, cutter.pos.z))
# Increase depth after a full pass
milling_trail.append(pos=vector(cutter.pos.x, clearance_point, cutter.pos.z))
if set_flag:
end_flag = False
if current_depth - depth_step < target_depth:
cutter.pos.y = target_depth + cutter_height
current_depth = target_depth
set_flag = True
else:
cutter.pos.y -= depth_step
current_depth -= depth_step
print("current", current_depth, ", target", target_depth)
# Function to close the program
def close_program():
global program_running
program_running = False
print("Program closed.")
# Adding a button to close the program
button(text="Close", pos=scene.title_anchor, bind=close_program)
# Keep the program running until the button is clicked
while program_running:
rate(10)