-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathVectorProcessorViewModel.cs
More file actions
141 lines (129 loc) · 5.1 KB
/
Copy pathVectorProcessorViewModel.cs
File metadata and controls
141 lines (129 loc) · 5.1 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
using ImageToGCode.Engine.GCodeGeneration;
using ImageToGCode.Engine.GCodeGeneration.VectorProcessor;
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 VectorProcessorViewModel : INotifyPropertyChanged
{
private VPathGroup _SelectedGroup;
public ObservableCollection<Engine.GCodeGeneration.VectorProcessor.VPathGroup> PathGroups { get; private set; }
public Command OpenSvg { get; private set; }
public Command MoveUp { get; private set; }
public Command MoveDown { get; private set; }
public VPathGroup SelectedGroup
{
get
{
return _SelectedGroup;
}
set
{
if (_SelectedGroup == value)
return;
_SelectedGroup = value;
RaisePropertyChanged("SelectedGroup");
MoveUp.RaiseCanExecuteChanged();
MoveDown.RaiseCanExecuteChanged();
}
}
public VectorProcessorViewModel()
{
PathGroups = new ObservableCollection<Engine.GCodeGeneration.VectorProcessor.VPathGroup>();
OpenSvg = new Command((x) => OpenSvgAction(), (x) => true);
MoveUp = new Command((x) => MoveUpAction(), (x) => SelectedGroup != null);
MoveDown = new Command((x) => MoveDownAction(), (x) => SelectedGroup != null);
}
private void OpenSvgAction()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "SVG file (*.svg)|*.svg";
if (openFileDialog.ShowDialog() == true)
{
//try {
PathGroups.Clear();
var doc = Svg.SvgDocument.Open(openFileDialog.FileName);
var gcg = new VPathGroupSVGGenerator(doc);
foreach (var vPath in gcg.GenerateVPathGroups().OrderBy(g => g.PathColor.GetHue()))
{
PathGroups.Add(vPath);
}
//}
//catch (Exception e)
//{
// System.Windows.MessageBox.Show("Ошибка открытия файла", "Ошибка", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
//}
}
}
private RectangleF GetBoundingBox()
{
double minX = double.PositiveInfinity, minY = double.PositiveInfinity;
double maxX = double.NegativeInfinity, maxY = double.NegativeInfinity;
foreach (var pg in PathGroups)
foreach (var path in pg.PathList)
{
var rect = path.GetBounds();
if (rect.Left < minX) minX = rect.Left;
if (rect.Bottom < minY) minY = rect.Bottom;
if (rect.Top > maxY) maxY = rect.Top;
if (rect.Right > maxX) maxX = rect.Right;
}
return new RectangleF((float)minX, (float)minY, (float)(maxX - minX), (float)(maxY - minY));
}
private void MoveUpAction()
{
if (SelectedGroup == null)
throw new Exception("SelectedGroup is null");
//var groupToMove = SelectedGroup;
var idx = PathGroups.IndexOf(SelectedGroup);
if (idx <= 0)
return;
PathGroups.Move(idx, idx - 1);
}
private void MoveDownAction()
{
if (SelectedGroup == null)
throw new Exception("SelectedGroup is null");
//var groupToMove = SelectedGroup;
var idx = PathGroups.IndexOf(SelectedGroup);
if (idx >= PathGroups.Count - 1)
return;
PathGroups.Move(idx, idx + 1);
}
public IEnumerable<BaseGCode> Generate()
{
yield return new BaseGCode("G21");
yield return new BaseGCode("G90");
yield return new BaseGCode("M3 S0");
foreach (var grp in PathGroups)
foreach (var gc in grp.Generate())
yield return gc;
yield return new BaseGCode("M5");
yield return new BaseGCode("%");
}
#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
}
}