Skip to content

Instantly share code, notes, and snippets.

@xunker
Last active July 2, 2018 17:32
Show Gist options
  • Save xunker/0517e4c1e624619a70567b47a846e467 to your computer and use it in GitHub Desktop.
Save xunker/0517e4c1e624619a70567b47a846e467 to your computer and use it in GitHub Desktop.
Split pcb2gcode drill files by bit size
#!/usr/bin/env ruby
#
# usage: split_drillfile.rb [<drill.ngc>]
#
# Reads a pcb2gcode drill file and creates individual files for each
# drill bit size. It will duplicate the preamble/postable between files.
# This is for programs like Universal Gcode Sender / UGS that do not let you
# change bits while streaming is paused because they do not allow you to move
# the head or reset the Z-axis while file streaming is paused.
#
# This is designed to work against files generated by pcb2gcode v1.3.2
# and will probably break with any different version. It looks for specific
# code comments, so if those change then this will change as well.
#
# github.com/xunker - Matthew Nielsen (C) 2018
require 'getoptlong'
opts = GetoptLong.new(
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
[ '--no-m6', '-6', GetoptLong::NO_ARGUMENT ],
[ '--no-m0', '-0', GetoptLong::NO_ARGUMENT ],
[ '--size', '-s', GetoptLong::NO_ARGUMENT ]
)
no_m6 = false
no_m0 = false
include_size = false
opts.each do |opt, arg|
case opt
when '--help'
puts <<-EOF
split_drillfile.rb [OPTION] ... [FILENAME]
-h, --help:
show help
--no-m6 , -6:
remove "M6" (Tool Change) commands from output files
--no-m0 , -0:
remove "M0" (Pause) commands from output files
--size, -s:
include drill bit size in the output file names
FILENAME: The file to load, by default uses "drill.ngc"
EOF
when '--no-m6'
no_m6 = true
when '--no-m0'
no_m0 = true
when '--size'
include_size = true
end
end
filename = ARGV.shift
filename ||= "./drill.ngc"
# match line like "G00 Z2.00000 (Retract)"
drill_block_marker = /\(Retract\)\Z/
# match line like "G00 Z2.000 ( All done -- retract )"
postable_marker = /\( All done \-\- retract \)\Z/
# match line like "M6 (Tool change.)"
m6_marker = /\A\s*M6/
# match line like "M0 (Temporary machine stop.)"
m0_marker = /\A\s*M0/
# pull size out of line like "(MSG, Change tool bit to drill size 0.889 mm)"
drill_bit_size_marker = /\(MSG, Change tool bit to drill size (.+)\)/
preamble = ""
postamble = ""
drill_blocks = []
drill_block_index = -1
preamble_complete = false
drill_blocks_complete = false
# read the various blocks from the file
File.open(filename) do |file|
until (file.eof?) do
line = file.readline
if line.match(drill_block_marker)
preamble_complete = true
drill_blocks_complete = false
drill_block_index += 1
drill_blocks[drill_block_index] ||= ""
end
if line.match(postable_marker)
preamble_complete = true
drill_blocks_complete = true
end
if preamble_complete == false
preamble += line
next
end
if drill_blocks_complete == false
next if no_m6 && line.match(m6_marker)
next if no_m0 && line.match(m0_marker)
drill_blocks[drill_block_index] += line
end
if drill_blocks_complete == true
postamble += line
next
end
end
end
puts "Writing #{drill_blocks.length} files."
# write each block to it's own file.
drill_blocks.each_with_index do |drill_block, idx|
drill_size = ""
if include_size
if m = drill_block.match(drill_bit_size_marker)
drill_size = m[1]
end
if drill_size.length > 0
drill_size = '.size_' + drill_size.tr(' ', '')
end
end
fn = "#{filename.sub(/\.ngc\Z/, ".#{idx}#{drill_size}.ngc")}"
puts "\t#{fn}"
File.open(fn, "w") do |file|
file.puts preamble
file.puts drill_block
file.puts postamble
end
end
puts "done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment