-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircuitShapes.cs
266 lines (231 loc) · 9.92 KB
/
CircuitShapes.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
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Controls;
using CircuitPro;
using System.Windows.Input;
namespace CircuitShapes
{
public abstract class ComponentDraw : Shape
{
protected static readonly int unit = 20;
protected static readonly int offset = unit;
protected static readonly int width = 5 * unit;
protected static readonly int height = 2 * unit;
private readonly bool fillShape;
public string Id { get; protected set; }
public CircuitPro.CircuitModel.Component Component { get; protected set; }
/// <summary>
/// Deseneaza un component de circuit
/// </summary>
/// <param name="component">Referinta catre componentul care este desenat</param>
/// <param name="x">Randul din tablou</param>
/// <param name="y">Coloana din tablou</param>
/// <param name="fillShape"">Daca figura sa fie umpluta cu transparenta (primeste evenimentul de click)</param>
protected ComponentDraw(CircuitPro.CircuitModel.Component component, string id, int x = 0, int y = 0, bool fillShape = true)
{
// setari desenare
Stroke = Brushes.Black;
StrokeThickness = 1;
Fill = Brushes.Transparent;
// setare tooltip
if(component != null)
ToolTip = component.Nume;
// setare informatii
this.fillShape = fillShape;
Id = id;
Component = component;
// setare pozitie in canvas
Canvas.SetTop(this, offset + height * x);
Canvas.SetLeft(this, offset + width * y);
}
/// <summary>
/// Generare geometrie figura (override din Shape)
/// </summary>
protected override Geometry DefiningGeometry
{
get
{
StreamGeometry geometry = new StreamGeometry();
using (StreamGeometryContext context = geometry.Open())
{
// desenare forma
DrawGeometry(context);
// dreptunghi zona (capteaza click-ul mouse-ului pe element)
if (fillShape)
{
DrawRectangle(context, new Point(0, 0), 5 * unit, 2 * unit, false);
}
}
return geometry;
}
}
/// <summary>
/// Metoda desenare, specifica fiecarui component
/// </summary>
/// <param name="context">Contextul de desenare</param>
protected abstract void DrawGeometry(StreamGeometryContext context);
/// <summary>
/// Deseneaza o linie dreapta de la un punct dat si avand o lungime
/// </summary>
/// <param name="context">Contextul de desenare</param>
/// <param name="start">Punctul de start al liniei</param>
/// <param name="len">Lungimea liniei</param>
/// <param name="horizontal">Linie orizontala sau verticala</param>
protected void DrawStraightLine(StreamGeometryContext context, Point start, double len, bool horizontal = true)
{
Point end = horizontal ? new Point(start.X + len, start.Y) : new Point(start.X, start.Y + len);
context.BeginFigure(start, false, false);
context.LineTo(end, true, true);
}
/// <summary>
/// Deseneaza un dreptunghi
/// </summary>
/// <param name="context">Contextul de desenare</param>
/// <param name="start">Punctul coltului stanga sus al dreptunghiului</param>
/// <param name="width">Latimea dreptunghiului</param>
/// <param name="height">Inaltimea dreptunghiului</param>
/// <param name="isStroked">Daca are contur</param>
/// <param name="fill">Daca este umplut (cu transparenta)</param>
protected void DrawRectangle(StreamGeometryContext context, Point start, double width, double height, bool isStroked, bool fill = true)
{
context.BeginFigure(start, fill, true);
List<Point> pointList = new List<Point>
{
new Point(start.X + width, start.Y),
new Point(start.X + width, start.Y + height),
new Point(start.X, start.Y + height),
new Point(start.X, start.Y)
};
context.PolyLineTo(pointList, isStroked, false);
}
/// <summary>
/// Eveniment selectare element
/// </summary>
/// <param name="e">Eveniment mouse</param>
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
((MainWindow)Application.Current.MainWindow).SetSelected(Component, Id);
e.Handled = true;
}
public void SetSelected(bool sel)
{
Stroke = sel ? Brushes.Blue : Brushes.Black;
}
public static void SetCanvasSize(Canvas c, int w, int h)
{
c.MinWidth = w * width + 2 * unit;
c.MinHeight = (h + 1) * height + 2 * unit;
}
}
class RezistentaDraw : ComponentDraw
{
/// <summary>
/// Deseneaza o rezistenta
/// </summary>
/// <param name="x">Randul din tablou</param>
/// <param name="y">Coloana din tablou</param>
public RezistentaDraw(CircuitPro.CircuitModel.Component component, string id, int x = 0, int y = 0) : base(component, id, x, y) { }
protected override void DrawGeometry(StreamGeometryContext context)
{
DrawStraightLine(context, new Point(0, unit), unit);
DrawRectangle(context, new Point(unit, unit / 2), 3 * unit, unit, true, false);
DrawStraightLine(context, new Point(4 * unit, unit), unit);
}
}
class CondensatorDraw : ComponentDraw
{
/// <summary>
/// Deseneaza un condensator
/// </summary>
/// <param name="x">Randul din tablou</param>
/// <param name="y">Coloana din tablou</param>
public CondensatorDraw(CircuitPro.CircuitModel.Component component, string id, int x = 0, int y = 0) : base(component, id, x, y) { }
protected override void DrawGeometry(StreamGeometryContext context)
{
DrawStraightLine(context, new Point(0, unit), 2.25 * unit);
DrawStraightLine(context, new Point(2.25 * unit, unit / 4), 1.5 * unit, false);
DrawStraightLine(context, new Point(2.75 * unit, unit / 4), 1.5 * unit, false);
DrawStraightLine(context, new Point(2.75 * unit, unit), 2.25 * unit);
}
}
class BobinaDraw : ComponentDraw
{
/// <summary>
/// Deseneaza o bobina
/// </summary>
/// <param name="x">Randul din tablou</param>
/// <param name="y">Coloana din tablou</param>
public BobinaDraw(CircuitPro.CircuitModel.Component component, string id, int x = 0, int y = 0) : base(component, id, x, y) { }
protected override void DrawGeometry(StreamGeometryContext context)
{
// legatura stanga
DrawStraightLine(context, new Point(0, unit), unit);
// bucle bobina
int len = 5;
for (int i = 0; i < len; i++)
{
int delta = i * unit / 2;
context.ArcTo(new Point(2 * unit + delta - 4, unit), new Size(1, 1.3), 0, false, SweepDirection.Clockwise, true, true);
if (i != len - 1)
{
context.ArcTo(new Point(1.5 * unit + delta, unit), new Size(1, 3), 0, false, SweepDirection.Clockwise, true, true);
}
}
// conexiune dreapta
context.LineTo(new Point(5 * unit, unit), true, true);
}
}
class GeneratorDraw : ComponentDraw
{
/// <summary>
/// Deseneaza o bobina
/// </summary>
/// <param name="x">Randul din tablou</param>
/// <param name="y">Coloana din tablou</param>
public GeneratorDraw(CircuitPro.CircuitModel.Component component, string id, int x = 0, int y = 0) : base(component, id, x, y) { }
protected override void DrawGeometry(StreamGeometryContext context)
{
// legatura stanga
DrawStraightLine(context, new Point(0, unit), 1.75 * unit);
// cerc
double diameter = 0.75 * unit;
context.ArcTo(new Point(3.25 * unit, unit), new Size(diameter, diameter), 0, false, SweepDirection.Clockwise, true, true);
context.ArcTo(new Point(1.75 * unit, unit), new Size(diameter, diameter), 0, true, SweepDirection.Clockwise, true, true);
// semn alternativ
context.BeginFigure(new Point(2 * unit, unit), false, false);
context.BezierTo(new Point(2.5 * unit, 0), new Point(2.5 * unit, 2 * unit), new Point(3 * unit, unit), true, true);
// conexiune dreapta
DrawStraightLine(context, new Point(3.25 * unit, unit), 1.75 * unit);
}
}
class FillLine : ComponentDraw
{
protected int len = 0;
public FillLine(string id, int row, int column, int len) : base(null, id, row, column, false)
{
this.len = len;
}
protected override void DrawGeometry(StreamGeometryContext context)
{
DrawStraightLine(context, new Point(0, unit), len * width);
}
}
class ParalelLine : ComponentDraw
{
protected int h = 0;
protected int w = 0;
public ParalelLine(string id, int row, int column, int h, int w) : base(null, id, row, column, false)
{
this.h = h;
this.w = w;
}
protected override void DrawGeometry(StreamGeometryContext context)
{
DrawStraightLine(context, new Point(0, unit), (h - 1) * height, false);
DrawStraightLine(context, new Point(w * width, unit), (h - 1) * height, false);
}
}
}