-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLinePatternViewerControlWpf.xaml.cs
158 lines (142 loc) · 5.48 KB
/
LinePatternViewerControlWpf.xaml.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
using System;
using System.ComponentModel;
using System.Windows;
using WShapes = System.Windows.Shapes;
using System.Windows.Media;
using Autodesk.Revit.DB;
namespace LinePatternViewer
{
/// <summary>
/// Interaction logic for LinePatternViewerControlWpf.xaml
/// </summary>
public partial class LinePatternViewerControlWpf : INotifyPropertyChanged
{
#region LinePattern DependencyProperty
public static readonly DependencyProperty
LinePatternProperty = DependencyProperty
.RegisterAttached("LinePattern",
typeof(LinePattern),
typeof(LinePatternViewerControlWpf),
new UIPropertyMetadata(null, OnLinePatternChanged));
private static void OnLinePatternChanged(
DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var linePatternViewerControl = d as LinePatternViewerControlWpf;
if (linePatternViewerControl == null) return;
linePatternViewerControl.OnPropertyChanged("LinePattern");
linePatternViewerControl.CreateLinePatternOnCanvas();
}
public LinePattern LinePattern
{
get
{
return (LinePattern)GetValue(LinePatternProperty);
}
set
{
SetValue(LinePatternProperty, value);
}
}
public LinePattern GetLinePattern(
DependencyObject obj)
{
return (LinePattern)obj.GetValue(
LinePatternProperty);
}
public void SetLinePattern(
DependencyObject obj,
LinePattern value)
{
obj.SetValue(LinePatternProperty, value);
}
#endregion
public System.Collections.Generic.List<string> colorCheckList = null;
public LinePatternViewerControlWpf()
{
InitializeComponent();
this.Foreground = System.Windows.Media.Brushes.Black;
theCanvas.Background = this.Background;
}
public event PropertyChangedEventHandler
PropertyChanged;
//[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged(
string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this,
new PropertyChangedEventArgs(propertyName));
}
private void CreateLinePatternOnCanvas()
{
var width =
(ActualWidth == 0 ? Width : ActualWidth) == 0
? 100
: (ActualWidth == 0 ? Width : ActualWidth);
if (double.IsNaN(width))
width = 100;
var height =
(ActualHeight == 0 ? Height : ActualHeight) == 0
? 30
: (ActualHeight == 0 ? Height : ActualHeight);
if (double.IsNaN(height))
height = 30;
System.Collections.Generic.IList<LinePatternSegment> segments = LinePattern.GetSegments();
if (segments.Count > 0)
{
double x1 = 0, x2 = 0;
while ((x2 * 96 * 12) <= width)
{
foreach (LinePatternSegment lps in segments)
{
x2 += lps.Length;
WShapes.Line l = new WShapes.Line();
l.StrokeThickness = 3d;
l.StrokeEndLineCap = PenLineCap.Square;
l.StrokeStartLineCap = PenLineCap.Square;
l.X1 = x1 * 96 * 12; // 96px per inch X 12 inches per foot
l.Y1 = height / 2;
l.X2 = x2 * 96 * 12;
l.Y2 = height / 2;
switch (lps.Type)
{
case LinePatternSegmentType.Dash:
l.Stroke = Foreground;
break;
case LinePatternSegmentType.Dot:
l.StrokeThickness = 2.5d;
l.StrokeEndLineCap = PenLineCap.Round;
l.StrokeStartLineCap = PenLineCap.Round;
l.Stroke = Foreground;
break;
case LinePatternSegmentType.Space:
l.Stroke = System.Windows.Media.Brushes.Transparent;
break;
default:
throw new ArgumentException("Invalid segment type");
}
theCanvas.Children.Add(l);
x1 += lps.Length;
}
}
}
else
{
//solid line
WShapes.Line l = new WShapes.Line();
l.StrokeThickness = 5;
l.StrokeEndLineCap = PenLineCap.Square;
l.StrokeStartLineCap = PenLineCap.Square;
l.X1 = 0 * 96 * 12; // 96px per inch X 12 inches per foot
l.Y1 = height / 2;
l.X2 = width;
l.Y2 = height / 2;
l.Stroke = System.Windows.Media.Brushes.Black;
theCanvas.Children.Add(l);
}
}
//OnPropertyChanged("LinePatternImage");
}
}