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

DomCR / ACadSharp / 25397603470

05 May 2026 07:27PM UTC coverage: 76.808% (+0.004%) from 76.804%
25397603470

Pull #1062

github

web-flow
Merge 9eb5f2824 into ab35acaf4
Pull Request #1062: issue-897 Refactor SortEntitiesTable

8577 of 12103 branches covered (70.87%)

Branch coverage included in aggregate %.

88 of 103 new or added lines in 3 files covered. (85.44%)

17 existing lines in 3 files now uncovered.

30765 of 39118 relevant lines covered (78.65%)

152823.73 hits per line

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

85.04
/src/ACadSharp/Objects/SortEntitiesTable.cs
1
using ACadSharp.Attributes;
2
using ACadSharp.Entities;
3
using ACadSharp.Tables;
4
using System;
5
using System.Collections;
6
using System.Collections.Generic;
7
using System.Linq;
8

9
namespace ACadSharp.Objects;
10

11
/// <summary>
12
/// Represents a <see cref="SortEntitiesTable"/> object
13
/// </summary>
14
/// <remarks>
15
/// Object name <see cref="DxfFileToken.ObjectSortEntsTable"/> <br/>
16
/// Dxf class name <see cref="DxfSubclassMarker.SortentsTable"/>
17
/// </remarks>
18
[DxfName(DxfFileToken.ObjectSortEntsTable)]
19
[DxfSubClass(DxfSubclassMarker.SortentsTable)]
20
public partial class SortEntitiesTable : NonGraphicalObject, IEnumerable<SortEntitiesTable.Sorter>
21
{
22
        /// <summary>
23
        /// Block owner where the table is applied
24
        /// </summary>
25
        [DxfCodeValue(330)]
26
        public BlockRecord BlockOwner { get; internal set; }
1,063✔
27

28
        /// <inheritdoc/>
29
        public override string ObjectName => DxfFileToken.ObjectSortEntsTable;
1,719✔
30

31
        /// <inheritdoc/>
NEW
32
        public override ObjectType ObjectType => ObjectType.UNLISTED;
×
33

34
        /// <inheritdoc/>
NEW
35
        public override string SubclassMarker => DxfSubclassMarker.SortentsTable;
×
36

37
        /// <summary>
38
        /// Dictionary entry name for the object <see cref="SortEntitiesTable"/>
39
        /// </summary>
40
        public const string DictionaryEntryName = "ACAD_SORTENTS";
41

42
        private List<Sorter> _sorters = new();
1,025✔
43

44
        internal SortEntitiesTable()
1,025✔
45
        {
1,025✔
46
                this.Name = DictionaryEntryName;
1,025✔
47
        }
1,025✔
48

49
        internal SortEntitiesTable(BlockRecord owner) : this()
19✔
50
        {
19✔
51
                this.BlockOwner = owner;
19✔
52
        }
19✔
53

54
        /// <summary>
55
        /// Sorter attached to an entity.
56
        /// </summary>
57
        /// <param name="entity">Entity in the block to be sorted.</param>
58
        /// <param name="sorterHandle">Sorter handle.</param>
59
        /// <exception cref="ArgumentException"></exception>
60
        public void Add(Entity entity, ulong sorterHandle)
61
        {
2,791✔
62
                this._sorters.Add(new Sorter(entity, sorterHandle));
2,791✔
63
        }
2,791✔
64

65
        /// <summary>
66
        /// Removes all elements in the collection.
67
        /// </summary>
68
        public void Clear()
NEW
69
        {
×
NEW
70
                this._sorters.Clear();
×
NEW
71
        }
×
72

73
        /// <inheritdoc/>
74
        public override CadObject Clone()
75
        {
122✔
76
                SortEntitiesTable clone = (SortEntitiesTable)base.Clone();
122✔
77

78
                clone._sorters = new List<Sorter>();
122✔
79

80
                return clone;
122✔
81
        }
122✔
82

83
        /// <inheritdoc/>
84
        public IEnumerator<Sorter> GetEnumerator()
85
        {
61✔
86
                this._sorters.Sort();
61✔
87
                return this._sorters.GetEnumerator();
61✔
88
        }
61✔
89

90
        /// <inheritdoc/>
91
        IEnumerator IEnumerable.GetEnumerator()
92
        {
1✔
93
                return this.GetEnumerator();
1✔
94
        }
1✔
95

96
        /// <summary>
97
        /// Get the sorter handle of an entity, if is not in the sorter table it will return the entity's handle.
98
        /// </summary>
99
        /// <param name="entity"></param>
100
        /// <returns></returns>
101
        public ulong GetSorterHandle(Entity entity)
102
        {
300✔
103
                Sorter sorter = this._sorters.FirstOrDefault(s => s.Entity.Equals(entity));
348✔
104

105
                if (sorter is not null)
300✔
106
                {
18✔
107
                        return sorter.SortHandle;
18✔
108
                }
109
                else
110
                {
282✔
111
                        return entity.Handle;
282✔
112
                }
113
        }
300✔
114

115
        /// <summary>
116
        /// Moves the specified entity to the bottom of the draw order.
117
        /// </summary>
118
        /// <param name="entity">Entity to move to the bottom.</param>
119
        public void MoveToBottom(Entity entity)
120
        {
3✔
121
                ulong maxHandle = this._sorters.Count > 0
3!
122
                        ? this._sorters.Max(s => s.SortHandle)
7✔
123
                        : entity.Handle;
3✔
124

125
                ulong newHandle = maxHandle < ulong.MaxValue ? maxHandle + 1 : ulong.MaxValue;
3!
126

127
                Sorter existing = this._sorters.FirstOrDefault(s => s.Entity.Equals(entity));
7✔
128
                if (existing is not null)
3✔
129
                {
2✔
130
                        existing.SortHandle = newHandle;
2✔
131
                }
2✔
132
                else
133
                {
1✔
134
                        this._sorters.Add(new Sorter(entity, newHandle));
1✔
135
                }
1✔
136
        }
3✔
137

138
        /// <summary>
139
        /// Moves the specified entity to the top of the draw order.
140
        /// </summary>
141
        /// <param name="entity">Entity to move to the top.</param>
142
        public void MoveToTop(Entity entity)
143
        {
3✔
144
                ulong minHandle = this._sorters.Count > 0
3!
145
                        ? this._sorters.Min(s => s.SortHandle)
7✔
146
                        : entity.Handle;
3✔
147

148
                ulong sorter = minHandle > 0 ? minHandle - 1 : 0;
3!
149

150
                Sorter existing = this._sorters.FirstOrDefault(s => s.Entity.Equals(entity));
9✔
151
                if (existing is not null)
3✔
152
                {
2✔
153
                        existing.SortHandle = sorter;
2✔
154
                }
2✔
155
                else
156
                {
1✔
157
                        this._sorters.Add(new Sorter(entity, sorter));
1✔
158
                }
1✔
159
        }
3✔
160

161
        /// <summary>
162
        /// Removes the first occurrence of a specific object from the sorters table.
163
        /// </summary>
164
        /// <param name="entity"></param>
165
        /// <returns></returns>
166
        public bool Remove(Entity entity)
NEW
167
        {
×
NEW
168
                var sorter = this._sorters.FirstOrDefault(s => s.Entity.Equals(entity));
×
NEW
169
                if (sorter is null)
×
UNCOV
170
                {
×
NEW
171
                        return false;
×
172
                }
173

NEW
174
                return this._sorters.Remove(sorter);
×
NEW
175
        }
×
176

177
        /// <summary>
178
        /// Moves the specified entity one step up in the draw order.
179
        /// If the entity is not in the table or is already at the top, no action is taken.
180
        /// </summary>
181
        /// <param name="entity">Entity to move one step up.</param>
182
        public void OneStepUp(Entity entity)
183
        {
6✔
184
                Sorter existing = this._sorters.FirstOrDefault(s => s.Entity.Equals(entity));
23✔
185
                if (existing is null)
6✔
186
                {
1✔
187
                        return;
1✔
188
                }
189

190
                int index = this._sorters.OrderBy(s => s.SortHandle).ToList().IndexOf(existing);
20✔
191
                if (index <= 0 || _sorters.Count < 2)
5✔
192
                {
1✔
193
                        return;
1✔
194
                }
195

196
                Sorter previous = this._sorters[index - 1];
4✔
197
                (existing.SortHandle, previous.SortHandle) = (previous.SortHandle, existing.SortHandle);
4✔
198
        }
6✔
199

200
        /// <summary>
201
        /// Moves the specified entity one step down in the draw order.
202
        /// If the entity is not in the table or is already at the bottom, no action is taken.
203
        /// </summary>
204
        /// <param name="entity">Entity to move one step down.</param>
205
        public void OneStepDown(Entity entity)
206
        {
6✔
207
                Sorter existing = this._sorters.FirstOrDefault(s => s.Entity.Equals(entity));
19✔
208
                if (existing is null)
6✔
209
                {
1✔
210
                        return;
1✔
211
                }
212

213
                this._sorters.Sort();
5✔
214

215
                int index = this._sorters.IndexOf(existing);
5✔
216
                if (index < 0 || index >= this._sorters.Count - 1)
5!
217
                {
1✔
218
                        return;
1✔
219
                }
220

221
                Sorter next = this._sorters[index + 1];
4✔
222
                (existing.SortHandle, next.SortHandle) = (next.SortHandle, existing.SortHandle);
4✔
223
        }
6✔
224
}
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