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

DomCR / ACadSharp / 12619114630

05 Jan 2025 11:27AM UTC coverage: 75.645% (+0.6%) from 75.001%
12619114630

Pull #490

github

web-flow
Merge 01b5ca4aa into 0d0db0394
Pull Request #490: Issue 474 header defaults

5217 of 7612 branches covered (68.54%)

Branch coverage included in aggregate %.

129 of 157 new or added lines in 5 files covered. (82.17%)

581 existing lines in 13 files now uncovered.

20882 of 26890 relevant lines covered (77.66%)

39172.96 hits per line

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

92.67
/src/ACadSharp/IO/DXF/DxfStreamReader/DxfObjectsSectionReader.cs
1
using ACadSharp.IO.Templates;
2
using ACadSharp.Objects;
3
using ACadSharp.Objects.Evaluations;
4
using System;
5
using System.Linq;
6
using System.Threading;
7
using static ACadSharp.IO.Templates.CadEvaluationGraphTemplate;
8

9
namespace ACadSharp.IO.DXF
10
{
11
        internal class DxfObjectsSectionReader : DxfSectionReaderBase
12
        {
13
                public delegate bool ReadObjectDelegate<T>(CadTemplate template, DxfMap map) where T : CadObject;
14

15
                public DxfObjectsSectionReader(IDxfStreamReader reader, DxfDocumentBuilder builder)
16
                        : base(reader, builder)
144✔
17
                {
144✔
18
                }
144✔
19

20
                public override void Read()
21
                {
144✔
22
                        //Advance to the first value in the section
23
                        this._reader.ReadNext();
144✔
24

25
                        //Loop until the section ends
26
                        while (this._reader.ValueAsString != DxfFileToken.EndSection)
31,456✔
27
                        {
31,312✔
28
                                CadTemplate template = null;
31,312✔
29

30
                                try
31
                                {
31,312✔
32
                                        template = this.readObject();
31,312✔
33
                                }
31,312✔
34
                                catch (Exception ex)
×
UNCOV
35
                                {
×
36
                                        if (!this._builder.Configuration.Failsafe)
×
UNCOV
37
                                                throw;
×
38

39
                                        this._builder.Notify($"Error while reading an object at line {this._reader.Position}", NotificationType.Error, ex);
×
40

UNCOV
41
                                        while (this._reader.DxfCode != DxfCode.Start)
×
UNCOV
42
                                                this._reader.ReadNext();
×
UNCOV
43
                                }
×
44

45
                                if (template == null)
31,312✔
46
                                        continue;
494✔
47

48
                                //Add the object and the template to the builder
49
                                this._builder.AddTemplate(template);
30,818✔
50
                        }
30,818✔
51
                }
144✔
52

53
                private CadTemplate readObject()
54
                {
31,312✔
55
                        switch (this._reader.ValueAsString)
31,312!
56
                        {
57
                                case DxfFileToken.ObjectDBColor:
58
                                        return this.readObjectCodes<BookColor>(new CadNonGraphicalObjectTemplate(new BookColor()), this.readBookColor);
10✔
59
                                case DxfFileToken.ObjectDictionary:
60
                                        return this.readObjectCodes<CadDictionary>(new CadDictionaryTemplate(), this.readDictionary);
10,056✔
61
                                case DxfFileToken.ObjectDictionaryWithDefault:
62
                                        return this.readObjectCodes<CadDictionaryWithDefault>(new CadDictionaryWithDefaultTemplate(), this.readDictionaryWithDefault);
132✔
63
                                case DxfFileToken.ObjectLayout:
64
                                        return this.readObjectCodes<Layout>(new CadLayoutTemplate(), this.readLayout);
540✔
65
                                case DxfFileToken.ObjectEvalGraph:
66
                                        return this.readObjectCodes<EvaluationGraph>(new CadEvaluationGraphTemplate(), this.readEvaluationGraph);
1✔
67
                                case DxfFileToken.ObjectDictionaryVar:
68
                                        return this.readObjectCodes<DictionaryVariable>(new CadTemplate<DictionaryVariable>(new DictionaryVariable()), this.readObjectSubclassMap);
1,830✔
69
                                case DxfFileToken.ObjectPdfDefinition:
UNCOV
70
                                        return this.readObjectCodes<PdfUnderlayDefinition>(new CadNonGraphicalObjectTemplate(new PdfUnderlayDefinition()), this.readObjectSubclassMap);
×
71
                                case DxfFileToken.ObjectSortEntsTable:
72
                                        return this.readSortentsTable();
241✔
73
                                case DxfFileToken.ObjectScale:
74
                                        return this.readObjectCodes<Scale>(new CadTemplate<Scale>(new Scale()), this.readScale);
4,560✔
75
                                case DxfFileToken.ObjectTableContent:
76
                                        return this.readObjectCodes<TableContent>(new CadTableContentTemplate(), this.readTableContent);
121✔
77
                                case DxfFileToken.ObjectVisualStyle:
78
                                        return this.readObjectCodes<VisualStyle>(new CadTemplate<VisualStyle>(new VisualStyle()), this.readVisualStyle);
3,168✔
79
                                case DxfFileToken.ObjectXRecord:
80
                                        return this.readObjectCodes<XRecord>(new CadXRecordTemplate(), this.readXRecord);
7,681✔
81
                                default:
82
                                        DxfMap map = DxfMap.Create<CadObject>();
2,972✔
83
                                        CadUnknownNonGraphicalObjectTemplate unknownEntityTemplate = null;
2,972✔
84
                                        if (this._builder.DocumentToBuild.Classes.TryGetByName(this._reader.ValueAsString, out Classes.DxfClass dxfClass))
2,972✔
85
                                        {
2,478✔
86
                                                this._builder.Notify($"NonGraphicalObject not supported read as an UnknownNonGraphicalObject: {this._reader.ValueAsString}", NotificationType.NotImplemented);
2,478✔
87
                                                unknownEntityTemplate = new CadUnknownNonGraphicalObjectTemplate(new UnknownNonGraphicalObject(dxfClass));
2,478✔
88
                                        }
2,478✔
89
                                        else
90
                                        {
494✔
91
                                                this._builder.Notify($"UnknownNonGraphicalObject not supported: {this._reader.ValueAsString}", NotificationType.NotImplemented);
494✔
92
                                        }
494✔
93

94
                                        this._reader.ReadNext();
2,972✔
95

96
                                        do
97
                                        {
168,969✔
98
                                                if (unknownEntityTemplate != null && this._builder.KeepUnknownEntities)
168,969✔
99
                                                {
15,600✔
100
                                                        this.readCommonCodes(unknownEntityTemplate, out bool isExtendedData, map);
15,600✔
101
                                                        if (isExtendedData)
15,600✔
102
                                                                continue;
26✔
103
                                                }
15,574✔
104

105
                                                this._reader.ReadNext();
168,943✔
106
                                        }
168,943✔
107
                                        while (this._reader.DxfCode != DxfCode.Start);
168,969✔
108

109
                                        return unknownEntityTemplate;
2,972✔
110
                        }
111
                }
31,312✔
112

113
                protected CadTemplate readObjectCodes<T>(CadTemplate template, ReadObjectDelegate<T> readObject)
114
                        where T : CadObject
115
                {
28,099✔
116
                        this._reader.ReadNext();
28,099✔
117

118
                        DxfMap map = DxfMap.Create<T>();
28,099✔
119

120
                        while (this._reader.DxfCode != DxfCode.Start)
850,223✔
121
                        {
822,124✔
122
                                if (!readObject(template, map))
822,124✔
123
                                {
444,316✔
124
                                        this.readCommonCodes(template, out bool isExtendedData, map);
444,316✔
125
                                        if (isExtendedData)
444,316✔
126
                                                continue;
3,186✔
127
                                }
441,130✔
128

129
                                if (this._reader.DxfCode != DxfCode.Start)
818,938✔
130
                                        this._reader.ReadNext();
811,256✔
131
                        }
818,938✔
132

133
                        return template;
28,099✔
134
                }
28,099✔
135

136
                private bool readObjectSubclassMap(CadTemplate template, DxfMap map)
137
                {
10,980✔
138
                        switch (this._reader.Code)
10,980✔
139
                        {
140
                                default:
141
                                        return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[template.CadObject.SubclassMarker]);
10,980✔
142
                        }
143
                }
10,980✔
144

145
                private bool readPlotSettings(CadTemplate template, DxfMap map)
146
                {
17,028✔
147
                        switch (this._reader.Code)
17,028✔
148
                        {
149
                                default:
150
                                        return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.PlotSettings]);
17,028✔
151
                        }
152
                }
17,028✔
153

154
                private bool readEvaluationGraph(CadTemplate template, DxfMap map)
155
                {
8✔
156
                        CadEvaluationGraphTemplate tmp = template as CadEvaluationGraphTemplate;
8✔
157
                        EvaluationGraph evGraph = tmp.CadObject;
8✔
158

159
                        switch (this._reader.Code)
8✔
160
                        {
161
                                case 91:
162
                                        while (this._reader.Code == 91)
13✔
163
                                        {
12✔
164
                                                GraphNodeTemplate nodeTemplate = new GraphNodeTemplate();
12✔
165
                                                EvaluationGraph.Node node = nodeTemplate.Node;
12✔
166

167
                                                node.Index = this._reader.ValueAsInt;
12✔
168

169
                                                this._reader.ExpectedCode(93);
12✔
170
                                                node.Flags = this._reader.ValueAsInt;
12✔
171

172
                                                this._reader.ExpectedCode(95);
12✔
173
                                                node.NextNodeIndex = this._reader.ValueAsInt;
12✔
174

175
                                                this._reader.ExpectedCode(360);
12✔
176
                                                nodeTemplate.ExpressionHandle = this._reader.ValueAsHandle;
12✔
177

178
                                                this._reader.ExpectedCode(92);
12✔
179
                                                node.Data1 = this._reader.ValueAsInt;
12✔
180
                                                this._reader.ExpectedCode(92);
12✔
181
                                                node.Data2 = this._reader.ValueAsInt;
12✔
182
                                                this._reader.ExpectedCode(92);
12✔
183
                                                node.Data3 = this._reader.ValueAsInt;
12✔
184
                                                this._reader.ExpectedCode(92);
12✔
185
                                                node.Data4 = this._reader.ValueAsInt;
12✔
186

187
                                                this._reader.ReadNext();
12✔
188

189
                                                tmp.NodeTemplates.Add(nodeTemplate);
12✔
190
                                        }
12✔
191

192
                                        if (this._reader.DxfCode == DxfCode.Start)
1!
UNCOV
193
                                        {
×
UNCOV
194
                                                return true;
×
195
                                        }
196

197
                                        return this.readEvaluationGraph(template, map);
1✔
198
                                case 92:
199
                                        //Edges
200
                                        while (this._reader.Code == 92)
11✔
201
                                        {
10✔
202
                                                this._reader.ExpectedCode(93);
10✔
203
                                                this._reader.ExpectedCode(94);
10✔
204
                                                this._reader.ExpectedCode(91);
10✔
205
                                                this._reader.ExpectedCode(91);
10✔
206
                                                this._reader.ExpectedCode(92);
10✔
207
                                                this._reader.ExpectedCode(92);
10✔
208
                                                this._reader.ExpectedCode(92);
10✔
209
                                                this._reader.ExpectedCode(92);
10✔
210
                                                this._reader.ExpectedCode(92);
10✔
211

212
                                                this._reader.ReadNext();
10✔
213
                                        }
10✔
214

215
                                        if(this._reader.DxfCode == DxfCode.Start)
1!
216
                                        {
1✔
217
                                                return true;
1✔
218
                                        }
219

UNCOV
220
                                        return this.readEvaluationGraph(template, map);
×
221
                                default:
222
                                        return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.EvalGraph]);
6✔
223
                        }
224
                }
8✔
225

226
                private bool readLayout(CadTemplate template, DxfMap map)
227
                {
34,690✔
228
                        CadLayoutTemplate tmp = template as CadLayoutTemplate;
34,690✔
229

230
                        switch (this._reader.Code)
34,690✔
231
                        {
232
                                case 330:
233
                                        tmp.PaperSpaceBlockHandle = this._reader.ValueAsHandle;
1,080✔
234
                                        return true;
1,080✔
235
                                case 331:
236
                                        tmp.LasActiveViewportHandle = (this._reader.ValueAsHandle);
492✔
237
                                        return true;
492✔
238
                                default:
239
                                        if (!this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.Layout]))
33,118✔
240
                                        {
17,028✔
241
                                                return this.readPlotSettings(template, map);
17,028✔
242
                                        }
243
                                        return true;
16,090✔
244
                        }
245
                }
34,690✔
246

247
                private bool readScale(CadTemplate template, DxfMap map)
248
                {
40,836✔
249
                        switch (this._reader.Code)
40,836✔
250
                        {
251
                                // Undocumented codes
252
                                case 70:
253
                                        //Always 0
254
                                        return true;
4,560✔
255
                                default:
256
                                        return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.Scale]);
36,276✔
257
                        }
258
                }
40,836✔
259

260
                private bool readTableContent(CadTemplate template, DxfMap map)
261
                {
367,117✔
262
                        switch (this._reader.Code)
367,117✔
263
                        {
264
                                default:
265
                                        return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.TableContent]);
367,117✔
266
                        }
267
                }
367,117✔
268

269
                private bool readVisualStyle(CadTemplate template, DxfMap map)
270
                {
239,884✔
271
                        switch (this._reader.Code)
239,884✔
272
                        {
273
                                // Undocumented codes
274
                                case 176:
275
                                case 177:
276
                                case 420:
277
                                        return true;
83,852✔
278
                                default:
279
                                        return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.VisualStyle]);
156,032✔
280
                        }
281
                }
239,884✔
282

283
                private bool readXRecord(CadTemplate template, DxfMap map)
284
                {
30,004✔
285
                        CadXRecordTemplate tmp = template as CadXRecordTemplate;
30,004✔
286

287
                        switch (this._reader.Code)
30,004✔
288
                        {
289
                                case 100 when this._reader.ValueAsString == DxfSubclassMarker.XRecord:
7,681✔
290
                                        this.readXRecordEntries(tmp);
7,681✔
291
                                        return true;
7,681✔
292
                                default:
293
                                        return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.XRecord]);
22,323✔
294
                        }
295
                }
30,004✔
296

297
                private void readXRecordEntries(CadXRecordTemplate template)
298
                {
7,681✔
299
                        this._reader.ReadNext();
7,681✔
300

301
                        while (this._reader.DxfCode != DxfCode.Start)
529,712✔
302
                        {
522,031✔
303
                                switch (this._reader.GroupCodeValue)
522,031!
304
                                {
305
                                        case GroupCodeValueType.Handle:
UNCOV
306
                                                template.AddHandleReference(this._reader.Code, this._reader.ValueAsHandle);
×
UNCOV
307
                                                break;
×
308
                                        default:
309
                                                template.CadObject.CreateEntry(this._reader.Code, this._reader.Value);
522,031✔
310
                                                break;
522,031✔
311
                                }
312

313
                                this._reader.ReadNext();
522,031✔
314
                        }
522,031✔
315
                }
7,681✔
316

317
                private bool readBookColor(CadTemplate template, DxfMap map)
318
                {
70✔
319
                        CadNonGraphicalObjectTemplate tmp = template as CadNonGraphicalObjectTemplate;
70✔
320
                        BookColor color = tmp.CadObject as BookColor;
70✔
321

322
                        switch (this._reader.Code)
70✔
323
                        {
324
                                case 430:
325
                                        color.Name = this._reader.ValueAsString;
8✔
326
                                        return true;
8✔
327
                                default:
328
                                        return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.DbColor]);
62✔
329
                        }
330
                }
70✔
331

332
                private bool readDictionary(CadTemplate template, DxfMap map)
333
                {
98,404✔
334
                        CadDictionaryTemplate tmp = template as CadDictionaryTemplate;
98,404✔
335
                        CadDictionary cadDictionary = tmp.CadObject;
98,404✔
336

337
                        switch (this._reader.Code)
98,404✔
338
                        {
339
                                case 280:
340
                                        cadDictionary.HardOwnerFlag = this._reader.ValueAsBool;
6,543✔
341
                                        return true;
6,543✔
342
                                case 281:
343
                                        cadDictionary.ClonningFlags = (DictionaryCloningFlags)this._reader.Value;
10,188✔
344
                                        return true;
10,188✔
345
                                case 3:
346
                                        tmp.Entries.Add(this._reader.ValueAsString, null);
23,615✔
347
                                        return true;
23,615✔
348
                                case 350: // Soft-owner ID/handle to entry object 
349
                                case 360: // Hard-owner ID/handle to entry object
350
                                        tmp.Entries[tmp.Entries.LastOrDefault().Key] = this._reader.ValueAsHandle;
23,615✔
351
                                        return true;
23,615✔
352
                                default:
353
                                        return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.Dictionary]);
34,443✔
354
                        }
355
                }
98,404✔
356

357
                private bool readDictionaryWithDefault(CadTemplate template, DxfMap map)
358
                {
1,188✔
359
                        CadDictionaryWithDefaultTemplate tmp = template as CadDictionaryWithDefaultTemplate;
1,188✔
360

361
                        switch (this._reader.Code)
1,188✔
362
                        {
363
                                case 340:
364
                                        tmp.DefaultEntryHandle = this._reader.ValueAsHandle;
132✔
365
                                        return true;
132✔
366
                                default:
367
                                        if (!this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.DictionaryWithDefault]))
1,056!
368
                                        {
1,056✔
369
                                                return this.readDictionary(template, map);
1,056✔
370
                                        }
UNCOV
371
                                        return true;
×
372
                        }
373
                }
1,188✔
374

375
                private CadTemplate readSortentsTable()
376
                {
241✔
377
                        SortEntitiesTable sortTable = new SortEntitiesTable();
241✔
378
                        CadSortensTableTemplate template = new CadSortensTableTemplate(sortTable);
241✔
379

380
                        //Jump the 0 marker
381
                        this._reader.ReadNext();
241✔
382

383
                        this.readCommonObjectData(template);
241✔
384

385
                        System.Diagnostics.Debug.Assert(DxfSubclassMarker.SortentsTable == this._reader.ValueAsString);
241✔
386

387
                        //Jump the 100 marker
388
                        this._reader.ReadNext();
241✔
389

390
                        (ulong?, ulong?) pair = (null, null);
241✔
391

392
                        while (this._reader.DxfCode != DxfCode.Start)
2,402✔
393
                        {
2,161✔
394
                                switch (this._reader.Code)
2,161!
395
                                {
396
                                        case 5:
397
                                                pair.Item1 = this._reader.ValueAsHandle;
960✔
398
                                                break;
960✔
399
                                        case 330:
400
                                                template.BlockOwnerHandle = this._reader.ValueAsHandle;
241✔
401
                                                break;
241✔
402
                                        case 331:
403
                                                pair.Item2 = this._reader.ValueAsHandle;
960✔
404
                                                break;
960✔
405
                                        default:
UNCOV
406
                                                this._builder.Notify($"Group Code not handled {this._reader.GroupCodeValue} for {typeof(SortEntitiesTable)}, code : {this._reader.Code} | value : {this._reader.ValueAsString}");
×
UNCOV
407
                                                break;
×
408
                                }
409

410
                                if (pair.Item1.HasValue && pair.Item2.HasValue)
2,161✔
411
                                {
960✔
412
                                        template.Values.Add((pair.Item1.Value, pair.Item2.Value));
960✔
413
                                        pair = (null, null);
960✔
414
                                }
960✔
415

416
                                this._reader.ReadNext();
2,161✔
417
                        }
2,161✔
418

419
                        return template;
241✔
420
                }
241✔
421
        }
422
}
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