Skip to content

Instantly share code, notes, and snippets.

@anjiro
Created July 16, 2019 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anjiro/30c9c0f8a4f372384d6b9dead0e318ba to your computer and use it in GitHub Desktop.
Save anjiro/30c9c0f8a4f372384d6b9dead0e318ba to your computer and use it in GitHub Desktop.
A script to generate gCode for drilling rectangular arrays of holes on the xCarve CNC mill. Originally developed for drilling in acrylic, going breadth-first to allow cooling in between drill steps.
"""Code to output gCode to drill holes. Drills breadth first to allow
cooling time for e.g. acrylic."""
import numpy as np
from mecode import G
def gen_holes(nx:int, ny:int, spacing:float, x0:float, y0:float,
depth:float, outfile, *, depth_step=1.0, feedrate=150.0,
z_clearance=4.0, start_depth=0.0, view=False):
"""Generate gCode to drill a matrix of holes, going breadth-first
for cooldown time.
**Note: assumes that the tip of the drill is touching the top of
the material at its bottom-left corner (e.g., it's already homed).**
:param nx: Number of holes in the x direction
:param ny: Number of holes in the y direction
:param spacing: Space in mm between hole centers
:param x0: x coordinate in mm for bottom left hole center
:param y0: y coordinate in mm for bottom left hole center
:param depth: Depth of holes in mm
:param outfile: File to write gcode to
:param depth_step: How many mm to drill at a time before moving to next hole
:param feedrate: gCode feedrate
:param z_clearance: How many mm to move up before doing an x/y move
:param start_depth: What depth to start drilling at
:param view: Preview the movements
"""
with G(outfile=outfile, print_lines=False, setup=False, aerotech_include=False) as g:
g.absolute()
g.setup()
g.write('G21 ;millimeters')
g.write('M3 S12000 ;start spindle')
g.feed(feedrate)
g.abs_move(z=z_clearance, rapid=True) #Fast move up to clearance
g.abs_move(x0, y0, rapid=True)
#Calculate plunge steps
plunges = np.arange(start_depth+depth_step, depth+depth_step, depth_step)
if plunges[-1] < depth:
plunges = np.append(plunges, [depth])
for pn, ss in enumerate(plunges):
for x in range(nx):
for y in range(ny):
g.abs_move(x=x0+x*spacing, y=y0+y*spacing, rapid=True)
close = -(ss-depth_step*2)
if close < z_clearance:
g.abs_move(z=-close, rapid=True) #Fast to near start of cut
g.abs_move(z=-ss) #Slow move down to plunge depth
if close < z_clearance:
g.abs_move(z=-(ss-depth_step*2)) #Slow move back
g.abs_move(z=z_clearance, rapid=True) #Fast move up to clearance
g.abs_move(z=z_clearance, rapid=True)
g.write('M5 ;stop spindle')
g.abs_move(x=0, y=0, rapid=True) #Go home
g.abs_move(z=0, rapid=True)
if view:
g.view('matplotlib')
if __name__ == "__main__":
import clize
clize.run(gen_holes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment