• 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/Objects/CadDictionary.cs
1
using ACadSharp.Attributes;
2
using CSUtilities.Extensions;
3
using System;
4
using System.Collections;
5
using System.Collections.Generic;
6
using System.Linq;
7

8
namespace ACadSharp.Objects
9
{
10
        /// <summary>
11
        /// Represents a <see cref="CadDictionary"/> object.
12
        /// </summary>
13
        /// <remarks>
14
        /// Object name <see cref="DxfFileToken.ObjectDictionary"/> <br/>
15
        /// Dxf class name <see cref="DxfSubclassMarker.Dictionary"/>
16
        /// </remarks>
17
        [DxfName(DxfFileToken.ObjectDictionary)]
18
        [DxfSubClass(DxfSubclassMarker.Dictionary)]
19
        public class CadDictionary : NonGraphicalObject, IObservableCadCollection<NonGraphicalObject>
20
        {
21
                public event EventHandler<CollectionChangedEventArgs> OnAdd;
22
                public event EventHandler<CollectionChangedEventArgs> OnRemove;
23

24
                #region Root dictionary entries
25

26
                /// <summary>
27
                /// ROOT dictionary, only used in the top level dictionary
28
                /// </summary>
29
                public const string Root = "ROOT";
30

31
                /// <summary>
32
                /// ACAD_COLOR dictionary entry
33
                /// </summary>
34
                public const string AcadColor = "ACAD_COLOR";
35

36
                /// <summary>
37
                /// ACAD_GROUP dictionary entry
38
                /// </summary>
39
                public const string AcadGroup = "ACAD_GROUP";
40

41
                /// <summary>
42
                /// ACAD_LAYOUT dictionary entry
43
                /// </summary>
44
                public const string AcadLayout = "ACAD_LAYOUT";
45

46
                /// <summary>
47
                /// ACAD_MATERIAL dictionary entry
48
                /// </summary>
49
                public const string AcadMaterial = "ACAD_MATERIAL";
50

51
                /// <summary>
52
                /// ACAD_SORTENTS dictionary entry
53
                /// </summary>
54
                public const string AcadSortEnts = "ACAD_SORTENTS";
55

56
                /// <summary>
57
                /// ACAD_MLEADERSTYLE dictionary entry
58
                /// </summary>
59
                public const string AcadMLeaderStyle = "ACAD_MLEADERSTYLE";
60

61
                /// <summary>
62
                /// ACAD_MLINESTYLE dictionary entry
63
                /// </summary>
64
                public const string AcadMLineStyle = "ACAD_MLINESTYLE";
65

66
                /// <summary>
67
                /// ACAD_TABLESTYLE dictionary entry
68
                /// </summary>
69
                public const string AcadTableStyle = "ACAD_TABLESTYLE";
70

71
                /// <summary>
72
                /// ACAD_PLOTSETTINGS dictionary entry
73
                /// </summary>
74
                public const string AcadPlotSettings = "ACAD_PLOTSETTINGS";
75

76
                /// <summary>
77
                /// AcDbVariableDictionary dictionary entry
78
                /// </summary>
79
                public const string VariableDictionary = "AcDbVariableDictionary";
80

81
                /// <summary>
82
                /// ACAD_PLOTSTYLENAME dictionary entry
83
                /// </summary>
84
                public const string AcadPlotStyleName = "ACAD_PLOTSTYLENAME";
85

86
                /// <summary>
87
                /// scales dictionary
88
                /// </summary>
89
                public const string AcadScaleList = "ACAD_SCALELIST";
90

91
                /// <summary>
92
                /// ACAD_VISUALSTYLE dictionary entry
93
                /// </summary>
94
                public const string AcadVisualStyle = "ACAD_VISUALSTYLE";
95

96
                /// <summary>
97
                /// ACAD_FIELDLIST dictionary entry
98
                /// </summary>
99
                public const string AcadFieldList = "ACAD_FIELDLIST";
100

101
                /// <summary>
102
                /// ACAD_IMAGE_DICT dictionary entry
103
                /// </summary>
104
                public const string AcadImageDict = "ACAD_IMAGE_DICT";
105

106
                #endregion
107

108
                /// <inheritdoc/>
109
                public override ObjectType ObjectType => ObjectType.DICTIONARY;
×
110

111
                /// <inheritdoc/>
112
                public override string ObjectName => DxfFileToken.ObjectDictionary;
×
113

114
                /// <inheritdoc/>
115
                public override string SubclassMarker => DxfSubclassMarker.Dictionary;
×
116

117
                /// <summary>
118
                /// indicates that elements of the dictionary are to be treated as hard-owned.
119
                /// </summary>
120
                [DxfCodeValue(280)]
121
                public bool HardOwnerFlag { get; set; }
×
122

123
                /// <summary>
124
                /// Duplicate record cloning flag (determines how to merge duplicate entries)
125
                /// </summary>
126
                [DxfCodeValue(281)]
127
                public DictionaryCloningFlags ClonningFlags { get; set; }
×
128

129
                /// <summary>
130
                /// Entry names
131
                /// </summary>
132
                [DxfCodeValue(3)]
133
                public string[] EntryNames { get { return this._entries.Keys.ToArray(); } }
×
134

135
                /// <summary>
136
                /// Soft-owner ID/handle to entry object
137
                /// </summary>
138
                [DxfCodeValue(350)]
139
                public ulong[] EntryHandles { get { return this._entries.Values.Select(c => c.Handle).ToArray(); } }
×
140

141
                public CadObject this[string key] { get { return this._entries[key]; } }
×
142

143
                private readonly Dictionary<string, NonGraphicalObject> _entries = new(StringComparer.OrdinalIgnoreCase);
×
144

145
                /// <summary>
146
                /// Creates the root dictionary with the default entries.
147
                /// </summary>
148
                /// <returns></returns>
149
                public static CadDictionary CreateRoot()
150
                {
×
151
                        CadDictionary root = new CadDictionary(Root);
×
152

153
                        CreateDefaultEntries(root);
×
154

155
                        return root;
×
156
                }
×
157

158
                /// <summary>
159
                /// Create the default entries for the root dictionary.
160
                /// </summary>
161
                public static void CreateDefaultEntries(CadDictionary root)
162
                {
×
163
                        root.TryAdd(new CadDictionary(AcadColor));
×
164
                        root.TryAdd(new CadDictionary(AcadGroup));
×
165

166
                        CadDictionary layouts = root.ensureCadDictionaryExist(AcadLayout);
×
167

168
                        root.TryAdd(new CadDictionary(AcadMaterial));
×
169
                        root.TryAdd(new CadDictionary(AcadSortEnts));
×
170

171
                        CadDictionary mLeaderStyles = root.ensureCadDictionaryExist(AcadMLeaderStyle);
×
172
                        mLeaderStyles.TryAdd(MultiLeaderStyle.Default);
×
173

174
                        CadDictionary mLineStyles = root.ensureCadDictionaryExist(AcadMLineStyle);
×
175
                        mLineStyles.TryAdd(MLineStyle.Default);
×
176

177
                        root.TryAdd(new CadDictionary(AcadTableStyle));
×
178
                        root.TryAdd(new CadDictionary(AcadPlotSettings));
×
179
                        // { AcadPlotStyleName, new CadDictionaryWithDefault() },        //Add default entry "Normal"        PlaceHolder        ??
180

181
                        root.TryAdd(new CadDictionary(VariableDictionary));
×
182
                        //DictionaryVars Entry DIMASSOC and HIDETEXT ??
183

184
                        CadDictionary scales = root.ensureCadDictionaryExist(AcadScaleList);
×
185
                        scales.TryAdd(new Scale { Name = "A0", PaperUnits = 1.0, DrawingUnits = 1.0, IsUnitScale = true });
×
186
                        scales.TryAdd(new Scale { Name = "A1", PaperUnits = 1.0, DrawingUnits = 2.0, IsUnitScale = false });
×
187
                        scales.TryAdd(new Scale { Name = "A2", PaperUnits = 1.0, DrawingUnits = 4.0, IsUnitScale = false });
×
188
                        scales.TryAdd(new Scale { Name = "A3", PaperUnits = 1.0, DrawingUnits = 5.0, IsUnitScale = false });
×
189
                        scales.TryAdd(new Scale { Name = "A4", PaperUnits = 1.0, DrawingUnits = 8.0, IsUnitScale = false });
×
190
                        scales.TryAdd(new Scale { Name = "A5", PaperUnits = 1.0, DrawingUnits = 10.0, IsUnitScale = false });
×
191
                        scales.TryAdd(new Scale { Name = "A6", PaperUnits = 1.0, DrawingUnits = 16.0, IsUnitScale = false });
×
192
                        scales.TryAdd(new Scale { Name = "A7", PaperUnits = 1.0, DrawingUnits = 20.0, IsUnitScale = false });
×
193
                        scales.TryAdd(new Scale { Name = "A8", PaperUnits = 1.0, DrawingUnits = 30.0, IsUnitScale = false });
×
194
                        scales.TryAdd(new Scale { Name = "A9", PaperUnits = 1.0, DrawingUnits = 40.0, IsUnitScale = false });
×
195
                        scales.TryAdd(new Scale { Name = "B0", PaperUnits = 1.0, DrawingUnits = 50.0, IsUnitScale = false });
×
196
                        scales.TryAdd(new Scale { Name = "B1", PaperUnits = 1.0, DrawingUnits = 100.0, IsUnitScale = false });
×
197
                        scales.TryAdd(new Scale { Name = "B2", PaperUnits = 2.0, DrawingUnits = 1.0, IsUnitScale = false });
×
198
                        scales.TryAdd(new Scale { Name = "B3", PaperUnits = 4.0, DrawingUnits = 1.0, IsUnitScale = false });
×
199
                        scales.TryAdd(new Scale { Name = "B4", PaperUnits = 8.0, DrawingUnits = 1.0, IsUnitScale = false });
×
200
                        scales.TryAdd(new Scale { Name = "B5", PaperUnits = 10.0, DrawingUnits = 1.0, IsUnitScale = false });
×
201
                        scales.TryAdd(new Scale { Name = "B6", PaperUnits = 100.0, DrawingUnits = 1.0, IsUnitScale = false });
×
202

203
                        root.TryAdd(new CadDictionary(AcadVisualStyle));
×
204
                        root.TryAdd(new CadDictionary(AcadFieldList));
×
205
                        root.TryAdd(new CadDictionary(AcadImageDict));
×
206
                }
×
207

208
                /// <summary>
209
                /// Default constructor.
210
                /// </summary>
211
                public CadDictionary() { }
×
212

213
                /// <summary>
214
                /// Constructor for a named dictionary.
215
                /// </summary>
216
                /// <param name="name">Dictionary name.</param>
217
                public CadDictionary(string name)
×
218
                {
×
219
                        this.Name = name;
×
220
                }
×
221

222
                /// <summary>
223
                /// Add a <see cref="NonGraphicalObject"/> to the collection, this method triggers <see cref="OnAdd"/>
224
                /// </summary>
225
                /// <param name="key">key for the entry in the dictionary</param>
226
                /// <param name="value"></param>
227
                public void Add(string key, NonGraphicalObject value)
228
                {
×
229
                        if (string.IsNullOrEmpty(key))
×
230
                        {
×
231
                                throw new ArgumentNullException(nameof(value), $"NonGraphicalObject [{this.GetType().FullName}] must have a name");
×
232
                        }
233

234
                        this._entries.Add(key, value);
×
235
                        value.Owner = this;
×
236

237
                        value.OnNameChanged += this.onEntryNameChanged;
×
238

239
                        OnAdd?.Invoke(this, new CollectionChangedEventArgs(value));
×
240
                }
×
241

242
                /// <summary>
243
                /// Add a <see cref="NonGraphicalObject"/> to the collection, this method triggers <see cref="OnAdd"/>
244
                /// </summary>
245
                /// <param name="value">the name of the NonGraphicalObject will be used as a key for the dictionary</param>
246
                /// <exception cref="ArgumentException"></exception>
247
                public void Add(NonGraphicalObject value)
248
                {
×
249
                        this.Add(value.Name, value);
×
250
                }
×
251

252
                /// <summary>
253
                /// Tries to add the <see cref="NonGraphicalObject"/> entry using the name as key.
254
                /// </summary>
255
                /// <param name="value"></param>
256
                /// <returns>true if the element is successfully added; otherwise, false.</returns>
257
                public bool TryAdd(NonGraphicalObject value)
258
                {
×
259
                        if (!this._entries.ContainsKey(value.Name))
×
260
                        {
×
261
                                this.Add(value.Name, value);
×
262
                                return true;
×
263
                        }
264

265
                        return false;
×
266
                }
×
267

268
                /// <summary>
269
                /// Determines whether the <see cref="CadDictionary"/> contains the specified key.
270
                /// </summary>
271
                /// <param name="key">The key to locate in the <see cref="CadDictionary"/></param>
272
                /// <returns></returns>
273
                public bool ContainsKey(string key)
274
                {
×
275
                        return this._entries.ContainsKey(key);
×
276
                }
×
277

278
                /// <summary>
279
                /// Removes a <see cref="NonGraphicalObject"/> from the collection, this method triggers <see cref="OnRemove"/>
280
                /// </summary>
281
                /// <param name="key"></param>
282
                /// <param name="item"></param>
283
                /// <returns>true if the element is successfully removed; otherwise, false.</returns>
284
                public bool Remove(string key, out NonGraphicalObject item)
285
                {
×
286
                        if (this._entries.Remove(key, out item))
×
287
                        {
×
288
                                item.Owner = null;
×
289
                                OnRemove?.Invoke(this, new CollectionChangedEventArgs(item));
×
290
                                return true;
×
291
                        }
292

293
                        return false;
×
294
                }
×
295

296
                /// <summary>
297
                /// Removes all keys and values from the <see cref="CadDictionary"/>.
298
                /// </summary>
299
                public void Clear()
300
                {
×
301
                        foreach (var item in this._entries)
×
302
                        {
×
303
                                this.Remove(item.Key, out _);
×
304
                        }
×
305
                }
×
306

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

320
                /// <summary>
321
                /// Gets the value associated with the specific key
322
                /// </summary>
323
                /// <typeparam name="T"></typeparam>
324
                /// <param name="name"></param>
325
                /// <param name="value"></param>
326
                /// <returns>true if the value is found or false if not found or different type</returns>
327
                public bool TryGetEntry<T>(string name, out T value)
328
                        where T : NonGraphicalObject
329
                {
×
330
                        if (this._entries.TryGetValue(name, out NonGraphicalObject obj))
×
331
                        {
×
332
                                if (obj is T t)
×
333
                                {
×
334
                                        value = t;
×
335
                                        return true;
×
336
                                }
337
                        }
×
338

339
                        value = null;
×
340
                        return false;
×
341
                }
×
342

343
                /// <inheritdoc/>
344
                public IEnumerator<NonGraphicalObject> GetEnumerator()
345
                {
×
346
                        return this._entries.Values.GetEnumerator();
×
347
                }
×
348

349
                /// <inheritdoc/>
350
                IEnumerator IEnumerable.GetEnumerator()
351
                {
×
352
                        return this._entries.Values.GetEnumerator();
×
353
                }
×
354

355
                private CadDictionary ensureCadDictionaryExist(string name)
356
                {
×
357
                        if (!this.TryGetEntry(name, out CadDictionary entry))
×
358
                        {
×
359
                                entry = new CadDictionary(name);
×
360
                                this.Add(entry);
×
361
                        }
×
362

363
                        return entry;
×
364
                }
×
365

366
                private void onEntryNameChanged(object sender, OnNameChangedArgs e)
367
                {
×
368

369
                        var entry = this._entries[e.OldName];
×
370
                        this._entries.Add(e.NewName, entry);
×
371
                        this._entries.Remove(e.OldName);
×
372
                }
×
373
        }
374
}
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