프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / MarkupToPDF_Old / Controls / Line / LineControl.cs @ 9fa712a5

이력 | 보기 | 이력해설 | 다운로드 (20.8 KB)

1
using System;
2
using System.Net;
3
using System.Windows;
4
using System.Windows.Controls;
5
using System.Windows.Documents;
6
using System.Windows.Ink;
7
using System.Windows.Input;
8
using System.Windows.Media;
9
using System.Windows.Media.Animation;
10
using System.Windows.Shapes;
11
using System.ComponentModel;
12
using System.Collections.Generic;
13
using MarkupToPDF.Controls.Common;
14

    
15
namespace MarkupToPDF.Controls.Line
16
{
17
    [TemplatePart(Name = "PART_LinePath", Type = typeof(Path))]
18
    public class LineControl : Control, IDisposable, INotifyPropertyChanged, IMarkupCommonData
19
    {
20
        public event PropertyChangedEventHandler PropertyChanged;
21
        public enum LineStyleSet { SingleLine, MultiLine, Normal, Twin, Dim, ArrowText };
22

    
23
        private const string PART_LinePath = "PART_LinePath";
24
        public Path Base_LinePath = null;
25

    
26
        static LineControl()
27
        {
28
            DefaultStyleKeyProperty.OverrideMetadata(typeof(LineControl), new FrameworkPropertyMetadata(typeof(LineControl)));
29
            //Application.Current.Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri("/MarkupToPDF;Component/Themes/generic.xaml")) as ResourceDictionary);
30
            ResourceDictionary dictionary = new ResourceDictionary();
31

    
32
            dictionary.Source = new Uri("/MarkupToPDF;component/Themes/generic.xaml", UriKind.RelativeOrAbsolute);
33
            Application.Current.Resources.MergedDictionaries.Add(dictionary);
34
             //System.Diagnostics.Debug.WriteLine("resource Count :" + Application.Current.Resources.MergedDictionaries.Count);
35
        }
36

    
37
        public double DimSize
38
        {
39
            get { return (double)GetValue(DimSizeProperty); }
40
            set
41
            {
42
                if (this.DimSize != value)
43
                {
44
                    SetValue(DimSizeProperty, value);
45
                    OnPropertyChanged("DimSize");
46
                }
47
            }
48
        }
49

    
50
        public LineControl()
51
        {
52
            this.DefaultStyleKey = typeof(LineControl);
53
        }
54

    
55
        public void Dispose()
56
        {
57
            GC.Collect();
58
            GC.SuppressFinalize(this);
59
        }
60
        protected void OnPropertyChanged(string propName)
61
        {
62
            if (PropertyChanged != null)
63
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
64
        }
65
        #region Dependency Properties
66

    
67
        public static readonly DependencyProperty IsSelectedProperty =
68
     DependencyProperty.Register("IsSelected", typeof(bool), typeof(LineControl), new FrameworkPropertyMetadata(false, IsSelectedChanged));
69

    
70
        public static readonly DependencyProperty ControlTypeProperty =
71
    DependencyProperty.Register("ControlType", typeof(ControlType), typeof(LineControl), new FrameworkPropertyMetadata(ControlType.SingleLine));
72

    
73
        public static readonly DependencyProperty OverViewPathDataProperty = DependencyProperty.Register(
74
                "OverViewPathData", typeof(Geometry), typeof(LineControl), new PropertyMetadata(null));
75

    
76
        public static readonly DependencyProperty UserIDProperty = DependencyProperty.Register(
77
                "UserID", typeof(string), typeof(LineControl), new PropertyMetadata(null));
78

    
79
        public static readonly DependencyProperty LineSizeProperty = DependencyProperty.Register(
80
                "LineSize", typeof(double), typeof(LineControl), new PropertyMetadata((Double)1));
81

    
82
        public static readonly DependencyProperty IntervalProperty = DependencyProperty.Register(
83
                "Interval", typeof(double), typeof(LineControl), new PropertyMetadata((double)10));
84

    
85
        public static readonly DependencyProperty DashSizeProperty = DependencyProperty.Register(
86
                "DashSize", typeof(DoubleCollection), typeof(LineControl), new PropertyMetadata(new DoubleCollection { 1, 1 }, PointValueChanged));
87

    
88
        public static readonly DependencyProperty StrokeColorProperty = DependencyProperty.Register(
89
               "StrokeColor", typeof(SolidColorBrush), typeof(LineControl), new PropertyMetadata(new SolidColorBrush(Colors.Red)));
90

    
91
        public static readonly DependencyProperty PathDataProperty = DependencyProperty.Register(
92
              "PathData", typeof(Geometry), typeof(LineControl), null);
93

    
94
        public static readonly DependencyProperty LineStyleProperty = DependencyProperty.Register(
95
               "LineStyle", typeof(LineStyleSet), typeof(LineControl), new PropertyMetadata(LineStyleSet.SingleLine));
96

    
97
        public static readonly DependencyProperty StartPointProperty = DependencyProperty.Register(
98
               "StartPoint", typeof(Point), typeof(LineControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
99

    
100
        public static readonly DependencyProperty EndPointProperty = DependencyProperty.Register(
101
               "EndPoint", typeof(Point), typeof(LineControl), new PropertyMetadata(new Point(100, 100), PointValueChanged));
102

    
103
        public static readonly DependencyProperty PointSetProperty = DependencyProperty.Register(
104
                "PointSet", typeof(List<Point>), typeof(LineControl), new PropertyMetadata(new List<Point>(), PointValueChanged));
105
        
106
        
107
        public static readonly DependencyProperty MiddlePointProperty = DependencyProperty.Register(
108
                "MiddlePoint", typeof(Point), typeof(LineControl), new PropertyMetadata(new Point(0, 0)));
109
        public static readonly DependencyProperty DimSizeProperty = DependencyProperty.Register(
110
                "DimSize", typeof(double), typeof(LineControl), new PropertyMetadata((double)5));
111

    
112
        public static readonly DependencyProperty AngleProperty =
113
            DependencyProperty.Register("AngleValue", typeof(double), typeof(LineControl), new PropertyMetadata((double)0.0, new PropertyChangedCallback(AngleValueChanged)));
114

    
115
        public static readonly DependencyProperty CenterXProperty =
116
            DependencyProperty.Register("CenterX", typeof(double), typeof(LineControl), new PropertyMetadata((double)0, OnCenterXYChanged));
117

    
118
        public static readonly DependencyProperty CenterYProperty =
119
            DependencyProperty.Register("CenterY", typeof(double), typeof(LineControl), new PropertyMetadata((double)0, OnCenterXYChanged));
120
        #endregion
121
        #region PropertyChanged Method
122

    
123
        public static void IsSelectedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
124
        {
125
            var instance = (LineControl)sender;
126

    
127
            if (e.OldValue != e.NewValue && instance.Base_LinePath != null)
128
            {
129

    
130
                instance.SetValue(e.Property, e.NewValue);
131

    
132
                if (instance.IsSelected)
133
                {
134
                    instance.Base_LinePath.Stroke = new SolidColorBrush(Colors.Blue);
135
                }
136
                else
137
                {
138
                    instance.Base_LinePath.Stroke = new SolidColorBrush(Colors.Red);
139
                }
140
            }
141
        }
142

    
143
        public static void PointValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
144
        {
145
            var instance = (LineControl)sender;
146
            if (e.OldValue != e.NewValue && instance.Base_LinePath != null)
147
            {
148
                instance.SetValue(e.Property, e.NewValue);
149
                instance.SetLinePath();
150
            }
151
        }
152
        public static void AngleValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
153
        {
154
            var instance = (LineControl)sender;
155
            if (e.OldValue != e.NewValue && instance.Base_LinePath != null)
156
            {
157
                instance.SetValue(e.Property, e.NewValue);
158
                instance.SetLinePath();
159
            }
160
        }
161
        public static void OnCenterXYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
162
        {
163
            var instance = (LineControl)sender;
164
            if (e.OldValue != e.NewValue && instance.Base_LinePath != null)
165
            {
166
                instance.SetValue(e.Property, e.NewValue);
167
                instance.SetLinePath();
168
            }
169
        }
170

    
171
        #endregion
172
        #region Properties
173
        public string UserID
174
        {
175
            get { return (string)GetValue(UserIDProperty); }
176
            set
177
            {
178
                if (this.UserID != value)
179
                {
180
                    SetValue(UserIDProperty, value);
181
                    OnPropertyChanged("UserID");
182
                }
183
            }
184
        }
185

    
186
        public double Interval
187
        {
188
            get { return (double)GetValue(IntervalProperty); }
189
            set
190
            {
191
                if (this.Interval != value)
192
                {
193
                    SetValue(IntervalProperty, value);
194
                    OnPropertyChanged("Interval");
195
                }
196
            }
197
        }
198
        public Double LineSize
199
        {
200
            get { return (Double)GetValue(LineSizeProperty); }
201
            set
202
            {
203
                if (this.LineSize != value)
204
                {
205
                    SetValue(LineSizeProperty, value);
206
                }
207
            }
208
        }
209
        public DoubleCollection DashSize
210
        {
211
            get { return (DoubleCollection)GetValue(DashSizeProperty); }
212
            set
213
            {
214
                if (this.DashSize != value)
215
                {
216
                    SetValue(DashSizeProperty, value);
217
                }
218
            }
219
        }
220
        public List<Point> PointSet
221
        {
222
            get { return (List<Point>)GetValue(PointSetProperty); }
223
            set { SetValue(PointSetProperty, value); }
224
        }
225
        public double CenterX
226
        {
227
            get { return (double)GetValue(CenterXProperty); }
228
            set { SetValue(CenterXProperty, value); }
229
        }
230
        public double CenterY
231
        {
232
            get { return (double)GetValue(CenterYProperty); }
233
            set { SetValue(CenterYProperty, value); }
234
        }
235
        public SolidColorBrush StrokeColor
236
        {
237
            get { return (SolidColorBrush)GetValue(StrokeColorProperty); }
238
            set
239
            {
240
                if (this.StrokeColor != value)
241
                {
242
                    SetValue(StrokeColorProperty, value);
243
                }
244
            }
245
        }
246

    
247

    
248
        public Geometry PathData
249
        {
250
            get { return (Geometry)GetValue(PathDataProperty); }
251
            set
252
            {
253
                SetValue(PathDataProperty, value);
254
                OnPropertyChanged("PathData");
255
            }
256
        }
257
        public LineStyleSet LineStyle
258
        {
259
            get { return (LineStyleSet)GetValue(LineStyleProperty); }
260
            set
261
            {
262
                if (this.LineStyle != value)
263
                {
264
                    SetValue(LineStyleProperty, value);
265
                }
266
            }
267
        }
268

    
269

    
270

    
271
        public Geometry OverViewPathData
272
        {
273
            get
274
            {
275
                return (Geometry)GetValue(OverViewPathDataProperty);
276
            }
277
            set
278
            {
279
                SetValue(OverViewPathDataProperty, value);
280
                OnPropertyChanged("OverViewPathData");
281
            }
282
        }
283

    
284
        public bool IsSelected
285
        {
286
            get
287
            {
288
                return (bool)GetValue(IsSelectedProperty);
289
            }
290
            set
291
            {
292
                SetValue(IsSelectedProperty, value);
293
            }
294
        }
295

    
296
        public ControlType ControlType
297
        {
298
            get
299
            {
300
                return (ControlType)GetValue(ControlTypeProperty);
301
            }
302
            set
303
            {
304
                SetValue(ControlTypeProperty, value);
305
            }
306
        }
307

    
308
        public double AngleValue
309
        {
310
            get { return (double)GetValue(AngleProperty); }
311
            set { SetValue(AngleProperty, value); }
312
        }
313
        public double Angle
314
        {
315
            get { return (double)GetValue(AngleProperty); }
316
            set
317
            {
318
                if (this.Angle != value)
319
                {
320
                    SetValue(AngleProperty, value);
321
                }
322
            }
323
        }
324
        public Point EndPoint
325
        {
326
            get { return (Point)GetValue(EndPointProperty); }
327
            set
328
            {
329
                SetValue(EndPointProperty, value);
330
                OnPropertyChanged("EndPoint");
331
            }
332
        }
333
        public Point StartPoint
334
        {
335
            get { return (Point)GetValue(StartPointProperty); }
336
            set
337
            {
338
                SetValue(StartPointProperty, value);
339
                OnPropertyChanged("StartPoint");
340
            }
341
        }
342
        GeometryGroup instanceGroup = new GeometryGroup();
343
        LineGeometry connectorGeometry = new LineGeometry();
344

    
345
        #endregion
346
        public override void OnApplyTemplate()
347
        {
348
            base.OnApplyTemplate();
349
            Base_LinePath = GetTemplateChild("PART_LinePath") as Path;
350

    
351
        }
352
        public void updateControl()
353
        {
354
            this.StartPoint = new Point(this.PointSet[0].X, this.PointSet[0].Y);
355
            this.EndPoint = new Point(this.PointSet[1].X, this.PointSet[1].Y);
356
        }
357
        public static PathGeometry SingleAllow(Point p2, Point p1, double lineSize)
358
        {
359
            double theta = Math.Atan2((p2.Y - p1.Y), (p2.X - p1.X)) * 180 / Math.PI;
360
            PathGeometry pathGeometry = new PathGeometry();
361
            PathFigure pathFigure = new PathFigure();
362
            pathFigure.StartPoint = p1;
363

    
364
            Point lpoint = new Point(p1.X + lineSize * 3, p1.Y + lineSize * 6);
365
            Point rpoint = new Point(p1.X - lineSize * 3, p1.Y + lineSize * 6);
366

    
367
            LineSegment seg1 = new LineSegment();
368
            seg1.Point = lpoint;
369
            pathFigure.Segments.Add(seg1);
370

    
371
            LineSegment seg2 = new LineSegment();
372
            seg2.Point = rpoint;
373
            pathFigure.Segments.Add(seg2);
374

    
375
            LineSegment seg3 = new LineSegment();
376
            seg3.Point = p1;
377
            pathFigure.Segments.Add(seg3);
378

    
379
            pathFigure.IsClosed = true;
380
            pathFigure.IsFilled = true;
381

    
382
            pathGeometry.Figures.Add(pathFigure);
383
            pathGeometry.FillRule = FillRule.Nonzero;
384
            RotateTransform transform = new RotateTransform();
385
            transform.Angle = theta - 90;
386
            transform.CenterX = p1.X;
387
            transform.CenterY = p1.Y;
388
            pathGeometry.Transform = transform;
389
            return pathGeometry;
390
        }
391
        public static PathGeometry ConverseAllow(Point p2, Point p1, double lineSize)
392
        {
393
            double theta = Math.Atan2((p2.Y - p1.Y), (p2.X - p1.X)) * 180 / Math.PI;
394
            PathGeometry pathGeometry2 = new PathGeometry();
395
            PathFigure pathFigure2 = new PathFigure();
396
            pathFigure2.StartPoint = p2;
397

    
398
            Point lpoint2 = new Point(p2.X + lineSize * 3, p2.Y + lineSize * 6);
399
            Point rpoint2 = new Point(p2.X - lineSize * 3, p2.Y + lineSize * 6);
400
            LineSegment seg1_1 = new LineSegment();
401
            seg1_1.Point = lpoint2;
402
            pathFigure2.Segments.Add(seg1_1);
403

    
404
            LineSegment seg2_1 = new LineSegment();
405
            seg2_1.Point = rpoint2;
406
            pathFigure2.Segments.Add(seg2_1);
407

    
408
            LineSegment seg3_1 = new LineSegment();
409
            seg3_1.Point = p2;
410
            pathFigure2.Segments.Add(seg3_1);
411

    
412
            LineSegment seg4_1 = new LineSegment();
413

    
414
            seg4_1.Point = new Point(lpoint2.X, lpoint2.Y);
415
            pathFigure2.Segments.Add(seg4_1);
416
            RotateTransform transform2 = new RotateTransform();
417
            transform2.Angle = theta + 90;
418
            transform2.CenterX = p2.X;
419
            transform2.CenterY = p2.Y;
420
            pathGeometry2.Figures.Add(pathFigure2);
421
            pathGeometry2.Transform = transform2;
422
            return pathGeometry2;
423
        }
424
        public static List<LineGeometry> DimAllow(Point p1, Point p2, Double lineSize, double DimSize)
425
        {
426
            List<LineGeometry> GeometrySet = new List<LineGeometry>();
427
            double theta = Math.Atan2((p2.Y - p1.Y), (p2.X - p1.X)) * 180 / Math.PI;
428

    
429

    
430
            LineGeometry ld = new LineGeometry();
431
            //ld.StartPoint = new Point(p2.X + lineSize, p2.Y - lineSize * 4 - DimSize);
432
            //ld.EndPoint = new Point(p2.X + lineSize, p2.Y + lineSize * 4 + DimSize);
433
            ld.StartPoint = new Point(p2.X + lineSize, p2.Y - lineSize * 4);
434
            ld.EndPoint = new Point(p2.X + lineSize, p2.Y + lineSize * 4 + DimSize);
435

    
436
            RotateTransform transform3 = new RotateTransform();
437
            transform3.Angle = theta;
438
            transform3.CenterX = p2.X;
439
            transform3.CenterY = p2.Y;
440
            ld.Transform = transform3;
441
            GeometrySet.Add(ld);
442

    
443
            LineGeometry ld1 = new LineGeometry();
444
            //ld1.StartPoint = new Point(p1.X - lineSize, p1.Y - lineSize * 4 - DimSize);
445
            //ld1.EndPoint = new Point(p1.X - lineSize, p1.Y + lineSize * 4 + DimSize);
446
            ld1.StartPoint = new Point(p1.X - lineSize, p1.Y - lineSize * 4);
447
            ld1.EndPoint = new Point(p1.X - lineSize, p1.Y + lineSize * 4 + DimSize);
448

    
449
            RotateTransform transform4 = new RotateTransform();
450
            transform4.Angle = theta;
451
            transform4.CenterX = p1.X;
452
            transform4.CenterY = p1.Y;
453
            ld1.Transform = transform4;
454
            GeometrySet.Add(ld1);
455
            return GeometrySet;
456
        }
457

    
458

    
459
        public void ApplyOverViewData()
460
        {
461
            this.OverViewPathData = this.PathData;
462
        }
463

    
464
        public void SetLinePath()
465
        {
466
            this.ApplyTemplate();           
467
            if (this.DashSize != null)
468
            {
469
                Base_LinePath.StrokeDashArray.Clear();
470
                foreach (var item in this.DashSize)
471
                {
472
                    Base_LinePath.StrokeDashArray.Add(item);
473
                }
474
                Base_LinePath.StrokeDashCap = PenLineCap.Square;
475
            }
476

    
477
            PathFigure pathFigure = new PathFigure(); 
478
            pathFigure.StartPoint = this.StartPoint;
479
            LineSegment lineSegment0 = new LineSegment();
480
            lineSegment0.Point = this.EndPoint;
481
            pathFigure.Segments.Add(lineSegment0);
482
            PathGeometry pathGeometry = new PathGeometry();
483
            pathGeometry.Figures = new PathFigureCollection();
484
            pathGeometry.Figures.Add(pathFigure);
485

    
486
            instanceGroup.Children.Clear();
487

    
488
            switch (ControlType)
489
            {
490
                case ControlType.ArrowLine:
491
                    instanceGroup.Children.Add(SingleAllow(this.StartPoint, this.EndPoint, this.LineSize));
492
                    break;
493
                case ControlType.TwinLine:
494
                    instanceGroup.Children.Add(SingleAllow(this.StartPoint, this.EndPoint, this.LineSize));
495
                    instanceGroup.Children.Add(ConverseAllow(this.StartPoint, this.EndPoint, this.LineSize));
496
                    break;
497
                case ControlType.DimLine:
498
                    List<LineGeometry> metrySet = DimAllow(this.StartPoint, this.EndPoint, this.LineSize, this.DimSize);
499
                    instanceGroup.Children.Add(metrySet[0]);
500
                    instanceGroup.Children.Add(metrySet[1]);
501
                    instanceGroup.Children.Add(SingleAllow(this.StartPoint, this.EndPoint, this.LineSize));
502
                    instanceGroup.Children.Add(ConverseAllow(this.StartPoint, this.EndPoint, this.LineSize));
503
                    break;
504
                case ControlType.CancelLine:
505
                    PathFigure pathFigure_Multi = new PathFigure();
506
                    LineSegment lineSegment1 = new LineSegment();
507

    
508
                    var x = Math.Abs((Math.Abs(this.StartPoint.X) - Math.Abs(this.EndPoint.X)));
509
                    var y = Math.Abs((Math.Abs(this.StartPoint.Y) - Math.Abs(this.EndPoint.Y)));
510

    
511
                    if (x > y)
512
                    {
513
                        pathFigure_Multi.StartPoint = new Point(this.StartPoint.X, this.StartPoint.Y + (this.Interval));
514
                        lineSegment1.Point = new Point(this.EndPoint.X, this.EndPoint.Y + (this.Interval));
515
                    }
516
                    else
517
                    {
518
                        pathFigure_Multi.StartPoint = new Point(this.StartPoint.X + (this.Interval), this.StartPoint.Y);
519
                        lineSegment1.Point = new Point(this.EndPoint.X + (this.Interval), this.EndPoint.Y);
520
                    }
521
                    pathFigure_Multi.Segments.Add(lineSegment1);
522
                    pathGeometry.Figures.Add(pathFigure_Multi);
523
                    
524
                    this.PathData = pathGeometry;
525
                    break;
526
                default:
527
                    break;
528
            }
529

    
530
            if (PathData != pathGeometry)
531
            {
532
                connectorGeometry.StartPoint = new Point(this.StartPoint.X, this.StartPoint.Y);
533
                connectorGeometry.EndPoint = new Point(this.EndPoint.X, this.EndPoint.Y);
534

    
535
                instanceGroup.Children.Add(connectorGeometry);
536
                this.PathData = instanceGroup;
537
                this.OverViewPathData = PathData;
538
            }
539
        }
540
    }
541
}
클립보드 이미지 추가 (최대 크기: 500 MB)