• 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/DxfMap.cs
1
using ACadSharp.Attributes;
2
using ACadSharp.Entities;
3
using ACadSharp.Tables;
4
using System;
5
using System.Collections.Concurrent;
6
using System.Collections.Generic;
7
using System.Linq;
8
using System.Reflection;
9

10
namespace ACadSharp
11
{
12
        public class DxfMap : DxfMapBase
13
        {
14
                /// <summary>
15
                /// Cache of created DXF mapped classes.
16
                /// </summary>
17
                private static readonly ConcurrentDictionary<Type, DxfMap> _cache = new ConcurrentDictionary<Type, DxfMap>();
×
18

19
                public Dictionary<string, DxfClassMap> SubClasses { get; private set; } = new Dictionary<string, DxfClassMap>();
×
20

21
                /// <summary>
22
                /// Creates a dxf map for an <see cref="Entity"/> or a <see cref="TableEntry"/>
23
                /// </summary>
24
                /// <remarks>
25
                /// This method does not work with the entities <see cref="AttributeEntity"/> and <see cref="AttributeDefinition"/>
26
                /// </remarks>
27
                public static DxfMap Create<T>()
28
                        where T : CadObject
29
                {
×
30
                        return DxfMap.Create(typeof(T));
×
31
                }
×
32

33
                //TODO: change to public? Using the type parameter does not constraing the use of the method
34
                internal static DxfMap Create(Type type)
35
                {
×
36
                        if (tryGetFromCache(type, out var map))
×
37
                        {
×
38
                                return map;
×
39
                        }
40

41
                        map = new DxfMap();
×
42
                        bool isDimensionStyle = false;
×
43

44
                        DxfNameAttribute dxf = type.GetCustomAttribute<DxfNameAttribute>();
×
45

46
                        map.Name = dxf?.Name;
×
47

48
                        for (Type t = type; t != null; t = t.BaseType)
×
49
                        {
×
50
                                DxfSubClassAttribute subclass = t.GetCustomAttribute<DxfSubClassAttribute>();
×
51

52
                                if (t.Equals(typeof(DimensionStyle)))
×
53
                                {
×
54
                                        isDimensionStyle = true;
×
55
                                }
×
56

57
                                if (t.Equals(typeof(CadObject)))
×
58
                                {
×
59
                                        addClassProperties(map, t);
×
60
                                        break;
×
61
                                }
62
                                else if (subclass != null && subclass.IsEmpty)
×
63
                                {
×
64
                                        DxfClassMap classMap = map.SubClasses.Last().Value;
×
65

66
                                        addClassProperties(classMap, t);
×
67

68
                                        if (subclass.ClassName != null)
×
69
                                        {
×
70
                                                map.SubClasses.Add(subclass.ClassName, new DxfClassMap(subclass.ClassName));
×
71
                                        }
×
72
                                }
×
73
                                else if (t.GetCustomAttribute<DxfSubClassAttribute>() != null)
×
74
                                {
×
75
                                        DxfClassMap classMap = new DxfClassMap();
×
76
                                        classMap.Name = subclass.ClassName;
×
77

78
                                        addClassProperties(classMap, t);
×
79

80
                                        map.SubClasses.Add(classMap.Name, classMap);
×
81
                                }
×
82
                        }
×
83

84
                        if (isDimensionStyle)
×
85
                        {
×
86
                                //TODO: Dimensions use the 105 instead of the 5... try to find a better fix
87
                                map.DxfProperties.Add(105, map.DxfProperties[5]);
×
88
                                map.DxfProperties.Remove(5);
×
89
                        }
×
90

91
                        map.SubClasses =
×
92
                                new Dictionary<string, DxfClassMap>(map.SubClasses.Reverse()
×
93
                                        .ToDictionary(o => o.Key, o => o.Value));
×
94

95
                        _cache.TryAdd(type, map);
×
96

97
                        if (tryGetFromCache(type, out map))
×
98
                        {
×
99
                                return map;
×
100
                        }
101

102
                        return map;
×
103
                }
×
104

105
                /// <summary>
106
                /// Clears the map cache.
107
                /// </summary>
108
                public static void ClearCache()
109
                {
×
110
                        _cache.Clear();
×
111
                }
×
112

113
                /// <inheritdoc/>
114
                public override string ToString()
115
                {
×
116
                        return $"DxfMap:{this.Name}";
×
117
                }
×
118

119
                private static bool tryGetFromCache(Type type, out DxfMap map)
120
                {
×
121
                        map = null;
×
122

123
                        if (_cache.TryGetValue(type, out var curr))
×
124
                        {
×
125
                                map = new DxfMap();
×
126
                                map.Name = curr.Name;
×
127

128
                                foreach (var p in curr.DxfProperties)
×
129
                                {
×
130
                                        map.DxfProperties.Add(p.Key, p.Value);
×
131
                                }
×
132

133
                                foreach (var sub in curr.SubClasses)
×
134
                                {
×
135
                                        map.SubClasses.Add(sub.Key, sub.Value);
×
136
                                }
×
137

138
                                return true;
×
139
                        }
140

141
                        return false;
×
142
                }
×
143
        }
144
}
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