Removal of Material (Milling process simulation)

47 views
Skip to first unread message

Szymon Gajos

unread,
Nov 6, 2024, 10:48:45 AM11/6/24
to VPython-users
I am having some trouble with making a simulation of milling process. I'm currently trying to show a process of face milling using vpython and as for now I can only show a simple generated path of a cutter. 
Ive been wondering weather is there a bool operation implemented to show the "removal" of the piece. Thank you for all answers. 
Heres the code with translated variable names:

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)

Bruce Sherwood

unread,
Nov 6, 2024, 1:44:26 PM11/6/24
to VPython-users
Not sure what the issue is. Is it that you want to erase the red curve or portions of the red curve? Here are the details on how to modify a curve:


Bruce

Szymon Gajos

unread,
Nov 6, 2024, 3:42:02 PM11/6/24
to VPython-users
Sorry, I wanted to know weather theres an function that can detect the "contact" of generated figures and "substract" one from the another. I know of the extrusion, but I don't think it would be able to show material loss in the piece generated properly.

Bruce Sherwood

unread,
Nov 6, 2024, 4:38:20 PM11/6/24
to VPython-users
No, VPython does not offer the ability to subtract part of an object (except for deleting or making invisible a triangle or quad, or a piece of a curve). Detection of an overlap is of course possible given the knowledge of the attributes of the two objects.

Bruce

Reply all
Reply to author
Forward
0 new messages