summaryrefslogtreecommitdiff
path: root/trunk/users/Rhys/Gcodeoffset/src/Main.java
blob: c5726fec0f0fd178fed1731b85ec34c1ea94cedc (plain)
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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


public class Main {

	
	public static void main(String[] args) 
	{
		BufferedReader in;
		String line, splitline[];
		int extruder=-1;
		float offsetX=0;
		float offsetY=0;
		
		float ex1_offsetX=100;
		float ex1_offsetY=100;
		
		BufferedWriter outfile;
	
	
		
		try {
			in = new BufferedReader(new FileReader("/home/ftx/Desktop/base.gcode"));
			outfile = new BufferedWriter(new FileWriter("/home/ftx/Desktop/base_fixed.gcode"));
			
			
			while((line = in.readLine()) != null)
			{
				//System.out.println(line);
				
				if(line.startsWith(";#!LAYER"))
				{
					offsetX=0;
					offsetY=0;
					outfile.write(line+"\n");
				}
				else
				if(line.startsWith("T"))
				{
					//System.out.println(line);
					extruder = Integer.parseInt(line.substring(1,2 ));
					//System.out.println("Found Extruder: " + extruder);
					if(extruder == 1)
					{
						offsetX=ex1_offsetX;
						offsetY=ex1_offsetY;
					}
					else
					{
						offsetX=0;
						offsetY=0;
					}
					outfile.write(line+"\n");
				}
				else
				if(line.startsWith("G1"))
				{
					splitline = line.split(" ");
					float xcords=0, ycords=0;
					
					for(int i=0; i<splitline.length; i++)
					{
						if(splitline[i].startsWith("X"))
						{
							xcords = Float.parseFloat(splitline[i].substring(1))+offsetX;
							splitline[i] = "X"+xcords;
						}
						if(splitline[i].startsWith("Y"))
						{
							ycords = Float.parseFloat(splitline[i].substring(1))+offsetY;
							splitline[i] = "Y"+ycords;
						}
	
					}
					
					line = "";
					
					for(int i=0; i<splitline.length; i++)
					{
						line += splitline[i]+" ";
					}
					
					outfile.write(line+"\n");
					
				}
				else
				{
					outfile.write(line+"\n");
					
				}
			}
			
			outfile.close();
		}
		catch(Exception e)
		{
			
		}
		
	}
}