Skip to content

Commit fde5690

Browse files
committed
Added relative coordinates
1 parent 3f414bc commit fde5690

File tree

6 files changed

+190
-78
lines changed

6 files changed

+190
-78
lines changed

resources/input4G02.file

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ G00 X0 Y50
44
G01 X0 Y200
55
G02 X50 Y250 I50 J200
66
G01 X200 Y250
7-
G02 X250 Y200 I150 J250
7+
G02 X250 Y200 I200 J200
88
G01 X250 Y50
99
G02 X200 Y0 I200 J50
1010
G01 X50 Y0

resources/input5G03.file

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ G03 X200 Y250 I200 J200
88
G01 X50 Y250
99
G03 X0 Y200 I50 J200
1010
G01 X0 Y50
11-
G03 X50 Y0 I50 J100
11+
G03 X50 Y0 I50 J50
1212
G00 X100 Y100
1313
M05 M09 M30

resources/inputG91.file

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
G91 T0101 M06
2+
G94 G97 F400 S260 M04 M08
3+
G00 X0 Y50
4+
G01 X0 Y150
5+
G02 X50 Y50 I50 J0
6+
G01 X150 Y0
7+
G02 X50 Y-50 I0 J-50
8+
G01 X0 Y-150
9+
G02 X-50 Y-50 I-50 J0
10+
G01 X-150 Y0
11+
G02 X-50 Y50 I0 J50
12+
G00 X100 Y100
13+
M05 M09 M30

resources/inputG91Semplice.file

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
G91 T0101 M06
2+
G94 G97 F400 S260 M04 M08
3+
G00 X60 Y20
4+
G01 X30 Y80
5+
G01 X60 Y10
6+
G01 X60 Y-100
7+
G00 X100 Y100
8+
M05 M09 M30

src/GCODECompiler/GCODEHandler.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class GCODEHandler {
3030

3131
static List<String> errorList;
3232
TokenStream lexerStream;
33+
public static boolean mode = false;
3334

3435
public GCODEHandler (TokenStream ls) {
3536
System.out.println ("Handler Inizializzato");
@@ -213,8 +214,10 @@ private String recognizeExit(String config) {
213214
private String recognizeConfig(String c) {
214215
switch(c) {
215216
case "G90":
217+
mode = false;
216218
return "Selezionate coordinate assolute";
217219
case "G91":
220+
mode = true;
218221
return "Selezionate coordinate relative";
219222
case "M00":
220223
return "M00";

src/myPackage/Graphic.java

Lines changed: 164 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import javax.swing.JFrame;
99
import javax.swing.JPanel;
1010

11+
import GCODECompiler.GCODEHandler;
1112
import antlr.Token;
1213

1314

@@ -34,86 +35,173 @@ public void paintComponent(Graphics g) {
3435
//System.out.println(prop[i]);
3536
}
3637

37-
if(prop[0].equals("G00")) {
38-
firstx = Integer.parseInt(prop[1]);
39-
firsty = yorigin - Integer.parseInt(prop[2]);
40-
}
41-
42-
if(prop[0].equals("G01")) {
43-
g.drawLine(firstx, firsty, Integer.parseInt(prop[1]), yorigin - Integer.parseInt(prop[2]));
44-
firstx = Integer.parseInt(prop[1]);
45-
firsty = yorigin - Integer.parseInt(prop[2]);
46-
}else if(prop[0].equals("G02")) {
47-
//System.out.println("conta:" + conta++);
48-
float x0 = Integer.parseInt(prop[3]);
49-
float y0 = 500 - Integer.parseInt(prop[4]);
50-
float x1 = firstx;
51-
float y1 = firsty;
52-
float x2 = Integer.parseInt(prop[1]);
53-
float y2 = 500 - Integer.parseInt(prop[2]);
54-
float r = (float)Math.sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));
55-
float x = x0-r;
56-
float y = y0-r;
57-
float width = 2*r;
58-
float height = 2*r;
59-
float startAngle = (float) (180/Math.PI*Math.atan2(y1-y0, x1-x0));
60-
float endAngle = (float) (180/Math.PI*Math.atan2(y2-y0, x2-x0));
61-
62-
float endDist = (float)Math.sqrt((x2-x0)*(x2-x0) + (y2-y0)*(y2-y0));
63-
float startDist = (float)Math.sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));
64-
65-
if(endDist != startDist) {
66-
//System.out.println("Il comando -> " + com + " non può collegare il punto: " + x1 + ";" + (500 - y1) + " con il punto " + x2 + ";" + (500 - y2) +".");
67-
String assad ="Il comando -> " + com + " non può collegare il punto: " + x1 + ";" + (500 - y1) + " con il punto " + x2 + ";" + (500 - y2) +".";
68-
ParserLauncher.getParser().getHandler().myErrorHandler(21, assad);
38+
if(!GCODEHandler.mode) { //coordinate assolute
39+
if(prop[0].equals("G00")) {
40+
firstx = Integer.parseInt(prop[1]);
41+
firsty = yorigin - Integer.parseInt(prop[2]);
6942
}
7043

71-
72-
//System.out.println(x0 + " " + y0 + " " + x1 + " " + y1 + " " + x2 + " " + y2 + " " + r + " " + x + " " + y + " " + width + " " + height + " " + startAngle + " " + endAngle);
73-
if(startAngle - endAngle < 0)
74-
g.drawArc((int)x, (int)y, (int)width, (int)height, (int)(360.0 - startAngle), (int)(startAngle - endAngle));
75-
else
76-
g.drawArc((int)x, (int)y, (int)width, (int)height, (int)(360.0 - startAngle), (int)-(360 - (startAngle - endAngle)));
77-
78-
79-
//g.drawArc(Integer.parseInt(prop[1]), Integer.parseInt(prop[2]),Integer.parseInt(prop[1]) - firstx ,Integer.parseInt(prop[2]) - firsty, 270, 90);
80-
firstx = Integer.parseInt(prop[1]);
81-
firsty = yorigin - Integer.parseInt(prop[2]);
82-
} else if(prop[0].equals("G03")) {
83-
//System.out.println("ciao");
84-
float x0 = Integer.parseInt(prop[3]);
85-
float y0 = 500 - Integer.parseInt(prop[4]);
86-
float x1 = firstx;
87-
float y1 = firsty;
88-
float x2 = Integer.parseInt(prop[1]);
89-
float y2 = 500 - Integer.parseInt(prop[2]);
90-
float r = (float)Math.sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));
91-
float x = x0-r;
92-
float y = y0-r;
93-
float width = 2*r;
94-
float height = 2*r;
95-
float startAngle = (float) (180/Math.PI*Math.atan2(y1-y0, x1-x0));
96-
float endAngle = (float) (180/Math.PI*Math.atan2(y2-y0, x2-x0));
97-
98-
float endDist = (float)Math.sqrt((x2-x0)*(x2-x0) + (y2-y0)*(y2-y0));
99-
float startDist = (float)Math.sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));
100-
101-
if(endDist != startDist) {
102-
//System.out.println("Il comando -> " + com + " non può collegare il punto: " + x1 + ";" + (500 - y1) + " con il punto " + x2 + ";" + (500 - y2) +".");
103-
String assad ="Il comando -> " + com + " non può collegare il punto: " + x1 + ";" + (500 - y1) + " con il punto " + x2 + ";" + (500 - y2) +".";
104-
ParserLauncher.getParser().getHandler().myErrorHandler(21, assad);
44+
if(prop[0].equals("G01")) {
45+
g.drawLine(firstx, firsty, Integer.parseInt(prop[1]), yorigin - Integer.parseInt(prop[2]));
46+
firstx = Integer.parseInt(prop[1]);
47+
firsty = yorigin - Integer.parseInt(prop[2]);
48+
}else if(prop[0].equals("G02")) {
49+
//System.out.println("conta:" + conta++);
50+
float x0 = Integer.parseInt(prop[3]);
51+
float y0 = 500 - Integer.parseInt(prop[4]);
52+
float x1 = firstx;
53+
float y1 = firsty;
54+
float x2 = Integer.parseInt(prop[1]);
55+
float y2 = 500 - Integer.parseInt(prop[2]);
56+
float r = (float)Math.sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));
57+
float x = x0-r;
58+
float y = y0-r;
59+
float width = 2*r;
60+
float height = 2*r;
61+
float startAngle = (float) (180/Math.PI*Math.atan2(y1-y0, x1-x0));
62+
float endAngle = (float) (180/Math.PI*Math.atan2(y2-y0, x2-x0));
63+
64+
float endDist = (float)Math.sqrt((x2-x0)*(x2-x0) + (y2-y0)*(y2-y0));
65+
float startDist = (float)Math.sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));
66+
67+
if(endDist != startDist) {
68+
//System.out.println("Il comando -> " + com + " non può collegare il punto: " + x1 + ";" + (500 - y1) + " con il punto " + x2 + ";" + (500 - y2) +".");
69+
String assad ="Il comando -> " + com + " non può collegare il punto: " + x1 + ";" + (500 - y1) + " con il punto " + x2 + ";" + (500 - y2) +".";
70+
ParserLauncher.getParser().getHandler().myErrorHandler(21, assad);
71+
}
72+
73+
74+
//System.out.println(x0 + " " + y0 + " " + x1 + " " + y1 + " " + x2 + " " + y2 + " " + r + " " + x + " " + y + " " + width + " " + height + " " + startAngle + " " + endAngle);
75+
if(startAngle - endAngle < 0)
76+
g.drawArc((int)x, (int)y, (int)width, (int)height, (int)(360.0 - startAngle), (int)(startAngle - endAngle));
77+
else
78+
g.drawArc((int)x, (int)y, (int)width, (int)height, (int)(360.0 - startAngle), (int)-(360 - (startAngle - endAngle)));
79+
80+
81+
//g.drawArc(Integer.parseInt(prop[1]), Integer.parseInt(prop[2]),Integer.parseInt(prop[1]) - firstx ,Integer.parseInt(prop[2]) - firsty, 270, 90);
82+
firstx = Integer.parseInt(prop[1]);
83+
firsty = yorigin - Integer.parseInt(prop[2]);
84+
} else if(prop[0].equals("G03")) {
85+
//System.out.println("ciao");
86+
float x0 = Integer.parseInt(prop[3]);
87+
float y0 = 500 - Integer.parseInt(prop[4]);
88+
float x1 = firstx;
89+
float y1 = firsty;
90+
float x2 = Integer.parseInt(prop[1]);
91+
float y2 = 500 - Integer.parseInt(prop[2]);
92+
float r = (float)Math.sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));
93+
float x = x0-r;
94+
float y = y0-r;
95+
float width = 2*r;
96+
float height = 2*r;
97+
float startAngle = (float) (180/Math.PI*Math.atan2(y1-y0, x1-x0));
98+
float endAngle = (float) (180/Math.PI*Math.atan2(y2-y0, x2-x0));
99+
100+
float endDist = (float)Math.sqrt((x2-x0)*(x2-x0) + (y2-y0)*(y2-y0));
101+
float startDist = (float)Math.sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));
102+
103+
if(endDist != startDist) {
104+
//System.out.println("Il comando -> " + com + " non può collegare il punto: " + x1 + ";" + (500 - y1) + " con il punto " + x2 + ";" + (500 - y2) +".");
105+
String assad ="Il comando -> " + com + " non può collegare il punto: " + x1 + ";" + (500 - y1) + " con il punto " + x2 + ";" + (500 - y2) +".";
106+
ParserLauncher.getParser().getHandler().myErrorHandler(21, assad);
107+
}
108+
109+
//System.out.println(x0 + " " + y0 + " " + x1 + " " + y1 + " " + x2 + " " + y2 + " " + r + " " + x + " " + y + " " + width + " " + height + " " + startAngle + " " + endAngle);
110+
if(startAngle - endAngle > 0)
111+
g.drawArc((int)x, (int)y, (int)width, (int)height, (int)(360.0 - startAngle), (int)(startAngle - endAngle));
112+
else
113+
g.drawArc((int)x, (int)y, (int)width, (int)height, (int)(360.0 - startAngle), (int)-(Math.abs(startAngle) - endAngle));
114+
115+
116+
//g.drawArc(Integer.parseInt(prop[1]), Integer.parseInt(prop[2]),Integer.parseInt(prop[1]) - firstx ,Integer.parseInt(prop[2]) - firsty, 270, 90);
117+
firstx = Integer.parseInt(prop[1]);
118+
firsty = yorigin - Integer.parseInt(prop[2]);
119+
}
120+
}else { //coordinate relative
121+
if(prop[0].equals("G00")) {
122+
firstx = Integer.parseInt(prop[1]);
123+
firsty = yorigin - Integer.parseInt(prop[2]);
124+
//System.out.println("x: " + firstx + " y: " + firsty);
105125
}
106126

107-
//System.out.println(x0 + " " + y0 + " " + x1 + " " + y1 + " " + x2 + " " + y2 + " " + r + " " + x + " " + y + " " + width + " " + height + " " + startAngle + " " + endAngle);
108-
if(startAngle - endAngle > 0)
109-
g.drawArc((int)x, (int)y, (int)width, (int)height, (int)(360.0 - startAngle), (int)(startAngle - endAngle));
110-
else
111-
g.drawArc((int)x, (int)y, (int)width, (int)height, (int)(360.0 - startAngle), (int)-(Math.abs(startAngle) - endAngle));
112-
113-
114-
//g.drawArc(Integer.parseInt(prop[1]), Integer.parseInt(prop[2]),Integer.parseInt(prop[1]) - firstx ,Integer.parseInt(prop[2]) - firsty, 270, 90);
115-
firstx = Integer.parseInt(prop[1]);
116-
firsty = yorigin - Integer.parseInt(prop[2]);
127+
if(prop[0].equals("G01")) {
128+
g.drawLine(firstx, firsty, firstx + Integer.parseInt(prop[1]), firsty - Integer.parseInt(prop[2]));
129+
firstx = firstx + Integer.parseInt(prop[1]);
130+
firsty = firsty - (Integer.parseInt(prop[2]));
131+
//System.out.println("x: " + firstx + " y: " + firsty);
132+
}else if(prop[0].equals("G02")) {
133+
//System.out.println("conta:" + conta++);
134+
float x0 = firstx + Integer.parseInt(prop[3]);
135+
float y0 = firsty - (Integer.parseInt(prop[4]));
136+
float x1 = firstx;
137+
float y1 = firsty;
138+
float x2 = firstx + Integer.parseInt(prop[1]);
139+
float y2 = firsty - (Integer.parseInt(prop[2]));
140+
float r = (float)Math.sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));
141+
float x = x0-r;
142+
float y = y0-r;
143+
float width = 2*r;
144+
float height = 2*r;
145+
float startAngle = (float) (180/Math.PI*Math.atan2(y1-y0, x1-x0));
146+
float endAngle = (float) (180/Math.PI*Math.atan2(y2-y0, x2-x0));
147+
148+
float endDist = (float)Math.sqrt((x2-x0)*(x2-x0) + (y2-y0)*(y2-y0));
149+
float startDist = (float)Math.sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));
150+
151+
if(endDist != startDist) {
152+
//System.out.println("Il comando -> " + com + " non può collegare il punto: " + x1 + ";" + (500 - y1) + " con il punto " + x2 + ";" + (500 - y2) +".");
153+
String assad ="Il comando -> " + com + " non può collegare il punto: " + x1 + ";" + (500 - y1) + " con il punto " + x2 + ";" + (500 - y2) +".";
154+
ParserLauncher.getParser().getHandler().myErrorHandler(21, assad);
155+
}
156+
157+
158+
//System.out.println(x0 + " " + y0 + " " + x1 + " " + y1 + " " + x2 + " " + y2 + " " + r + " " + x + " " + y + " " + width + " " + height + " " + startAngle + " " + endAngle);
159+
if(startAngle - endAngle < 0)
160+
g.drawArc((int)x, (int)y, (int)width, (int)height, (int)(360.0 - startAngle), (int)(startAngle - endAngle));
161+
else
162+
g.drawArc((int)x, (int)y, (int)width, (int)height, (int)(360.0 - startAngle), (int)-(360 - (startAngle - endAngle)));
163+
164+
165+
//g.drawArc(Integer.parseInt(prop[1]), Integer.parseInt(prop[2]),Integer.parseInt(prop[1]) - firstx ,Integer.parseInt(prop[2]) - firsty, 270, 90);
166+
firstx = firstx + Integer.parseInt(prop[1]);
167+
firsty = firsty - (Integer.parseInt(prop[2]));
168+
//System.out.println("x: " + firstx + " y: " + firsty);
169+
} else if(prop[0].equals("G03")) {
170+
//System.out.println("ciao");
171+
float x0 = firstx + Integer.parseInt(prop[3]);
172+
float y0 = firsty - Integer.parseInt(prop[4]);
173+
float x1 = firstx;
174+
float y1 = firsty;
175+
float x2 = firstx + Integer.parseInt(prop[1]);
176+
float y2 = firsty - Integer.parseInt(prop[2]);
177+
float r = (float)Math.sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));
178+
float x = x0-r;
179+
float y = y0-r;
180+
float width = 2*r;
181+
float height = 2*r;
182+
float startAngle = (float) (180/Math.PI*Math.atan2(y1-y0, x1-x0));
183+
float endAngle = (float) (180/Math.PI*Math.atan2(y2-y0, x2-x0));
184+
185+
float endDist = (float)Math.sqrt((x2-x0)*(x2-x0) + (y2-y0)*(y2-y0));
186+
float startDist = (float)Math.sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));
187+
188+
if(endDist != startDist) {
189+
//System.out.println("Il comando -> " + com + " non può collegare il punto: " + x1 + ";" + (500 - y1) + " con il punto " + x2 + ";" + (500 - y2) +".");
190+
String assad ="Il comando -> " + com + " non può collegare il punto: " + x1 + ";" + (500 - y1) + " con il punto " + x2 + ";" + (500 - y2) +".";
191+
ParserLauncher.getParser().getHandler().myErrorHandler(21, assad);
192+
}
193+
194+
//System.out.println(x0 + " " + y0 + " " + x1 + " " + y1 + " " + x2 + " " + y2 + " " + r + " " + x + " " + y + " " + width + " " + height + " " + startAngle + " " + endAngle);
195+
if(startAngle - endAngle > 0)
196+
g.drawArc((int)x, (int)y, (int)width, (int)height, (int)(360.0 - startAngle), (int)(startAngle - endAngle));
197+
else
198+
g.drawArc((int)x, (int)y, (int)width, (int)height, (int)(360.0 - startAngle), (int)-(Math.abs(startAngle) - endAngle));
199+
200+
201+
//g.drawArc(Integer.parseInt(prop[1]), Integer.parseInt(prop[2]),Integer.parseInt(prop[1]) - firstx ,Integer.parseInt(prop[2]) - firsty, 270, 90);
202+
firstx = firstx + Integer.parseInt(prop[1]);
203+
firsty = firsty - Integer.parseInt(prop[2]);
204+
}
117205
}
118206
}
119207
}

0 commit comments

Comments
 (0)