• 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/DxfBlockSectionReader.cs
1
using ACadSharp.Blocks;
2
using ACadSharp.Exceptions;
3
using ACadSharp.IO.Templates;
4
using ACadSharp.Tables;
5
using System;
6
using System.Diagnostics;
7

8
namespace ACadSharp.IO.DXF
9
{
10
        internal class DxfBlockSectionReader : DxfSectionReaderBase
11
        {
12
                public DxfBlockSectionReader(IDxfStreamReader reader, DxfDocumentBuilder builder)
13
                        : base(reader, builder)
×
14
                {
×
15
                }
×
16

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

22
                        //Loop until the section ends
23
                        while (this._reader.ValueAsString != DxfFileToken.EndSection)
×
24
                        {
×
25
                                try
26
                                {
×
27
                                        if (this._reader.ValueAsString == DxfFileToken.Block)
×
28
                                                this.readBlock();
×
29
                                        else
30
                                                throw new DxfException($"Unexpected token at the BLOCKS table: {this._reader.ValueAsString}", this._reader.Position);
×
31
                                }
×
32
                                catch (Exception ex)
×
33
                                {
×
34
                                        if (!this._builder.Configuration.Failsafe)
×
35
                                                throw;
×
36

37
                                        this._builder.Notify($"Error while reading a block at line {this._reader.Position}", NotificationType.Error, ex);
×
38

39
                                        while (!(this._reader.DxfCode == DxfCode.Start && this._reader.ValueAsString == DxfFileToken.EndSection)
×
40
                                                        && !(this._reader.DxfCode == DxfCode.Start && this._reader.ValueAsString == DxfFileToken.Block))
×
41
                                        {
×
42
                                                this._reader.ReadNext();
×
43
                                        }
×
44
                                }
×
45
                        }
×
46
                }
×
47

48
                private void readBlock()
49
                {
×
50
                        Debug.Assert(this._reader.ValueAsString == DxfFileToken.Block);
×
51

52
                        //Read the table name
53
                        this._reader.ReadNext();
×
54

55
                        DxfMap map = DxfMap.Create<Block>();
×
56

57
                        Block blckEntity = new Block();
×
58
                        CadEntityTemplate template = new CadEntityTemplate(blckEntity);
×
59

60
                        string name = null;
×
61
                        BlockRecord record = null;
×
62

63
                        while (this._reader.DxfCode != DxfCode.Start)
×
64
                        {
×
65
                                switch (this._reader.Code)
×
66
                                {
67
                                        case 2:
68
                                        case 3:
69
                                                name = this._reader.ValueAsString;
×
70
                                                if (name.Equals("$MODEL_SPACE", StringComparison.OrdinalIgnoreCase))
×
71
                                                {
×
72
                                                        name = BlockRecord.ModelSpaceName;
×
73
                                                }
×
74
                                                else if (name.Equals("$PAPER_SPACE", StringComparison.OrdinalIgnoreCase))
×
75
                                                {
×
76
                                                        name = BlockRecord.PaperSpaceName;
×
77
                                                }
×
78

79
                                                if (record == null && this._builder.TryGetTableEntry(name, out record))
×
80
                                                {
×
81
                                                        record.BlockEntity = blckEntity;
×
82
                                                }
×
83
                                                else if (record == null)
×
84
                                                {
×
85
                                                        this._builder.Notify($"Block record [{name}] not found at line {this._reader.Position}", NotificationType.Warning);
×
86
                                                }
×
87
                                                break;
×
88
                                        case 330:
89
                                                if (record == null && this._builder.TryGetCadObject(this._reader.ValueAsHandle, out record))
×
90
                                                {
×
91
                                                        record.BlockEntity = blckEntity;
×
92
                                                }
×
93
                                                else if (record == null)
×
94
                                                {
×
95
                                                        this._builder.Notify($"Block record with handle [{this._reader.ValueAsString}] not found at line {this._reader.Position}", NotificationType.Warning);
×
96
                                                }
×
97
                                                break;
×
98
                                        default:
99
                                                if (!this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.BlockBegin]))
×
100
                                                {
×
101
                                                        this.readCommonEntityCodes(template, out bool isExtendedData, map);
×
102
                                                        if (isExtendedData)
×
103
                                                                continue;
×
104
                                                }
×
105
                                                break;
×
106
                                }
107

108
                                this._reader.ReadNext();
×
109
                        }
×
110

111
                        if (record == null)
×
112
                        {
×
113
                                record = new BlockRecord(name);
×
114
                                record.BlockEntity = blckEntity;
×
115
                                CadBlockRecordTemplate recordTemplate = new CadBlockRecordTemplate(record);
×
116

117
                                this._builder.BlockRecords.Add(record);
×
118

119
                                if (recordTemplate.CadObject.Name.Equals(BlockRecord.ModelSpaceName, StringComparison.OrdinalIgnoreCase))
×
120
                                {
×
121
                                        this._builder.ModelSpaceTemplate = recordTemplate;
×
122
                                }
×
123
                        }
×
124

125
                        while (this._reader.ValueAsString != DxfFileToken.EndBlock)
×
126
                        {
×
127
                                CadEntityTemplate entityTemplate = null;
×
128

129
                                try
130
                                {
×
131
                                        entityTemplate = this.readEntity();
×
132
                                }
×
133
                                catch (Exception ex)
×
134
                                {
×
135
                                        if (!this._builder.Configuration.Failsafe)
×
136
                                                throw;
×
137

138
                                        this._builder.Notify($"Error while reading a block with name {record.Name} at line {this._reader.Position}", NotificationType.Error, ex);
×
139

140
                                        while (this._reader.DxfCode != DxfCode.Start)
×
141
                                                this._reader.ReadNext();
×
142
                                }
×
143

144
                                if (entityTemplate == null)
×
145
                                        continue;
×
146

147
                                //Add the object and the template to the builder
148
                                this._builder.AddTemplate(entityTemplate);
×
149
                                record.Entities.Add(entityTemplate.CadObject);
×
150
                        }
×
151

152
                        this.readBlockEnd(record.BlockEnd);
×
153
                        this._builder.AddTemplate(template);
×
154
                }
×
155

156
                private void readBlockEnd(BlockEnd block)
157
                {
×
158
                        DxfMap map = DxfMap.Create<BlockEnd>();
×
159
                        CadEntityTemplate template = new CadEntityTemplate(block);
×
160

161
                        if (this._reader.DxfCode == DxfCode.Start)
×
162
                        {
×
163
                                this._reader.ReadNext();
×
164
                        }
×
165

166
                        while (this._reader.DxfCode != DxfCode.Start)
×
167
                        {
×
168
                                switch (this._reader.Code)
×
169
                                {
170
                                        default:
171
                                                if (!this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.BlockEnd]))
×
172
                                                {
×
173
                                                        this.readCommonEntityCodes(template, out bool isExtendedData, map);
×
174
                                                        if (isExtendedData)
×
175
                                                                continue;
×
176
                                                }
×
177
                                                break;
×
178
                                }
179

180
                                this._reader.ReadNext();
×
181
                        }
×
182

183
                        this._builder.AddTemplate(template);
×
184
                }
×
185
        }
186
}
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