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

DomCR / ACadSharp / 27152123002

08 Jun 2026 04:32PM UTC coverage: 76.597% (-0.4%) from 76.999%
27152123002

Pull #1105

github

web-flow
Merge d8dea56ce into 5027f0b2b
Pull Request #1105: Hatch explode

8656 of 12271 branches covered (70.54%)

Branch coverage included in aggregate %.

350 of 644 new or added lines in 10 files covered. (54.35%)

300 existing lines in 20 files now uncovered.

31237 of 39811 relevant lines covered (78.46%)

155999.42 hits per line

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

81.0
/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; }
76,590✔
23
                set
24
                {
297✔
25
                        if (this.Document != null)
297✔
26
                        {
275✔
27
                                this._bookColor = updateCollection(value, this.Document.Colors);
275✔
28
                        }
275✔
29
                        else
30
                        {
22✔
31
                                this._bookColor = value;
22✔
32
                        }
22✔
33
                }
297✔
34
        }
35

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

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

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

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

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

72
                        this._lineType = updateCollection(value, this.Document?.LineTypes);
642,469✔
73
                }
642,469✔
74
        }
75

76
        /// <inheritdoc/>
77
        [DxfCodeValue(48)]
78
        public double LineTypeScale { get; set; } = 1.0;
2,108,826✔
79

80
        /// <inheritdoc/>
81
        [DxfCodeValue(370)]
82
        public LineWeightType LineWeight { get; set; } = LineWeightType.ByLayer;
2,118,008✔
83

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

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

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

95
        private BookColor _bookColor = null;
893,570✔
96

97
        private Layer _layer = Layer.Default;
893,570✔
98

99
        private LineType _lineType = LineType.ByLayer;
893,570✔
100

101
        /// <inheritdoc/>
102
        public Entity() : base() { }
2,680,710✔
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
        {
11✔
111
                Transform transform = Transform.CreateRotation(axis, rotation);
11✔
112
                this.ApplyTransform(transform);
11✔
113
        }
11✔
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
        {
8,039✔
152
                Entity clone = (Entity)base.Clone();
8,039✔
153

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

158
                return clone;
8,039✔
159
        }
8,039✔
160

161
        /// <inheritdoc/>
162
        public Color GetActiveColor()
163
        {
411✔
164
                Color color;
165
                if (this.Color.IsByLayer)
411✔
166
                {
212✔
167
                        color = this.Layer.Color;
212✔
168
                }
212✔
169
                else if (this.Color.IsByBlock && this.Owner is BlockRecord record)
199✔
170
                {
123✔
171
                        color = record.BlockEntity.Color;
123✔
172
                        if (color.IsByLayer)
123✔
173
                        {
96✔
174
                                color = this.Layer.Color;
96✔
175
                        }
96✔
176
                }
123✔
177
                else
178
                {
76✔
179
                        color = this.Color;
76✔
180
                }
76✔
181

182
                return color;
411✔
183
        }
411✔
184

185
        /// <inheritdoc/>
186
        public LineType GetActiveLineType()
187
        {
328✔
188
                if (this.LineType.Name.Equals(LineType.ByLayerName, StringComparison.InvariantCultureIgnoreCase))
328✔
189
                {
239✔
190
                        return this.Layer.LineType;
239✔
191
                }
192
                else if (this.LineType.Name.Equals(LineType.ByBlockName, StringComparison.InvariantCultureIgnoreCase)
89✔
193
                        && this.Owner is BlockRecord record)
89✔
194
                {
27✔
195
                        return record.BlockEntity.LineType;
27✔
196
                }
197

198
                return this.LineType;
62✔
199
        }
328✔
200

201
        /// <inheritdoc/>
202
        public LineWeightType GetActiveLineWeightType()
203
        {
423✔
204
                switch (this.LineWeight)
423✔
205
                {
206
                        case LineWeightType.ByLayer:
207
                                return this.Layer.LineWeight;
302✔
208
                        case LineWeightType.ByBlock:
209
                                if (this.Owner is BlockRecord record)
52!
210
                                {
52✔
211
                                        return record.BlockEntity.GetActiveLineWeightType();
52✔
212
                                }
213
                                else
UNCOV
214
                                {
×
UNCOV
215
                                        return this.LineWeight;
×
216
                                }
217
                        default:
218
                                return this.LineWeight;
69✔
219
                }
220
        }
423✔
221

222
        /// <inheritdoc/>
223
        public abstract BoundingBox GetBoundingBox();
224

225
        /// <inheritdoc/>
226
        public void MatchProperties(IEntity entity)
227
        {
551,590✔
228
                if (entity is null)
551,590!
UNCOV
229
                {
×
UNCOV
230
                        throw new ArgumentNullException(nameof(entity));
×
231
                }
232

233
                if (entity.Handle == 0 || this.Document != entity.Document)
551,590✔
234
                {
550,466✔
235
                        this.Layer = (Layer)entity.Layer.Clone();
550,466✔
236
                        this.LineType = (LineType)entity.LineType.Clone();
550,466✔
237
                        this.Material = (Material)entity.Material?.Clone();
550,466!
238
                }
550,466✔
239
                else
240
                {
1,124✔
241
                        this.Layer = entity.Layer;
1,124✔
242
                        this.LineType = entity.LineType;
1,124✔
243
                        this.Material = entity.Material;
1,124✔
244
                }
1,124✔
245

246
                this.Color = entity.Color;
551,590✔
247
                this.LineWeight = entity.LineWeight;
551,590✔
248
                this.LineTypeScale = entity.LineTypeScale;
551,590✔
249
                this.IsInvisible = entity.IsInvisible;
551,590✔
250
                this.Transparency = entity.Transparency;
551,590✔
251
        }
551,590✔
252

253
        internal override void AssignDocument(CadDocument doc)
254
        {
830,161✔
255
                base.AssignDocument(doc);
830,161✔
256

257
                this._layer = CadObject.updateCollection(this.Layer, doc.Layers);
830,161✔
258
                this._lineType = CadObject.updateCollection(this.LineType, doc.LineTypes);
830,161✔
259

260
                doc.Layers.OnRemove += this.tableOnRemove;
830,161✔
261
                doc.LineTypes.OnRemove += this.tableOnRemove;
830,161✔
262
        }
830,161✔
263

264
        internal override void UnassignDocument()
265
        {
4,358✔
266
                this.Document.Layers.OnRemove -= this.tableOnRemove;
4,358✔
267
                this.Document.LineTypes.OnRemove -= this.tableOnRemove;
4,358✔
268

269
                base.UnassignDocument();
4,358✔
270

271
                this.Layer = (Layer)this.Layer.Clone();
4,358✔
272
                this.LineType = (LineType)this.LineType.Clone();
4,358✔
273
        }
4,358✔
274

275
        protected List<XY> applyRotation(IEnumerable<XY> points, double rotation)
276
        {
59✔
277
                if (points == null)
59!
UNCOV
278
                {
×
UNCOV
279
                        throw new ArgumentNullException(nameof(points));
×
280
                }
281

282
                if (MathHelper.IsZero(rotation))
59!
283
                {
59✔
284
                        return new List<XY>(points);
59✔
285
                }
286

UNCOV
287
                double sin = Math.Sin(rotation);
×
288
                double cos = Math.Cos(rotation);
×
289

290
                List<XY> transPoints;
291

292
                transPoints = new List<XY>();
×
293
                foreach (XY p in points)
×
UNCOV
294
                {
×
UNCOV
295
                        transPoints.Add(new XY(p.X * cos - p.Y * sin, p.X * sin + p.Y * cos));
×
UNCOV
296
                }
×
297
                return transPoints;
×
298
        }
59✔
299

300
        protected XYZ applyWorldMatrix(XYZ xyz, Transform transform, Matrix3 transOW, Matrix3 transWO)
301
        {
×
302
                XYZ v = transOW * xyz;
×
UNCOV
303
                v = transform.ApplyTransform(v);
×
UNCOV
304
                v = transWO * v;
×
UNCOV
305
                return v;
×
UNCOV
306
        }
×
307

308
        protected Matrix3 getWorldMatrix(Transform transform, XYZ normal, XYZ newNormal, out Matrix3 transOW, out Matrix3 transWO)
309
        {
69✔
310
                transOW = Matrix3.ArbitraryAxis(normal);
69✔
311
                transWO = Matrix3.ArbitraryAxis(newNormal).Transpose();
69✔
312
                return new Matrix3(transform.Matrix);
69✔
313
        }
69✔
314

315
        protected virtual void tableOnRemove(object sender, CollectionChangedEventArgs e)
316
        {
51✔
317
                if (e.Item.Equals(this.Layer))
51✔
318
                {
1✔
319
                        this.Layer = this.Document.Layers[Layer.DefaultName];
1✔
320
                }
1✔
321

322
                if (e.Item.Equals(this.LineType))
51✔
323
                {
1✔
324
                        this.LineType = this.Document.LineTypes[LineType.ByLayerName];
1✔
325
                }
1✔
326
        }
51✔
327

328
        protected XYZ transformNormal(Transform transform, XYZ normal)
329
        {
71✔
330
                return transform.ApplyRotation(normal).Normalize();
71✔
331
        }
71✔
332
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc