Skip to content

Instantly share code, notes, and snippets.

@joshnuss
Last active November 18, 2023 06:02
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 joshnuss/803291a468293d1b8e57328b36f133ec to your computer and use it in GitHub Desktop.
Save joshnuss/803291a468293d1b8e57328b36f133ec to your computer and use it in GitHub Desktop.
Excellon drill format
// `x`, `y` are the coordinates
// `tool` is the drill bit diameter
[
{ x: 0, y: 0, tool: 0.2 },
{ x: 0, y: 0.2 , tool: 0.4 },
{ x: 0, y: 0.3 , tool: 0.2 }
]
import fs from 'fs'
import json from 'json5'
function mapTools(holes) {
const tools = new Set()
const indexes = new Map()
let count = 0
holes.forEach(({ tool }, index) => {
if (!tools.has(tool)) {
tools.add(tool)
indexes.set(tool, ++count)
}
})
return { tools, indexes }
}
class ExcellonWriter {
constructor(holes, options = { unit: 'inch' }) {
this.holes = holes
this.options = options
const { tools, indexes } = mapTools(holes)
this.tools = tools
this.indexes = indexes
}
write(io) {
// start header
io.write('M48\n')
// use format 2 commands
io.write('FMAT,2\n')
// specify measurement unit (inch or metric)
io.write(`${this.options.unit.toUpperCase()}\n`)
// specify tools
this.indexes.forEach((tool, index) => {
io.write(`T${index}C${tool}\n`)
})
// end header
io.write('%\n')
// absolute mode
io.write('G90\n')
// drill mode
io.write('G05\n')
// specify holes
this.holes.forEach((hole) => {
const index = this.indexes.get(hole.tool)
// choose tool
io.write(`T${index}\n`)
// specify drill position
io.write(`X${hole.x}Y${hole.y}\n`)
})
// select no tool
io.write('T0\n')
// end file
io.write('M30\n')
}
}
// load drill holes
const data = await fs.promises.readFile('./holes.json5')
// parse json
const holes = json.parse(data)
// create writer
const writer = new ExcellonWriter(holes, { unit: 'inch' })
// output .drl format
await writer.write(process.stdout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment