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

DomCR / ACadSharp / 17217466513

25 Aug 2025 06:33PM UTC coverage: 78.151% (-0.1%) from 78.27%
17217466513

Pull #749

github

web-flow
Merge d4cbfc409 into 5776981da
Pull Request #749: Issue 745 insert explode

6520 of 9071 branches covered (71.88%)

Branch coverage included in aggregate %.

19 of 79 new or added lines in 9 files covered. (24.05%)

31 existing lines in 5 files now uncovered.

25311 of 31659 relevant lines covered (79.95%)

104973.24 hits per line

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

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

10
namespace ACadSharp.Entities
11
{
12
        /// <summary>
13
        /// Represents a <see cref="Insert"/> entity.
14
        /// </summary>
15
        /// <remarks>
16
        /// Object name <see cref="DxfFileToken.EntityInsert"/> <br/>
17
        /// Dxf class name <see cref="DxfSubclassMarker.Insert"/>
18
        /// </remarks>
19
        [DxfName(DxfFileToken.EntityInsert)]
20
        [DxfSubClass(DxfSubclassMarker.Insert)]
21
        public class Insert : Entity
22
        {
23
                /// <summary>
24
                /// Attributes from the block reference
25
                /// </summary>
26
                /// <remarks>
27
                /// If an attribute should be added in this collection a definition will be added into the block reference as well
28
                /// </remarks>
29
                public SeqendCollection<AttributeEntity> Attributes { get; private set; }
21,057✔
30

31
                /// <summary>
32
                /// Gets the insert block definition.
33
                /// </summary>
34
                [DxfCodeValue(DxfReferenceType.Name, 2)]
35
                public BlockRecord Block { get; internal set; }
19,982✔
36

37
                /// <summary>
38
                /// Column count
39
                /// </summary>
40
                [DxfCodeValue(DxfReferenceType.Optional, 70)]
41
                public ushort ColumnCount { get; set; } = 1;
9,643✔
42

43
                /// <summary>
44
                /// Column spacing
45
                /// </summary>
46
                [DxfCodeValue(DxfReferenceType.Optional, 44)]
47
                public double ColumnSpacing { get; set; } = 0;
9,208✔
48

49
                /// <summary>
50
                /// True if the insert has attribute entities in it
51
                /// </summary>
52
                [DxfCodeValue(DxfReferenceType.Ignored, 66)]
53
                public bool HasAttributes
54
                { get { return this.Attributes.Any(); } }
1,362✔
55

56
                /// <inheritdoc/>
57
                public override bool HasDynamicSubclass => true;
2✔
58

59
                /// <summary>
60
                /// A 3D WCS coordinate representing the insertion or origin point.
61
                /// </summary>
62
                [DxfCodeValue(10, 20, 30)]
63
                public XYZ InsertPoint { get; set; } = XYZ.Zero;
50,402✔
64

65
                /// <summary>
66
                /// Specifies the rotation angle for the object.
67
                /// </summary>
68
                public bool IsMultiple { get { return this.RowCount > 1 || this.ColumnCount > 1; } }
1,206✔
69

70
                /// <summary>
71
                /// Specifies the three-dimensional normal unit vector for the object.
72
                /// </summary>
73
                [DxfCodeValue(210, 220, 230)]
74
                public XYZ Normal { get; set; } = XYZ.AxisZ;
11,282✔
75

76
                /// <inheritdoc/>
77
                public override string ObjectName => DxfFileToken.EntityInsert;
14,315✔
78

79
                /// <inheritdoc/>
80
                public override ObjectType ObjectType
81
                {
82
                        get
83
                        {
57✔
84
                                if (this.RowCount > 1 || this.ColumnCount > 1)
57✔
85
                                {
16✔
86
                                        return ObjectType.MINSERT;
16✔
87
                                }
88
                                else
89
                                {
41✔
90
                                        return ObjectType.INSERT;
41✔
91
                                }
92
                        }
57✔
93
                }
94

95
                /// <summary>
96
                /// Specifies the rotation angle for the object.
97
                /// </summary>
98
                /// <value>
99
                /// The rotation angle in radians.
100
                /// </value>
101
                [DxfCodeValue(DxfReferenceType.IsAngle, 50)]
102
                public double Rotation { get; set; } = 0.0;
12,038✔
103

104
                /// <summary>
105
                /// Row count
106
                /// </summary>
107
                [DxfCodeValue(DxfReferenceType.Optional, 71)]
108
                public ushort RowCount { get; set; } = 1;
9,667✔
109

110
                /// <summary>
111
                /// Row spacing
112
                /// </summary>
113
                [DxfCodeValue(DxfReferenceType.Optional, 45)]
114
                public double RowSpacing { get; set; } = 0;
9,208✔
115

116
                /// <summary>
117
                /// Gets or set the spatial filter entry for this <see cref="Insert"/> entity.
118
                /// </summary>
119
                public SpatialFilter SpatialFilter
120
                {
121
                        get
NEW
122
                        {
×
NEW
123
                                if (this.XDictionary != null
×
NEW
124
                                        && this.XDictionary.TryGetEntry(Filter.FilterEntryName, out CadDictionary filters))
×
NEW
125
                                {
×
NEW
126
                                        return filters.GetEntry<SpatialFilter>(SpatialFilter.SpatialFilterEntryName);
×
127
                                }
128

NEW
129
                                return null;
×
NEW
130
                        }
×
131
                        set
132
                        {
9✔
133
                                if (this.XDictionary == null)
9✔
134
                                {
9✔
135
                                        this.CreateExtendedDictionary();
9✔
136
                                }
9✔
137

138
                                if (!this.XDictionary.TryGetEntry(Filter.FilterEntryName, out CadDictionary filters))
9✔
139
                                {
9✔
140
                                        filters = new CadDictionary(Filter.FilterEntryName);
9✔
141
                                        this.XDictionary.Add(filters);
9✔
142
                                }
9✔
143

144
                                filters.Remove(SpatialFilter.SpatialFilterEntryName);
9✔
145
                                filters.Add(SpatialFilter.SpatialFilterEntryName, value);
9✔
146
                        }
9✔
147
                }
148

149
                /// <inheritdoc/>
150
                public override string SubclassMarker => this.IsMultiple ? DxfSubclassMarker.MInsert : DxfSubclassMarker.Insert;
374!
151

152
                /// <summary>
153
                /// X scale factor.
154
                /// </summary>
155
                [DxfCodeValue(41)]
156
                public double XScale
157
                {
158
                        get
159
                        {
471✔
160
                                return this._xscale;
471✔
161
                        }
471✔
162
                        set
163
                        {
5,183✔
164
                                if (value.Equals(0))
5,183✔
165
                                {
1✔
166
                                        string name = nameof(this.XScale);
1✔
167
                                        throw new ArgumentOutOfRangeException(name, value, $"{name} value must be none zero.");
1✔
168
                                }
169
                                this._xscale = value;
5,182✔
170
                        }
5,182✔
171
                }
172

173
                /// <summary>
174
                /// Y scale factor.
175
                /// </summary>
176
                [DxfCodeValue(42)]
177
                public double YScale
178
                {
179
                        get
180
                        {
415✔
181
                                return this._yscale;
415✔
182
                        }
415✔
183
                        set
184
                        {
4,955✔
185
                                if (value.Equals(0))
4,955✔
186
                                {
1✔
187
                                        string name = nameof(this.YScale);
1✔
188
                                        throw new ArgumentOutOfRangeException(name, value, $"{name} value must be none zero.");
1✔
189
                                }
190
                                this._yscale = value;
4,954✔
191
                        }
4,954✔
192
                }
193

194
                /// <summary>
195
                /// Z scale factor.
196
                /// </summary>
197
                [DxfCodeValue(43)]
198
                public double ZScale
199
                {
200
                        get
201
                        {
409✔
202
                                return this._zscale;
409✔
203
                        }
409✔
204
                        set
205
                        {
4,946✔
206
                                if (value.Equals(0))
4,946✔
207
                                {
1✔
208
                                        string name = nameof(this.ZScale);
1✔
209
                                        throw new ArgumentOutOfRangeException(name, value, $"{name} value must be none zero.");
1✔
210
                                }
211
                                this._zscale = value;
4,945✔
212
                        }
4,945✔
213
                }
214

215
                private double _xscale = 1;
8,801✔
216

217
                private double _yscale = 1;
8,801✔
218

219
                private double _zscale = 1;
8,801✔
220

221
                /// <summary>
222
                /// Constructor to reference an insert to a block record
223
                /// </summary>
224
                /// <param name="block">Block Record to reference</param>
225
                /// <exception cref="ArgumentNullException"></exception>
226
                public Insert(BlockRecord block) : this()
66✔
227
                {
66✔
228
                        if (block is null) throw new ArgumentNullException(nameof(block));
66!
229

230
                        if (block.Document != null)
66✔
231
                        {
20✔
232
                                this.Block = (BlockRecord)block.Clone();
20✔
233
                        }
20✔
234
                        else
235
                        {
46✔
236
                                this.Block = block;
46✔
237
                        }
46✔
238

239
                        foreach (var item in block.AttributeDefinitions)
240✔
240
                        {
21✔
241
                                this.Attributes.Add(new AttributeEntity(item));
21✔
242
                        }
21✔
243
                }
66✔
244

245
                internal Insert() : base()
8,801✔
246
                {
8,801✔
247
                        this.Attributes = new SeqendCollection<AttributeEntity>(this);
8,801✔
248
                }
8,801✔
249

250
                /// <inheritdoc/>
251
                public override void ApplyTransform(Transform transform)
252
                {
×
253
                        XYZ newPosition = transform.ApplyTransform(this.InsertPoint);
×
254
                        XYZ newNormal = this.transformNormal(transform, this.Normal);
×
255

256
                        Matrix3 transOW = Matrix3.ArbitraryAxis(this.Normal);
×
257
                        transOW *= Matrix3.RotationZ(this.Rotation);
×
258

259
                        Matrix3 transWO = Matrix3.ArbitraryAxis(newNormal);
×
260
                        transWO = transWO.Transpose();
×
261

262
                        var transformation = new Matrix3(transform.Matrix);
×
263
                        XYZ v = transOW * XYZ.AxisX;
×
264
                        v = transformation * v;
×
265
                        v = transWO * v;
×
266
                        double newRotation = new XY(v.X, v.Y).GetAngle();
×
267

268
                        transWO = Matrix3.RotationZ(newRotation).Transpose() * transWO;
×
269

270
                        XYZ s = transOW * new XYZ(this.XScale, this.YScale, this.ZScale);
×
271
                        s = transformation * s;
×
272
                        s = transWO * s;
×
273
                        XYZ newScale = new XYZ(
×
274
                                MathHelper.IsZero(s.X) ? MathHelper.Epsilon : s.X,
×
275
                                MathHelper.IsZero(s.Y) ? MathHelper.Epsilon : s.Y,
×
276
                                MathHelper.IsZero(s.Z) ? MathHelper.Epsilon : s.Z);
×
277

278
                        this.Normal = newNormal;
×
279
                        this.InsertPoint = newPosition;
×
280
                        this.XScale = newScale.X;
×
281
                        this.YScale = newScale.Y;
×
282
                        this.ZScale = newScale.Z;
×
283
                        this.Rotation = newRotation;
×
284

285
                        foreach (AttributeEntity att in this.Attributes)
×
286
                        {
×
287
                                att.ApplyTransform(transform);
×
288
                        }
×
289
                }
×
290

291
                /// <inheritdoc/>
292
                public override CadObject Clone()
293
                {
217✔
294
                        Insert clone = (Insert)base.Clone();
217✔
295

296
                        clone.Block = (BlockRecord)this.Block?.Clone();
217✔
297

298
                        clone.Attributes = new SeqendCollection<AttributeEntity>(clone);
217✔
299
                        foreach (var att in this.Attributes)
653✔
300
                        {
1✔
301
                                clone.Attributes.Add((AttributeEntity)att.Clone());
1✔
302
                        }
1✔
303

304
                        return clone;
217✔
305
                }
217✔
306

307
                public IEnumerable<Entity> Explode()
NEW
308
                {
×
NEW
309
                        Transform transform = this.GetTransform();
×
NEW
310
                        foreach (var e in this.Block.Entities)
×
NEW
311
                        {
×
312
                                Entity c;
NEW
313
                                switch (e)
×
314
                                {
315
                                        case Arc arc:
NEW
316
                                                c = new Ellipse()
×
NEW
317
                                                {
×
NEW
318
                                                        StartParameter = arc.StartAngle,
×
NEW
319
                                                        EndParameter = arc.EndAngle,
×
NEW
320
                                                        MajorAxisEndPoint = XYZ.AxisX * arc.Radius,
×
NEW
321
                                                        RadiusRatio = 1,
×
NEW
322
                                                        Center = arc.Center,
×
NEW
323
                                                };
×
NEW
324
                                                c.MatchProperties(e);
×
NEW
325
                                                break;
×
326
                                        case Circle circle:
NEW
327
                                                c = new Ellipse()
×
NEW
328
                                                {
×
NEW
329
                                                        MajorAxisEndPoint = XYZ.AxisX * circle.Radius,
×
NEW
330
                                                        RadiusRatio = 1,
×
NEW
331
                                                        Center = circle.Center,
×
NEW
332
                                                };
×
NEW
333
                                                c.MatchProperties(e);
×
NEW
334
                                                break;
×
335
                                        default:
NEW
336
                                                c = e.CloneTyped();
×
NEW
337
                                                break;
×
338
                                }
339

NEW
340
                                c.ApplyTransform(transform);
×
341

NEW
342
                                yield return c;
×
NEW
343
                        }
×
NEW
344
                }
×
345

346
                /// <inheritdoc/>
347
                public override BoundingBox GetBoundingBox()
348
                {
3✔
349
                        BoundingBox box = this.Block.GetBoundingBox();
3✔
350

351
                        var scale = new XYZ(this.XScale, this.YScale, this.ZScale);
3✔
352
                        var min = box.Min * scale + this.InsertPoint;
3✔
353
                        var max = box.Max * scale + this.InsertPoint;
3✔
354

355
                        return new BoundingBox(min, max);
3✔
356
                }
3✔
357

358
                /// <summary>
359
                /// Get the transform that will be applied to the entities in the <see cref="BlockRecord"/> when this entity is processed.
360
                /// </summary>
361
                /// <returns></returns>
362
                public Transform GetTransform()
363
                {
1✔
364
                        var world = Matrix4.GetArbitraryAxis(this.Normal);
1✔
365
                        var translation = Transform.CreateTranslation(this.InsertPoint);
1✔
366
                        var rotation = Transform.CreateRotation(XYZ.AxisZ, this.Rotation);
1✔
367
                        var scale = Transform.CreateScaling(new XYZ(this.XScale, this.YScale, this.ZScale));
1✔
368

369
                        return new Transform(world * translation.Matrix * rotation.Matrix * scale.Matrix);
1✔
370
                }
1✔
371

372
                /// <summary>
373
                /// Updates all attribute definitions contained in the block reference as <see cref="AttributeDefinition"/> entities in the insert.
374
                /// </summary>
375
                /// <remarks>
376
                /// This will update the attributes based on their <see cref="AttributeBase.Tag"/>.
377
                /// </remarks>
378
                public void UpdateAttributes()
379
                {
×
380
                        var atts = this.Attributes.ToArray();
×
381

382
                        foreach (AttributeEntity att in atts)
×
383
                        {
×
384
                                //Tags are not unique, is it needed? check how the different applications link the atts
385
                                if (!this.Block.AttributeDefinitions.Select(d => d.Tag).Contains(att.Tag))
×
386
                                {
×
387
                                        this.Attributes.Remove(att);
×
388
                                }
×
389
                        }
×
390

391
                        foreach (AttributeDefinition attdef in this.Block.AttributeDefinitions)
×
392
                        {
×
393
                                if (!this.Attributes.Select(d => d.Tag).Contains(attdef.Tag))
×
394
                                {
×
395
                                        AttributeEntity att = new AttributeEntity(attdef);
×
396

397
                                        this.Attributes.Add(att);
×
398
                                }
×
399
                        }
×
400
                }
×
401

402
                internal override void AssignDocument(CadDocument doc)
403
                {
8,856✔
404
                        base.AssignDocument(doc);
8,856✔
405

406
                        doc.RegisterCollection(this.Attributes);
8,856✔
407

408
                        //Should only be triggered for internal use
409
                        if (this.Block == null)
8,856✔
410
                                return;
8,314✔
411

412
                        if (doc.BlockRecords.TryGetValue(this.Block.Name, out BlockRecord blk))
542✔
413
                        {
163✔
414
                                this.Block = blk;
163✔
415
                        }
163✔
416
                        else
417
                        {
379✔
418
                                doc.BlockRecords.Add(this.Block);
379✔
419
                        }
379✔
420
                }
8,856✔
421

422
                internal override void UnassignDocument()
423
                {
276✔
424
                        this.Block = (BlockRecord)this.Block.Clone();
276✔
425
                        this.Document.UnregisterCollection(this.Attributes);
276✔
426

427
                        base.UnassignDocument();
276✔
428
                }
276✔
429
        }
430
}
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