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

DomCR / ACadSharp / 12204402896

06 Dec 2024 06:48PM UTC coverage: 75.284% (-0.02%) from 75.306%
12204402896

push

github

web-flow
Merge pull request #502 from DomCR/xrecord-reference

Xrecord linked

4966 of 7297 branches covered (68.06%)

Branch coverage included in aggregate %.

37 of 52 new or added lines in 4 files covered. (71.15%)

7 existing lines in 2 files now uncovered.

19831 of 25641 relevant lines covered (77.34%)

36388.52 hits per line

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

92.51
/src/ACadSharp/IO/DXF/DxfStreamReader/DxfObjectsSectionReader.cs
1
using ACadSharp.IO.Templates;
2
using ACadSharp.Objects;
3
using System;
4
using System.Linq;
5

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

12
                public DxfObjectsSectionReader(IDxfStreamReader reader, DxfDocumentBuilder builder)
13
                        : base(reader, builder)
130✔
14
                {
130✔
15
                }
130✔
16

17
                public override void Read()
18
                {
130✔
19
                        //Advance to the first value in the section
20
                        this._reader.ReadNext();
130✔
21

22
                        //Loop until the section ends
23
                        while (this._reader.ValueAsString != DxfFileToken.EndSection)
28,251✔
24
                        {
28,121✔
25
                                CadTemplate template = null;
28,121✔
26

27
                                try
28
                                {
28,121✔
29
                                        template = this.readObject();
28,121✔
30
                                }
28,121✔
31
                                catch (Exception ex)
×
32
                                {
×
33
                                        if (!this._builder.Configuration.Failsafe)
×
34
                                                throw;
×
35

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

38
                                        while (this._reader.DxfCode != DxfCode.Start)
×
39
                                                this._reader.ReadNext();
×
40
                                }
×
41

42
                                if (template == null)
28,121✔
43
                                        continue;
444✔
44

45
                                //Add the object and the template to the builder
46
                                this._builder.AddTemplate(template);
27,677✔
47
                        }
27,677✔
48
                }
130✔
49

50
                private CadTemplate readObject()
51
                {
28,121✔
52
                        switch (this._reader.ValueAsString)
28,121!
53
                        {
54
                                case DxfFileToken.ObjectDBColor:
55
                                        return this.readObjectCodes<BookColor>(new CadNonGraphicalObjectTemplate(new BookColor()), this.readBookColor);
10✔
56
                                case DxfFileToken.ObjectDictionary:
57
                                        return this.readObjectCodes<CadDictionary>(new CadDictionaryTemplate(), this.readDictionary);
9,042✔
58
                                case DxfFileToken.ObjectDictionaryWithDefault:
59
                                        return this.readObjectCodes<CadDictionaryWithDefault>(new CadDictionaryWithDefaultTemplate(), this.readDictionaryWithDefault);
118✔
60
                                case DxfFileToken.ObjectLayout:
61
                                        return this.readObjectCodes<Layout>(new CadLayoutTemplate(), this.readLayout);
486✔
62
                                case DxfFileToken.ObjectDictionaryVar:
63
                                        return this.readObjectCodes<DictionaryVariable>(new CadTemplate<DictionaryVariable>(new DictionaryVariable()), this.readObjectSubclassMap);
1,641✔
64
                                case DxfFileToken.ObjectPdfDefinition:
65
                                        return this.readObjectCodes<PdfUnderlayDefinition>(new CadNonGraphicalObjectTemplate(new PdfUnderlayDefinition()), this.readObjectSubclassMap);
×
66
                                case DxfFileToken.ObjectSortEntsTable:
67
                                        return this.readSortentsTable();
216✔
68
                                case DxfFileToken.ObjectScale:
69
                                        return this.readObjectCodes<Scale>(new CadTemplate<Scale>(new Scale()), this.readScale);
4,098✔
70
                                case DxfFileToken.ObjectVisualStyle:
71
                                        return this.readObjectCodes<VisualStyle>(new CadTemplate<VisualStyle>(new VisualStyle()), this.readVisualStyle);
2,832✔
72
                                case DxfFileToken.ObjectXRecord:
73
                                        return this.readObjectCodes<XRecord>(new CadXRecordTemplate(), this.readXRecord);
6,916✔
74
                                default:
75
                                        DxfMap map = DxfMap.Create<CadObject>();
2,762✔
76
                                        CadUnknownNonGraphicalObjectTemplate unknownEntityTemplate = null;
2,762✔
77
                                        if (this._builder.DocumentToBuild.Classes.TryGetByName(this._reader.ValueAsString, out Classes.DxfClass dxfClass))
2,762✔
78
                                        {
2,318✔
79
                                                this._builder.Notify($"NonGraphicalObject not supported read as an UnknownNonGraphicalObject: {this._reader.ValueAsString}", NotificationType.NotImplemented);
2,318✔
80
                                                unknownEntityTemplate = new CadUnknownNonGraphicalObjectTemplate(new UnknownNonGraphicalObject(dxfClass));
2,318✔
81
                                        }
2,318✔
82
                                        else
83
                                        {
444✔
84
                                                this._builder.Notify($"UnknownNonGraphicalObject not supported: {this._reader.ValueAsString}", NotificationType.NotImplemented);
444✔
85
                                        }
444✔
86

87
                                        this._reader.ReadNext();
2,762✔
88

89
                                        do
90
                                        {
479,872✔
91
                                                if (unknownEntityTemplate != null && this._builder.KeepUnknownEntities)
479,872✔
92
                                                {
51,152✔
93
                                                        this.readCommonCodes(unknownEntityTemplate, out bool isExtendedData, map);
51,152✔
94
                                                        if (isExtendedData)
51,152✔
95
                                                                continue;
24✔
96
                                                }
51,128✔
97

98
                                                this._reader.ReadNext();
479,848✔
99
                                        }
479,848✔
100
                                        while (this._reader.DxfCode != DxfCode.Start);
479,872✔
101

102
                                        return unknownEntityTemplate;
2,762✔
103
                        }
104
                }
28,121✔
105

106
                protected CadTemplate readObjectCodes<T>(CadTemplate template, ReadObjectDelegate<T> readEntity)
107
                        where T : CadObject
108
                {
25,143✔
109
                        this._reader.ReadNext();
25,143✔
110

111
                        DxfMap map = DxfMap.Create<T>();
25,143✔
112

113
                        while (this._reader.DxfCode != DxfCode.Start)
430,709✔
114
                        {
405,566✔
115
                                if (!readEntity(template, map))
405,566✔
116
                                {
132,160✔
117
                                        this.readCommonCodes(template, out bool isExtendedData, map);
132,160✔
118
                                        if (isExtendedData)
132,160✔
119
                                                continue;
2,888✔
120
                                }
129,272✔
121

122
                                if (this._reader.DxfCode != DxfCode.Start)
402,678✔
123
                                        this._reader.ReadNext();
395,762✔
124
                        }
402,678✔
125

126
                        return template;
25,143✔
127
                }
25,143✔
128

129
                private bool readObjectSubclassMap(CadTemplate template, DxfMap map)
130
                {
9,846✔
131
                        switch (this._reader.Code)
9,846✔
132
                        {
133
                                default:
134
                                        return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[template.CadObject.SubclassMarker]);
9,846✔
135
                        }
136
                }
9,846✔
137

138
                private bool readPlotSettings(CadTemplate template, DxfMap map)
139
                {
15,314✔
140
                        switch (this._reader.Code)
15,314✔
141
                        {
142
                                default:
143
                                        return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.PlotSettings]);
15,314✔
144
                        }
145
                }
15,314✔
146

147
                private bool readLayout(CadTemplate template, DxfMap map)
148
                {
31,206✔
149
                        CadLayoutTemplate tmp = template as CadLayoutTemplate;
31,206✔
150

151
                        switch (this._reader.Code)
31,206✔
152
                        {
153
                                case 330:
154
                                        tmp.PaperSpaceBlockHandle = this._reader.ValueAsHandle;
972✔
155
                                        return true;
972✔
156
                                case 331:
157
                                        tmp.LasActiveViewportHandle = (this._reader.ValueAsHandle);
442✔
158
                                        return true;
442✔
159
                                default:
160
                                        if (!this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.Layout]))
29,792✔
161
                                        {
15,314✔
162
                                                return this.readPlotSettings(template, map);
15,314✔
163
                                        }
164
                                        return true;
14,478✔
165
                        }
166
                }
31,206✔
167

168
                private bool readScale(CadTemplate template, DxfMap map)
169
                {
36,678✔
170
                        switch (this._reader.Code)
36,678✔
171
                        {
172
                                // Undocumented codes
173
                                case 70:
174
                                        //Always 0
175
                                        return true;
4,098✔
176
                                default:
177
                                        return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.Scale]);
32,580✔
178
                        }
179
                }
36,678✔
180

181
                private bool readVisualStyle(CadTemplate template, DxfMap map)
182
                {
212,308✔
183
                        switch (this._reader.Code)
212,308✔
184
                        {
185
                                // Undocumented codes
186
                                case 176:
187
                                case 177:
188
                                case 420:
189
                                        return true;
73,520✔
190
                                default:
191
                                        return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.VisualStyle]);
138,788✔
192
                        }
193
                }
212,308✔
194

195
                private bool readXRecord(CadTemplate template, DxfMap map)
196
                {
27,016✔
197
                        CadXRecordTemplate tmp = template as CadXRecordTemplate;
27,016✔
198

199
                        switch (this._reader.Code)
27,016✔
200
                        {
201
                                case 100 when this._reader.ValueAsString == DxfSubclassMarker.XRecord:
6,916✔
202
                                        this.readXRecordEntries(tmp);
6,916✔
203
                                        return true;
6,916✔
204
                                default:
205
                                        return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.XRecord]);
20,100✔
206
                        }
207
                }
27,016✔
208

209
                private void readXRecordEntries(CadXRecordTemplate template)
210
                {
6,916✔
211
                        this._reader.ReadNext();
6,916✔
212

213
                        while (this._reader.DxfCode != DxfCode.Start)
479,606✔
214
                        {
472,690✔
215
                                switch (this._reader.GroupCodeValue)
472,690!
216
                                {
217
                                        case GroupCodeValueType.Handle:
NEW
218
                                                template.AddHandleReference(this._reader.Code, this._reader.ValueAsHandle);
×
NEW
219
                                                break;
×
220
                                        default:
221
                                                template.CadObject.CreateEntry(this._reader.Code, this._reader.Value);
472,690✔
222
                                                break;
472,690✔
223
                                }
224

225
                                this._reader.ReadNext();
472,690✔
226
                        }
472,690✔
227
                }
6,916✔
228

229
                private bool readBookColor(CadTemplate template, DxfMap map)
230
                {
70✔
231
                        CadNonGraphicalObjectTemplate tmp = template as CadNonGraphicalObjectTemplate;
70✔
232
                        BookColor color = tmp.CadObject as BookColor;
70✔
233

234
                        switch (this._reader.Code)
70✔
235
                        {
236
                                case 430:
237
                                        color.Name = this._reader.ValueAsString;
8✔
238
                                        return true;
8✔
239
                                default:
240
                                        return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.DbColor]);
62✔
241
                        }
242
                }
70✔
243

244
                private bool readDictionary(CadTemplate template, DxfMap map)
245
                {
88,324✔
246
                        CadDictionaryTemplate tmp = template as CadDictionaryTemplate;
88,324✔
247
                        CadDictionary cadDictionary = tmp.CadObject;
88,324✔
248

249
                        switch (this._reader.Code)
88,324✔
250
                        {
251
                                case 280:
252
                                        cadDictionary.HardOwnerFlag = this._reader.ValueAsBool;
5,896✔
253
                                        return true;
5,896✔
254
                                case 281:
255
                                        cadDictionary.ClonningFlags = (DictionaryCloningFlags)this._reader.Value;
9,160✔
256
                                        return true;
9,160✔
257
                                case 3:
258
                                        tmp.Entries.Add(this._reader.ValueAsString, null);
21,159✔
259
                                        return true;
21,159✔
260
                                case 350: // Soft-owner ID/handle to entry object 
261
                                case 360: // Hard-owner ID/handle to entry object
262
                                        tmp.Entries[tmp.Entries.LastOrDefault().Key] = this._reader.ValueAsHandle;
21,159✔
263
                                        return true;
21,159✔
264
                                default:
265
                                        return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.Dictionary]);
30,950✔
266
                        }
267
                }
88,324✔
268

269
                private bool readDictionaryWithDefault(CadTemplate template, DxfMap map)
270
                {
1,062✔
271
                        CadDictionaryWithDefaultTemplate tmp = template as CadDictionaryWithDefaultTemplate;
1,062✔
272

273
                        switch (this._reader.Code)
1,062✔
274
                        {
275
                                case 340:
276
                                        tmp.DefaultEntryHandle = this._reader.ValueAsHandle;
118✔
277
                                        return true;
118✔
278
                                default:
279
                                        if (!this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.DictionaryWithDefault]))
944!
280
                                        {
944✔
281
                                                return this.readDictionary(template, map);
944✔
282
                                        }
283
                                        return true;
×
284
                        }
285
                }
1,062✔
286

287
                private CadTemplate readSortentsTable()
288
                {
216✔
289
                        SortEntitiesTable sortTable = new SortEntitiesTable();
216✔
290
                        CadSortensTableTemplate template = new CadSortensTableTemplate(sortTable);
216✔
291

292
                        //Jump the 0 marker
293
                        this._reader.ReadNext();
216✔
294

295
                        this.readCommonObjectData(template);
216✔
296

297
                        System.Diagnostics.Debug.Assert(DxfSubclassMarker.SortentsTable == this._reader.ValueAsString);
216✔
298

299
                        //Jump the 100 marker
300
                        this._reader.ReadNext();
216✔
301

302
                        (ulong?, ulong?) pair = (null, null);
216✔
303

304
                        while (this._reader.DxfCode != DxfCode.Start)
2,160✔
305
                        {
1,944✔
306
                                switch (this._reader.Code)
1,944!
307
                                {
308
                                        case 5:
309
                                                pair.Item1 = this._reader.ValueAsHandle;
864✔
310
                                                break;
864✔
311
                                        case 330:
312
                                                template.BlockOwnerHandle = this._reader.ValueAsHandle;
216✔
313
                                                break;
216✔
314
                                        case 331:
315
                                                pair.Item2 = this._reader.ValueAsHandle;
864✔
316
                                                break;
864✔
317
                                        default:
318
                                                this._builder.Notify($"Group Code not handled {this._reader.GroupCodeValue} for {typeof(SortEntitiesTable)}, code : {this._reader.Code} | value : {this._reader.ValueAsString}");
×
319
                                                break;
×
320
                                }
321

322
                                if (pair.Item1.HasValue && pair.Item2.HasValue)
1,944✔
323
                                {
864✔
324
                                        template.Values.Add((pair.Item1.Value, pair.Item2.Value));
864✔
325
                                        pair = (null, null);
864✔
326
                                }
864✔
327

328
                                this._reader.ReadNext();
1,944✔
329
                        }
1,944✔
330

331
                        return template;
216✔
332
                }
216✔
333
        }
334
}
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