1package com.example.mazatrol.gcode;
2
3import java.util.List;
4
5public class DrillingGCodeGenerator {
6
7
8private static final double DEFAULT_FEED_RATE = 0.1;
9
10private static final int DEFAULT_SPINDLE_SPEED = 1500;
11
12private static final double DEFAULT_DEPTH_OF_CUT = 5.0;
13
14
15public static String generateDrillingGCode(List<Hole> holes, ToolInfo tool) {
16if (holes == null || holes.isEmpty()) {
17return "";
18}
19if (tool == null) {
20
21
22return "";
23}
24
25StringBuilder gcode = new StringBuilder();
26
27
28gcode.append(setupDrilling(tool));
29
30
31for (int i = 0; i < holes.size(); i++) {
32Hole hole = holes.get(i);
33gcode.append(generateHoleGCode(hole, tool, i));
34}
35
36
37gcode.append(finalizeDrilling());
38
39return gcode.toString();
40}
41
42
43private static String setupDrilling(ToolInfo tool) {
44StringBuilder setup = new StringBuilder();
45setup.append("G90 ; Absolute positioning\n");
46setup.append("G17 ; XY plane selection\n");
47setup.append("G40 ; Tool radius compensation off\n");
48setup.append("G49 ; Tool length compensation off\n");
49setup.append("G80 ; Canned cycle cancel\n");
50setup.append("T" + tool.getToolNumber() + " M6 ; Tool change\n");
51setup.append("G43 H" + tool.getToolNumber() + " ; Tool length compensation on\n");
52setup.append("S" + DEFAULT_SPINDLE_SPEED + " M3 ; Spindle on clockwise\n");
53setup.append("G54 ; Work coordinate system selection\n");
54setup.append("M8 ; Coolant on\n");
55setup.append("G0 Z5.0 ; Rapid move to safe Z height\n");
56return setup.toString();
57}
58
59
60private static String generateHoleGCode(Hole hole, ToolInfo tool, int holeIndex) {
61StringBuilder holeGcode = new StringBuilder();
62
63
64holeGcode.append("G0 X" + String.format("%.3f", hole.getX()) + " Y" + String.format("%.3f", hole.getY()) + " ; Move to hole " + (holeIndex + 1) + " position\n");
65
66
67
68
69holeGcode.append("G99 ; Retract to R plane\n");
70holeGcode.append("G81 X" + String.format("%.3f", hole.getX()) + " Y" + String.format("%.3f", hole.getY()) + " Z" + String.format("%.3f", hole.getDepth()) + " R" + String.format("%.3f", hole.getRPLANE()) + " F" + DEFAULT_FEED_RATE + " ; Drill cycle\n");
71holeGcode.append("G80 ; Cancel canned cycle\n");
72
73
74if (hole.getDepth() <= 0) {
75holeGcode.insert(0, "; Skipped drilling hole " + (holeIndex + 1) + " due to non-positive depth\n");
76}
77
78return holeGcode.toString();
79}
80
81
82private static String finalizeDrilling() {
83StringBuilder finalization = new StringBuilder();
84finalization.append("G0 Z5.0 ; Rapid move to safe Z height\n");
85finalization.append("M9 ; Coolant off\n");
86finalization.append("M5 ; Spindle off\n");
87finalization.append("G91 G28 Z0 ; Return to machine Z home\n");
88finalization.append("G91 G28 X0 Y0 ; Return to machine XY home\n");
89finalization.append("M30 ; Program end and reset\n");
90return finalization.toString();
91}
92
93
94public static class Hole {
95private double x;
96private double y;
97private double depth;
98private double rPlane;
99
100public Hole(double x, double y, double depth, double rPlane) {
101this.x = x;
102this.y = y;
103this.depth = depth;
104this.rPlane = rPlane;
105}
106
107public double getX() { return x; }
108public double getY() { return y; }
109public double getDepth() { return depth; }
110public double getRPLANE() { return rPlane; }
111}
112
113
114public static class ToolInfo {
115private int toolNumber;
116
117
118public ToolInfo(int toolNumber) {
119this.toolNumber = toolNumber;
120}
121
122public int getToolNumber() { return toolNumber; }
123}
124
125
126public static void main(String[] args) {
127ToolInfo drillTool = new ToolInfo(1);
128List<Hole> holesToDrill = List.of(
129new Hole(10.0, 20.0, 5.0, 2.0),
130new Hole(30.0, 40.0, 7.0, 2.0),
131new Hole(50.0, 60.0, -2.0, 2.0)
132);
133
134String gcodeOutput = generateDrillingGCode(holesToDrill, drillTool);
135System.out.println(gcodeOutput);
136}
137}