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

DomCR / ACadSharp / 17513172785

06 Sep 2025 10:07AM UTC coverage: 78.107% (-0.01%) from 78.117%
17513172785

Pull #777

github

web-flow
Merge 05d6b0249 into 2cb0aac44
Pull Request #777: Get active properties

6589 of 9167 branches covered (71.88%)

Branch coverage included in aggregate %.

40 of 55 new or added lines in 1 file covered. (72.73%)

10 existing lines in 3 files now uncovered.

25495 of 31910 relevant lines covered (79.9%)

105487.95 hits per line

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

69.07
/src/ACadSharp/Entities/Entity.cs
1
using ACadSharp.Attributes;
2
using ACadSharp.Objects;
3
using ACadSharp.Tables;
4
using CSMath;
5
using System;
6
using System.Collections.Generic;
7

8
namespace ACadSharp.Entities
9
{
10
        /// <summary>
11
        /// The standard class for a basic CAD entity or a graphical object.
12
        /// </summary>
13
        [DxfSubClass(DxfSubclassMarker.Entity)]
14
        public abstract class Entity : CadObject, IEntity
15
        {
16
                /// <summary>
17
                /// Book color for this entity.
18
                /// </summary>
19
                [DxfCodeValue(DxfReferenceType.Name, 430)]
20
                public BookColor BookColor
21
                {
22
                        get { return this._bookColor; }
58,587✔
23
                        set
24
                        {
293✔
25
                                if (this.Document != null)
293✔
26
                                {
275✔
27
                                        this._bookColor = updateCollection(value, this.Document.Colors);
275✔
28
                                }
275✔
29
                                else
30
                                {
18✔
31
                                        this._bookColor = value;
18✔
32
                                }
18✔
33
                        }
293✔
34
                }
35

36
                /// <inheritdoc/>
37
                [DxfCodeValue(62, 420)]
38
                public Color Color { get; set; } = Color.ByLayer;
1,079,284✔
39

40
                /// <inheritdoc/>
41
                [DxfCodeValue(60)]
42
                public bool IsInvisible { get; set; } = false;
971,603✔
43

44
                /// <inheritdoc/>
45
                [DxfCodeValue(DxfReferenceType.Name, 8)]
46
                public Layer Layer
47
                {
48
                        get { return this._layer; }
2,556,819✔
49
                        set
50
                        {
288,100✔
51
                                if (value == null)
288,100!
52
                                {
×
53
                                        throw new ArgumentNullException(nameof(value));
×
54
                                }
55

56
                                this._layer = updateCollection(value, this.Document?.Layers);
288,100✔
57
                        }
288,100✔
58
                }
59

60
                /// <inheritdoc/>
61
                [DxfCodeValue(DxfReferenceType.Name, 6)]
62
                public LineType LineType
63
                {
64
                        get { return this._lineType; }
2,567,952✔
65
                        set
66
                        {
89,396✔
67
                                if (value == null)
89,396!
68
                                {
×
69
                                        throw new ArgumentNullException(nameof(value));
×
70
                                }
71

72
                                this._lineType = CadObject.updateCollection(value, this.Document?.LineTypes);
89,396✔
73
                        }
89,396✔
74
                }
75

76
                /// <inheritdoc/>
77
                [DxfCodeValue(48)]
78
                public double LinetypeScale { get; set; } = 1.0;
971,928✔
79

80
                /// <inheritdoc/>
81
                [DxfCodeValue(370)]
82
                public LineweightType LineWeight { get; set; } = LineweightType.ByLayer;
982,103✔
83

84
                /// <inheritdoc/>
85
                [DxfCodeValue(DxfReferenceType.Handle, 347)]
86
                public Material Material { get; set; }
23,134✔
87

88
                /// <inheritdoc/>
89
                public override string SubclassMarker => DxfSubclassMarker.Entity;
494,595✔
90

91
                /// <inheritdoc/>
92
                [DxfCodeValue(440)]
93
                public Transparency Transparency { get; set; } = Transparency.ByLayer;
991,958✔
94

95
                private BookColor _bookColor = null;
879,088✔
96

97
                private Layer _layer = Layer.Default;
879,088✔
98

99
                private LineType _lineType = LineType.ByLayer;
879,088✔
100

101
                /// <inheritdoc/>
102
                public Entity() : base() { }
2,637,264✔
103

104
                /// <summary>
105
                /// Apply a rotation to this entity.
106
                /// </summary>
107
                /// <param name="axis"></param>
108
                /// <param name="rotation">The angle to rotate around the given axis, in radians.</param>
109
                public void ApplyRotation(XYZ axis, double rotation)
110
                {
9✔
111
                        Transform transform = Transform.CreateRotation(axis, rotation);
9✔
112
                        this.ApplyTransform(transform);
9✔
113
                }
9✔
114

115
                /// <summary>
116
                /// Apply a scaling transformation to this entity.
117
                /// </summary>
118
                /// <param name="scale"></param>
119
                public void ApplyScaling(XYZ scale)
120
                {
×
121
                        Transform transform = Transform.CreateScaling(scale);
×
122
                        this.ApplyTransform(transform);
×
123
                }
×
124

125
                /// <summary>
126
                /// Apply a scaling transformation to this entity.
127
                /// </summary>
128
                /// <param name="scale"></param>
129
                /// <param name="origin"></param>
130
                public void ApplyScaling(XYZ scale, XYZ origin)
131
                {
×
132
                        Transform transform = Transform.CreateScaling(scale, origin);
×
133
                        this.ApplyTransform(transform);
×
134
                }
×
135

136
                /// <inheritdoc/>
137
                public abstract void ApplyTransform(Transform transform);
138

139
                /// <summary>
140
                /// Apply a translation to this entity.
141
                /// </summary>
142
                /// <param name="translation"></param>
143
                public void ApplyTranslation(XYZ translation)
144
                {
1✔
145
                        Transform transform = Transform.CreateTranslation(translation);
1✔
146
                        this.ApplyTransform(transform);
1✔
147
                }
1✔
148

149
                /// <inheritdoc/>
150
                public override CadObject Clone()
151
                {
11,567✔
152
                        Entity clone = (Entity)base.Clone();
11,567✔
153

154
                        clone.Layer = (Layer)this.Layer.Clone();
11,567✔
155
                        clone.LineType = (LineType)this.LineType.Clone();
11,567✔
156
                        clone.Material = (Material)this.Material?.Clone();
11,567!
157

158
                        return clone;
11,567✔
159
                }
11,567✔
160

161
                /// <inheritdoc/>
162
                public Color GetActiveColor()
163
                {
140✔
164
                        Color color;
165
                        if (this.Color.IsByLayer)
140✔
166
                        {
107✔
167
                                color = this.Layer.Color;
107✔
168
                        }
107✔
169
                        else if (this.Color.IsByBlock && this.Owner is BlockRecord record)
33✔
170
                        {
7✔
171
                                color = record.BlockEntity.Color;
7✔
172
                        }
7✔
173
                        else
174
                        {
26✔
175
                                color = this.Color;
26✔
176
                        }
26✔
177

178
                        return color;
140✔
179
                }
140✔
180

181
                /// <inheritdoc/>
182
                public LineType GetActiveLineType()
183
                {
21✔
184
                        if (this.LineType.Name.Equals(LineType.ByLayerName, StringComparison.InvariantCultureIgnoreCase))
21✔
185
                        {
7✔
186
                                return this.Layer.LineType;
7✔
187
                        }
188
                        else if (this.LineType.Name.Equals(LineType.ByBlockName, StringComparison.InvariantCultureIgnoreCase)
14✔
189
                                && this.Owner is BlockRecord record)
14✔
190
                        {
7✔
191
                                return record.BlockEntity.LineType;
7✔
192
                        }
193

194
                        return this.LineType;
7✔
195
                }
21✔
196

197
                /// <inheritdoc/>
198
                public LineweightType GetActiveLineWeightType()
199
                {
21✔
200
                        switch (this.LineWeight)
21✔
201
                        {
202
                                case LineweightType.ByLayer:
203
                                        return this.Layer.LineWeight;
7✔
204
                                case LineweightType.ByBlock:
205
                                        if (this.Owner is BlockRecord record)
7!
206
                                        {
7✔
207
                                                return record.BlockEntity.LineWeight;
7✔
208
                                        }
209
                                        else
NEW
210
                                        {
×
NEW
211
                                                return this.LineWeight;
×
212
                                        }
213
                                default:
214
                                        return this.LineWeight;
7✔
215
                        }
216
                }
21✔
217

218
                /// <inheritdoc/>
219
                public abstract BoundingBox GetBoundingBox();
220

221
                /// <inheritdoc/>
222
                public void MatchProperties(IEntity entity)
223
                {
32✔
224
                        if (entity is null)
32!
225
                        {
×
226
                                throw new ArgumentNullException(nameof(entity));
×
227
                        }
228

229
                        if (entity.Handle == 0)
32!
230
                        {
32✔
231
                                entity.Layer = (Layer)this.Layer.Clone();
32✔
232
                                entity.LineType = (LineType)this.LineType.Clone();
32✔
233
                        }
32✔
234
                        else
235
                        {
×
236
                                entity.Layer = this.Layer;
×
237
                                entity.LineType = this.LineType;
×
238
                        }
×
239

240
                        entity.Color = this.Color;
32✔
241
                        entity.LineWeight = this.LineWeight;
32✔
242
                        entity.LinetypeScale = this.LinetypeScale;
32✔
243
                        entity.IsInvisible = this.IsInvisible;
32✔
244
                        entity.Transparency = this.Transparency;
32✔
245
                }
32✔
246

247
                internal override void AssignDocument(CadDocument doc)
248
                {
812,558✔
249
                        base.AssignDocument(doc);
812,558✔
250

251
                        this._layer = CadObject.updateCollection(this.Layer, doc.Layers);
812,558✔
252
                        this._lineType = CadObject.updateCollection(this.LineType, doc.LineTypes);
812,558✔
253

254
                        doc.Layers.OnRemove += this.tableOnRemove;
812,558✔
255
                        doc.LineTypes.OnRemove += this.tableOnRemove;
812,558✔
256
                }
812,558✔
257

258
                internal override void UnassignDocument()
259
                {
5,533✔
260
                        this.Document.Layers.OnRemove -= this.tableOnRemove;
5,533✔
261
                        this.Document.LineTypes.OnRemove -= this.tableOnRemove;
5,533✔
262

263
                        base.UnassignDocument();
5,533✔
264

265
                        this.Layer = (Layer)this.Layer.Clone();
5,533✔
266
                        this.LineType = (LineType)this.LineType.Clone();
5,533✔
267
                }
5,533✔
268

269
                protected List<XY> applyRotation(IEnumerable<XY> points, double rotation)
270
                {
10✔
271
                        if (points == null)
10!
272
                        {
×
273
                                throw new ArgumentNullException(nameof(points));
×
274
                        }
275

276
                        if (MathHelper.IsZero(rotation))
10!
277
                        {
10✔
278
                                return new List<XY>(points);
10✔
279
                        }
280

281
                        double sin = Math.Sin(rotation);
×
282
                        double cos = Math.Cos(rotation);
×
283

284
                        List<XY> transPoints;
285

286
                        transPoints = new List<XY>();
×
287
                        foreach (XY p in points)
×
288
                        {
×
289
                                transPoints.Add(new XY(p.X * cos - p.Y * sin, p.X * sin + p.Y * cos));
×
290
                        }
×
291
                        return transPoints;
×
292
                }
10✔
293

294
                protected List<XYZ> applyRotation(IEnumerable<XYZ> points, XYZ zAxis)
295
                {
×
296
                        if (points == null)
×
297
                        {
×
298
                                throw new ArgumentNullException(nameof(points));
×
299
                        }
300

301
                        Matrix3 trans = Matrix3.ArbitraryAxis(zAxis);
×
302
                        List<XYZ> transPoints;
303
                        transPoints = new List<XYZ>();
×
304
                        foreach (XYZ p in points)
×
305
                        {
×
306
                                transPoints.Add(trans * p);
×
307
                        }
×
308
                        return transPoints;
×
309
                }
×
310

311
                protected XYZ applyRotation(XYZ points, XYZ zAxis)
312
                {
×
313
                        Matrix4 trans = Matrix4.GetArbitraryAxis(zAxis).Transpose();
×
314
                        return trans * points;
×
315
                }
×
316

317
                protected XYZ applyWorldMatrix(XYZ xyz, Transform transform, Matrix3 transOW, Matrix3 transWO)
NEW
318
                {
×
NEW
319
                        XYZ v = transOW * xyz;
×
NEW
320
                        v = transform.ApplyTransform(v);
×
NEW
321
                        v = transWO * v;
×
NEW
322
                        return v;
×
NEW
323
                }
×
324

325
                protected XYZ applyWorldMatrix(XYZ xyz, XYZ normal, XYZ newNormal)
NEW
326
                {
×
NEW
327
                        var transOW = Matrix3.ArbitraryAxis(normal).Transpose();
×
NEW
328
                        var transWO = Matrix3.ArbitraryAxis(newNormal);
×
NEW
329
                        XYZ v = transOW * xyz;
×
NEW
330
                        v = transWO * v;
×
NEW
331
                        return v;
×
NEW
332
                }
×
333

334
                protected Matrix3 getWorldMatrix(Transform transform, XYZ normal, XYZ newNormal, out Matrix3 transOW, out Matrix3 transWO)
335
                {
19✔
336
                        transOW = Matrix3.ArbitraryAxis(normal);
19✔
337
                        transWO = Matrix3.ArbitraryAxis(newNormal).Transpose();
19✔
338
                        return new Matrix3(transform.Matrix);
19✔
339
                }
19✔
340

341
                protected virtual void tableOnRemove(object sender, CollectionChangedEventArgs e)
342
                {
46✔
343
                        if (e.Item.Equals(this.Layer))
46✔
344
                        {
1✔
345
                                this.Layer = this.Document.Layers[Layer.DefaultName];
1✔
346
                        }
1✔
347

348
                        if (e.Item.Equals(this.LineType))
46✔
349
                        {
1✔
350
                                this.LineType = this.Document.LineTypes[LineType.ByLayerName];
1✔
351
                        }
1✔
352
                }
46✔
353

354
                protected XYZ transformNormal(Transform transform, XYZ normal)
355
                {
22✔
356
                        return transform.ApplyRotation(normal).Normalize();
22✔
357
                }
22✔
358
        }
359
}
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