-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathImageProcessorViewModel.cs
More file actions
393 lines (364 loc) · 12 KB
/
Copy pathImageProcessorViewModel.cs
File metadata and controls
393 lines (364 loc) · 12 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
using ImageToGCode.Engine.GCodeGeneration;
using ImageToGCode.Tools;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
namespace ImageToGCode
{
class ImageProcessorViewModel : INotifyPropertyChanged
{
private double _Height = 100;
private double _Width = 100;
private int _MinPower = 15;
private int _MaxPower = 40;
private Engine.Interpolators.IInterpolator _SelectedInterpolator;
private List<Engine.Interpolators.IInterpolator> _InterpolatorsSource;
private double _Angle = 90;
private double _MinFeed = 4000;
private double _MaxFeed = 10000;
private bool _EngraveBothDirection = true;
private bool _UseFreeZone = true;
private double _FreeZone = 10;
private double _LineResolution = 0.2;
private double _PointResolution = 0.2;
private double _AspectRate = 2;
private bool _KeepAspectRatio = true;
private string _PathToFile;
private Bitmap _Bitmap;
private TimeSpan _EstimatedTime;
private ObservableCollection<BaseGCode> _GCode;
public string PathToFile
{
get
{
return _PathToFile;
}
set
{
if (_PathToFile == value)
return;
_PathToFile = value;
OnPropertyChanged("_PathToFile");
}
}
public double Width
{
get { return _Width; }
set
{
if (_Width == value)
return;
_Width = value;
OnPropertyChanged("Width");
if (KeepAspectRatio)
CountHeight(value);
CountEstimatedTime();
}
}
public double Height
{
get { return _Height; }
set
{
if (_Height == value)
return;
_Height = value;
OnPropertyChanged("Height");
if (KeepAspectRatio)
CountWidth(value);
CountEstimatedTime();
}
}
public double LineResolution
{
get { return _LineResolution; }
set
{
if (_LineResolution == value)
return;
_LineResolution = value;
OnPropertyChanged("LineResolution");
CountEstimatedTime();
}
}
public double PointResolution
{
get
{
return _PointResolution;
}
set
{
if (_PointResolution == value)
return;
_PointResolution = value;
OnPropertyChanged("PointResolution");
}
}
public double Angle
{
get
{
return _Angle;
}
set
{
if (_Angle == value)
return;
_Angle = value;
OnPropertyChanged("Angle");
}
}
public bool UseFreeZone
{
get { return _UseFreeZone; }
set
{
if (_UseFreeZone == value)
return;
_UseFreeZone = value;
OnPropertyChanged("UseFreeZone");
CountEstimatedTime();
}
}
public double FreeZone
{
get { return _FreeZone; }
set
{
if (_FreeZone == value)
return;
_FreeZone = value;
OnPropertyChanged("FreeZone");
CountEstimatedTime();
}
}
public double MaxFeed
{
get { return _MaxFeed; }
set
{
if (_MaxFeed == value)
return;
_MaxFeed = value;
OnPropertyChanged("MaxFeed");
CountEstimatedTime();
}
}
public double MinFeed
{
get
{
return _MinFeed;
}
set
{
if (_MinFeed == value)
return;
_MinFeed = value;
OnPropertyChanged("MinFeed");
}
}
public bool EngraveBothDirection
{
get { return _EngraveBothDirection; }
set
{
_EngraveBothDirection = value;
}
}
public double AspectRate
{
get { return _AspectRate; }
set
{
if (_AspectRate == value)
return;
_AspectRate = value;
OnPropertyChanged("AspectRate");
if (KeepAspectRatio)
CountHeight(Width);
}
}
public bool KeepAspectRatio
{
get
{
return _KeepAspectRatio;
}
set
{
if (_KeepAspectRatio == value)
return;
_KeepAspectRatio = value;
OnPropertyChanged("KeepAspectRatio");
CountHeight(Width);
}
}
public List<Engine.Interpolators.IInterpolator> InterpolatorsSource
{
get
{
return _InterpolatorsSource;
}
}
public Engine.Interpolators.IInterpolator SelectedInterpolator
{
get
{
return _SelectedInterpolator;
}
set
{
if (_SelectedInterpolator == value)
return;
_SelectedInterpolator = value;
OnPropertyChanged("SelectedInterpolator");
}
}
public TimeSpan EstimatedTime
{
get
{
return _EstimatedTime;
}
set
{
if (_EstimatedTime == value)
return;
_EstimatedTime = value;
RaisePropertyChanged("EstimatedTime");
}
}
public int MaxPower
{
get
{
return _MaxPower;
}
set
{
if (_MaxPower == value)
return;
if (value <= _MinPower)
_MinPower = value;//throw new Exception("Max power can't be more then min power");
_MaxPower = value;
RaisePropertyChanged("MaxPower");
}
}
public int MinPower
{
get
{
return _MinPower;
}
set
{
if (_MinPower == value)
return;
if (value >= _MaxPower)
_MaxPower = value;
_MinPower = value;
RaisePropertyChanged("MinPower");
}
}
public Command OpenImage { get; private set; }
public Command Generate { get; private set; }
public ImageProcessorViewModel(ObservableCollection<BaseGCode> gcode)
{
_GCode = gcode;
Generate = new Command((x) => GenerateAction(), (x) => _Bitmap != null);
OpenImage = new Command((x) => OpenImageAction(), (x) => true);
_InterpolatorsSource = new List<Engine.Interpolators.IInterpolator>();
InterpolatorsSource.Add(new Engine.Interpolators.StepInterpolator());
InterpolatorsSource.Add(new Engine.Interpolators.BilinearInterpolator());
SelectedInterpolator = InterpolatorsSource[0];
}
private void OpenImageAction()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true)
{
try
{
_Bitmap = (Bitmap)Bitmap.FromFile(openFileDialog.FileName);
AspectRate = (double)_Bitmap.Width / (double)_Bitmap.Height;
Generate.RaiseCanExecuteChanged();
}
catch (Exception e)
{
System.Windows.MessageBox.Show("Ошибка открытия файла", "Ошибка", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
}
}
}
private void GenerateAction()
{
_GCode.Clear();
foreach (var gc in Process())
{
_GCode.Add(gc);
}
}
private IEnumerable<BaseGCode> Process()
{
var processor = new Engine.ImageProcessor(_Bitmap, Width, Height, LineResolution, PointResolution, Angle, SelectedInterpolator);
var presenter = processor.CreatePresenter();
var strokeGenerator = new Engine.GCodeGeneration.ImageProcessor.StrokesFromImageLinesGenerator(presenter.Lines, UseFreeZone, FreeZone, EngraveBothDirection);
strokeGenerator.GenerateStrokes();
var gcGen = new Engine.GCodeGeneration.ImageProcessor.GCodeGenerator(strokeGenerator.Strokes, (int)MinFeed, (int)MaxFeed, MaxPower, MinPower);
var gcode = gcGen.GenerateCode();
yield return new BaseGCode("G21");
yield return new BaseGCode("G90");
yield return new BaseGCode("M3 S0");
foreach (var str in gcode)
yield return str;
yield return new BaseGCode("M5");
yield return new BaseGCode("%");
yield return new BaseGCode("(----ImageGenerator----)");
yield return new BaseGCode(string.Format("(FileName: {0})", _PathToFile));
yield return new BaseGCode(string.Format("(Width: {0} mm, Height: {1} mm)", Width, Height));
yield return new BaseGCode(string.Format("(Resolution line: {0} mm, point: {1} mm)", LineResolution, PointResolution));
yield return new BaseGCode(string.Format("(Feed max: {0} mm/min, min: {1} mm/min)", MaxFeed, MinFeed));
yield return new BaseGCode(string.Format("(Power max: {0}, min: {1})", MaxPower, MinPower));
yield return new BaseGCode(string.Format("(Angle: {0})", Angle));
yield return new BaseGCode(string.Format("(Idle zones: {0})", UseFreeZone ? FreeZone : 0.0));
yield return new BaseGCode(string.Format("(Engrave both directions: {0})", EngraveBothDirection));
}
private void CountEstimatedTime()
{
double secs = ((Height / LineResolution) * (Width + (UseFreeZone ? FreeZone * 2 : 0))) / ((MinFeed + MaxFeed) / 2.0) * 60.0;
if (!double.IsInfinity(secs) & !double.IsNaN(secs))
EstimatedTime = TimeSpan.FromSeconds(Math.Round(secs));
}
private void CountWidth(double height)
{
Width = height * AspectRate;
}
private void CountHeight(double width)
{
Height = width / AspectRate;
}
#region IPropertyChanged
private SynchronizationContext _synchronizationContext = SynchronizationContext.Current;
private void OnPropertyChanged(string propertyName)
{
if (SynchronizationContext.Current != _synchronizationContext)
RaisePropertyChanged(propertyName);
else
_synchronizationContext.Post(RaisePropertyChanged, propertyName);
}
private void RaisePropertyChanged(object param)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs((string)param));
}
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
}