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

DomCR / ACadSharp / 17737836230

15 Sep 2025 03:12PM UTC coverage: 2.092% (-76.2%) from 78.245%
17737836230

push

github

web-flow
Merge pull request #790 from DomCR/addflag-refactor

addflag refactor

141 of 9225 branches covered (1.53%)

Branch coverage included in aggregate %.

0 of 93 new or added lines in 10 files covered. (0.0%)

24910 existing lines in 372 files now uncovered.

724 of 32119 relevant lines covered (2.25%)

5.76 hits per line

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

0.0
/src/ACadSharp/Objects/CadDictionary.cs
1
using ACadSharp.Attributes;
2
using ACadSharp.Extensions;
3
using CSUtilities.Extensions;
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="CadDictionary"/> object.
13
        /// </summary>
14
        /// <remarks>
15
        /// Object name <see cref="DxfFileToken.ObjectDictionary"/> <br/>
16
        /// Dxf class name <see cref="DxfSubclassMarker.Dictionary"/>
17
        /// </remarks>
18
        [DxfName(DxfFileToken.ObjectDictionary)]
19
        [DxfSubClass(DxfSubclassMarker.Dictionary)]
20
        public class CadDictionary : NonGraphicalObject, IObservableCadCollection<NonGraphicalObject>
21
        {
22
                public event EventHandler<CollectionChangedEventArgs> OnAdd;
23

24
                public event EventHandler<CollectionChangedEventArgs> OnRemove;
25

26
                /// <summary>
27
                /// Duplicate record cloning flag (determines how to merge duplicate entries)
28
                /// </summary>
29
                [DxfCodeValue(281)]
UNCOV
30
                public DictionaryCloningFlags ClonningFlags { get; set; }
×
31

32
                /// <summary>
33
                /// Soft-owner ID/handle to entry object
34
                /// </summary>
35
                [DxfCodeValue(350)]
36
                public ulong[] EntryHandles { get { return this._entries.Values.Select(c => c.Handle).ToArray(); } }
×
37

38
                /// <summary>
39
                /// Entry names
40
                /// </summary>
41
                [DxfCodeValue(3)]
42
                public string[] EntryNames { get { return this._entries.Keys.ToArray(); } }
×
43

44
                /// <summary>
45
                /// indicates that elements of the dictionary are to be treated as hard-owned.
46
                /// </summary>
47
                [DxfCodeValue(280)]
UNCOV
48
                public bool HardOwnerFlag { get; set; }
×
49

50
                /// <inheritdoc/>
UNCOV
51
                public override string ObjectName => DxfFileToken.ObjectDictionary;
×
52

53
                /// <inheritdoc/>
UNCOV
54
                public override ObjectType ObjectType => ObjectType.DICTIONARY;
×
55

56
                /// <inheritdoc/>
57
                public override string SubclassMarker => DxfSubclassMarker.Dictionary;
×
58

59
                /// <summary>
60
                /// ACAD_COLOR dictionary entry.
61
                /// </summary>
62
                public const string AcadColor = "ACAD_COLOR";
63

64
                /// <summary>
65
                /// ACAD_FIELDLIST dictionary entry.
66
                /// </summary>
67
                public const string AcadFieldList = "ACAD_FIELDLIST";
68

69
                /// <summary>
70
                /// ACAD_GROUP dictionary entry.
71
                /// </summary>
72
                public const string AcadGroup = "ACAD_GROUP";
73

74
                /// <summary>
75
                /// ACAD_IMAGE_DICT dictionary entry.
76
                /// </summary>
77
                public const string AcadImageDict = "ACAD_IMAGE_DICT";
78

79
                /// <summary>
80
                /// ACAD_LAYOUT dictionary entry.
81
                /// </summary>
82
                public const string AcadLayout = "ACAD_LAYOUT";
83

84
                /// <summary>
85
                /// ACAD_MATERIAL dictionary entry.
86
                /// </summary>
87
                public const string AcadMaterial = "ACAD_MATERIAL";
88

89
                /// <summary>
90
                /// ACAD_MLEADERSTYLE dictionary entry.
91
                /// </summary>
92
                public const string AcadMLeaderStyle = "ACAD_MLEADERSTYLE";
93

94
                /// <summary>
95
                /// ACAD_MLINESTYLE dictionary entry.
96
                /// </summary>
97
                public const string AcadMLineStyle = "ACAD_MLINESTYLE";
98

99
                /// <summary>
100
                /// ACAD_PDFDEFINITIONS dictionary entry.
101
                /// </summary>
102
                public const string AcadPdfDefinitions = "ACAD_PDFDEFINITIONS";
103

104
                /// <summary>
105
                /// ACAD_PLOTSETTINGS dictionary entry.
106
                /// </summary>
107
                public const string AcadPlotSettings = "ACAD_PLOTSETTINGS";
108

109
                /// <summary>
110
                /// ACAD_PLOTSTYLENAME dictionary entry.
111
                /// </summary>
112
                public const string AcadPlotStyleName = "ACAD_PLOTSTYLENAME";
113

114
                /// <summary>
115
                /// scales dictionary
116
                /// </summary>
117
                public const string AcadScaleList = "ACAD_SCALELIST";
118

119
                /// <summary>
120
                /// ACAD_SORTENTS dictionary entry.
121
                /// </summary>
122
                public const string AcadSortEnts = "ACAD_SORTENTS";
123

124
                /// <summary>
125
                /// ACAD_TABLESTYLE dictionary entry.
126
                /// </summary>
127
                public const string AcadTableStyle = "ACAD_TABLESTYLE";
128

129
                /// <summary>
130
                /// ACAD_VISUALSTYLE dictionary entry.
131
                /// </summary>
132
                public const string AcadVisualStyle = "ACAD_VISUALSTYLE";
133

134
                /// <summary>
135
                /// ACAD_GEOGRAPHICDATA dictionary entry.
136
                /// </summary>
137
                public const string GeographicData = "ACAD_GEOGRAPHICDATA";
138

139
                /// <summary>
140
                /// ROOT dictionary, only used in the top level dictionary.
141
                /// </summary>
142
                public const string Root = "ROOT";
143

144
                /// <summary>
145
                /// AcDbVariableDictionary dictionary entry.
146
                /// </summary>
147
                public const string VariableDictionary = "AcDbVariableDictionary";
148

UNCOV
149
                private Dictionary<string, NonGraphicalObject> _entries = new(StringComparer.OrdinalIgnoreCase);
×
150

151
                /// <summary>
152
                /// Default constructor.
153
                /// </summary>
UNCOV
154
                public CadDictionary()
×
UNCOV
155
                { }
×
156

157
                /// <summary>
158
                /// Constructor for a named dictionary.
159
                /// </summary>
160
                /// <param name="name">Dictionary name.</param>
UNCOV
161
                public CadDictionary(string name)
×
UNCOV
162
                {
×
UNCOV
163
                        this.Name = name;
×
UNCOV
164
                }
×
165

166
                /// <summary>
167
                /// Create the default entries for the root dictionary.
168
                /// </summary>
169
                public static void CreateDefaultEntries(CadDictionary root)
UNCOV
170
                {
×
UNCOV
171
                        root.TryAdd(new CadDictionary(AcadColor));
×
UNCOV
172
                        root.TryAdd(new CadDictionary(AcadGroup));
×
173

UNCOV
174
                        CadDictionary layouts = root.ensureCadDictionaryExist(AcadLayout);
×
175

UNCOV
176
                        root.TryAdd(new CadDictionary(AcadMaterial));
×
UNCOV
177
                        root.TryAdd(new CadDictionary(AcadSortEnts));
×
178

UNCOV
179
                        CadDictionary mLeaderStyles = root.ensureCadDictionaryExist(AcadMLeaderStyle);
×
UNCOV
180
                        mLeaderStyles.TryAdd(MultiLeaderStyle.Default);
×
181

UNCOV
182
                        CadDictionary mLineStyles = root.ensureCadDictionaryExist(AcadMLineStyle);
×
UNCOV
183
                        mLineStyles.TryAdd(MLineStyle.Default);
×
184

UNCOV
185
                        root.TryAdd(new CadDictionary(AcadTableStyle));
×
UNCOV
186
                        root.TryAdd(new CadDictionary(AcadPlotSettings));
×
187
                        // { AcadPlotStyleName, new CadDictionaryWithDefault() },        //Add default entry "Normal"        PlaceHolder        ??
188

UNCOV
189
                        CadDictionary variableDictionary = root.ensureCadDictionaryExist(VariableDictionary);
×
UNCOV
190
                        root.TryAdd(variableDictionary);
×
UNCOV
191
                        DictionaryVariable cmLeaderStyleEntry = new DictionaryVariable
×
UNCOV
192
                        (
×
UNCOV
193
                                DictionaryVariable.CurrentMultiLeaderStyle,
×
UNCOV
194
                                MultiLeaderStyle.DefaultName
×
UNCOV
195
                        );
×
UNCOV
196
                        variableDictionary.TryAdd(cmLeaderStyleEntry);
×
197

198
                        //DictionaryVars Entry DIMASSOC and HIDETEXT ??
199

UNCOV
200
                        CadDictionary scales = root.ensureCadDictionaryExist(AcadScaleList);
×
UNCOV
201
                        scales.TryAdd(new Scale { Name = "A0", PaperUnits = 1.0, DrawingUnits = 1.0, IsUnitScale = true });
×
UNCOV
202
                        scales.TryAdd(new Scale { Name = "A1", PaperUnits = 1.0, DrawingUnits = 2.0, IsUnitScale = false });
×
UNCOV
203
                        scales.TryAdd(new Scale { Name = "A2", PaperUnits = 1.0, DrawingUnits = 4.0, IsUnitScale = false });
×
UNCOV
204
                        scales.TryAdd(new Scale { Name = "A3", PaperUnits = 1.0, DrawingUnits = 5.0, IsUnitScale = false });
×
UNCOV
205
                        scales.TryAdd(new Scale { Name = "A4", PaperUnits = 1.0, DrawingUnits = 8.0, IsUnitScale = false });
×
UNCOV
206
                        scales.TryAdd(new Scale { Name = "A5", PaperUnits = 1.0, DrawingUnits = 10.0, IsUnitScale = false });
×
UNCOV
207
                        scales.TryAdd(new Scale { Name = "A6", PaperUnits = 1.0, DrawingUnits = 16.0, IsUnitScale = false });
×
UNCOV
208
                        scales.TryAdd(new Scale { Name = "A7", PaperUnits = 1.0, DrawingUnits = 20.0, IsUnitScale = false });
×
UNCOV
209
                        scales.TryAdd(new Scale { Name = "A8", PaperUnits = 1.0, DrawingUnits = 30.0, IsUnitScale = false });
×
UNCOV
210
                        scales.TryAdd(new Scale { Name = "A9", PaperUnits = 1.0, DrawingUnits = 40.0, IsUnitScale = false });
×
UNCOV
211
                        scales.TryAdd(new Scale { Name = "B0", PaperUnits = 1.0, DrawingUnits = 50.0, IsUnitScale = false });
×
UNCOV
212
                        scales.TryAdd(new Scale { Name = "B1", PaperUnits = 1.0, DrawingUnits = 100.0, IsUnitScale = false });
×
UNCOV
213
                        scales.TryAdd(new Scale { Name = "B2", PaperUnits = 2.0, DrawingUnits = 1.0, IsUnitScale = false });
×
UNCOV
214
                        scales.TryAdd(new Scale { Name = "B3", PaperUnits = 4.0, DrawingUnits = 1.0, IsUnitScale = false });
×
UNCOV
215
                        scales.TryAdd(new Scale { Name = "B4", PaperUnits = 8.0, DrawingUnits = 1.0, IsUnitScale = false });
×
UNCOV
216
                        scales.TryAdd(new Scale { Name = "B5", PaperUnits = 10.0, DrawingUnits = 1.0, IsUnitScale = false });
×
UNCOV
217
                        scales.TryAdd(new Scale { Name = "B6", PaperUnits = 100.0, DrawingUnits = 1.0, IsUnitScale = false });
×
218

UNCOV
219
                        root.TryAdd(new CadDictionary(AcadVisualStyle));
×
UNCOV
220
                        root.TryAdd(new CadDictionary(AcadFieldList));
×
UNCOV
221
                        root.TryAdd(new CadDictionary(AcadImageDict));
×
UNCOV
222
                }
×
223

224
                /// <summary>
225
                /// Creates the root dictionary with the default entries.
226
                /// </summary>
227
                /// <returns></returns>
228
                public static CadDictionary CreateRoot()
UNCOV
229
                {
×
UNCOV
230
                        CadDictionary root = new CadDictionary(Root);
×
231

UNCOV
232
                        CreateDefaultEntries(root);
×
233

UNCOV
234
                        return root;
×
UNCOV
235
                }
×
236

237
                /// <summary>
238
                /// Add a <see cref="NonGraphicalObject"/> to the collection, this method triggers <see cref="OnAdd"/>
239
                /// </summary>
240
                /// <param name="key">key for the entry in the dictionary</param>
241
                /// <param name="value"></param>
242
                public void Add(string key, NonGraphicalObject value)
UNCOV
243
                {
×
UNCOV
244
                        if (string.IsNullOrEmpty(key))
×
245
                        {
×
246
                                throw new ArgumentNullException(nameof(value), $"NonGraphicalObject [{this.GetType().FullName}] must have a name");
×
247
                        }
248

UNCOV
249
                        this._entries.Add(key, value);
×
UNCOV
250
                        value.Owner = this;
×
251

UNCOV
252
                        value.OnNameChanged += this.onEntryNameChanged;
×
253

UNCOV
254
                        OnAdd?.Invoke(this, new CollectionChangedEventArgs(value));
×
UNCOV
255
                }
×
256

257
                /// <summary>
258
                /// Add a <see cref="NonGraphicalObject"/> to the collection, this method triggers <see cref="OnAdd"/>
259
                /// </summary>
260
                /// <param name="value">the name of the NonGraphicalObject will be used as a key for the dictionary</param>
261
                /// <exception cref="ArgumentException"></exception>
262
                public void Add(NonGraphicalObject value)
UNCOV
263
                {
×
UNCOV
264
                        this.Add(value.Name, value);
×
UNCOV
265
                }
×
266

267
                /// <summary>
268
                /// Removes all keys and values from the <see cref="CadDictionary"/>.
269
                /// </summary>
270
                public void Clear()
271
                {
×
272
                        foreach (var item in this._entries)
×
273
                        {
×
274
                                this.Remove(item.Key, out _);
×
275
                        }
×
276
                }
×
277

278
                /// <inheritdoc/>
279
                public override CadObject Clone()
UNCOV
280
                {
×
UNCOV
281
                        CadDictionary clone = (CadDictionary)base.Clone();
×
282

UNCOV
283
                        clone.OnAdd = null;
×
UNCOV
284
                        clone.OnRemove = null;
×
285

UNCOV
286
                        clone._entries = new Dictionary<string, NonGraphicalObject>();
×
UNCOV
287
                        foreach (NonGraphicalObject item in this._entries.Values)
×
UNCOV
288
                        {
×
UNCOV
289
                                clone.Add(item.CloneTyped());
×
UNCOV
290
                        }
×
291

UNCOV
292
                        return clone;
×
UNCOV
293
                }
×
294

295
                /// <summary>
296
                /// Determines whether the <see cref="CadDictionary"/> contains the specified key.
297
                /// </summary>
298
                /// <param name="key">The key to locate in the <see cref="CadDictionary"/></param>
299
                /// <returns></returns>
300
                public bool ContainsKey(string key)
UNCOV
301
                {
×
UNCOV
302
                        return this._entries.ContainsKey(key);
×
UNCOV
303
                }
×
304

305
                /// <summary>
306
                /// Gets the value associated with the specific key
307
                /// </summary>
308
                /// <typeparam name="T"></typeparam>
309
                /// <param name="name"></param>
310
                /// <returns>The value with Type T or null if not found or different type</returns>
311
                public T GetEntry<T>(string name)
312
                        where T : NonGraphicalObject
UNCOV
313
                {
×
UNCOV
314
                        this.TryGetEntry<T>(name, out T value);
×
UNCOV
315
                        return value;
×
UNCOV
316
                }
×
317

318
                /// <inheritdoc/>
319
                public IEnumerator<NonGraphicalObject> GetEnumerator()
UNCOV
320
                {
×
UNCOV
321
                        return this._entries.Values.GetEnumerator();
×
UNCOV
322
                }
×
323

324
                /// <inheritdoc/>
325
                IEnumerator IEnumerable.GetEnumerator()
UNCOV
326
                {
×
UNCOV
327
                        return this._entries.Values.GetEnumerator();
×
UNCOV
328
                }
×
329

330
                /// <summary>
331
                /// Removes a <see cref="NonGraphicalObject"/> from the collection, this method triggers <see cref="OnRemove"/>
332
                /// </summary>
333
                /// <param name="key"></param>
334
                /// <param name="item"></param>
335
                /// <returns>true if the element is successfully removed; otherwise, false.</returns>
336
                public bool Remove(string key, out NonGraphicalObject item)
UNCOV
337
                {
×
UNCOV
338
                        if (this._entries.Remove(key, out item))
×
UNCOV
339
                        {
×
UNCOV
340
                                item.Owner = null;
×
UNCOV
341
                                OnRemove?.Invoke(this, new CollectionChangedEventArgs(item));
×
UNCOV
342
                                item.OnNameChanged -= this.onEntryNameChanged;
×
UNCOV
343
                                return true;
×
344
                        }
345

UNCOV
346
                        return false;
×
UNCOV
347
                }
×
348

349
                /// <summary>
350
                /// Removes a <see cref="NonGraphicalObject"/> from the collection, this method triggers <see cref="OnRemove"/>
351
                /// </summary>
352
                /// <param name="key"></param>
353
                /// <returns>true if the element is successfully removed; otherwise, false.</returns>
354
                public bool Remove(string key)
UNCOV
355
                {
×
UNCOV
356
                        return this.Remove(key, out _);
×
UNCOV
357
                }
×
358

359
                /// <summary>
360
                /// Tries to add the <see cref="NonGraphicalObject"/> entry using the name as key.
361
                /// </summary>
362
                /// <param name="value"></param>
363
                /// <returns>true if the element is successfully added; otherwise, false.</returns>
364
                public bool TryAdd(NonGraphicalObject value)
UNCOV
365
                {
×
UNCOV
366
                        if (!this._entries.ContainsKey(value.Name))
×
UNCOV
367
                        {
×
UNCOV
368
                                this.Add(value.Name, value);
×
UNCOV
369
                                return true;
×
370
                        }
371

UNCOV
372
                        return false;
×
UNCOV
373
                }
×
374

375
                /// <summary>
376
                /// Gets the value associated with the specific key
377
                /// </summary>
378
                /// <typeparam name="T"></typeparam>
379
                /// <param name="name"></param>
380
                /// <param name="value"></param>
381
                /// <returns>true if the value is found or false if not found or different type</returns>
382
                public bool TryGetEntry<T>(string name, out T value)
383
                        where T : NonGraphicalObject
UNCOV
384
                {
×
UNCOV
385
                        if (this._entries.TryGetValue(name, out NonGraphicalObject obj))
×
UNCOV
386
                        {
×
UNCOV
387
                                if (obj is T t)
×
UNCOV
388
                                {
×
UNCOV
389
                                        value = t;
×
UNCOV
390
                                        return true;
×
391
                                }
392
                        }
×
393

UNCOV
394
                        value = null;
×
UNCOV
395
                        return false;
×
UNCOV
396
                }
×
397

398
                private CadDictionary ensureCadDictionaryExist(string name)
UNCOV
399
                {
×
UNCOV
400
                        if (!this.TryGetEntry(name, out CadDictionary entry))
×
UNCOV
401
                        {
×
UNCOV
402
                                entry = new CadDictionary(name);
×
UNCOV
403
                                this.Add(entry);
×
UNCOV
404
                        }
×
405

UNCOV
406
                        return entry;
×
UNCOV
407
                }
×
408

409
                private void onEntryNameChanged(object sender, OnNameChangedArgs e)
UNCOV
410
                {
×
UNCOV
411
                        var entry = this._entries[e.OldName];
×
UNCOV
412
                        this._entries.Add(e.NewName, entry);
×
UNCOV
413
                        this._entries.Remove(e.OldName);
×
UNCOV
414
                }
×
415

UNCOV
416
                public CadObject this[string key] { get { return this._entries[key]; } }
×
417
        }
418
}
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