• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DomCR / ACadSharp / 12905761471

22 Jan 2025 10:03AM UTC coverage: 2.0% (-74.0%) from 75.963%
12905761471

push

github

DomCR
version 1.0.6

104 of 7676 branches covered (1.35%)

Branch coverage included in aggregate %.

590 of 27024 relevant lines covered (2.18%)

5.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/ACadSharp/DxfPropertyBase.cs
1
using ACadSharp.Attributes;
2
using ACadSharp.Objects;
3
using CSMath;
4
using CSUtilities.Converters;
5
using CSUtilities.Extensions;
6
using System;
7
using System.Collections;
8
using System.Drawing;
9
using System.Linq;
10
using System.Reflection;
11

12
namespace ACadSharp
13
{
14
        public abstract class DxfPropertyBase<T>
15
                where T : Attribute, ICodeValueAttribute
16
        {
17
                public DxfReferenceType ReferenceType { get { return this._attributeData.ReferenceType; } }
×
18

19
                public int AssignedCode
20
                {
21
                        get
22
                        {
×
23
                                if (this._assignedCode.HasValue)
×
24
                                        return this._assignedCode.Value;
×
25

26
                                if (this.DxfCodes.Length == 1)
×
27
                                        return this.DxfCodes.First();
×
28
                                else
29
                                        return (int)DxfCode.Invalid;
×
30
                        }
×
31
                }
32

33
                public int[] DxfCodes { get { return this._attributeData.ValueCodes.Select(c => (int)c).ToArray(); } }
×
34

35
                protected int? _assignedCode;
36

37
                protected PropertyInfo _property;
38

39
                protected T _attributeData;
40

41
                protected DxfPropertyBase(PropertyInfo property)
×
42
                {
×
43
                        this._attributeData = property.GetCustomAttribute<T>();
×
44

45
                        if (this._attributeData == null)
×
46
                                throw new ArgumentException($"The property does not implement the {nameof(DxfCodeValueAttribute)}", nameof(property));
×
47

48
                        this._property = property;
×
49
                }
×
50

51
                /// <summary>
52
                /// Set the value for the property using the AssignedCode
53
                /// </summary>
54
                /// <typeparam name="TCadObject"></typeparam>
55
                /// <param name="obj"></param>
56
                /// <param name="value"></param>
57
                /// <exception cref="InvalidOperationException"></exception>
58
                public void SetValue<TCadObject>(TCadObject obj, object value)
59
                        where TCadObject : CadObject
60
                {
×
61
                        if (this.AssignedCode == (int)DxfCode.Invalid)
×
62
                                throw new InvalidOperationException("This property has multiple dxf values assigned or doesn't have a default value assigned");
×
63

64
                        this.SetValue(this.AssignedCode, obj, value);
×
65
                }
×
66

67
                public void SetValue<TCadObject>(int code, TCadObject obj, object value)
68
                        where TCadObject : CadObject
69
                {
×
70
                        if (this._property.PropertyType.IsEquivalentTo(typeof(XY)))
×
71
                        {
×
72
                                XY vector = (XY)this._property.GetValue(obj);
×
73

74
                                int index = (code / 10) % 10 - 1;
×
75
                                vector[index] = Convert.ToDouble(value);
×
76

77
                                this._property.SetValue(obj, vector);
×
78
                        }
×
79
                        else if (this._property.PropertyType.IsEquivalentTo(typeof(XYZ)))
×
80
                        {
×
81
                                XYZ vector = (XYZ)this._property.GetValue(obj);
×
82

83
                                int index = (code / 10) % 10 - 1;
×
84
                                vector[index] = Convert.ToDouble(value);
×
85

86
                                this._property.SetValue(obj, vector);
×
87
                        }
×
88
                        else if (this._property.PropertyType.IsEquivalentTo(typeof(Color)))
×
89
                        {
×
90
                                switch (code)
×
91
                                {
92
                                        case 62:
93
                                                this._property.SetValue(obj, new Color((short)value));
×
94
                                                break;
×
95
                                        case 420:
96
                                                byte[] b = LittleEndianConverter.Instance.GetBytes((int)value);
×
97
                                                // true color
98
                                                this._property.SetValue(obj, new Color(b[2], b[1], b[0]));
×
99
                                                break;
×
100
                                }
101
                        }
×
102
                        else if (_property.PropertyType.IsEquivalentTo(typeof(PaperMargin)))
×
103
                        {
×
104
                                PaperMargin margin = (PaperMargin)_property.GetValue(obj);
×
105

106
                                switch (code)
×
107
                                {
108
                                        //40        Size, in millimeters, of unprintable margin on left side of paper
109
                                        case 40:
110
                                                margin = new PaperMargin((double)value, margin.Bottom, margin.Right, margin.Top);
×
111
                                                break;
×
112
                                        //41        Size, in millimeters, of unprintable margin on bottom of paper
113
                                        case 41:
114
                                                margin = new PaperMargin(margin.Left, (double)value, margin.Right, margin.Top);
×
115
                                                break;
×
116
                                        //42        Size, in millimeters, of unprintable margin on right side of paper
117
                                        case 42:
118
                                                margin = new PaperMargin(margin.Left, margin.Bottom, (double)value, margin.Top);
×
119
                                                break;
×
120
                                        //43        Size, in millimeters, of unprintable margin on top of paper
121
                                        case 43:
122
                                                margin = new PaperMargin(margin.Left, margin.Bottom, margin.Right, (double)value);
×
123
                                                break;
×
124
                                }
125

126
                                this._property.SetValue(obj, margin);
×
127
                        }
×
128
                        else if (this._property.PropertyType.IsEquivalentTo(typeof(Transparency)))
×
129
                        {
×
130
                                this._property.SetValue(obj, Transparency.FromAlphaValue((int)value));
×
131
                        }
×
132
                        else if (this._property.PropertyType.IsEquivalentTo(typeof(bool)))
×
133
                        {
×
134
                                this._property.SetValue(obj, Convert.ToBoolean(value));
×
135
                        }
×
136
                        else if (this._property.PropertyType.IsEquivalentTo(typeof(char)))
×
137
                        {
×
138
                                this._property.SetValue(obj, Convert.ToChar(value));
×
139
                        }
×
140
                        else if (this._property.PropertyType.IsEquivalentTo(typeof(byte)))
×
141
                        {
×
142
                                this._property.SetValue(obj, Convert.ToByte(value));
×
143
                        }
×
144
                        else if (this._property.PropertyType.IsEnum)
×
145
                        {
×
146
                                this._property.SetValue(obj, Enum.ToObject(this._property.PropertyType, value));
×
147
                        }
×
148
                        else if (this._property.PropertyType.IsEquivalentTo(typeof(ushort)))
×
149
                        {
×
150
                                this._property.SetValue(obj, Convert.ToUInt16(value));
×
151
                        }
×
152
                        else
153
                        {
×
154
                                this._property.SetValue(obj, value);
×
155
                        }
×
156
                }
×
157

158
                public object GetValue<TCadObject>(int code, TCadObject obj)
159
                {
×
160
                        switch (this._attributeData.ReferenceType)
×
161
                        {
162
                                case DxfReferenceType.Unprocess:
163
                                        return this._property.GetValue(obj);
×
164
                                case DxfReferenceType.Handle:
165
                                        return this.getHandledValue(obj);
×
166
                                case DxfReferenceType.Name:
167
                                        return this.getNamedValue(obj);
×
168
                                case DxfReferenceType.Count:
169
                                        return this.getCounterValue(obj);
×
170
                                case DxfReferenceType.None:
171
                                default:
172
                                        return getRawValue(code, obj);
×
173
                        }
174
                }
×
175

176
                private ulong? getHandledValue<TCadObject>(TCadObject obj)
177
                {
×
178
                        if (!this._property.PropertyType.HasInterface<IHandledCadObject>())
×
179
                                throw new ArgumentException($"Property {this._property.Name} for type : {obj.GetType().FullName} does not implement IHandledCadObject");
×
180

181
                        IHandledCadObject handled = (IHandledCadObject)this._property.GetValue(obj);
×
182

183
                        return handled?.Handle;
×
184
                }
×
185

186
                private string getNamedValue<TCadObject>(TCadObject obj)
187
                {
×
188
                        if (!this._property.PropertyType.HasInterface<INamedCadObject>())
×
189
                                throw new ArgumentException($"Property {this._property.Name} for type : {obj.GetType().FullName} does not implement INamedCadObject");
×
190

191
                        INamedCadObject handled = (INamedCadObject)this._property.GetValue(obj);
×
192

193
                        return handled?.Name;
×
194
                }
×
195

196
                private int getCounterValue<TCadObject>(TCadObject obj)
197
                {
×
198
                        if (!this._property.PropertyType.HasInterface<IEnumerable>())
×
199
                                throw new ArgumentException();
×
200

201
                        IEnumerable collection = (IEnumerable)this._property.GetValue(obj);
×
202
                        if (collection == null)
×
203
                                return 0;
×
204

205
                        int counter = 0;
×
206
                        foreach (var item in collection)
×
207
                        {
×
208
                                counter++;
×
209
                        }
×
210

211
                        return counter;
×
212
                }
×
213

214
                private object getRawValue<TCadObject>(int code, TCadObject obj)
215
                {
×
216
                        GroupCodeValueType groupCode = GroupCodeValue.TransformValue(code);
×
217

218
                        switch (groupCode)
×
219
                        {
220
                                case GroupCodeValueType.Handle:
221
                                case GroupCodeValueType.ObjectId:
222
                                case GroupCodeValueType.ExtendedDataHandle:
223
                                        return this.getHandledValue<TCadObject>(obj);
×
224
                        }
225

226
                        if (this._property.PropertyType.HasInterface<IVector>())
×
227
                        {
×
228
                                IVector vector = (IVector)this._property.GetValue(obj);
×
229

230
                                int index = (code / 10) % 10 - 1;
×
231
                                return vector[index];
×
232
                        }
233
                        else if (this._property.PropertyType.IsEquivalentTo(typeof(DateTime)))
×
234
                        {
×
235
                                return CadUtils.ToJulianCalendar((DateTime)this._property.GetValue(obj));
×
236
                        }
237
                        else if (this._property.PropertyType.IsEquivalentTo(typeof(TimeSpan)))
×
238
                        {
×
239
                                return ((TimeSpan)this._property.GetValue(obj)).TotalDays;
×
240
                        }
241
                        else if (this._property.PropertyType.IsEquivalentTo(typeof(Color)))
×
242
                        {
×
243
                                //TODO: Implement color getter
244
                                Color color = (Color)this._property.GetValue(obj);
×
245

246
                                switch (code)
×
247
                                {
248
                                        case 62:
249
                                        case 70:
250
                                                return color.Index;
×
251
                                        case 420:
252
                                                // true color
253
                                                return color.TrueColor;
×
254
                                        case 430:
255
                                                // dictionary color
256
                                                break;
×
257
                                }
258

259
                                return null;
×
260
                        }
261
                        else if (this._property.PropertyType.IsEquivalentTo(typeof(PaperMargin)))
×
262
                        {
×
263
                                switch (code)
×
264
                                {
265
                                        case 40:
266
                                                return ((PaperMargin)this._property.GetValue(obj)).Left;
×
267
                                        case 41:
268
                                                return ((PaperMargin)this._property.GetValue(obj)).Bottom;
×
269
                                        case 42:
270
                                                return ((PaperMargin)this._property.GetValue(obj)).Right;
×
271
                                        case 43:
272
                                                return ((PaperMargin)this._property.GetValue(obj)).Top;
×
273
                                        default:
274
                                                throw new Exception();
×
275
                                }
276
                        }
277
                        else if (this._property.PropertyType.IsEquivalentTo(typeof(Transparency)))
×
278
                        {
×
279
                                //TODO: Implement transparency getter
280
                                //return this._property.GetValue(obj);
281
                                return null;
×
282
                        }
283
                        else
284
                        {
×
285
                                return this._property.GetValue(obj);
×
286
                        }
287
                }
×
288
        }
289
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc