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

DomCR / ACadSharp / 12905761471

22 Jan 2025 10:03AM UTC coverage: 2.0% (-74.0%) from 75.963%
12905761471

push

github

DomCR
version 1.0.6

104 of 7676 branches covered (1.35%)

Branch coverage included in aggregate %.

590 of 27024 relevant lines covered (2.18%)

5.13 hits per line

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

0.0
/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)
×
17
                {
×
18
                }
×
19

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

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

30
                                try
31
                                {
×
32
                                        template = this.readObject();
×
33
                                }
×
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)
×
46
                                        continue;
×
47

48
                                //Add the object and the template to the builder
49
                                this._builder.AddTemplate(template);
×
50
                        }
×
51
                }
×
52

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

94
                                        this._reader.ReadNext();
×
95

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

105
                                                this._reader.ReadNext();
×
106
                                        }
×
107
                                        while (this._reader.DxfCode != DxfCode.Start);
×
108

109
                                        return unknownEntityTemplate;
×
110
                        }
111
                }
×
112

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

118
                        DxfMap map = DxfMap.Create<T>();
×
119

120
                        while (this._reader.DxfCode != DxfCode.Start)
×
121
                        {
×
122
                                if (!readObject(template, map))
×
123
                                {
×
124
                                        this.readCommonCodes(template, out bool isExtendedData, map);
×
125
                                        if (isExtendedData)
×
126
                                                continue;
×
127
                                }
×
128

129
                                if (this._reader.DxfCode != DxfCode.Start)
×
130
                                        this._reader.ReadNext();
×
131
                        }
×
132

133
                        return template;
×
134
                }
×
135

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

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

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

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

167
                                                node.Index = this._reader.ValueAsInt;
×
168

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

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

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

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

187
                                                this._reader.ReadNext();
×
188

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

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

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

212
                                                this._reader.ReadNext();
×
213
                                        }
×
214

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

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

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

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

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

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

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

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

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

297
                private void readXRecordEntries(CadXRecordTemplate template)
298
                {
×
299
                        this._reader.ReadNext();
×
300

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

313
                                this._reader.ReadNext();
×
314
                        }
×
315
                }
×
316

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

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

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

337
                        switch (this._reader.Code)
×
338
                        {
339
                                case 280:
340
                                        cadDictionary.HardOwnerFlag = this._reader.ValueAsBool;
×
341
                                        return true;
×
342
                                case 281:
343
                                        cadDictionary.ClonningFlags = (DictionaryCloningFlags)this._reader.Value;
×
344
                                        return true;
×
345
                                case 3:
346
                                        tmp.Entries.Add(this._reader.ValueAsString, null);
×
347
                                        return true;
×
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;
×
351
                                        return true;
×
352
                                default:
353
                                        return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.Dictionary]);
×
354
                        }
355
                }
×
356

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

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

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

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

383
                        this.readCommonObjectData(template);
×
384

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

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

390
                        (ulong?, ulong?) pair = (null, null);
×
391

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

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

416
                                this._reader.ReadNext();
×
417
                        }
×
418

419
                        return template;
×
420
                }
×
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