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

DomCR / ACadSharp / 20091252147

10 Dec 2025 07:49AM UTC coverage: 77.88% (-0.1%) from 78.004%
20091252147

push

github

web-flow
Merge pull request #915 from DomCR/issue-914_dxf-owner-assignation-blocks-section

issue 914

7572 of 10557 branches covered (71.72%)

Branch coverage included in aggregate %.

12 of 17 new or added lines in 4 files covered. (70.59%)

38 existing lines in 2 files now uncovered.

28125 of 35279 relevant lines covered (79.72%)

162436.13 hits per line

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

72.73
/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)
280✔
14
                {
280✔
15
                }
280✔
16

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

22
                        //Loop until the section ends
23
                        while (this._reader.ValueAsString != DxfFileToken.EndSection)
10,076✔
24
                        {
9,796✔
25
                                try
26
                                {
9,796✔
27
                                        if (this._reader.ValueAsString == DxfFileToken.Block)
9,796!
28
                                                this.readBlock();
9,796✔
29
                                        else
30
                                                throw new DxfException($"Unexpected token at the BLOCKS table: {this._reader.ValueAsString}", this._reader.Position);
×
31
                                }
9,796✔
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
                        }
9,796✔
46
                }
280✔
47

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

52
                        //Read the table name
53
                        this._reader.ReadNext();
9,796✔
54

55
                        DxfMap map = DxfMap.Create<Block>();
9,796✔
56

57
                        Block blckEntity = new Block();
9,796✔
58
                        CadBlockEntityTemplate template = new CadBlockEntityTemplate(blckEntity);
9,796✔
59

60
                        string name = null;
9,796✔
61
                        BlockRecord record = null;
9,796✔
62
                        CadBlockRecordTemplate recordTemplate = null;
9,796✔
63

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

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

109
                                this._reader.ReadNext();
110,014✔
110
                        }
110,014✔
111

112
                        if (record == null)
9,796✔
113
                        {
3,610✔
114
                                record = new BlockRecord(name);
3,610✔
115
                                record.BlockEntity = blckEntity;
3,610✔
116
                                recordTemplate = new CadBlockRecordTemplate(record);
3,610✔
117

118
                                this._builder.AddTemplate(recordTemplate);
3,610✔
119
                                this._builder.BlockRecords.Add(record);
3,610✔
120

121
                                if (recordTemplate.CadObject.Name.Equals(BlockRecord.ModelSpaceName, StringComparison.OrdinalIgnoreCase))
3,610✔
122
                                {
38✔
123
                                        this._builder.ModelSpaceTemplate = recordTemplate;
38✔
124
                                }
38✔
125
                        }
3,610✔
126
                        else if (!this._builder.TryGetObjectTemplate<CadBlockRecordTemplate>(record.Handle, out recordTemplate))
6,186!
127
                        {
×
128
                                recordTemplate = new CadBlockRecordTemplate(record);
×
129
                        }
×
130

131
                        //Add the entity template for owner information
132
                        recordTemplate.BlockEntityTemplate = template;
9,796✔
133

134
                        while (this._reader.ValueAsString != DxfFileToken.EndBlock)
134,930✔
135
                        {
125,134✔
136
                                CadEntityTemplate entityTemplate = null;
125,134✔
137

138
                                try
139
                                {
125,134✔
140
                                        entityTemplate = this.readEntity();
125,134✔
141
                                }
125,134✔
142
                                catch (Exception ex)
×
143
                                {
×
144
                                        if (!this._builder.Configuration.Failsafe)
×
145
                                                throw;
×
146

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

149
                                        while (this._reader.DxfCode != DxfCode.Start)
×
150
                                                this._reader.ReadNext();
×
151
                                }
×
152

153
                                if (entityTemplate == null)
125,134✔
154
                                        continue;
×
155

156
                                //Add the object and the template to the builder
157
                                this._builder.AddTemplate(entityTemplate);
125,134✔
158

159
                                if (entityTemplate.OwnerHandle == null)
125,134✔
160
                                {
85,006✔
161
                                        recordTemplate.OwnedObjectsHandlers.Add(entityTemplate.CadObject.Handle);
85,006✔
162
                                }
85,006✔
163
                                else if (this._builder.TryGetObjectTemplate(entityTemplate.OwnerHandle, out ICadOwnerTemplate owner))
40,128!
164
                                {
40,128✔
165
                                        owner.OwnedObjectsHandlers.Add(entityTemplate.CadObject.Handle);
40,128✔
166
                                }
40,128✔
167
                                else
NEW
168
                                {
×
NEW
169
                                        _builder.OrphanTemplates.Add(entityTemplate);
×
NEW
170
                                }
×
171
                        }
125,134✔
172

173
                        this.readBlockEnd(record.BlockEnd);
9,796✔
174
                        this._builder.AddTemplate(template);
9,796✔
175
                }
9,796✔
176

177
                private void readBlockEnd(BlockEnd block)
178
                {
9,796✔
179
                        DxfMap map = DxfMap.Create<BlockEnd>();
9,796✔
180
                        CadEntityTemplate template = new CadEntityTemplate(block);
9,796✔
181

182
                        if (this._reader.DxfCode == DxfCode.Start)
9,796✔
183
                        {
9,796✔
184
                                this._reader.ReadNext();
9,796✔
185
                        }
9,796✔
186

187
                        while (this._reader.DxfCode != DxfCode.Start)
54,524✔
188
                        {
44,728✔
189
                                switch (this._reader.Code)
44,728✔
190
                                {
191
                                        default:
192
                                                if (!this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.BlockEnd]))
44,728✔
193
                                                {
44,728✔
194
                                                        this.readCommonEntityCodes(template, out bool isExtendedData, map);
44,728✔
195
                                                        if (isExtendedData)
44,728✔
196
                                                                continue;
×
197
                                                }
44,728✔
198
                                                break;
44,728✔
199
                                }
200

201
                                this._reader.ReadNext();
44,728✔
202
                        }
44,728✔
203

204
                        this._builder.AddTemplate(template);
9,796✔
205
                }
9,796✔
206
        }
207
}
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