프로젝트

일반

사용자정보

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

markus / MarkupToPDF / Controls / Etc / SignControl.cs @ 08be599f

이력 | 보기 | 이력해설 | 다운로드 (22.5 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.Collections.Generic;
12
using System.ComponentModel;
13
using System.Linq;
14
using MarkupToPDF.Controls.Common;
15
using MarkupToPDF.Common;
16
using MarkupToPDF.Serialize.Core;
17
using MarkupToPDF.Serialize.S_Control;
18
using System.Windows.Media.Imaging;
19
using System.Threading.Tasks;
20
using static System.Windows.Forms.AxHost;
21

    
22
//강인구 추가
23
namespace MarkupToPDF.Controls.Etc
24
{
25
    [TemplatePart(Name = "PART_Image", Type = typeof(Image))]
26
    public class SignControl : CommentUserInfo, IDisposable, INormal, INotifyPropertyChanged, IViewBox
27
    {
28
        #region 초기선언
29
        public string ProjectNO { get; set; }
30

    
31
        private const string PART_Image = "PART_Image";
32
        public Image Base_Image = null;
33
        #endregion
34

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

    
45
        public override void Copy(CommentUserInfo lhs)
46
        {
47
            if(lhs is SignControl item)
48
            {
49
                this.CommentAngle = item.CommentAngle;
50
                this.StartPoint = new Point(item.StartPoint.X, item.StartPoint.Y);
51
                this.TopRightPoint = new Point(item.TopRightPoint.X, item.TopRightPoint.Y);
52
                this.EndPoint = new Point(item.EndPoint.X, item.EndPoint.Y);
53
                this.LeftBottomPoint = new Point(item.LeftBottomPoint.X, item.LeftBottomPoint.Y);
54
                this.PointSet = item.PointSet.ConvertAll(x => new Point(x.X, x.Y));
55
                this.Opacity = item.Opacity;
56
                this.SignImage = item.SignImage;
57
                this.UserID = item.UserID;
58
                this.UserNumber = item.UserNumber;
59
                this.Memo = item.Memo;
60
            }
61
        }
62

    
63
        public override CommentUserInfo Clone()
64
        {
65
            var clone = new SignControl();
66
            clone.Copy(this);
67
            return clone;
68
        }
69

    
70
        public void Dispose()
71
        {
72
            //GC.Collect();
73
            ////GC.SuppressFinalize(this);
74
            this.Base_Image = null;
75
        }
76
        #region Dependency Properties
77
        public static readonly DependencyProperty UserIDProperty = DependencyProperty.Register(
78
                "UserID", typeof(string), typeof(SignControl), new PropertyMetadata(null));
79
        public static readonly DependencyProperty SignImageProperty = DependencyProperty.Register(
80
            "SignImage", typeof(ImageSource), typeof(SignControl), new PropertyMetadata(null));
81
        public static readonly DependencyProperty StartPointProperty = DependencyProperty.Register(
82
            "StartPoint", typeof(Point), typeof(SignControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
83
        public static readonly DependencyProperty EndPointProperty = DependencyProperty.Register(
84
            "EndPoint", typeof(Point), typeof(SignControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
85
        public static readonly DependencyProperty PointSetProperty = DependencyProperty.Register(
86
            "PointSet", typeof(List<Point>), typeof(SignControl), new PropertyMetadata(new List<Point>()));
87
        public static readonly DependencyProperty UserNubmerProperty = DependencyProperty.Register(
88
            "UserNumber", typeof(string), typeof(SignControl), new PropertyMetadata(null));
89
        public static readonly DependencyProperty TopRightPointProperty = DependencyProperty.Register(
90
             "TopRightPoint", typeof(Point), typeof(SignControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
91
        public static readonly DependencyProperty LeftBottomPointProperty = DependencyProperty.Register(
92
              "LeftBottomPoint", typeof(Point), typeof(SignControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
93
        public static readonly DependencyProperty AngleProperty = DependencyProperty.Register("Angle", typeof(double), typeof(SignControl),
94
                        new PropertyMetadata((double)0.0, new PropertyChangedCallback(AngleValueChanged)));
95
        public static readonly DependencyProperty CenterXProperty = DependencyProperty.Register("CenterX", typeof(double), typeof(SignControl),
96
                        new PropertyMetadata((double)0, OnCenterXYChanged));
97
        public static readonly DependencyProperty CenterYProperty = DependencyProperty.Register("CenterY", typeof(double), typeof(SignControl),
98
                        new PropertyMetadata((double)0, OnCenterXYChanged));
99
        public static readonly DependencyProperty IsSelectedProperty =
100
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(SignControl), new FrameworkPropertyMetadata(false, IsSelectedChanged));
101

    
102
        public static readonly DependencyProperty ControlTypeProperty =
103
            DependencyProperty.Register("ControlType", typeof(ControlType), typeof(SignControl), new FrameworkPropertyMetadata(ControlType.Sign));
104
        #endregion
105
        #region PropertyChanged Method
106
        public static void PointValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
107
        {
108
            var instance = (SignControl)sender;
109
            if (e.OldValue != e.NewValue && instance.Base_Image != null)
110
            {
111
                instance.SetValue(e.Property, e.NewValue);
112
                instance.SetImage();
113
            }
114
        }
115
        public static void IsSelectedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
116
        {
117

    
118
        }
119
            public static void OnCenterXYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
120
        {
121
            var instance = (SignControl)sender;
122
            if (e.OldValue != e.NewValue && instance.Base_Image != null)
123
            {
124
                instance.SetValue(e.Property, e.NewValue);
125
                instance.SetImage();
126
            }
127
        }
128
        public static void AngleValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
129
        {
130
            var instance = (SignControl)sender;
131
            if (e.OldValue != e.NewValue && instance.Base_Image != null)
132
            {
133
                instance.SetValue(e.Property, e.NewValue);
134
                instance.SetImage();
135
            }
136
        }
137
        #endregion
138
        #region Properties
139
        public string UserID
140
        {
141
            get { return (string)GetValue(UserIDProperty); }
142
            set
143
            {
144
                if (this.UserID != value)
145
                {
146
                    SetValue(UserIDProperty, value);
147
                    RaisePropertyChanged("UserID");
148
                }
149
            }
150
        }
151

    
152
        public double CenterX
153
        {
154
            get { return (double)GetValue(CenterXProperty); }
155
            set { SetValue(CenterXProperty, value); }
156
        }
157
        public double CenterY
158
        {
159
            get { return (double)GetValue(CenterYProperty); }
160
            set { SetValue(CenterYProperty, value); }
161
        }
162
        public Point EndPoint
163
        {
164
            get { return (Point)GetValue(EndPointProperty); }
165
            set
166
            {
167
                if (this.EndPoint != value)
168
                {
169
                    SetValue(EndPointProperty, value);
170
                    RaisePropertyChanged("EndPoint");
171
                }
172
            }
173
        }
174
        public List<Point> PointSet
175
        {
176
            get { return (List<Point>)GetValue(PointSetProperty); }
177
            set { SetValue(PointSetProperty, value); }
178
        }
179
        public Point StartPoint
180
        {
181
            get { return (Point)GetValue(StartPointProperty); }
182
            set
183
            {
184
                if (this.StartPoint != value)
185
                {
186
                    SetValue(StartPointProperty, value);
187
                    RaisePropertyChanged("StartPoint");
188
                }
189
            }
190
        }
191
        public string UserNumber
192
        {
193
            get { return (string)GetValue(UserNubmerProperty); }
194
            set
195
            {
196
                if (this.UserNumber != value)
197
                {
198
                    SetValue(UserNubmerProperty, value);
199
                    RaisePropertyChanged("UserNumber");
200
                }
201
            }
202
        }
203
        public Point TopRightPoint
204
        {
205
            get { return (Point)GetValue(TopRightPointProperty); }
206
            set
207
            {
208
                SetValue(TopRightPointProperty, value);
209
                RaisePropertyChanged("TopRightPoint");
210
            }
211
        }
212
        public Point LeftBottomPoint
213
        {
214
            get { return (Point)GetValue(LeftBottomPointProperty); }
215
            set
216
            {
217
                SetValue(LeftBottomPointProperty, value);
218
                RaisePropertyChanged("LeftBottomPoint");
219
            }
220
        }
221
        public override double CommentAngle
222
        {
223
            get { return (double)GetValue(AngleProperty); }
224
            set
225
            {
226
                if (this.CommentAngle != value)
227
                {
228
                    SetValue(AngleProperty, value);
229
                }
230
            }
231
        }
232
        public ImageSource SignImage
233
        {
234
            get { return (ImageSource)this.GetValue(SignImageProperty); }
235
            set { this.SetValue(SignImageProperty, value); }
236
        }
237

    
238
        public override bool IsSelected
239
        {
240
            get
241
            {
242
                return (bool)GetValue(IsSelectedProperty);
243
            }
244
            set
245
            {
246
                SetValue(IsSelectedProperty, value);
247
            }
248
        }
249

    
250
        public override ControlType ControlType
251
        {
252
            set
253
            {
254
                SetValue(ControlTypeProperty, value);
255
            }
256
            get
257
            {
258
                return (ControlType)GetValue(ControlTypeProperty);
259
            }
260
        }
261

    
262
        #endregion
263
        public override void OnApplyTemplate()
264
        {
265
            base.OnApplyTemplate();
266
            Base_Image = GetTemplateChild(PART_Image) as Image;
267
            SetImage();
268
        }
269

    
270
        private void SetImage()
271
        {
272
            this.ApplyTemplate();
273

    
274
            Point mid = MathSet.FindCentroid(new List<Point>()
275
            {
276
                this.StartPoint,
277
                this.LeftBottomPoint,
278
                this.EndPoint,
279
                this.TopRightPoint,                
280
            });
281

    
282
            double AngleData = this.CommentAngle * -1;
283

    
284
            PathFigure pathFigure = new PathFigure();
285
            pathFigure.StartPoint = MathSet.RotateAbout(mid, this.StartPoint, AngleData);
286

    
287
            LineSegment lineSegment0 = new LineSegment();
288
            lineSegment0.Point = MathSet.RotateAbout(mid, this.StartPoint, AngleData);
289
            pathFigure.Segments.Add(lineSegment0);
290

    
291
            LineSegment lineSegment1 = new LineSegment();
292
            lineSegment1.Point = MathSet.RotateAbout(mid, this.LeftBottomPoint, AngleData);
293
            pathFigure.Segments.Add(lineSegment1);
294

    
295
            LineSegment lineSegment2 = new LineSegment();
296
            lineSegment2.Point = MathSet.RotateAbout(mid, this.EndPoint, AngleData);
297
            pathFigure.Segments.Add(lineSegment2);
298

    
299
            LineSegment lineSegment3 = new LineSegment();
300
            lineSegment3.Point = MathSet.RotateAbout(mid, this.TopRightPoint, AngleData);
301
            pathFigure.Segments.Add(lineSegment3);
302

    
303
            PathGeometry pathGeometry = new PathGeometry();
304
            pathGeometry.Figures = new PathFigureCollection();
305
            pathFigure.IsClosed = true;
306
            pathGeometry.Figures.Add(pathFigure);
307

    
308
            // 상 하 좌 우 에 마진값 적용
309
            double dMargin = -20;
310

    
311
            this.Base_Image.Width = Math.Abs(pathGeometry.Bounds.Width+ dMargin);
312
            this.Base_Image.Height = Math.Abs(pathGeometry.Bounds.Height+ dMargin);
313

    
314
            this.Tag = pathGeometry;
315

    
316
            Canvas.SetLeft(this, MathSet.RotateAbout(mid, mid, AngleData).X - this.Base_Image.Width / 2);
317
            Canvas.SetTop(this, MathSet.RotateAbout(mid, mid, AngleData).Y - this.Base_Image.Height / 2);
318
        }
319
        public event PropertyChangedEventHandler PropertyChanged;
320
        protected void RaisePropertyChanged(string propName)
321
        {
322
            if (PropertyChanged != null)
323
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
324
        }
325

    
326
        public override void UpdateControl()
327
        {
328
            this.StartPoint = new Point(this.PointSet[0].X, this.PointSet[0].Y);
329
            this.TopRightPoint = new Point(this.PointSet[3].X, this.PointSet[3].Y);
330
            this.LeftBottomPoint = new Point(this.PointSet[1].X, this.PointSet[1].Y);
331
            this.EndPoint = new Point(this.PointSet[2].X, this.PointSet[2].Y);
332
        }
333

    
334
        /// <summary>
335
        /// call when mouse is moving while drawing control
336
        /// </summary>
337
        /// <author>humkyung</author>
338
        /// <param name="pt"></param>
339
        /// <param name="bAxisLocked"></param>
340
        public override  void OnCreatingMouseMove(Point pt, bool bAxisLocked)
341
        {
342
            this.EndPoint = pt;
343
            this.LeftBottomPoint = new Point(this.StartPoint.X, this.EndPoint.Y);
344
            this.TopRightPoint = new Point(this.EndPoint.X, this.StartPoint.Y);
345

    
346
            var _sign = Application.Current.FindResource("UserSign");
347

    
348
            if (this.StartPoint != this.EndPoint && this.SignImage == null && _sign != null)
349
            {
350
                //var _sign = GetUserSign.GetSign(this.UserNumber, this.ProjectNO);
351

    
352
                byte[] imageBytes = System.Convert.FromBase64String(_sign.ToString());
353

    
354
                System.IO.MemoryStream stream = new System.IO.MemoryStream();
355
                stream.Write(imageBytes, 0, imageBytes.Length);
356
                stream.Position = 0;
357
                System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
358
                BitmapImage returnImage = new BitmapImage();
359
                returnImage.BeginInit();
360
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
361
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
362
                ms.Seek(0, System.IO.SeekOrigin.Begin);
363
                returnImage.StreamSource = ms;
364
                returnImage.EndInit();
365
                stream.Close();
366

    
367
                this.SignImage = returnImage;
368

    
369
            }
370

    
371
            this.PointSet = new List<Point>
372
            {
373
                this.StartPoint,
374
                this.LeftBottomPoint,
375
                this.EndPoint,
376
                this.TopRightPoint,
377
            };
378
        }
379

    
380
        /// <summary>
381
        /// move control point has same location of given pt along given delta
382
        /// </summary>
383
        /// <author>humkyung</author>
384
        /// <date>2019.06.20</date>
385
        /// <param name="pt"></param>
386
        /// <param name="dx"></param>
387
        /// <param name="dy"></param>
388
        public override void OnMoveCtrlPoint(Point pt, double dx, double dy, bool bAxisLocked = false)
389
        {
390
            IPath path = (this as IPath);
391

    
392
            Point selected = MathSet.getNearPoint(path.PointSet, pt);
393
            selected.X += dx;
394
            selected.Y += dy;
395

    
396
            int idx = (this as IPath).PointSet.FindIndex(x => x.Equals(pt));
397

    
398
            var OppositeP = (idx + path.PointSet.Count / 2) % path.PointSet.Count;
399
            var PreviousP = (idx + (path.PointSet.Count - 1)) % path.PointSet.Count;
400
            var NextP = (idx + 1) % path.PointSet.Count;
401

    
402
            if (bAxisLocked)
403
            {
404
                var PrevV = path.PointSet[PreviousP] - path.PointSet[OppositeP];
405
                double PrevLength = PrevV.Length;
406
                PrevV.Normalize();
407

    
408
                var NextV = path.PointSet[NextP] - path.PointSet[OppositeP];
409
                double NextVLength = NextV.Length;
410
                NextV.Normalize();
411

    
412
                double _dx = selected.X - path.PointSet[OppositeP].X;
413
                double _dy = selected.Y - path.PointSet[OppositeP].Y;
414
                var dir = new Vector(_dx, _dy);
415
                if (PrevLength > NextVLength)
416
                {
417
                    double ratio = NextVLength / PrevLength;
418

    
419
                    double dot = MathSet.DotProduct(PrevV.X, PrevV.Y, dir.X, dir.Y);
420

    
421
                    path.PointSet[PreviousP] = path.PointSet[OppositeP] + PrevV * dot;
422
                    path.PointSet[NextP] = path.PointSet[OppositeP] + NextV * dot * ratio;
423
                    path.PointSet[idx] = path.PointSet[OppositeP] + PrevV * dot + NextV * dot * ratio;
424
                }
425
                else
426
                {
427
                    double ratio = PrevLength / NextVLength;
428

    
429
                    double dot = MathSet.DotProduct(NextV.X, NextV.Y, dir.X, dir.Y);
430

    
431
                    path.PointSet[PreviousP] = path.PointSet[OppositeP] + PrevV * dot * ratio;
432
                    path.PointSet[NextP] = path.PointSet[OppositeP] + NextV * dot;
433
                    path.PointSet[idx] = path.PointSet[OppositeP] + PrevV * dot * ratio + NextV * dot;
434
                }
435
            }
436
            else
437
            {
438
                var PreviousV = MathSet.GetNormVectorBetween(path.PointSet[OppositeP], path.PointSet[PreviousP]);
439
                var l = MathSet.DotProduct(PreviousV.X, PreviousV.Y, path.PointSet[idx].X - path.PointSet[OppositeP].X,
440
                    path.PointSet[idx].Y - path.PointSet[OppositeP].Y);
441
                path.PointSet[PreviousP] = new Point(path.PointSet[OppositeP].X + PreviousV.X * l, path.PointSet[OppositeP].Y + PreviousV.Y * l);
442

    
443
                var NextV = MathSet.GetNormVectorBetween(path.PointSet[OppositeP], path.PointSet[NextP]);
444
                l = MathSet.DotProduct(NextV.X, NextV.Y, path.PointSet[idx].X - path.PointSet[OppositeP].X, path.PointSet
445
                    [idx].Y - path.PointSet[OppositeP].Y);
446
                path.PointSet[NextP] = new Point(path.PointSet[OppositeP].X + NextV.X * l, path.PointSet[OppositeP].Y + NextV.Y * l);
447

    
448
                path.PointSet[idx] = selected;
449
            }
450

    
451
            this.UpdateControl();
452
        }
453

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

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

    
472
        /// <summary>
473
        /// Serialize this
474
        /// </summary>
475
        /// <param name="sUserId"></param>
476
        /// <returns></returns>
477
        public override string Serialize()
478
        {
479
            using (S_SignControl STemp = new S_SignControl())
480
            {
481
                STemp.Angle = this.CommentAngle;
482
                STemp.EndPoint = this.EndPoint;
483
                STemp.UserID = this.UserID;
484
                STemp.LB = this.LeftBottomPoint;
485
                STemp.Name = this.GetType().Name;
486
                STemp.PointSet = this.PointSet;
487
                STemp.StartPoint = this.StartPoint;
488
                STemp.Opac = this.Opacity;
489
                STemp.TR = this.TopRightPoint;
490
                STemp.UserNumber = this.UserNumber == null ? this.UserID: this.UserNumber;
491

    
492
                ///강인구 추가(2017.11.02)
493
                ///Memo 추가
494
                STemp.Memo = this.Memo;
495

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

    
500
        /// <summary>
501
        /// create a signcontrol from given string
502
        /// </summary>
503
        /// <param name="str"></param>
504
        /// <returns></returns>
505
        public static SignControl FromString(string str, SolidColorBrush brush, string sProjectNo)
506
        {
507
            SignControl instance = null;
508
            using (S_SignControl s = JsonSerializerHelper.JsonDeserialize<S_SignControl>(str))
509
            {
510
                instance = new SignControl
511
                {
512
                    CommentAngle = s.Angle,
513
                    StartPoint = s.StartPoint,
514
                    TopRightPoint = s.TR,
515
                    EndPoint = s.EndPoint,
516
                    LeftBottomPoint = s.LB,
517
                    PointSet = s.PointSet,
518
                    Opacity = s.Opac,
519
                    SignImage = null,
520
                    UserID = s.UserID,
521
                    UserNumber = s.UserNumber,
522
                    Memo = s.Memo
523
                };
524

    
525
                if (s.UserNumber != null)
526
                {
527
                    var _sign = GetUserSign.GetSign(s.UserNumber, sProjectNo);
528
                    if (_sign != null)
529
                    {
530
                        byte[] imageBytes = System.Convert.FromBase64String(_sign);
531

    
532
                        BitmapImage returnImage = new BitmapImage();
533

    
534
                        using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
535
                        {
536
                            stream.WriteAsync(imageBytes, 0, imageBytes.Length);
537

    
538
                            stream.Position = 0;
539
                            System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
540
                            returnImage.BeginInit();
541
                            System.IO.MemoryStream ms = new System.IO.MemoryStream();
542
                            img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
543
                            ms.Seek(0, System.IO.SeekOrigin.Begin);
544
                            returnImage.StreamSource = ms;
545
                            returnImage.EndInit();
546
                            stream.Close();
547
                        }
548

    
549
                        instance.SignImage = returnImage;
550
                    }
551
                }
552
            }
553

    
554
            return instance;
555
        }
556

    
557
        public double LineSize
558
        {
559
            get;
560
            set;
561
        }
562
        public Geometry PathData
563
        {
564
            get;
565
            set;
566
        }
567
    }
568
}
클립보드 이미지 추가 (최대 크기: 500 MB)