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

DomCR / ACadSharp / 14084601072

26 Mar 2025 01:32PM UTC coverage: 76.345% (-3.4%) from 79.72%
14084601072

Pull #211

github

web-flow
Merge 070f025e9 into c902191a1
Pull Request #211: Cad to svg

5536 of 7962 branches covered (69.53%)

Branch coverage included in aggregate %.

259 of 305 new or added lines in 8 files covered. (84.92%)

5 existing lines in 2 files now uncovered.

21926 of 28009 relevant lines covered (78.28%)

74951.67 hits per line

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

84.46
/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
23
        {
24
                /// <summary>
25
                /// Default block record name for the model space
26
                /// </summary>
27
                public const string ModelSpaceName = "*Model_Space";
28

29
                /// <summary>
30
                /// Default block record name for the paper space
31
                /// </summary>
32
                public const string PaperSpaceName = "*Paper_Space";
33

34
                /// <summary>
35
                /// Create an instance of the *Model_Space block.
36
                /// </summary>
37
                /// <remarks>
38
                /// It only can be one Model in each document.
39
                /// </remarks>
40
                public static BlockRecord ModelSpace
41
                {
42
                        get
43
                        {
175✔
44
                                BlockRecord record = new BlockRecord(ModelSpaceName);
175✔
45

46
                                Layout layout = new Layout();
175✔
47
                                layout.Name = Layout.ModelLayoutName;
175✔
48
                                layout.AssociatedBlock = record;
175✔
49

50
                                return record;
175✔
51
                        }
175✔
52
                }
53

54
                /// <summary>
55
                /// Create an instance of the *Paper_Space block.
56
                /// </summary>
57
                /// <remarks>
58
                /// This is the default paper space in the document.
59
                /// </remarks>
60
                public static BlockRecord PaperSpace
61
                {
62
                        get
63
                        {
175✔
64
                                BlockRecord record = new BlockRecord(PaperSpaceName);
175✔
65

66
                                Layout layout = new Layout();
175✔
67
                                layout.Name = Layout.PaperLayoutName;
175✔
68
                                layout.AssociatedBlock = record;
175✔
69

70
                                return record;
175✔
71
                        }
175✔
72
                }
73

74
                /// <inheritdoc/>
75
                public override ObjectType ObjectType => ObjectType.BLOCK_HEADER;
168✔
76

77
                /// <inheritdoc/>
78
                public override string ObjectName => DxfFileToken.TableBlockRecord;
530✔
79

80
                /// <inheritdoc/>
81
                public override string SubclassMarker => DxfSubclassMarker.BlockRecord;
56,525✔
82

83
                /// <summary>
84
                /// Block insertion units
85
                /// </summary>
86
                // [DxfCodeValue(70)]        //Table entry uses flags and has the same code but dwg saves also the block record flags
87
                public UnitsType Units { get; set; }
2,365✔
88

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

92
                /// <summary>
93
                /// Specifies whether the block can be exploded.
94
                /// </summary>
95
                [DxfCodeValue(DxfReferenceType.Optional, 280)]
96
                public bool IsExplodable { get; set; }
5,526✔
97

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

104
                /// <summary>
105
                /// DXF: Binary data for bitmap preview.
106
                /// </summary>
107
                /// <remarks>
108
                /// Optional
109
                /// </remarks>
110
                [DxfCodeValue(DxfReferenceType.Optional, 310)]
111
                public byte[] Preview { get; set; }
16,147✔
112

113
                /// <summary>
114
                /// Associated Layout.
115
                /// </summary>
116
                [DxfCodeValue(DxfReferenceType.Handle, 340)]
117
                public Layout Layout
118
                {
119
                        get { return this._layout; }
3,198✔
120
                        internal set
121
                        {
3,028✔
122
                                this._layout = value;
3,028✔
123
                        }
3,028✔
124
                }
125

126
                /// <summary>
127
                /// Attribute definitions in this block
128
                /// </summary>
129
                public IEnumerable<AttributeDefinition> AttributeDefinitions
130
                {
131
                        get
132
                        {
21✔
133
                                return this.Entities.OfType<AttributeDefinition>();
21✔
134
                        }
21✔
135
                }
136

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

148
                /// <summary>
149
                /// ViewPorts attached to this block
150
                /// </summary>
151
                public IEnumerable<Viewport> Viewports
152
                {
153
                        get
154
                        {
315✔
155
                                return this.Entities.OfType<Viewport>();
315✔
156
                        }
315✔
157
                }
158

159
                /// <summary>
160
                /// Entities owned by this block.
161
                /// </summary>
162
                /// <remarks>
163
                /// Entities with an owner cannot be added to another block.
164
                /// </remarks>
165
                public CadObjectCollection<Entity> Entities { get; private set; }
130,415✔
166

167
                /// <summary>
168
                /// Sort entities table for this block record.
169
                /// </summary>
170
                public SortEntitiesTable SortEntitiesTable
171
                {
172
                        get
173
                        {
3✔
174
                                if (this.XDictionary == null)
3!
175
                                {
×
176
                                        return null;
×
177
                                }
178
                                else if (this.XDictionary.TryGetEntry(SortEntitiesTable.DictionaryEntryName, out SortEntitiesTable table))
3!
179
                                {
3✔
180
                                        return table;
3✔
181
                                }
182
                                else
183
                                {
×
184
                                        return null;
×
185
                                }
186
                        }
3✔
187
                }
188

189
                /// <summary>
190
                /// Active flag if it has an <see cref="Objects.Evaluations.EvaluationGraph"/> attached to it with dynamic expressions.
191
                /// </summary>
192
                public bool IsDynamic
193
                {
194
                        get
195
                        {
13✔
196
                                return this.EvaluationGraph != null;
13✔
197
                        }
13✔
198
                }
199

200
                /// <summary>
201
                /// Gets the evaluation graph for this block if it has dynamic properties attached to it.
202
                /// </summary>
203
                public EvaluationGraph EvaluationGraph
204
                {
205
                        get
206
                        {
13✔
207
                                if (this.XDictionary == null)
13!
208
                                {
×
209
                                        return null;
×
210
                                }
211
                                else if (this.XDictionary.TryGetEntry(EvaluationGraph.DictionaryEntryName, out EvaluationGraph table))
13!
212
                                {
13✔
213
                                        return table;
13✔
214
                                }
215
                                else
216
                                {
×
217
                                        return null;
×
218
                                }
219
                        }
13✔
220
                }
221

222
                /// <summary>
223
                /// Block entity for this record
224
                /// </summary>
225
                public Block BlockEntity
226
                {
227
                        get { return this._blockEntity; }
94,233✔
228
                        internal set
229
                        {
27,936✔
230
                                this._blockEntity = value;
27,936✔
231
                                this._blockEntity.Owner = this;
27,936✔
232
                        }
27,936✔
233
                }
234

235
                /// <summary>
236
                /// End block entity for this Block record.
237
                /// </summary>
238
                public BlockEnd BlockEnd
239
                {
240
                        get { return this._blockEnd; }
61,167✔
241
                        internal set
242
                        {
20,738✔
243
                                this._blockEnd = value;
20,738✔
244
                                this._blockEnd.Owner = this;
20,738✔
245
                        }
20,738✔
246
                }
247

248
                private Block _blockEntity;
249

250
                private BlockEnd _blockEnd;
251

252
                private Layout _layout;
253

254
                internal BlockRecord() : base()
14,124✔
255
                {
14,124✔
256
                        this.BlockEntity = new Block(this);
14,124✔
257
                        this.BlockEnd = new BlockEnd(this);
14,124✔
258
                        this.Entities = new CadObjectCollection<Entity>(this);
14,124✔
259
                }
14,124✔
260

261
                /// <summary>
262
                /// Default constructor.
263
                /// </summary>
264
                /// <param name="name">Unique name for this block record.</param>
265
                public BlockRecord(string name) : base(name)
3,386✔
266
                {
3,386✔
267
                        this.BlockEntity = new Block(this);
3,386✔
268
                        this.BlockEnd = new BlockEnd(this);
3,386✔
269
                        this.Entities = new CadObjectCollection<Entity>(this);
3,386✔
270
                }
3,386✔
271

272
                /// <summary>
273
                /// 
274
                /// </summary>
275
                /// <returns></returns>
276
                public SortEntitiesTable CreateSortEntitiesTable()
277
                {
1✔
278
                        CadDictionary dictionary = this.CreateExtendedDictionary();
1✔
279

280
                        if (dictionary.TryGetEntry(SortEntitiesTable.DictionaryEntryName, out SortEntitiesTable table))
1!
281
                        {
×
282
                                return table;
×
283
                        }
284

285
                        table = new SortEntitiesTable(this);
1✔
286

287
                        dictionary.Add(table);
1✔
288

289
                        return table;
1✔
290
                }
1✔
291

292
                /// <summary>
293
                /// Get the bounding box for all the entities in the block.
294
                /// </summary>
295
                /// <param name="ignoreInfinite">Ignore infinite entities, default: true</param>
296
                /// <returns></returns>
297
                public BoundingBox GetBoundingBox(bool ignoreInfinite = true)
298
                {
1✔
299
                        BoundingBox box = BoundingBox.Null;
1✔
300
                        foreach (var item in this.Entities)
47✔
301
                        {
22✔
302
                                if (item.GetBoundingBox().Extent == BoundingBoxExtent.Infinite
22!
303
                                        && ignoreInfinite)
22✔
NEW
304
                                {
×
NEW
305
                                        continue;
×
306
                                }
307

308
                                box = box.Merge(item.GetBoundingBox());
22✔
309
                        }
22✔
310

311
                        return box;
1✔
312
                }
1✔
313

314
                /// <inheritdoc/>
315
                public override CadObject Clone()
316
                {
118✔
317
                        BlockRecord clone = (BlockRecord)base.Clone();
118✔
318

319
                        Layout layout = (Layout)(this.Layout?.Clone());
118!
320
                        if (layout is not null)
118!
321
                        {
×
322
                                layout.AssociatedBlock = this;
×
323
                        }
×
324

325
                        clone.Entities = new CadObjectCollection<Entity>(clone);
118✔
326
                        foreach (var item in this.Entities)
1,518✔
327
                        {
582✔
328
                                clone.Entities.Add((Entity)item.Clone());
582✔
329
                        }
582✔
330

331
                        clone.BlockEntity = (Block)this.BlockEntity.Clone();
118✔
332
                        clone.BlockEntity.Owner = clone;
118✔
333
                        clone.BlockEnd = (BlockEnd)this.BlockEnd.Clone();
118✔
334
                        clone.BlockEnd.Owner = clone;
118✔
335

336
                        return clone;
118✔
337
                }
118✔
338

339
                internal override void AssignDocument(CadDocument doc)
340
                {
11,031✔
341
                        base.AssignDocument(doc);
11,031✔
342

343
                        doc.RegisterCollection(this.Entities);
11,031✔
344
                }
11,031✔
345

346
                internal override void UnassignDocument()
347
                {
1✔
348
                        this.Document.UnregisterCollection(this.Entities);
1✔
349

350
                        base.UnassignDocument();
1✔
351
                }
1✔
352
        }
353
}
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