-
-
Notifications
You must be signed in to change notification settings - Fork 756
/
DowelGenerator.kt
111 lines (88 loc) · 3.51 KB
/
DowelGenerator.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
Copyprintln 2017 Will Winder
This file is part of Universal Gcode Sender (UGS).
UGS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
UGS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with UGS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.willwinder.ugs.platform.dowel
import com.google.common.collect.Iterables
import com.willwinder.universalgcodesender.gcode.util.GcodeUtils
import com.willwinder.universalgcodesender.model.UnitUtils
import com.willwinder.universalgcodesender.model.Position
import java.io.PrintWriter
import java.util.Date
/*
@author wwinder
Created on Oct 29, 2017
*/
public class DowelGenerator(var settings: DowelSettings) {
public fun unitMultiplier() = UnitUtils.scaleUnits(settings.units, UnitUtils.Units.MM)
public fun getLocations(target: UnitUtils.Units): List<Position> {
val mult= UnitUtils.scaleUnits(settings.units, target)
val offset = mult * (settings.dowelDiameter + settings.bitDiameter * 1.25)
val corner = Position(
mult * settings.dowelDiameter / 2.0,
mult * settings.dowelDiameter / 2.0,
0.0)
val ret: MutableList<Position> = mutableListOf()
for (x in 0 until settings.numDowelsX) {
for (y in 0 until settings.numDowelsY) {
ret.add(Position(corner.x + x * offset, corner.y + y * offset, 0.0))
}
}
return ret
}
public fun generate(output: PrintWriter) {
// Set units and absolute movement/IJK mode.
output.println("(Generated by Universal Gcode Sender ${Date()})")
output.println("${GcodeUtils.unitCommand(settings.units)} G90 G91.1")
output.println("G17 F${settings.feed}")
output.println("M3")
for (point in getLocations(settings.units)) {
generateOne(point, output)
}
output.println("\n(All done!)")
output.println("M5")
output.println("M30")
}
public fun generateOne(at: Position, output: PrintWriter) {
output.println("\n(Dowel at x:${at.x} y:${at.y})")
val radius = settings.bitDiameter / 2.0 + settings.dowelDiameter / 2.0
val quarterDepth = settings.cutDepth / 4.0
val arcSequence: Iterator<Position> = Iterables.cycle(listOf(
Position(at.x - radius, at.y, 0.0),
Position(at.x, at.y + radius, 0.0),
Position(at.x + radius, at.y, 0.0),
Position(at.x, at.y - radius, 0.0)
)).iterator()
var last = arcSequence.next();
// Start
output.println("G0 X${last.x} Y${last.y}")
output.println("G1 Z0")
output.println("G17 F${settings.feed}")
// Create the helix
var currentDepth = 0.0
while (currentDepth > -settings.dowelLength) {
val n = arcSequence.next()
output.println("G02 X${n.x} Y${n.y} Z${currentDepth} I${at.x - last.x} J${at.y - last.y}")
last = n
currentDepth -= quarterDepth
}
// final loop at final depth
for (i in 0..4) {
val n = arcSequence.next()
output.println("G02 X${n.x} Y${n.y} Z${-settings.dowelLength} I${at.x - last.x} J${at.y - last.y}")
last = n
}
// Lift tool out of pocket.
output.println("G0 Z${settings.safetyHeight}")
}
}