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

DomCR / ACadSharp / 13724254762

07 Mar 2025 03:49PM UTC coverage: 75.29% (-0.8%) from 76.11%
13724254762

Pull #457

github

web-flow
Merge b30f393b9 into a1ba04cda
Pull Request #457: Geometric transform

5484 of 8007 branches covered (68.49%)

Branch coverage included in aggregate %.

220 of 615 new or added lines in 40 files covered. (35.77%)

292 existing lines in 16 files now uncovered.

21823 of 28262 relevant lines covered (77.22%)

74130.22 hits per line

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

59.46
/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
                /// <inheritdoc/>
19
                public override string SubclassMarker => DxfSubclassMarker.Entity;
440,803✔
20

21
                /// <inheritdoc/>
22
                [DxfCodeValue(DxfReferenceType.Name, 8)]
23
                public Layer Layer
24
                {
25
                        get { return this._layer; }
425,289✔
26
                        set
27
                        {
235,725✔
28
                                if (value == null)
235,725!
29
                                {
×
30
                                        throw new ArgumentNullException(nameof(value));
×
31
                                }
32

33
                                if (this.Document != null)
235,725✔
34
                                {
120,977✔
35
                                        this._layer = this.updateTable(value, this.Document.Layers);
120,977✔
36
                                }
120,977✔
37
                                else
38
                                {
114,748✔
39
                                        this._layer = value;
114,748✔
40
                                }
114,748✔
41
                        }
235,725✔
42
                }
43

44
                /// <inheritdoc/>
45
                [DxfCodeValue(62, 420)]
46
                public Color Color { get; set; } = Color.ByLayer;
453,567✔
47

48
                /// <inheritdoc/>
49
                [DxfCodeValue(370)]
50
                public LineweightType LineWeight { get; set; } = LineweightType.ByLayer;
380,193✔
51

52
                /// <inheritdoc/>
53
                [DxfCodeValue(48)]
54
                public double LinetypeScale { get; set; } = 1.0;
375,108✔
55

56
                /// <inheritdoc/>
57
                [DxfCodeValue(60)]
58
                public bool IsInvisible { get; set; } = false;
374,789✔
59

60
                /// <inheritdoc/>
61
                [DxfCodeValue(440)]
62
                public Transparency Transparency { get; set; } = Transparency.ByLayer;
388,897✔
63

64
                /// <inheritdoc/>
65
                [DxfCodeValue(DxfReferenceType.Name, 6)]
66
                public LineType LineType
67
                {
68
                        get { return this._lineType; }
438,177✔
69
                        set
70
                        {
65,204✔
71
                                if (value == null)
65,204!
72
                                {
×
73
                                        throw new ArgumentNullException(nameof(value));
×
74
                                }
75

76
                                if (this.Document != null)
65,204✔
77
                                {
50,790✔
78
                                        this._lineType = this.updateTable(value, this.Document.LineTypes);
50,790✔
79
                                }
50,790✔
80
                                else
81
                                {
14,414✔
82
                                        this._lineType = value;
14,414✔
83
                                }
14,414✔
84
                        }
65,204✔
85
                }
86

87
                /// <inheritdoc/>
88
                [DxfCodeValue(DxfReferenceType.Handle, 347)]
89
                public Material Material { get; set; }
1,820✔
90

91
                /// <summary>
92
                /// Book color for this entity.
93
                /// </summary> 
94
                [DxfCodeValue(DxfReferenceType.Name, 430)]
95
                public BookColor BookColor
96
                {
97
                        get { return this._bookColor; }
24,600✔
98
                        set
99
                        {
255✔
100
                                if (this.Document != null)
255!
101
                                {
255✔
102
                                        this._bookColor = this.updateCollection(value, this.Document.Colors);
255✔
103
                                }
255✔
104
                                else
105
                                {
×
106
                                        this._bookColor = value;
×
107
                                }
×
108
                        }
255✔
109
                }
110

111
                private Layer _layer = Layer.Default;
298,790✔
112

113
                private LineType _lineType = LineType.ByLayer;
298,790✔
114

115
                private BookColor _bookColor = null;
298,790✔
116

117
                /// <inheritdoc/>
118
                public Entity() : base() { }
896,370✔
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)
NEW
136
                {
×
NEW
137
                        Transform transform = Transform.CreateRotation(axis, rotation);
×
NEW
138
                        this.ApplyTransform(transform);
×
NEW
139
                }
×
140

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

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

157
                /// <summary>
158
                /// Apply a transform matrix to this entity.
159
                /// </summary>
160
                /// <param name="transform"></param>
161
                public abstract void ApplyTransform(Transform transform);
162

163
                /// <inheritdoc/>
164
                public abstract BoundingBox GetBoundingBox();
165

166
                /// <inheritdoc/>
167
                public void MatchProperties(IEntity entity)
168
                {
11✔
169
                        if (entity is null)
11!
170
                        {
×
171
                                throw new ArgumentNullException(nameof(entity));
×
172
                        }
173

174
                        if (entity.Handle == 0)
11!
175
                        {
11✔
176
                                entity.Layer = (Layer)this.Layer.Clone();
11✔
177
                                entity.LineType = (LineType)this.LineType.Clone();
11✔
178
                        }
11✔
179
                        else
180
                        {
×
181
                                entity.Layer = this.Layer;
×
182
                                entity.LineType = this.LineType;
×
183
                        }
×
184

185
                        entity.Color = this.Color;
11✔
186
                        entity.LineWeight = this.LineWeight;
11✔
187
                        entity.LinetypeScale = this.LinetypeScale;
11✔
188
                        entity.IsInvisible = this.IsInvisible;
11✔
189
                        entity.Transparency = this.Transparency;
11✔
190
                }
11✔
191

192
                /// <inheritdoc/>
193
                public override CadObject Clone()
194
                {
910✔
195
                        Entity clone = (Entity)base.Clone();
910✔
196

197
                        clone.Layer = (Layer)this.Layer.Clone();
910✔
198
                        clone.LineType = (LineType)this.LineType.Clone();
910✔
199
                        clone.Material = (Material)this.Material?.Clone();
910!
200

201
                        return clone;
910✔
202
                }
910✔
203

204
                internal override void AssignDocument(CadDocument doc)
205
                {
125,494✔
206
                        base.AssignDocument(doc);
125,494✔
207

208
                        this._layer = this.updateTable(this.Layer, doc.Layers);
125,494✔
209
                        this._lineType = this.updateTable(this.LineType, doc.LineTypes);
125,494✔
210

211
                        doc.Layers.OnRemove += this.tableOnRemove;
125,494✔
212
                        doc.LineTypes.OnRemove += this.tableOnRemove;
125,494✔
213
                }
125,494✔
214

215
                internal override void UnassignDocument()
216
                {
2,344✔
217
                        this.Document.Layers.OnRemove -= this.tableOnRemove;
2,344✔
218
                        this.Document.LineTypes.OnRemove -= this.tableOnRemove;
2,344✔
219

220
                        base.UnassignDocument();
2,344✔
221

222
                        this.Layer = (Layer)this.Layer.Clone();
2,344✔
223
                        this.LineType = (LineType)this.LineType.Clone();
2,344✔
224
                }
2,344✔
225

226
                protected XYZ transformNormal(Transform transform, XYZ normal)
227
                {
12✔
228
                        return transform.Rotate(normal).Normalize();
12✔
229
                }
12✔
230

231
                protected virtual void tableOnRemove(object sender, CollectionChangedEventArgs e)
232
                {
10✔
233
                        if (e.Item.Equals(this.Layer))
10✔
234
                        {
1✔
235
                                this.Layer = this.Document.Layers[Layer.DefaultName];
1✔
236
                        }
1✔
237

238
                        if (e.Item.Equals(this.LineType))
10✔
239
                        {
1✔
240
                                this.LineType = this.Document.LineTypes[LineType.ByLayerName];
1✔
241
                        }
1✔
242
                }
10✔
243

244
                protected Matrix3 getWorldMatrix(Transform transform, XYZ normal, XYZ newNormal, out Matrix3 transOW, out Matrix3 transWO)
245
                {
9✔
246
                        transOW = Matrix3.ArbitraryAxis(normal);
9✔
247
                        transWO = Matrix3.ArbitraryAxis(newNormal).Transpose();
9✔
248
                        return new Matrix3(transform.Matrix);
9✔
249
                }
9✔
250

251
                protected XYZ applyWorldMatrix(XYZ xyz, Transform transform, Matrix3 transOW, Matrix3 transWO)
NEW
252
                {
×
NEW
253
                        XYZ v = transOW * xyz;
×
NEW
254
                        v = transform.ApplyTransform(v);
×
NEW
255
                        v = transWO * v;
×
NEW
256
                        return v;
×
NEW
257
                }
×
258

259
                protected List<XY> applyRotation(IEnumerable<XY> points, double rotation)
NEW
260
                {
×
NEW
261
                        if (points == null)
×
NEW
262
                        {
×
NEW
263
                                throw new ArgumentNullException(nameof(points));
×
264
                        }
265

NEW
266
                        if (MathHelper.IsZero(rotation))
×
NEW
267
                        {
×
NEW
268
                                return new List<XY>(points);
×
269
                        }
270

NEW
271
                        double sin = Math.Sin(rotation);
×
NEW
272
                        double cos = Math.Cos(rotation);
×
273

274
                        List<XY> transPoints;
275

NEW
276
                        transPoints = new List<XY>();
×
NEW
277
                        foreach (XY p in points)
×
NEW
278
                        {
×
NEW
279
                                transPoints.Add(new XY(p.X * cos - p.Y * sin, p.X * sin + p.Y * cos));
×
NEW
280
                        }
×
NEW
281
                        return transPoints;
×
NEW
282
                }
×
283

284
                protected List<XYZ> applyRotation(IEnumerable<XYZ> points, XYZ zAxis)
NEW
285
                {
×
NEW
286
                        if (points == null)
×
NEW
287
                        {
×
NEW
288
                                throw new ArgumentNullException(nameof(points));
×
289
                        }
290

NEW
291
                        Matrix3 trans = Matrix3.ArbitraryAxis(zAxis);
×
292
                        List<XYZ> transPoints;
NEW
293
                        transPoints = new List<XYZ>();
×
NEW
294
                        foreach (XYZ p in points)
×
NEW
295
                        {
×
NEW
296
                                transPoints.Add(trans * p);
×
NEW
297
                        }
×
NEW
298
                        return transPoints;
×
NEW
299
                }
×
300
        }
301
}
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