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

DomCR / ACadSharp / 14305968221

07 Apr 2025 09:41AM UTC coverage: 75.187% (-1.0%) from 76.181%
14305968221

push

github

DomCR
badge fix

5615 of 8186 branches covered (68.59%)

Branch coverage included in aggregate %.

22386 of 29056 relevant lines covered (77.04%)

72281.98 hits per line

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

61.93
/src/ACadSharp/Entities/Entity.cs
1
using ACadSharp.Attributes;
2
using ACadSharp.Entities;
3
using ACadSharp.Objects;
4
using ACadSharp.Tables;
5
using CSMath;
6
using CSUtilities.Extensions;
7
using System;
8
using System.Collections.Generic;
9

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

38
                /// <inheritdoc/>
39
                [DxfCodeValue(62, 420)]
40
                public Color Color { get; set; } = Color.ByLayer;
453,975✔
41

42
                /// <inheritdoc/>
43
                [DxfCodeValue(60)]
44
                public bool IsInvisible { get; set; } = false;
375,171✔
45

46
                /// <inheritdoc/>
47
                [DxfCodeValue(DxfReferenceType.Name, 8)]
48
                public Layer Layer
49
                {
50
                        get { return this._layer; }
425,880✔
51
                        set
52
                        {
235,876✔
53
                                if (value == null)
235,876!
54
                                {
×
55
                                        throw new ArgumentNullException(nameof(value));
×
56
                                }
57

58
                                if (this.Document != null)
235,876✔
59
                                {
121,075✔
60
                                        this._layer = this.updateTable(value, this.Document.Layers);
121,075✔
61
                                }
121,075✔
62
                                else
63
                                {
114,801✔
64
                                        this._layer = value;
114,801✔
65
                                }
114,801✔
66
                        }
235,876✔
67
                }
68

69
                /// <inheritdoc/>
70
                [DxfCodeValue(DxfReferenceType.Name, 6)]
71
                public LineType LineType
72
                {
73
                        get { return this._lineType; }
438,663✔
74
                        set
75
                        {
65,355✔
76
                                if (value == null)
65,355!
77
                                {
×
78
                                        throw new ArgumentNullException(nameof(value));
×
79
                                }
80

81
                                if (this.Document != null)
65,355✔
82
                                {
50,888✔
83
                                        this._lineType = this.updateTable(value, this.Document.LineTypes);
50,888✔
84
                                }
50,888✔
85
                                else
86
                                {
14,467✔
87
                                        this._lineType = value;
14,467✔
88
                                }
14,467✔
89
                        }
65,355✔
90
                }
91

92
                /// <inheritdoc/>
93
                [DxfCodeValue(48)]
94
                public double LinetypeScale { get; set; } = 1.0;
375,490✔
95

96
                /// <inheritdoc/>
97
                [DxfCodeValue(370)]
98
                public LineweightType LineWeight { get; set; } = LineweightType.ByLayer;
380,546✔
99

100
                /// <inheritdoc/>
101
                [DxfCodeValue(DxfReferenceType.Handle, 347)]
102
                public Material Material { get; set; }
1,920✔
103

104
                /// <inheritdoc/>
105
                public override string SubclassMarker => DxfSubclassMarker.Entity;
440,803✔
106

107
                /// <inheritdoc/>
108
                [DxfCodeValue(440)]
109
                public Transparency Transparency { get; set; } = Transparency.ByLayer;
389,279✔
110

111
                private BookColor _bookColor = null;
299,024✔
112

113
                private Layer _layer = Layer.Default;
299,024✔
114

115
                private LineType _lineType = LineType.ByLayer;
299,024✔
116

117
                /// <inheritdoc/>
118
                public Entity() : base() { }
897,072✔
119

120
                /// <summary>
121
                /// Apply a translation to this entity.
122
                /// </summary>
123
                /// <param name="translation"></param>
124
                public void ApplyTranslation(XYZ translation)
125
                {
1✔
126
                        Transform transform = Transform.CreateTranslation(translation);
1✔
127
                        this.ApplyTransform(transform);
1✔
128
                }
1✔
129

130
                /// <summary>
131
                /// Apply a rotation to this entity.
132
                /// </summary>
133
                /// <param name="axis"></param>
134
                /// <param name="rotation"></param>
135
                public void ApplyRotation(XYZ axis, double rotation)
136
                {
×
137
                        Transform transform = Transform.CreateRotation(axis, rotation);
×
138
                        this.ApplyTransform(transform);
×
139
                }
×
140

141
                /// <summary>
142
                /// Apply a scale to this entity.
143
                /// </summary>
144
                /// <param name="scale"></param>
145
                public void ApplyScaling(XYZ scale)
146
                {
×
147
                        Transform transform = Transform.CreateScaling(scale);
×
148
                        this.ApplyTransform(transform);
×
149
                }
×
150

151
                public void ApplyScaling(XYZ scale, XYZ origin)
152
                {
×
153
                        Transform transform = Transform.CreateScaling(scale, origin);
×
154
                        this.ApplyTransform(transform);
×
155
                }
×
156

157
                /// <inheritdoc/>
158
                public abstract void ApplyTransform(Transform transform);
159

160
                /// <inheritdoc/>
161
                public override CadObject Clone()
162
                {
960✔
163
                        Entity clone = (Entity)base.Clone();
960✔
164

165
                        clone.Layer = (Layer)this.Layer.Clone();
960✔
166
                        clone.LineType = (LineType)this.LineType.Clone();
960✔
167
                        clone.Material = (Material)this.Material?.Clone();
960!
168

169
                        return clone;
960✔
170
                }
960✔
171

172
                /// <inheritdoc/>
173
                public Color GetActiveColor()
174
                {
22✔
175
                        Color color;
176
                        if (this.Color.IsByLayer)
22✔
177
                        {
18✔
178
                                color = this.Layer.Color;
18✔
179
                        }
18✔
180
                        else
181
                        {
4✔
182
                                color = this.Color;
4✔
183
                        }
4✔
184

185
                        return color;
22✔
186
                }
22✔
187

188
                /// <inheritdoc/>
189
                public abstract BoundingBox GetBoundingBox();
190

191
                /// <inheritdoc/>
192
                public void MatchProperties(IEntity entity)
193
                {
13✔
194
                        if (entity is null)
13!
195
                        {
×
196
                                throw new ArgumentNullException(nameof(entity));
×
197
                        }
198

199
                        if (entity.Handle == 0)
13!
200
                        {
13✔
201
                                entity.Layer = (Layer)this.Layer.Clone();
13✔
202
                                entity.LineType = (LineType)this.LineType.Clone();
13✔
203
                        }
13✔
204
                        else
205
                        {
×
206
                                entity.Layer = this.Layer;
×
207
                                entity.LineType = this.LineType;
×
208
                        }
×
209

210
                        entity.Color = this.Color;
13✔
211
                        entity.LineWeight = this.LineWeight;
13✔
212
                        entity.LinetypeScale = this.LinetypeScale;
13✔
213
                        entity.IsInvisible = this.IsInvisible;
13✔
214
                        entity.Transparency = this.Transparency;
13✔
215
                }
13✔
216

217
                internal override void AssignDocument(CadDocument doc)
218
                {
125,603✔
219
                        base.AssignDocument(doc);
125,603✔
220

221
                        this._layer = this.updateTable(this.Layer, doc.Layers);
125,603✔
222
                        this._lineType = this.updateTable(this.LineType, doc.LineTypes);
125,603✔
223

224
                        doc.Layers.OnRemove += this.tableOnRemove;
125,603✔
225
                        doc.LineTypes.OnRemove += this.tableOnRemove;
125,603✔
226
                }
125,603✔
227

228
                internal override void UnassignDocument()
229
                {
2,345✔
230
                        this.Document.Layers.OnRemove -= this.tableOnRemove;
2,345✔
231
                        this.Document.LineTypes.OnRemove -= this.tableOnRemove;
2,345✔
232

233
                        base.UnassignDocument();
2,345✔
234

235
                        this.Layer = (Layer)this.Layer.Clone();
2,345✔
236
                        this.LineType = (LineType)this.LineType.Clone();
2,345✔
237
                }
2,345✔
238

239
                protected XYZ transformNormal(Transform transform, XYZ normal)
240
                {
12✔
241
                        return transform.Rotate(normal).Normalize();
12✔
242
                }
12✔
243

244
                protected virtual void tableOnRemove(object sender, CollectionChangedEventArgs e)
245
                {
10✔
246
                        if (e.Item.Equals(this.Layer))
10✔
247
                        {
1✔
248
                                this.Layer = this.Document.Layers[Layer.DefaultName];
1✔
249
                        }
1✔
250

251
                        if (e.Item.Equals(this.LineType))
10✔
252
                        {
1✔
253
                                this.LineType = this.Document.LineTypes[LineType.ByLayerName];
1✔
254
                        }
1✔
255
                }
10✔
256

257
                protected Matrix3 getWorldMatrix(Transform transform, XYZ normal, XYZ newNormal, out Matrix3 transOW, out Matrix3 transWO)
258
                {
9✔
259
                        transOW = Matrix3.ArbitraryAxis(normal);
9✔
260
                        transWO = Matrix3.ArbitraryAxis(newNormal).Transpose();
9✔
261
                        return new Matrix3(transform.Matrix);
9✔
262
                }
9✔
263

264
                protected XYZ applyWorldMatrix(XYZ xyz, Transform transform, Matrix3 transOW, Matrix3 transWO)
265
                {
×
266
                        XYZ v = transOW * xyz;
×
267
                        v = transform.ApplyTransform(v);
×
268
                        v = transWO * v;
×
269
                        return v;
×
270
                }
×
271

272
                protected List<XY> applyRotation(IEnumerable<XY> points, double rotation)
273
                {
×
274
                        if (points == null)
×
275
                        {
×
276
                                throw new ArgumentNullException(nameof(points));
×
277
                        }
278

279
                        if (MathHelper.IsZero(rotation))
×
280
                        {
×
281
                                return new List<XY>(points);
×
282
                        }
283

284
                        double sin = Math.Sin(rotation);
×
285
                        double cos = Math.Cos(rotation);
×
286

287
                        List<XY> transPoints;
288

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

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

304
                        Matrix3 trans = Matrix3.ArbitraryAxis(zAxis);
×
305
                        List<XYZ> transPoints;
306
                        transPoints = new List<XYZ>();
×
307
                        foreach (XYZ p in points)
×
308
                        {
×
309
                                transPoints.Add(trans * p);
×
310
                        }
×
311
                        return transPoints;
×
312
                }
×
313
        }
314
}
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