-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
318 lines (268 loc) · 14.1 KB
/
Form1.cs
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RepRapGCodeTestGenerator
{
public partial class Form1 : Form
{
// Class variables
// Init to most common defaults before calling TryParse on user entered data, then just use these values if TryParse fails.
double gdblXMin = 0;
double gdblYMin = 0;
double gdblXMax = 200;
double gdblYMax = 200;
double gdblCircleSegmentCount = 5;
// F command is feedrate in millimeters per minute, so multiply millimeters/second by 60.
int gintTravelSpeedmms = 200;
// carriage return + line feed
string crlf = Environment.NewLine;
public Form1()
{
InitializeComponent();
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
string filePath = desktopPath + "\\BreakInGCode.gcode";
txtFileName.Text = filePath;
}
private void btnCreateFile_Click(object sender, EventArgs e)
{
string strOutputFile = txtFileName.Text;
double dblRepeatMax = 1; // Only do it once if the textbox has an invalid value.
bool ignore = false; // Ignore the parse results for now; just use pre-initialized values if the user enters something invalid.
// Get the user-defined numerical values. Silently use defaults on user entry error.
ignore = double.TryParse(txtXMin.Text, out gdblXMin);
ignore = double.TryParse(txtXMax.Text, out gdblXMax);
ignore = double.TryParse(txtYMin.Text, out gdblYMin);
ignore = double.TryParse(txtYMax.Text, out gdblYMax);
ignore = double.TryParse(txtRepeats.Text, out dblRepeatMax);
ignore = double.TryParse(txtCircleSegmentCount.Text, out gdblCircleSegmentCount);
// Get the user-defined speed.
ignore = int.TryParse(txtSpeed.Text, out gintTravelSpeedmms); // Ignore result and use the global print speed setting if the parse fails.
// Create header
string strGCode =
"G28\t\t; home all axes" + crlf +
"G90\t\t; use absolute positioning" + crlf +
"G21\t\t; use metric values" + crlf +
GotoPositionXY(gdblXMin, gdblYMin, "move X/Y to min endstops") +
//"G28 Z0\t\t; move Z to min endstops" + crlf +
"G1 Z8.0 F600\t; move the platform down 8mm at 10mm/s" + crlf;
// Loop through the all g-code generation features until done.
for (int idx = 1; idx <= dblRepeatMax; idx++)
{
strGCode += crlf;
strGCode += ";========== Begin Loop # " + idx.ToString() + " ==========" + crlf;
strGCode += GenerateGCode();
strGCode += ";========== End Loop # " + idx.ToString() + " ==========" + crlf;
}
// When it is done, if it is not at the X/Y home, then steps were probably lost!
strGCode += GotoPositionXY(gdblXMin, gdblYMin, "move X/Y to min endstops");
// Write out the g-code file!
File.WriteAllText(strOutputFile, strGCode);
// debug
//MessageBox.Show(strGCode);
MessageBox.Show("Generated gcode successfully sent to file:" + crlf + strOutputFile, "Done!");
}
// Generate the G-Code!
string GenerateGCode()
{
string strOutputText = "";
//MessageBox.Show(GenerateCircle(200,100,100,30));
if (chkBigSquare.Checked == true) strOutputText += GenerateLargeSquare();
if (chkSmallSquare.Checked == true) strOutputText += GenerateSmallSquare();
if (chkDiagonals.Checked == true) strOutputText += GenerateDiagonals();
if (chkLargeCircle.Checked == true) strOutputText += GenerateLargeCircle();
//if (chkSmallCircle.Checked == true) strOutputText += GenerateSmallCircle();
return strOutputText;
}
// ********************* Large and Small Squares *********************
/// <summary>
/// Generate a square path
/// </summary>
/// <param name="travelSpeedmms">Travel speed in millimeters per second</param>
/// <param name="xMin"></param>
/// <param name="xMax"></param>
/// <param name="yMin"></param>
/// <param name="yMax"></param>
/// <param name="xOffset"></param>
/// <param name="yOffset"></param>
/// <param name="comment"></param>
/// <returns>The g-code for the square</returns>
string GenerateSquare(int travelSpeedmms, double xMin, double xMax, double yMin, double yMax, double xOffset, double yOffset, string comment)
{
double dblXMinSq = xMin + xOffset;
double dblXMaxSq = xMax - xOffset;
double dblYMinSq = yMin + yOffset;
double dblYMaxSq = yMax - yOffset;
string strOutputText = "";
strOutputText += AddComment(comment);
strOutputText += "G1 F" + (travelSpeedmms*60).ToString() + "\t; set XY travel speed to " + travelSpeedmms.ToString() + "mm/s" + crlf;
strOutputText += GotoPositionXY(dblXMinSq, dblYMinSq, "go to the origin of the square");
strOutputText += GotoPositionX(dblXMaxSq);
strOutputText += GotoPositionY(dblYMaxSq);
strOutputText += GotoPositionX(dblXMinSq);
strOutputText += GotoPositionY(dblYMinSq);
strOutputText += AddComment("******* End Box ******");
return strOutputText + crlf;
}
string GenerateLargeSquare()
{
// Define the size of square using offsets from min/max values.
double dblXOffset = 0; // Convert.ToInt16(gintXMax / 10);
double dblYOffset = 0; // Convert.ToInt16(gintYMax / 10);
double dblXMinSq = gdblXMin;
double dblXMaxSq = gdblXMax;
double dblYMinSq = gdblYMin;
double dblYMaxSq = gdblYMax;
string strComment = "******* Begin Large Box at " + gintTravelSpeedmms.ToString() + "mm/s ******";
// Get the g-code
string strOutputText = GenerateSquare(gintTravelSpeedmms, dblXMinSq, dblXMaxSq, dblYMinSq, dblYMaxSq, dblXOffset, dblYOffset, strComment);
// return it
return strOutputText;
}
string GenerateSmallSquare()
{
// Define the size of square as 1/4 full build plate size using offsets from min/max values.
double dblXOffset = Convert.ToInt16(gdblXMax / 4);
double dblYOffset = Convert.ToInt16(gdblYMax / 4);
double dblXMinSq = gdblXMin;
double dblXMaxSq = gdblXMax;
double dblYMinSq = gdblYMin;
double dblYMaxSq = gdblYMax;
string strComment = "******* Begin Small Box at " + gintTravelSpeedmms.ToString() + "mm/s ******";
// Get the g-code
string strOutputText = GenerateSquare(gintTravelSpeedmms, dblXMinSq, dblXMaxSq, dblYMinSq, dblYMaxSq, dblXOffset, dblYOffset, strComment);
// return it
return strOutputText;
}
// ********************************************************************
// similar to a square...
string GenerateDiagonals()
{
string strComment = "******* Begin diagonal lines at " + gintTravelSpeedmms.ToString() + "mm/s ******";
//string strOutputText = GenerateDiagonalLines(gintTravelSpeedmms, gintXMin, gintXMax, gintYMin, gintYMax, gintXMax / 10, gintYMax / 10, strComment);
string strOutputText = GenerateDiagonalLines(gintTravelSpeedmms, gdblXMin, gdblXMax, gdblYMin, gdblYMax, 0, 0, strComment);
return strOutputText + crlf;
}
/// <summary>
/// Generate g-code for diagonal lines, and travel in both directions.
/// </summary>
/// <param name="travelSpeedmms"></param>
/// <param name="xMin"></param>
/// <param name="xMax"></param>
/// <param name="yMin"></param>
/// <param name="yMax"></param>
/// <param name="xOffset"></param>
/// <param name="yOffset"></param>
/// <param name="comment"></param>
/// <returns>The g-code</returns>
string GenerateDiagonalLines(int travelSpeedmms, double xMin, double xMax, double yMin, double yMax, double xOffset, double yOffset, string comment)
{
double dblXMinSq = xMin + xOffset;
double dblXMaxSq = xMax - xOffset;
double dblYMinSq = yMin + yOffset;
double dblYMaxSq = yMax - yOffset;
string strOutputText = "";
strOutputText += AddComment(comment);
strOutputText += "G1 F" + (travelSpeedmms * 60).ToString() + "\t; set XY travel speed to " + travelSpeedmms.ToString() + "mm/s" + crlf;
// 0. Start at origin (plus any offset)
strOutputText += GotoPositionXY(dblXMinSq, dblYMinSq, "go to the origin of the square");
// 1. upper right: Origin to max x & y
strOutputText += GotoPositionXY(dblXMaxSq, dblYMaxSq);
// 2. lower right: max x, min y
strOutputText += GotoPositionXY(dblXMaxSq, dblYMinSq);
// 3. upper left: min x, max y
strOutputText += GotoPositionXY(dblXMinSq, dblYMaxSq);
// 4. upper right: go to max x & y
strOutputText += GotoPositionXY(dblXMaxSq, dblYMaxSq);
// 5. lower left: go to min x & y
strOutputText += GotoPositionXY(dblXMinSq, dblYMinSq);
// 6. upper left: go to max x & y
strOutputText += GotoPositionXY(dblXMaxSq, dblYMaxSq);
// 7. lower right: max x, min y
strOutputText += GotoPositionXY(dblXMaxSq, dblYMinSq);
strOutputText += AddComment("******* End Diagonal Lines ******");
return strOutputText + crlf;
}
// *********************** Circles and Ellipses ***********************
//string GenerateEllipse(int travelSpeedmms, int xCenter, int yCenter, int xRadius, int yRadius, int xOffset=0, int yOffset=0, string comment="")
// start simpler (get it done)
string GenerateCircle(int travelSpeedmms, double xCenter, double yCenter, double radius, double granularity = 10, double xOffset = 0, double yOffset = 0, string comment = "")
{
double xOrigin = xCenter + xOffset;
double yOrigin = yCenter + yOffset;
double xMin = xOrigin - radius;
double xMax = xOrigin + radius;
double yMin = yOrigin - radius;
double yMax = yOrigin + radius;
if (xMin < 0 || yMin < 0 || xMax > gdblXMax || yMax > gdblYMax)
{
throw new System.Exception("Circle cannot be outside of the bed max limits!");
}
string strOutputText = "";
strOutputText += AddComment(comment);
strOutputText += "G1 F" + (travelSpeedmms * 60).ToString() + "\t; set XY travel speed to " + travelSpeedmms.ToString() + "mm/s" + crlf;
// Get the points on the circle.
double curX = xOrigin;
double curY = yOrigin;
for (double curAngle = 0; curAngle < 360; curAngle += granularity)
{
GetPointsOnCircle(radius, xCenter, yCenter, curAngle, out curX, out curY);
strOutputText += GotoPositionXY(curX, curY);
}
// return it
return strOutputText + crlf;
}
void GetPointsOnCircle(double radius, double xOrigin, double yOrigin, double AngleInDegrees, out double xPoint, out double yPoint)
{
double curRadians = Math.PI * AngleInDegrees / 180;
xPoint = (radius * Math.Cos(curRadians) + xOrigin);
yPoint = (radius * Math.Sin(curRadians) + yOrigin);
// TODO: Truncate the number of significant digits; these numbers are much longer than necessary and are bloating the file.
}
string GenerateLargeCircle()
{
// Define the size of square using offsets from min/max values.
string strComment = "******* Begin Large Circle at " + gintTravelSpeedmms.ToString() + "mm/s ******";
double granularity = 360/gdblCircleSegmentCount;
string strOutputText = GenerateCircle(gintTravelSpeedmms, gdblXMax / 2, gdblYMax / 2, gdblXMax / 4, granularity, 0, 0, strComment);
strComment = "******* End Large Circle ******";
strOutputText += AddComment(strComment);
return strOutputText + crlf;
}
// ********************************************************************
// ********************* G-code Creation Methods **********************
// Create the g-code to move to a position, with an optional comment at the end of the line.
string GotoPositionXY(double x, double y = 0, string strComment = "")
{
string strOutput = "G1 X" + x.ToString() + " Y" + y.ToString() + (strComment == "" ? "" : "\t; " + strComment);
return strOutput + crlf;
}
string GotoPositionX(double x, string strComment = "")
{
string strOutput = "G1 X" + x.ToString() + (strComment=="" ? "" : "\t; " + strComment);
return strOutput + crlf;
}
string GotoPositionY(double y = 0, string strComment = "")
{
string strOutput = "G1 Y" + y.ToString() + (strComment == "" ? "" : "\t; " + strComment);
return strOutput + crlf;
}
string AddComment(string strComment = "")
{
string strOutput = "";
strOutput += "; " + strComment;
return strOutput + crlf;
}
private void btnOpenInNotepad_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(txtFileName.Text, "notepad.exe");
}
// ********************************************************************
}
}