프로젝트

일반

사용자정보

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

markus / MarkupToPDF / Controls / Etc / SymControl.cs @ 0d00f9c8

이력 | 보기 | 이력해설 | 다운로드 (20.9 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
using MarkupToPDF.Common;
15
using MarkupToPDF.Serialize.Core;
16
using MarkupToPDF.Serialize.S_Control;
17
using System.Linq;
18

    
19
namespace MarkupToPDF.Controls.Etc
20
{
21
    [TemplatePart(Name = "PART_SymPath", Type = typeof(Path))]
22
    [TemplatePart(Name = "PART_ViewBox", Type = typeof(Viewbox))]
23

    
24
    public class SymControl : CommentUserInfo, IDisposable, INotifyPropertyChanged, IPath, IViewBox, IMarkupCommonData
25
    {
26
        #region 초기선언
27
        public enum SymStyleSet { None, Fill };
28
        private const string PART_ViewBox = "PART_ViewBox";
29
        private const string PART_SymPath = "PART_SymPath";
30
        public Viewbox Base_ViewBox = null;
31
        public Path Base_SymPath = null;
32
        #endregion
33

    
34
        static SymControl()
35
        {
36
            DefaultStyleKeyProperty.OverrideMetadata(typeof(SymControl), new FrameworkPropertyMetadata(typeof(SymControl)));
37
            //Application.Current.Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri("/MarkupToPDF;Component/Themes/generic.xaml")) as ResourceDictionary);
38
            ResourceDictionary dictionary = new ResourceDictionary();
39
            dictionary.Source = new Uri("/MarkupToPDF;component/themes/generic.xaml", UriKind.RelativeOrAbsolute);
40
            Application.Current.Resources.MergedDictionaries.Add(dictionary);
41
            System.Diagnostics.Debug.WriteLine("resource Count :" + Application.Current.Resources.MergedDictionaries.Count);
42
        }
43

    
44
        public void Dispose()
45
        {
46
            GC.Collect();
47
            GC.SuppressFinalize(this);
48
        }
49
        public event PropertyChangedEventHandler PropertyChanged;
50
        protected void RaisePropertyChanged(string propName)
51
        {
52
            if (PropertyChanged != null)
53
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
54
        }
55
        #region Dependency Properties
56
        public static readonly DependencyProperty UserIDProperty = DependencyProperty.Register(
57
                "UserID", typeof(string), typeof(SymControl), new PropertyMetadata(null));
58
        public static readonly DependencyProperty LineSizeProperty = DependencyProperty.Register(
59
                "LineSize", typeof(double), typeof(SymControl), new PropertyMetadata((Double)1));
60
        public static readonly DependencyProperty StrokeColorProperty = DependencyProperty.Register(
61
                "StrokeColor", typeof(SolidColorBrush), typeof(SymControl), new PropertyMetadata(new SolidColorBrush(Colors.Red)));
62
        public static readonly DependencyProperty PathDataProperty = DependencyProperty.Register(
63
                "PathData", typeof(Geometry), typeof(SymControl), null);
64
        public static readonly DependencyProperty EndPointProperty = DependencyProperty.Register(
65
                "EndPoint", typeof(Point), typeof(SymControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
66
        public static readonly DependencyProperty StartPointProperty = DependencyProperty.Register(
67
                "StartPoint", typeof(Point), typeof(SymControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
68
        public static readonly DependencyProperty TopRightPointProperty = DependencyProperty.Register(
69
                "TopRightPoint", typeof(Point), typeof(SymControl), new PropertyMetadata(new Point(0, 0), TRPointValueChanged));
70
        public static readonly DependencyProperty LeftBottomPointProperty = DependencyProperty.Register(
71
                 "LeftBottomPoint", typeof(Point), typeof(SymControl), new PropertyMetadata(new Point(0, 0), LBPointValueChanged));
72
        public static readonly DependencyProperty PointSetProperty = DependencyProperty.Register(
73
                 "PointSet", typeof(List<Point>), typeof(SymControl), new PropertyMetadata(new List<Point>()));
74
        public static readonly DependencyProperty AngleProperty = DependencyProperty.Register("Angle", typeof(double), typeof(SymControl),
75
            new PropertyMetadata((double)0.0, new PropertyChangedCallback(AngleValueChanged)));
76
        public static readonly DependencyProperty CenterXProperty = DependencyProperty.Register("CenterX", typeof(double), typeof(SymControl),
77
            new PropertyMetadata((double)0, OnCenterXYChanged));
78
        public static readonly DependencyProperty CenterYProperty = DependencyProperty.Register("CenterY", typeof(double), typeof(SymControl),
79
            new PropertyMetadata((double)0, OnCenterXYChanged));
80
        public static readonly DependencyProperty PaintProperty = DependencyProperty.Register(
81
                "Paint", typeof(PaintSet), typeof(SymControl), new PropertyMetadata(PaintSet.None));
82

    
83
        public static readonly DependencyProperty IsSelectedProperty =
84
     DependencyProperty.Register("IsSelected", typeof(bool), typeof(SymControl), new FrameworkPropertyMetadata(false, IsSelectedChanged));
85

    
86
        public static readonly DependencyProperty ControlTypeProperty =
87
            DependencyProperty.Register("ControlType", typeof(ControlType), typeof(SymControl), new FrameworkPropertyMetadata(ControlType.Symbol));
88
        #endregion
89
        #region PropertyChanged Method
90
        public static void PointValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
91
        {
92
            var instance = (SymControl)sender;
93
            if (e.OldValue != e.NewValue && instance.Base_SymPath != null)
94
            {
95
                instance.SetValue(e.Property, e.NewValue);
96
                instance.SetSymPath();
97
            }
98
        }
99

    
100
        public static void TRPointValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
101
        {
102
            var instance = (SymControl)sender;
103
            if (e.OldValue != e.NewValue && instance.Base_SymPath != null)
104
            {
105
                instance.SetValue(e.Property, e.NewValue);
106
                instance.SetSymPath();
107
            }
108
        }
109
        public static void IsSelectedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
110
        {
111

    
112
        }
113
        public static void OnCenterXYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
114
        {
115
            var instance = (SymControl)sender;
116
            if (e.OldValue != e.NewValue && instance.Base_SymPath != null)
117
            {
118
                instance.SetValue(e.Property, e.NewValue);
119
                instance.SetSymPath();
120
            }
121
        }
122

    
123
        public static void LBPointValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
124
        {
125
            var instance = (SymControl)sender;
126
            if (e.OldValue != e.NewValue && instance.Base_SymPath != null)
127
            {
128
                instance.SetValue(e.Property, e.NewValue);
129
                //instance.EndPoint = new Point(instance.EndPoint.X, ((Point)e.NewValue).Y);
130
                //instance.StartPoint = new Point(((Point)e.NewValue).X, instance.StartPoint.Y);
131
                ////instance.PointSet[0] = instance.StartPoint;
132
                ////instance.PointSet[2] = instance.EndPoint;
133
                instance.SetSymPath();
134
            }
135
        }
136

    
137
        public static void AngleValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
138
        {
139
            var instance = (SymControl)sender;
140
            if (e.OldValue != e.NewValue && instance.Base_SymPath != null)
141
            {
142
                instance.SetValue(e.Property, e.NewValue);
143
                instance.SetSymPath();
144
            }
145
        }
146
        #endregion
147
        #region Properties
148
        public string UserID
149
        {
150
            get { return (string)GetValue(UserIDProperty); }
151
            set
152
            {
153
                if (this.UserID != value)
154
                {
155
                    SetValue(UserIDProperty, value);
156
                    RaisePropertyChanged("UserID");
157
                }
158
            }
159
        }
160
        public Double LineSize
161
        {
162
            get { return (Double)GetValue(LineSizeProperty); }
163
            set
164
            {
165
                if (this.LineSize != value)
166
                {
167
                    SetValue(LineSizeProperty, value);
168
                }
169
            }
170
        }
171
        public PaintSet Paint
172
        {
173
            get { return (PaintSet)GetValue(PaintProperty); }
174
            set
175
            {
176
                if (this.Paint != value)
177
                {
178
                    SetValue(PaintProperty, value);
179
                }
180
            }
181
        }
182
        public List<Point> PointSet
183
        {
184
            get { return (List<Point>)GetValue(PointSetProperty); }
185
            set { SetValue(PointSetProperty, value); }
186
        }
187
        public double Angle
188
        {
189
            get { return (double)GetValue(AngleProperty); }
190
            set
191
            {
192
                if (this.Angle != value)
193
                {
194
                    SetValue(AngleProperty, value);
195
                }
196
            }
197
        }
198
        public SolidColorBrush StrokeColor
199
        {
200
            get { return (SolidColorBrush)GetValue(StrokeColorProperty); }
201
            set
202
            {
203
                if (this.StrokeColor != value)
204
                {
205
                    SetValue(StrokeColorProperty, value);
206
                }
207
            }
208
        }
209
        public Geometry PathData
210
        {
211
            get { return (Geometry)GetValue(PathDataProperty); }
212
            set
213
            {
214
                SetValue(PathDataProperty, value);
215
                RaisePropertyChanged("PathData");
216
            }
217
        }
218
        public Point TopRightPoint
219
        {
220
            get { return (Point)GetValue(TopRightPointProperty); }
221
            set
222
            {
223
                SetValue(TopRightPointProperty, value);
224
                RaisePropertyChanged("TopRightPoint");
225
            }
226
        }
227
        public Point LeftBottomPoint
228
        {
229
            get { return (Point)GetValue(LeftBottomPointProperty); }
230
            set
231
            {
232
                SetValue(LeftBottomPointProperty, value);
233
                RaisePropertyChanged("LeftBottomPoint");
234
            }
235
        }
236
        public Point EndPoint
237
        {
238
            get { return (Point)GetValue(EndPointProperty); }
239
            set
240
            {
241
                SetValue(EndPointProperty, value);
242
                RaisePropertyChanged("EndPoint");
243
            }
244
        }
245
        public Point StartPoint
246
        {
247
            get { return (Point)GetValue(StartPointProperty); }
248
            set
249
            {
250
                SetValue(StartPointProperty, value);
251
                RaisePropertyChanged("StartPoint");
252
            }
253
        }
254
        public double CenterX
255
        {
256
            get { return (double)GetValue(CenterXProperty); }
257
            set { SetValue(CenterXProperty, value); }
258
        }
259
        public double CenterY
260
        {
261
            get { return (double)GetValue(CenterYProperty); }
262
            set { SetValue(CenterYProperty, value); }
263
        }
264
        public double AngleValue
265
        {
266
            get { return (double)GetValue(AngleProperty); }
267
            set { SetValue(AngleProperty, value); }
268
        }
269
        public override bool IsSelected
270
        {
271
            get
272
            {
273
                return (bool)GetValue(IsSelectedProperty);
274
            }
275
            set
276
            {
277
                SetValue(IsSelectedProperty, value);
278
            }
279
        }
280

    
281
        public override ControlType ControlType
282
        {
283
            set
284
            {
285
                SetValue(ControlTypeProperty, value);
286
            }
287
            get
288
            {
289
                return (ControlType)GetValue(ControlTypeProperty);
290
            }
291
        }
292

    
293
        #endregion
294
        public override void OnApplyTemplate()
295
        {
296
            base.OnApplyTemplate();
297
            Base_ViewBox = GetTemplateChild(PART_ViewBox) as Viewbox;
298
            Base_SymPath = GetTemplateChild(PART_SymPath) as Path;
299
            SetSymPath();
300
        }
301

    
302
        private void SetSymPath()
303
        {
304
            this.ApplyTemplate();
305
            switch (this.Paint)
306
            {
307
                case PaintSet.None:
308
                    Base_SymPath.Fill = null;
309
                    break;
310
                case PaintSet.Fill:
311
                    Base_SymPath.Fill = this.StrokeColor;
312
                    break;
313
                default:
314
                    break;
315
            }
316

    
317
            Point mid = MathSet.FindCentroid(new List<Point>()
318
            {
319
                this.StartPoint,
320
                this.LeftBottomPoint,
321
                this.EndPoint,
322
                this.TopRightPoint,
323
            });
324

    
325
            double AngleData = this.Angle * -1;
326

    
327
            PathFigure pathFigure = new PathFigure();
328
            pathFigure.StartPoint = MathSet.RotateAbout(mid, this.StartPoint, AngleData);
329

    
330
            LineSegment lineSegment0 = new LineSegment();
331
            lineSegment0.Point = MathSet.RotateAbout(mid, this.StartPoint, AngleData);
332
            pathFigure.Segments.Add(lineSegment0);
333

    
334
            LineSegment lineSegment1 = new LineSegment();
335
            lineSegment1.Point = MathSet.RotateAbout(mid, this.LeftBottomPoint, AngleData);
336
            pathFigure.Segments.Add(lineSegment1);
337

    
338
            LineSegment lineSegment2 = new LineSegment();
339
            lineSegment2.Point = MathSet.RotateAbout(mid, this.EndPoint, AngleData);
340
            pathFigure.Segments.Add(lineSegment2);
341

    
342
            LineSegment lineSegment3 = new LineSegment();
343
            lineSegment3.Point = MathSet.RotateAbout(mid, this.TopRightPoint, AngleData);
344
            pathFigure.Segments.Add(lineSegment3);
345

    
346
            PathGeometry pathGeometry = new PathGeometry();
347
            pathGeometry.Figures = new PathFigureCollection();
348
            pathFigure.IsClosed = true;
349
            pathGeometry.Figures.Add(pathFigure);
350
            this.Base_ViewBox.Width = pathGeometry.Bounds.Width;
351
            this.Base_ViewBox.Height = pathGeometry.Bounds.Height; ;
352
            this.Tag = pathGeometry;
353

    
354
            Canvas.SetLeft(this, MathSet.RotateAbout(mid, mid, AngleData).X - this.Base_ViewBox.Width / 2);
355
            Canvas.SetTop(this, MathSet.RotateAbout(mid, mid, AngleData).Y - this.Base_ViewBox.Height / 2);
356

    
357
        }
358

    
359
        public override void UpdateControl()
360
        {
361
            this.StartPoint = new Point(this.PointSet[0].X, this.PointSet[0].Y);
362
            this.LeftBottomPoint = new Point(this.PointSet[1].X, this.PointSet[1].Y);
363
            this.EndPoint = new Point(this.PointSet[2].X, this.PointSet[2].Y);
364
            this.TopRightPoint = new Point(this.PointSet[3].X, this.PointSet[3].Y);
365
            this.SetSymPath();
366
        }
367

    
368

    
369
        public void ChangePaint(PaintSet state)
370
        {
371
            this.Paint = state;
372
            this.SetSymPath();
373
        }
374

    
375
        /// <summary>
376
        /// call when mouse is moving while drawing control
377
        /// </summary>
378
        /// <author>humkyung</author>
379
        /// <param name="pt"></param>
380
        /// <param name="bAxisLocked"></param>
381
        public override void OnCreatingMouseMove(Point pt, bool bAxisLocked, bool bShiftKeyPressed)
382
        {
383
            this.EndPoint = pt;
384
            this.LeftBottomPoint = new Point(this.StartPoint.X, this.EndPoint.Y);
385
            this.TopRightPoint = new Point(this.EndPoint.X, this.StartPoint.Y);
386
            
387
            this.StrokeColor = new SolidColorBrush(Colors.Red);
388

    
389
            if (this.StartPoint != this.EndPoint)
390
            {
391
                if (this.PathData == null)
392
                {
393
                    using (StringToPathConverter Convert = new StringToPathConverter())
394
                    {
395
                        this.PathData = Convert.Convert("M-5,5L0,0L20,30L40,-20 ");
396
                    }
397
                }
398
            }
399

    
400
            this.PointSet = new List<Point>
401
            {
402
                this.StartPoint,
403
                this.LeftBottomPoint,
404
                this.EndPoint,
405
                this.TopRightPoint,
406
            };
407
        }
408

    
409
        /// <summary>
410
        /// move control point has same location of given pt along given delta
411
        /// </summary>
412
        /// <author>humkyung</author>
413
        /// <date>2019.06.20</date>
414
        /// <param name="pt"></param>
415
        /// <param name="dx"></param>
416
        /// <param name="dy"></param>
417
        public override void OnMoveCtrlPoint(Point pt, double dx, double dy)
418
        {
419
            IPath path = (this as IPath);
420

    
421
            Point selected = MathSet.getNearPoint(path.PointSet, pt);
422
            selected.X += dx;
423
            selected.Y += dy;
424
            int i = 0;
425
            for (i = 0; i < (this as IPath).PointSet.Count; i++)
426
            {
427
                if (pt.Equals((this as IPath).PointSet[i]))
428
                {
429
                    path.PointSet[i] = selected;
430
                    break;
431
                }
432
            }
433

    
434
            var ReverseP = (i + path.PointSet.Count / 2) % path.PointSet.Count;
435
            var PreviousP = (i + (path.PointSet.Count - 1)) % path.PointSet.Count;
436
            var NextP = (i + 1) % path.PointSet.Count;
437

    
438
            var distance = MathSet.DistanceTo(path.PointSet[ReverseP], path.PointSet[i]);
439

    
440
            var PreviousV = MathSet.GetNormVectorBetween(path.PointSet[ReverseP], path.PointSet[PreviousP]);
441
            var l = MathSet.DotProduct(PreviousV.X, PreviousV.Y, path.PointSet[i].X - path.PointSet[ReverseP].X,
442
                path.PointSet[i].Y - path.PointSet[ReverseP].Y);
443
            path.PointSet[PreviousP] = new Point(path.PointSet[ReverseP].X + PreviousV.X * l, path.PointSet[ReverseP].Y + PreviousV.Y * l);
444

    
445
            var NextV = MathSet.GetNormVectorBetween(path.PointSet[ReverseP], path.PointSet[NextP]);
446
            l = MathSet.DotProduct(NextV.X, NextV.Y, path.PointSet[i].X - path.PointSet[ReverseP].X, path.PointSet
447
                [i].Y - path.PointSet[ReverseP].Y);
448
            path.PointSet[NextP] = new Point(path.PointSet[ReverseP].X + NextV.X * l, path.PointSet[ReverseP].Y + NextV.Y * l);
449

    
450
            this.UpdateControl();
451
        }
452

    
453
        /// <summary>
454
        /// return SymControl's area
455
        /// </summary>
456
        /// <author>humkyung</author>
457
        /// <date>2019.06.13</date>
458
        public override Rect ItemRect
459
        {
460
            get
461
            {
462
                double dMinX = Math.Min(this.StartPoint.X, this.EndPoint.X);
463
                double dMinY = Math.Min(this.StartPoint.Y, this.EndPoint.Y);
464
                double dMaxX = Math.Max(this.StartPoint.X, this.EndPoint.X);
465
                double dMaxY = Math.Max(this.StartPoint.Y, this.EndPoint.Y);
466

    
467
                return new Rect(new Point(dMinX, dMinY), new Point(dMaxX, dMaxY));
468
            }
469
        }
470

    
471
        /// <summary>
472
        /// Serialize this
473
        /// </summary>
474
        /// <param name="sUserId"></param>
475
        /// <returns></returns>
476
        public override string Serialize()
477
        {
478
            using (S_SymControl STemp = new S_SymControl())
479
            {
480
                STemp.TransformPoint = "0|0";
481
                STemp.PointSet = this.PointSet;
482
                STemp.UserID = this.UserID;
483
                STemp.SizeSet = String.Format("{0}", this.LineSize.ToString());
484
                STemp.PaintState = this.Paint;
485
                STemp.PathInfo = this.PathData.ToString();
486
                STemp.StrokeColor = this.StrokeColor.Color.ToString();
487
                STemp.StartPoint = this.StartPoint;
488
                STemp.Angle = this.Angle;
489
                STemp.EndPoint = this.EndPoint;
490
                STemp.LB = this.LeftBottomPoint;
491
                STemp.TR = this.TopRightPoint;
492
                STemp.Opac = this.Opacity;
493
                STemp.Name = this.GetType().Name.ToString();
494

    
495
                return "|DZ|" + JsonSerializerHelper.CompressString((STemp.JsonSerialize()));
496
            }
497
        }
498

    
499
        /// <summary>
500
        /// create a symcontrol from given string
501
        /// </summary>
502
        /// <param name="str"></param>
503
        /// <returns></returns>
504
        public static SymControl FromString(string str, SolidColorBrush brush, string sProjectNo)
505
        {
506
            SymControl instance = null;
507
            using (S_SymControl s = JsonSerializerHelper.JsonDeserialize<S_SymControl>(str))
508
            {
509
                string[] data2 = s.SizeSet.Split(delimiterChars2, StringSplitOptions.RemoveEmptyEntries);
510
                Common.StringToPathConverter sm = new Common.StringToPathConverter();
511
                instance = new SymControl
512
                {
513
                    LineSize = Convert.ToDouble(data2.First()),
514
                    PointSet = s.PointSet,
515
                    Paint = s.PaintState,
516
                    StartPoint = s.StartPoint,
517
                    StrokeColor = brush,
518
                    EndPoint = s.EndPoint,
519
                    Angle = s.Angle,
520
                    UserID = s.UserID,
521
                    LeftBottomPoint = s.LB,
522
                    TopRightPoint = s.TR,
523
                    PathData = sm.Convert(s.PathInfo.ToString()),
524
                    Opacity = s.Opac,
525
                    Memo = s.Memo
526
                };
527
            }
528

    
529
            return instance;
530
        }
531
    }
532
}
클립보드 이미지 추가 (최대 크기: 500 MB)