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

DomCR / ACadSharp / 14214082189

02 Apr 2025 07:34AM UTC coverage: 75.523% (-0.8%) from 76.343%
14214082189

Pull #457

github

web-flow
Merge b2c68aa3f into 04434c5d6
Pull Request #457: Geometric transform

5606 of 8150 branches covered (68.79%)

Branch coverage included in aggregate %.

282 of 716 new or added lines in 47 files covered. (39.39%)

318 existing lines in 23 files now uncovered.

22348 of 28864 relevant lines covered (77.43%)

72757.24 hits per line

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

84.77
/src/ACadSharp/Tables/BlockRecord.cs
1
using ACadSharp.Attributes;
2
using ACadSharp.Types.Units;
3
using ACadSharp.Objects;
4
using ACadSharp.Blocks;
5
using ACadSharp.Entities;
6
using System.Linq;
7
using System.Collections.Generic;
8
using ACadSharp.Objects.Evaluations;
9
using CSMath;
10

11
namespace ACadSharp.Tables
12
{
13
        /// <summary>
14
        /// Represents a <see cref="BlockRecord"/> entry
15
        /// </summary>
16
        /// <remarks>
17
        /// Object name <see cref="DxfFileToken.TableBlockRecord"/> <br/>
18
        /// Dxf class name <see cref="DxfSubclassMarker.BlockRecord"/>
19
        /// </remarks>
20
        [DxfName(DxfFileToken.TableBlockRecord)]
21
        [DxfSubClass(DxfSubclassMarker.BlockRecord)]
22
        public class BlockRecord : TableEntry, IGeometricEntity
23
        {
24
                /// <summary>
25
                /// Create an instance of the *Model_Space block.
26
                /// </summary>
27
                /// <remarks>
28
                /// It only can be one Model in each document.
29
                /// </remarks>
30
                public static BlockRecord ModelSpace
31
                {
32
                        get
33
                        {
175✔
34
                                BlockRecord record = new BlockRecord(ModelSpaceName);
175✔
35

36
                                Layout layout = new Layout();
175✔
37
                                layout.Name = Layout.ModelLayoutName;
175✔
38
                                layout.AssociatedBlock = record;
175✔
39

40
                                return record;
175✔
41
                        }
175✔
42
                }
43

44
                /// <summary>
45
                /// Create an instance of the *Paper_Space block.
46
                /// </summary>
47
                /// <remarks>
48
                /// This is the default paper space in the document.
49
                /// </remarks>
50
                public static BlockRecord PaperSpace
51
                {
52
                        get
53
                        {
175✔
54
                                BlockRecord record = new BlockRecord(PaperSpaceName);
175✔
55

56
                                Layout layout = new Layout();
175✔
57
                                layout.Name = Layout.PaperLayoutName;
175✔
58
                                layout.AssociatedBlock = record;
175✔
59

60
                                return record;
175✔
61
                        }
175✔
62
                }
63

64
                /// <summary>
65
                /// Attribute definitions in this block
66
                /// </summary>
67
                public IEnumerable<AttributeDefinition> AttributeDefinitions
68
                {
69
                        get
70
                        {
22✔
71
                                return this.Entities.OfType<AttributeDefinition>();
22✔
72
                        }
22✔
73
                }
74

75
                /// <summary>
76
                /// End block entity for this Block record.
77
                /// </summary>
78
                public BlockEnd BlockEnd
79
                {
80
                        get { return this._blockEnd; }
61,167✔
81
                        internal set
82
                        {
20,739✔
83
                                this._blockEnd = value;
20,739✔
84
                                this._blockEnd.Owner = this;
20,739✔
85
                        }
20,739✔
86
                }
87

88
                /// <summary>
89
                /// Block entity for this record
90
                /// </summary>
91
                public Block BlockEntity
92
                {
93
                        get { return this._blockEntity; }
94,224✔
94
                        internal set
95
                        {
27,937✔
96
                                this._blockEntity = value;
27,937✔
97
                                this._blockEntity.Owner = this;
27,937✔
98
                        }
27,937✔
99
                }
100

101
                /// <summary>
102
                /// Specifies the scaling allowed for the block.
103
                /// </summary>
104
                [DxfCodeValue(DxfReferenceType.Optional, 281)]
105
                public bool CanScale { get; set; } = true;
23,037✔
106

107
                /// <summary>
108
                /// Entities owned by this block.
109
                /// </summary>
110
                /// <remarks>
111
                /// Entities with an owner cannot be added to another block.
112
                /// </remarks>
113
                public CadObjectCollection<Entity> Entities { get; private set; }
130,420✔
114

115
                /// <summary>
116
                /// Gets the evaluation graph for this block if it has dynamic properties attached to it.
117
                /// </summary>
118
                public EvaluationGraph EvaluationGraph
119
                {
120
                        get
121
                        {
13✔
122
                                if (this.XDictionary == null)
13!
123
                                {
×
124
                                        return null;
×
125
                                }
126
                                else if (this.XDictionary.TryGetEntry(EvaluationGraph.DictionaryEntryName, out EvaluationGraph table))
13!
127
                                {
13✔
128
                                        return table;
13✔
129
                                }
130
                                else
131
                                {
×
132
                                        return null;
×
133
                                }
134
                        }
13✔
135
                }
136

137
                //This seems to be the right way to set the flags for the block records
138
                public new BlockTypeFlags Flags { get { return this.BlockEntity.Flags; } set { this.BlockEntity.Flags = value; } }
1,716✔
139

140
                /// <summary>
141
                /// Flag indicating if the Block has Attributes attached
142
                /// </summary>
143
                public bool HasAttributes
144
                {
145
                        get
146
                        {
84✔
147
                                return this.Entities.OfType<AttributeDefinition>().Any();
84✔
148
                        }
84✔
149
                }
150

151
                /// <summary>
152
                /// Active flag if it has an <see cref="Objects.Evaluations.EvaluationGraph"/> attached to it with dynamic expressions.
153
                /// </summary>
154
                public bool IsDynamic
155
                {
156
                        get
157
                        {
13✔
158
                                return this.EvaluationGraph != null;
13✔
159
                        }
13✔
160
                }
161

162
                /// <summary>
163
                /// Specifies whether the block can be exploded.
164
                /// </summary>
165
                [DxfCodeValue(DxfReferenceType.Optional, 280)]
166
                public bool IsExplodable { get; set; }
5,526✔
167

168
                /// <summary>
169
                /// Associated Layout.
170
                /// </summary>
171
                [DxfCodeValue(DxfReferenceType.Handle, 340)]
172
                public Layout Layout
173
                {
174
                        get { return this._layout; }
3,198✔
175
                        internal set
176
                        {
3,028✔
177
                                this._layout = value;
3,028✔
178
                        }
3,028✔
179
                }
180

181
                /// <inheritdoc/>
182
                public override string ObjectName => DxfFileToken.TableBlockRecord;
530✔
183

184
                /// <inheritdoc/>
185
                public override ObjectType ObjectType => ObjectType.BLOCK_HEADER;
168✔
186

187
                /// <summary>
188
                /// DXF: Binary data for bitmap preview.
189
                /// </summary>
190
                /// <remarks>
191
                /// Optional
192
                /// </remarks>
193
                [DxfCodeValue(DxfReferenceType.Optional, 310)]
194
                public byte[] Preview { get; set; }
16,147✔
195

196
                /// <summary>
197
                /// Sort entities table for this block record.
198
                /// </summary>
199
                public SortEntitiesTable SortEntitiesTable
200
                {
201
                        get
202
                        {
3✔
203
                                if (this.XDictionary == null)
3!
204
                                {
×
205
                                        return null;
×
206
                                }
207
                                else if (this.XDictionary.TryGetEntry(SortEntitiesTable.DictionaryEntryName, out SortEntitiesTable table))
3!
208
                                {
3✔
209
                                        return table;
3✔
210
                                }
211
                                else
212
                                {
×
213
                                        return null;
×
214
                                }
215
                        }
3✔
216
                }
217

218
                /// <inheritdoc/>
219
                public override string SubclassMarker => DxfSubclassMarker.BlockRecord;
56,525✔
220

221
                /// <summary>
222
                /// Block insertion units
223
                /// </summary>
224
                // [DxfCodeValue(70)]        //Table entry uses flags and has the same code but dwg saves also the block record flags
225
                public UnitsType Units { get; set; }
2,365✔
226

227
                /// <summary>
228
                /// ViewPorts attached to this block
229
                /// </summary>
230
                public IEnumerable<Viewport> Viewports
231
                {
232
                        get
233
                        {
315✔
234
                                return this.Entities.OfType<Viewport>();
315✔
235
                        }
315✔
236
                }
237

238
                /// <summary>
239
                /// Default block record name for the model space
240
                /// </summary>
241
                public const string ModelSpaceName = "*Model_Space";
242

243
                /// <summary>
244
                /// Default block record name for the paper space
245
                /// </summary>
246
                public const string PaperSpaceName = "*Paper_Space";
247

248
                private BlockEnd _blockEnd;
249

250
                private Block _blockEntity;
251

252
                private Layout _layout;
253

254
                /// <summary>
255
                /// Default constructor.
256
                /// </summary>
257
                /// <param name="name">Unique name for this block record.</param>
258
                public BlockRecord(string name) : base(name)
3,387✔
259
                {
3,387✔
260
                        this.BlockEntity = new Block(this);
3,387✔
261
                        this.BlockEnd = new BlockEnd(this);
3,387✔
262
                        this.Entities = new CadObjectCollection<Entity>(this);
3,387✔
263
                }
3,387✔
264

265
                internal BlockRecord() : base()
14,124✔
266
                {
14,124✔
267
                        this.BlockEntity = new Block(this);
14,124✔
268
                        this.BlockEnd = new BlockEnd(this);
14,124✔
269
                        this.Entities = new CadObjectCollection<Entity>(this);
14,124✔
270
                }
14,124✔
271

272
                /// <inheritdoc/>
273
                public override CadObject Clone()
274
                {
118✔
275
                        BlockRecord clone = (BlockRecord)base.Clone();
118✔
276

277
                        Layout layout = (Layout)(this.Layout?.Clone());
118!
278
                        if (layout is not null)
118!
NEW
279
                        {
×
NEW
280
                                layout.AssociatedBlock = this;
×
UNCOV
281
                        }
×
282

283
                        clone.Entities = new CadObjectCollection<Entity>(clone);
118✔
284
                        foreach (var item in this.Entities)
1,518✔
285
                        {
582✔
286
                                clone.Entities.Add((Entity)item.Clone());
582✔
287
                        }
582✔
288

289
                        clone.BlockEntity = (Block)this.BlockEntity.Clone();
118✔
290
                        clone.BlockEntity.Owner = clone;
118✔
291
                        clone.BlockEnd = (BlockEnd)this.BlockEnd.Clone();
118✔
292
                        clone.BlockEnd.Owner = clone;
118✔
293

294
                        return clone;
118✔
295
                }
118✔
296

297
                /// <summary>
298
                ///
299
                /// </summary>
300
                /// <returns></returns>
301
                public SortEntitiesTable CreateSortEntitiesTable()
302
                {
1✔
303
                        CadDictionary dictionary = this.CreateExtendedDictionary();
1✔
304

305
                        if (dictionary.TryGetEntry(SortEntitiesTable.DictionaryEntryName, out SortEntitiesTable table))
1!
UNCOV
306
                        {
×
UNCOV
307
                                return table;
×
308
                        }
309

310
                        table = new SortEntitiesTable(this);
1✔
311

312
                        dictionary.Add(table);
1✔
313

314
                        return table;
1✔
315
                }
1✔
316

317
                /// <summary>
318
                /// Get the bounding box for all the entities in the block.
319
                /// </summary>
320
                /// <returns></returns>
321
                public BoundingBox GetBoundingBox()
322
                {
4✔
323
                        return this.GetBoundingBox(true);
4✔
324
                }
4✔
325

326
                /// <summary>
327
                /// Get the bounding box for all the entities in the block.
328
                /// </summary>
329
                /// <param name="ignoreInfinite">Ignore infinite entities, default: true</param>
330
                /// <returns></returns>
331
                public BoundingBox GetBoundingBox(bool ignoreInfinite)
332
                {
4✔
333
                        BoundingBox box = BoundingBox.Null;
4✔
334
                        foreach (var item in this.Entities)
60✔
335
                        {
24✔
336
                                if (item.GetBoundingBox().Extent == BoundingBoxExtent.Infinite
24!
337
                                        && ignoreInfinite)
24✔
UNCOV
338
                                {
×
UNCOV
339
                                        continue;
×
340
                                }
341

342
                                box = box.Merge(item.GetBoundingBox());
24✔
343
                        }
24✔
344

345
                        return box;
4✔
346
                }
4✔
347

348
                internal override void AssignDocument(CadDocument doc)
349
                {
11,031✔
350
                        base.AssignDocument(doc);
11,031✔
351

352
                        doc.RegisterCollection(this.Entities);
11,031✔
353
                }
11,031✔
354

355
                internal override void UnassignDocument()
356
                {
1✔
357
                        this.Document.UnregisterCollection(this.Entities);
1✔
358

359
                        base.UnassignDocument();
1✔
360
                }
1✔
361
        }
362
}
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