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

DomCR / ACadSharp / 13736546802

08 Mar 2025 10:12AM UTC coverage: 76.202% (+0.1%) from 76.062%
13736546802

Pull #577

github

web-flow
Merge e422066c4 into 57db78f49
Pull Request #577: write reactors

5466 of 7881 branches covered (69.36%)

Branch coverage included in aggregate %.

34 of 42 new or added lines in 5 files covered. (80.95%)

2 existing lines in 1 file now uncovered.

21594 of 27630 relevant lines covered (78.15%)

75839.31 hits per line

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

94.04
/src/ACadSharp/CadObject.cs
1
using ACadSharp.Attributes;
2
using ACadSharp.Objects;
3
using ACadSharp.Objects.Collections;
4
using ACadSharp.Tables;
5
using ACadSharp.Tables.Collections;
6
using ACadSharp.XData;
7
using System.Collections.Generic;
8
using System.Linq;
9

10
namespace ACadSharp
11
{
12
        /// <summary>
13
        /// Represents an element in a CadDocument.
14
        /// </summary>
15
        public abstract class CadObject : IHandledCadObject
16
        {
17
                /// <summary>
18
                /// Document where this element belongs.
19
                /// </summary>
20
                public CadDocument Document { get; private set; }
1,108,377✔
21

22
                /// <summary>
23
                /// Extended data attached to this object.
24
                /// </summary>
25
                public ExtendedDataDictionary ExtendedData { get; private set; }
1,772,273✔
26

27
                /// <inheritdoc/>
28
                /// <remarks>
29
                /// If the value is 0 the object is not assigned to a document or a parent.
30
                /// </remarks>
31
                [DxfCodeValue(5)]
32
                public ulong Handle { get; internal set; }
3,894,280✔
33

34
                /// <summary>
35
                /// Flag that indicates if this object has a dynamic dxf sublcass.
36
                /// </summary>
37
                public virtual bool HasDynamicSubclass { get { return false; } }
156✔
38

39
                /// <summary>
40
                /// The CAD class name of an object.
41
                /// </summary>
42
                public virtual string ObjectName { get; }
×
43

44
                /// <summary>
45
                /// Get the object type.
46
                /// </summary>
47
                public abstract ObjectType ObjectType { get; }
48

49
                /// <summary>
50
                /// Soft-pointer ID/handle to owner object.
51
                /// </summary>
52
                [DxfCodeValue(DxfReferenceType.Handle, 330)]
53
                public IHandledCadObject Owner { get; internal set; }
741,546✔
54

55
                /// <summary>
56
                /// Objects that are attached to this object.
57
                /// </summary>
58
                public IEnumerable<CadObject> Reactors
59
                {
60
                        get
61
                        {
18,052✔
62
                                if (this.Document == null)
18,052!
NEW
63
                                {
×
NEW
64
                                        return Enumerable.Empty<CadObject>();
×
65
                                }
66
                                else
67
                                {
18,052✔
68
                                        return this._reactors;
18,052✔
69
                                }
70
                        }
18,052✔
71
                }
72

73
                /// <summary>
74
                /// Object Subclass marker.
75
                /// </summary>
76
                public abstract string SubclassMarker { get; }
77

78
                /// <summary>
79
                /// Extended Dictionary object.
80
                /// </summary>
81
                /// <remarks>
82
                /// An extended dictionary can be created using <see cref="CreateExtendedDictionary"/>.
83
                /// </remarks>
84
                public CadDictionary XDictionary
85
                {
86
                        get { return this._xdictionary; }
868,197✔
87
                        internal set
88
                        {
19,344✔
89
                                if (value == null)
19,344✔
90
                                        return;
4,522✔
91

92
                                this._xdictionary = value;
14,822✔
93
                                this._xdictionary.Owner = this;
14,822✔
94

95
                                if (this.Document != null)
14,822✔
96
                                        this.Document.RegisterCollection(this._xdictionary);
11,601✔
97
                        }
19,344✔
98
                }
99

100
                private List<CadObject> _reactors = new List<CadObject>();
1,436,226✔
101

102
                private CadDictionary _xdictionary = null;
1,436,226✔
103

104
                /// <summary>
105
                /// Default constructor.
106
                /// </summary>
107
                public CadObject()
1,436,226✔
108
                {
1,436,226✔
109
                        this.ExtendedData = new ExtendedDataDictionary(this);
1,436,226✔
110
                }
1,436,226✔
111

112
                /// <summary>
113
                /// Creates a new object that is a copy of the current instance.
114
                /// </summary>
115
                /// <remarks>
116
                /// The copy will be unattached from the document or any reference.
117
                /// </remarks>
118
                /// <returns>A new object that is a copy of this instance.</returns>
119
                public virtual CadObject Clone()
120
                {
13,233✔
121
                        CadObject clone = (CadObject)this.MemberwiseClone();
13,233✔
122

123
                        clone.Handle = 0;
13,233✔
124

125
                        clone.Document = null;
13,233✔
126
                        clone.Owner = null;
13,233✔
127

128
                        //Collections
129
                        clone._reactors = new List<CadObject>();
13,233✔
130
                        clone._xdictionary = null;
13,233✔
131
                        clone.ExtendedData = new ExtendedDataDictionary(clone);
13,233✔
132

133
                        return clone;
13,233✔
134
                }
13,233✔
135

136
                /// <summary>
137
                /// Creates the extended dictionary if null.
138
                /// </summary>
139
                /// <returns>The <see cref="CadDictionary"/> attached to this <see cref="CadObject"/></returns>
140
                public CadDictionary CreateExtendedDictionary()
141
                {
2✔
142
                        if (this._xdictionary == null)
2✔
143
                        {
2✔
144
                                this.XDictionary = new CadDictionary();
2✔
145
                        }
2✔
146

147
                        return this._xdictionary;
2✔
148
                }
2✔
149

150
                /// <inheritdoc/>
151
                public override string ToString()
152
                {
6,230✔
153
                        return $"{this.ObjectName}:{this.Handle}";
6,230✔
154
                }
6,230✔
155

156
                /// <summary>
157
                /// Removes any reactor object that doesn't belong to the same <see cref="CadDocument"/> as this <see cref="CadObject"/>.
158
                /// </summary>
159
                public void CleanReactors()
160
                {
15,404✔
161
                        var reactors = this._reactors.ToArray();
15,404✔
162
                        foreach (var reactor in reactors)
46,986✔
163
                        {
387✔
164
                                if (reactor.Document != this.Document)
387!
NEW
165
                                {
×
NEW
166
                                        this._reactors.Remove(reactor);
×
NEW
167
                                }
×
168
                        }
387✔
169
                }
15,404✔
170

171
                /// <summary>
172
                /// Add a reactor object linked to this one.
173
                /// </summary>
174
                /// <remarks>
175
                /// The <see cref="CadObject"/> and its reactors must be in the same <see cref="CadDocument"/> to be valid.
176
                /// </remarks>
177
                /// <param name="cadObject"></param>
178
                public void AddReactor(CadObject cadObject)
179
                {
38,026✔
180
                        this._reactors.Add(cadObject);
38,026✔
181
                }
38,026✔
182

183
                internal virtual void AssignDocument(CadDocument doc)
184
                {
245,767✔
185
                        this.Document = doc;
245,767✔
186

187
                        if (this.XDictionary != null)
245,767✔
188
                        {
521✔
189
                                doc.RegisterCollection(this.XDictionary);
521✔
190
                        }
521✔
191

192
                        if (this.ExtendedData.Any())
245,767✔
193
                        {
650✔
194
                                //Reset existing collection
195
                                var entries = this.ExtendedData.ToArray();
650✔
196
                                this.ExtendedData.Clear();
650✔
197

198
                                foreach (var item in entries)
3,782✔
199
                                {
916✔
200
                                        this.ExtendedData.Add(item.Key, item.Value);
916✔
201
                                }
916✔
202
                        }
650✔
203
                }
245,767✔
204

205
                internal virtual void UnassignDocument()
206
                {
3,013✔
207
                        if (this.XDictionary != null)
3,013✔
208
                        {
214✔
209
                                this.Document.UnregisterCollection(this.XDictionary);
214✔
210
                        }
214✔
211

212
                        this.Handle = 0;
3,013✔
213
                        this.Document = null;
3,013✔
214

215
                        if (this.ExtendedData.Any())
3,013✔
216
                        {
649✔
217
                                //Reset existing collection
218
                                var entries = this.ExtendedData.ToArray();
649✔
219
                                this.ExtendedData.Clear();
649✔
220

221
                                foreach (var item in entries)
3,777✔
222
                                {
915✔
223
                                        this.ExtendedData.Add(item.Key.Clone() as AppId, item.Value);
915✔
224
                                }
915✔
225
                        }
649✔
226

227
                        this._reactors.Clear();
3,013✔
228
                }
3,013✔
229

230
                protected T updateCollection<T>(T entry, ObjectDictionaryCollection<T> collection)
231
                        where T : NonGraphicalObject
232
                {
5,205✔
233
                        if (collection == null || entry == null)
5,205!
234
                        {
2,233✔
235
                                return entry;
2,233✔
236
                        }
237

238
                        if (collection.TryGetValue(entry.Name, out T existing))
2,972✔
239
                        {
2,765✔
240
                                return existing;
2,765✔
241
                        }
242
                        else
243
                        {
207✔
244
                                collection.Add(entry);
207✔
245
                                return entry;
207✔
246
                        }
247
                }
5,205✔
248

249
                protected T updateTable<T>(T entry, Table<T> table)
250
                                        where T : TableEntry
251
                {
484,246✔
252
                        if (table == null)
484,246✔
253
                        {
166✔
254
                                return entry;
166✔
255
                        }
256

257
                        if (table.TryGetValue(entry.Name, out T existing))
484,080✔
258
                        {
483,711✔
259
                                return existing;
483,711✔
260
                        }
261
                        else
262
                        {
369✔
263
                                table.Add(entry);
369✔
264
                                return entry;
369✔
265
                        }
266
                }
484,246✔
267
        }
268
}
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