-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathViewerDevice.cs
More file actions
401 lines (311 loc) · 9.9 KB
/
Copy pathViewerDevice.cs
File metadata and controls
401 lines (311 loc) · 9.9 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Diagnostics;
namespace gcodeparser
{
public class ViewerDevice: Device
{
// Machine simulation
public static float MmPerMinute = 119.8f;
public static float ZCutLevel = 0;
public float CutAreaWidth = 200f;
public float CutAreaHeight = 100f;
public float GridStep = 10f;
protected float mToolDiameter = 0.1f;
protected int mCurrentStep = 0;
protected ViewerLine mCurrentLine;
protected int mCurrentLineIndex = 0;
protected string mLastCodeLine;
protected Control mParentControl;
protected List<ViewerStep> mSteps = new List<ViewerStep>();
protected List<ViewerLine> mCodeLines = new List<ViewerLine>();
protected float mCurrentDistance = 0f;
public event EventHandler CodeChanged;
// 2D Implementation
private float mDeviceScale = 1.0f;
private float mVisualScale = 10f;
private Pen mGridPen = new Pen(Color.FromArgb(15, Color.White), 0.1f);
private Pen mToolPen;
internal static Pen CutPen;
internal static Pen DonePen;
private static Pen MovePen = new Pen(Color.LightGray, 0.1f);
public float VisualScale
{
get
{
return mVisualScale;
}
set
{
mVisualScale = value;
mParentControl.Invalidate();
}
}
public float DeviceScale
{
get
{
return mDeviceScale;
}
set
{
mDeviceScale = value;
mParentControl.Invalidate();
}
}
public float ToolDiameter
{
get
{
return mToolDiameter;
}
set
{
mToolDiameter = value;
ViewerStep.ToolDiameter = mToolDiameter;
SetToolPensDiameter(mToolDiameter);
if (mToolPen != null) mToolPen.Dispose();
mToolPen = new Pen(Color.LightSalmon, mToolDiameter / 2);
mParentControl.Invalidate();
}
}
internal ViewerStep CurrentStep
{
get
{
return mSteps[mCurrentStep];
}
}
public float CurrentDistance
{
get
{
return mCurrentDistance;
}
}
public int CurrentStepIndex
{
get
{
return mCurrentStep;
}
set
{
mCurrentStep = value;
mParentControl.Invalidate();
}
}
public int TotalSteps
{
get
{
return mSteps.Count;
}
}
public List<ViewerLine> CodeLines
{
get
{
return mCodeLines;
}
}
public int CurrentCodeLineIndex
{
get
{
return mCurrentLineIndex;
}
set
{
mCurrentLineIndex = value;
mParentControl.Invalidate();
}
}
public ViewerDevice(Control parentControl)
{
mParentControl = parentControl;
}
public virtual void Initialize()
{
mParentControl.Paint += new PaintEventHandler(this.Paint);
if (CutPen == null) SetToolPensDiameter(3.2f);
}
// __ Device Impl _______________________________________________________________
public override void Print(string s)
{
}
public override void SetFeedRate(float mmPerMinute)
{
// Do nothing
}
public override void SetPosition(float x, float y, float z)
{
SetCurrent(x, y, z);
}
public override void MoveAbsoluteLinear(float x, float y, float z)
{
AddStep(mCurrentX, mCurrentY, mCurrentZ, x, y, z);
SetCurrent(x, y, z);
}
public override void MoveRawX(int steps)
{
throw new NotImplementedException();
}
public override void MoveRawY(int steps)
{
throw new NotImplementedException();
}
public override void MoveRawZ(int steps)
{
throw new NotImplementedException();
}
public override void Calibrate(float xStepsPerMm, float yStepsPerMm, float zStepsPerMm)
{
// Do nothing
}
public float GetTotalDistance()
{
float result = 0f;
foreach (ViewerStep op in mSteps)
{
result += op.Distance;
}
return result;
}
private void AddStep(float fromX, float fromY, float fromZ, float toX, float toY, float toZ)
{
ViewerStep op = new ViewerStep(
fromX * mDeviceScale,
fromY * mDeviceScale,
fromZ,
toX * mDeviceScale,
toY * mDeviceScale,
toZ);
string line = GCodeParser.Line;
op.GCodeLine = line;
if (mLastCodeLine != line)
{
mCurrentLine = new ViewerLine(line);
mLastCodeLine = line;
mCodeLines.Add(mCurrentLine);
}
mCurrentLine.Steps.Add(op);
mSteps.Add(op);
if (CodeChanged != null) CodeChanged(this, EventArgs.Empty);
}
private void SetCurrent(float x, float y, float z)
{
if (x > float.MinValue) mCurrentX = x;
if (y > float.MinValue) mCurrentY = y;
if (z > float.MinValue) mCurrentZ = z;
}
// __ Paint handling ____________________________________________________________
public void Clear()
{
mCodeLines.Clear();
mSteps.Clear();
mCurrentStep = 0;
mCurrentX = 0;
mCurrentY = 0;
mCurrentZ = 0;
}
internal void Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.Clear(mParentControl.BackColor);
g.TranslateTransform(0, mParentControl.Height - 40);
g.ScaleTransform(mVisualScale, -mVisualScale);
g.TranslateTransform(5, 5);
g.SmoothingMode = SmoothingMode.AntiAlias;
DrawGrid(g);
ViewerStep op = null;
ViewerStep head = null;
mCurrentDistance = 0f;
for (int i = 0; i < mSteps.Count; ++i)
{
op = mSteps[i];
if (i <= mCurrentStep)
{
PaintStep(op, g, DonePen);
mCurrentDistance += op.Distance;
head = op;
}
else
{
PaintStep(op, g);
}
}
if (head != null)
{
if (mToolPen == null) ToolDiameter = ToolDiameter;
g.DrawEllipse(mToolPen, GetRect(head.End.X, head.End.Y, mToolDiameter / 4));
//g.DrawString(
////string.Format("{0}, {1}", op.End.X, op.End.Y),
//string.Format("{0}", mCurrentStep),
//mParentControl.Font, Brushes.Red, op.End);
}
}
private void PaintStep(ViewerStep op, Graphics g)
{
Pen p = op.IsCuttingOp ? CutPen : MovePen;
PaintStep(op, g, p);
}
private void PaintStep(ViewerStep op, Graphics g, Pen p)
{
float xx = Math.Abs(op.Start.X - op.End.X);
float yy = Math.Abs(op.Start.Y - op.End.Y);
if (xx < 0.00001 && yy < 0.00001) return;
p = op.IsCuttingOp ? p : MovePen;
g.DrawLine(p, op.Start.X, op.Start.Y, op.End.X, op.End.Y);
}
protected virtual void SetToolPensDiameter(float diameter)
{
if (CutPen != null) CutPen.Dispose();
if (DonePen != null) DonePen.Dispose();
CutPen = GetRoundedPen(Color.White, diameter);
DonePen = GetRoundedPen(Color.Red, diameter);
}
private static Pen GetRoundedPen(Color col, float diameter)
{
Pen result = new Pen(col, diameter);
result.StartCap = LineCap.Round;
result.EndCap = LineCap.Round;
return result;
}
private void DrawGrid(Graphics g)
{
int w = (int)CutAreaWidth;
int h = (int)CutAreaHeight;
for (int x = 0; x <= w; x += (int)GridStep) g.DrawLine(mGridPen, x, 0, x, h);
for (int y = 0; y <= h; y += (int)GridStep) g.DrawLine(mGridPen, 0, y, w, y);
string s = string.Format("Each square = {0:0.0}mm", GridStep);
GraphicsState state = g.Save();
g.ResetTransform();
g.DrawString(s, mParentControl.Font, Brushes.White, 20, mParentControl.Height - 40);
g.Restore(state);
}
private static RectangleF GetRect(float x, float y)
{
return GetRect(x, y, 4);
}
private static RectangleF GetRect(float x, float y, float r)
{
float r2 = r * 2;
return new RectangleF(x - r, y - r, r2, r2);
}
}
public class ViewerLine
{
public string GCodeLine;
public List<ViewerStep> Steps = new List<ViewerStep>();
public ViewerLine(string gCodeLine)
{
GCodeLine = gCodeLine;
}
}
}