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

DomCR / ACadSharp / 17737836230

15 Sep 2025 03:12PM UTC coverage: 2.092% (-76.2%) from 78.245%
17737836230

push

github

web-flow
Merge pull request #790 from DomCR/addflag-refactor

addflag refactor

141 of 9225 branches covered (1.53%)

Branch coverage included in aggregate %.

0 of 93 new or added lines in 10 files covered. (0.0%)

24910 existing lines in 372 files now uncovered.

724 of 32119 relevant lines covered (2.25%)

5.76 hits per line

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

0.0
/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>
UNCOV
29
                public SeqendCollection<AttributeEntity> Attributes { get; private set; }
×
30

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

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

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

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

56
                /// <inheritdoc/>
UNCOV
57
                public override bool HasDynamicSubclass => true;
×
58

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

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

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

76
                /// <inheritdoc/>
UNCOV
77
                public override string ObjectName => DxfFileToken.EntityInsert;
×
78

79
                /// <inheritdoc/>
80
                public override ObjectType ObjectType
81
                {
82
                        get
UNCOV
83
                        {
×
UNCOV
84
                                if (this.RowCount > 1 || this.ColumnCount > 1)
×
UNCOV
85
                                {
×
UNCOV
86
                                        return ObjectType.MINSERT;
×
87
                                }
88
                                else
UNCOV
89
                                {
×
UNCOV
90
                                        return ObjectType.INSERT;
×
91
                                }
UNCOV
92
                        }
×
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)]
UNCOV
102
                public double Rotation { get; set; } = 0.0;
×
103

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

110
                /// <summary>
111
                /// Row spacing
112
                /// </summary>
113
                [DxfCodeValue(DxfReferenceType.Optional, 45)]
UNCOV
114
                public double RowSpacing { get; set; } = 0;
×
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
122
                        {
×
123
                                if (this.XDictionary != null
×
124
                                        && this.XDictionary.TryGetEntry(Filter.FilterEntryName, out CadDictionary filters))
×
125
                                {
×
126
                                        return filters.GetEntry<SpatialFilter>(SpatialFilter.SpatialFilterEntryName);
×
127
                                }
128

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

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

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

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

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

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

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

UNCOV
215
                private double _xscale = 1;
×
216

UNCOV
217
                private double _yscale = 1;
×
218

UNCOV
219
                private double _zscale = 1;
×
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>
UNCOV
226
                public Insert(BlockRecord block) : this()
×
UNCOV
227
                {
×
UNCOV
228
                        if (block is null) throw new ArgumentNullException(nameof(block));
×
229

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

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

UNCOV
245
                internal Insert() : base()
×
UNCOV
246
                {
×
UNCOV
247
                        this.Attributes = new SeqendCollection<AttributeEntity>(this);
×
UNCOV
248
                }
×
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()
UNCOV
293
                {
×
UNCOV
294
                        Insert clone = (Insert)base.Clone();
×
295

UNCOV
296
                        clone.Block = (BlockRecord)this.Block?.Clone();
×
297

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

UNCOV
304
                        return clone;
×
UNCOV
305
                }
×
306

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

344
                                c.ApplyTransform(transform);
×
345

346
                                yield return c;
×
347
                        }
×
348
                }
×
349

350
                /// <inheritdoc/>
351
                public override BoundingBox GetBoundingBox()
UNCOV
352
                {
×
UNCOV
353
                        BoundingBox box = this.Block.GetBoundingBox();
×
354

UNCOV
355
                        var scale = new XYZ(this.XScale, this.YScale, this.ZScale);
×
UNCOV
356
                        var min = box.Min * scale + this.InsertPoint;
×
UNCOV
357
                        var max = box.Max * scale + this.InsertPoint;
×
358

UNCOV
359
                        return new BoundingBox(min, max);
×
UNCOV
360
                }
×
361

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

UNCOV
373
                        return new Transform(world * translation.Matrix * rotation.Matrix * scale.Matrix);
×
UNCOV
374
                }
×
375

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

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

395
                        foreach (AttributeDefinition attdef in this.Block.AttributeDefinitions)
×
396
                        {
×
397
                                if (!this.Attributes.Select(d => d.Tag).Contains(attdef.Tag))
×
398
                                {
×
399
                                        AttributeEntity att = new AttributeEntity(attdef);
×
400

401
                                        this.Attributes.Add(att);
×
402
                                }
×
403
                        }
×
404
                }
×
405

406
                internal override void AssignDocument(CadDocument doc)
UNCOV
407
                {
×
UNCOV
408
                        base.AssignDocument(doc);
×
409

UNCOV
410
                        doc.RegisterCollection(this.Attributes);
×
411

412
                        //Should only be triggered for internal use
UNCOV
413
                        if (this.Block == null)
×
UNCOV
414
                                return;
×
415

UNCOV
416
                        if (doc.BlockRecords.TryGetValue(this.Block.Name, out BlockRecord blk))
×
UNCOV
417
                        {
×
UNCOV
418
                                this.Block = blk;
×
UNCOV
419
                        }
×
420
                        else
UNCOV
421
                        {
×
UNCOV
422
                                doc.BlockRecords.Add(this.Block);
×
UNCOV
423
                        }
×
UNCOV
424
                }
×
425

426
                internal override void UnassignDocument()
UNCOV
427
                {
×
UNCOV
428
                        this.Block = (BlockRecord)this.Block.Clone();
×
UNCOV
429
                        this.Document.UnregisterCollection(this.Attributes);
×
430

UNCOV
431
                        base.UnassignDocument();
×
UNCOV
432
                }
×
433
        }
434
}
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