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

DomCR / ACadSharp / 13075286726

31 Jan 2025 03:02PM UTC coverage: 76.172% (-0.05%) from 76.226%
13075286726

Pull #546

github

web-flow
Merge 63345678f into da8587f47
Pull Request #546: Issue 529 geodata

5344 of 7740 branches covered (69.04%)

Branch coverage included in aggregate %.

37 of 94 new or added lines in 5 files covered. (39.36%)

213 existing lines in 3 files now uncovered.

21339 of 27290 relevant lines covered (78.19%)

39476.62 hits per line

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

92.8
/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)
151✔
17
                {
151✔
18
                }
151✔
19

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

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

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

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

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

45
                                if (template == null)
32,138✔
46
                                        continue;
508✔
47

48
                                //Add the object and the template to the builder
49
                                this._builder.AddTemplate(template);
31,630✔
50
                        }
31,630✔
51
                }
151✔
52

53
                private CadTemplate readObject()
54
                {
32,138✔
55
                        switch (this._reader.ValueAsString)
32,138!
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,234✔
61
                                case DxfFileToken.ObjectDictionaryWithDefault:
62
                                        return this.readObjectCodes<CadDictionaryWithDefault>(new CadDictionaryWithDefaultTemplate(), this.readDictionaryWithDefault);
139✔
63
                                case DxfFileToken.ObjectLayout:
64
                                        return this.readObjectCodes<Layout>(new CadLayoutTemplate(), this.readLayout);
561✔
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,904✔
69
                                case DxfFileToken.ObjectPdfDefinition:
70
                                        return this.readObjectCodes<PdfUnderlayDefinition>(new CadNonGraphicalObjectTemplate(new PdfUnderlayDefinition()), this.readObjectSubclassMap);
×
71
                                case DxfFileToken.ObjectSortEntsTable:
72
                                        return this.readSortentsTable();
246✔
73
                                case DxfFileToken.ObjectGeoData:
74
                                        return this.readObjectCodes<GeoData>(new CadNonGraphicalObjectTemplate(new GeoData()), this.readObjectSubclassMap);
1✔
75
                                case DxfFileToken.ObjectScale:
76
                                        return this.readObjectCodes<Scale>(new CadTemplate<Scale>(new Scale()), this.readScale);
4,791✔
77
                                case DxfFileToken.ObjectTableContent:
78
                                        return this.readObjectCodes<TableContent>(new CadTableContentTemplate(), this.readTableContent);
121✔
79
                                case DxfFileToken.ObjectVisualStyle:
80
                                        return this.readObjectCodes<VisualStyle>(new CadTemplate<VisualStyle>(new VisualStyle()), this.readVisualStyle);
3,336✔
81
                                case DxfFileToken.ObjectXRecord:
82
                                        return this.readObjectCodes<XRecord>(new CadXRecordTemplate(), this.readXRecord);
7,744✔
83
                                default:
84
                                        DxfMap map = DxfMap.Create<CadObject>();
3,050✔
85
                                        CadUnknownNonGraphicalObjectTemplate unknownEntityTemplate = null;
3,050✔
86
                                        if (this._builder.DocumentToBuild.Classes.TryGetByName(this._reader.ValueAsString, out Classes.DxfClass dxfClass))
3,050✔
87
                                        {
2,542✔
88
                                                this._builder.Notify($"NonGraphicalObject not supported read as an UnknownNonGraphicalObject: {this._reader.ValueAsString}", NotificationType.NotImplemented);
2,542✔
89
                                                unknownEntityTemplate = new CadUnknownNonGraphicalObjectTemplate(new UnknownNonGraphicalObject(dxfClass));
2,542✔
90
                                        }
2,542✔
91
                                        else
92
                                        {
508✔
93
                                                this._builder.Notify($"UnknownNonGraphicalObject not supported: {this._reader.ValueAsString}", NotificationType.NotImplemented);
508✔
94
                                        }
508✔
95

96
                                        this._reader.ReadNext();
3,050✔
97

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

107
                                                this._reader.ReadNext();
174,236✔
108
                                        }
174,236✔
109
                                        while (this._reader.DxfCode != DxfCode.Start);
174,262✔
110

111
                                        return unknownEntityTemplate;
3,050✔
112
                        }
113
                }
32,138✔
114

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

120
                        DxfMap map = DxfMap.Create<T>();
28,842✔
121

122
                        while (this._reader.DxfCode != DxfCode.Start)
879,134✔
123
                        {
850,292✔
124
                                if (!readObject(template, map))
850,292✔
125
                                {
447,233✔
126
                                        this.readCommonCodes(template, out bool isExtendedData, map);
447,233✔
127
                                        if (isExtendedData)
447,233✔
128
                                                continue;
3,186✔
129
                                }
444,047✔
130

131
                                if (this._reader.DxfCode != DxfCode.Start)
847,106✔
132
                                        this._reader.ReadNext();
839,361✔
133
                        }
847,106✔
134

135
                        return template;
28,842✔
136
                }
28,842✔
137

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

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

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

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

169
                                                node.Index = this._reader.ValueAsInt;
12✔
170

171
                                                this._reader.ExpectedCode(93);
12✔
172
                                                node.Flags = this._reader.ValueAsInt;
12✔
173

174
                                                this._reader.ExpectedCode(95);
12✔
175
                                                node.NextNodeIndex = this._reader.ValueAsInt;
12✔
176

177
                                                this._reader.ExpectedCode(360);
12✔
178
                                                nodeTemplate.ExpressionHandle = this._reader.ValueAsHandle;
12✔
179

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

189
                                                this._reader.ReadNext();
12✔
190

191
                                                tmp.NodeTemplates.Add(nodeTemplate);
12✔
192
                                        }
12✔
193

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

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

214
                                                this._reader.ReadNext();
10✔
215
                                        }
10✔
216

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

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

228
                private bool readLayout(CadTemplate template, DxfMap map)
229
                {
36,027✔
230
                        CadLayoutTemplate tmp = template as CadLayoutTemplate;
36,027✔
231

232
                        switch (this._reader.Code)
36,027✔
233
                        {
234
                                case 330:
235
                                        tmp.PaperSpaceBlockHandle = this._reader.ValueAsHandle;
1,122✔
236
                                        return true;
1,122✔
237
                                case 331:
238
                                        tmp.LasActiveViewportHandle = (this._reader.ValueAsHandle);
499✔
239
                                        return true;
499✔
240
                                default:
241
                                        if (!this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.Layout]))
34,406✔
242
                                        {
17,686✔
243
                                                return this.readPlotSettings(template, map);
17,686✔
244
                                        }
245
                                        return true;
16,720✔
246
                        }
247
                }
36,027✔
248

249
                private bool readScale(CadTemplate template, DxfMap map)
250
                {
42,915✔
251
                        switch (this._reader.Code)
42,915✔
252
                        {
253
                                // Undocumented codes
254
                                case 70:
255
                                        //Always 0
256
                                        return true;
4,791✔
257
                                default:
258
                                        return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.Scale]);
38,124✔
259
                        }
260
                }
42,915✔
261

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

271
                private bool readVisualStyle(CadTemplate template, DxfMap map)
272
                {
261,402✔
273
                        switch (this._reader.Code)
261,402✔
274
                        {
275
                                // Undocumented codes
276
                                case 176:
277
                                case 177:
278
                                case 420:
279
                                        return true;
94,282✔
280
                                default:
281
                                        return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.VisualStyle]);
167,120✔
282
                        }
283
                }
261,402✔
284

285
                private bool readXRecord(CadTemplate template, DxfMap map)
286
                {
30,256✔
287
                        CadXRecordTemplate tmp = template as CadXRecordTemplate;
30,256✔
288

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

299
                private void readXRecordEntries(CadXRecordTemplate template)
300
                {
7,744✔
301
                        this._reader.ReadNext();
7,744✔
302

303
                        while (this._reader.DxfCode != DxfCode.Start)
530,937✔
304
                        {
523,193✔
305
                                switch (this._reader.GroupCodeValue)
523,193!
306
                                {
307
                                        case GroupCodeValueType.Handle:
UNCOV
308
                                                template.AddHandleReference(this._reader.Code, this._reader.ValueAsHandle);
×
UNCOV
309
                                                break;
×
310
                                        default:
311
                                                template.CadObject.CreateEntry(this._reader.Code, this._reader.Value);
523,193✔
312
                                                break;
523,193✔
313
                                }
314

315
                                this._reader.ReadNext();
523,193✔
316
                        }
523,193✔
317
                }
7,744✔
318

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

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

334
                private bool readDictionary(CadTemplate template, DxfMap map)
335
                {
100,841✔
336
                        CadDictionaryTemplate tmp = template as CadDictionaryTemplate;
100,841✔
337
                        CadDictionary cadDictionary = tmp.CadObject;
100,841✔
338

339
                        switch (this._reader.Code)
100,841✔
340
                        {
341
                                case 280:
342
                                        cadDictionary.HardOwnerFlag = this._reader.ValueAsBool;
6,605✔
343
                                        return true;
6,605✔
344
                                case 281:
345
                                        cadDictionary.ClonningFlags = (DictionaryCloningFlags)this._reader.Value;
10,373✔
346
                                        return true;
10,373✔
347
                                case 3:
348
                                        tmp.Entries.Add(this._reader.ValueAsString, null);
24,371✔
349
                                        return true;
24,371✔
350
                                case 350: // Soft-owner ID/handle to entry object 
351
                                case 360: // Hard-owner ID/handle to entry object
352
                                        tmp.Entries[tmp.Entries.LastOrDefault().Key] = this._reader.ValueAsHandle;
24,371✔
353
                                        return true;
24,371✔
354
                                default:
355
                                        return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.Dictionary]);
35,121✔
356
                        }
357
                }
100,841✔
358

359
                private bool readDictionaryWithDefault(CadTemplate template, DxfMap map)
360
                {
1,251✔
361
                        CadDictionaryWithDefaultTemplate tmp = template as CadDictionaryWithDefaultTemplate;
1,251✔
362

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

377
                private CadTemplate readSortentsTable()
378
                {
246✔
379
                        SortEntitiesTable sortTable = new SortEntitiesTable();
246✔
380
                        CadSortensTableTemplate template = new CadSortensTableTemplate(sortTable);
246✔
381

382
                        //Jump the 0 marker
383
                        this._reader.ReadNext();
246✔
384

385
                        this.readCommonObjectData(template);
246✔
386

387
                        System.Diagnostics.Debug.Assert(DxfSubclassMarker.SortentsTable == this._reader.ValueAsString);
246✔
388

389
                        //Jump the 100 marker
390
                        this._reader.ReadNext();
246✔
391

392
                        (ulong?, ulong?) pair = (null, null);
246✔
393

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

412
                                if (pair.Item1.HasValue && pair.Item2.HasValue)
2,246✔
413
                                {
1,000✔
414
                                        template.Values.Add((pair.Item1.Value, pair.Item2.Value));
1,000✔
415
                                        pair = (null, null);
1,000✔
416
                                }
1,000✔
417

418
                                this._reader.ReadNext();
2,246✔
419
                        }
2,246✔
420

421
                        return template;
246✔
422
                }
246✔
423
        }
424
}
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

© 2025 Coveralls, Inc