diff --git a/src/ACadSharp.Tests/IO/IOTests.cs b/src/ACadSharp.Tests/IO/IOTests.cs index 515c44ad..00aec809 100644 --- a/src/ACadSharp.Tests/IO/IOTests.cs +++ b/src/ACadSharp.Tests/IO/IOTests.cs @@ -3,6 +3,7 @@ using ACadSharp.Tests.TestModels; using System.Collections.Generic; using System.IO; +using System.Linq; using Xunit; using Xunit.Abstractions; @@ -100,6 +101,11 @@ public void DwgEntitiesToDxfFile(FileModel test) List entities = new List(doc.Entities); foreach (var item in entities) { + if (item.ExtendedData.Any()) + { + + } + Entity e = doc.Entities.Remove(item); transfer.Entities.Add(e); } diff --git a/src/ACadSharp.Tests/XData/ExtendedDataTests.cs b/src/ACadSharp.Tests/XData/ExtendedDataTests.cs new file mode 100644 index 00000000..7483a8f1 --- /dev/null +++ b/src/ACadSharp.Tests/XData/ExtendedDataTests.cs @@ -0,0 +1,35 @@ +using ACadSharp.Entities; +using ACadSharp.Tables; +using ACadSharp.XData; +using System.Linq; +using Xunit; + +namespace ACadSharp.Tests.XData +{ + public class ExtendedDataTests + { + [Fact] + public void AddObjectWithXData() + { + CadDocument doc = new CadDocument(); + + string appName = "my_custom_app"; + AppId app = new AppId(appName); + Line line = new Line(); + ExtendedData extendedData = new ExtendedData(); + ExtendedDataRecord extendedDataRecord = new ExtendedDataString("extended data record"); + extendedData.Records.Add(extendedDataRecord); + + line.ExtendedData.Add(app, extendedData); + + doc.Entities.Add(line); + + Assert.Contains(app, doc.AppIds); + Assert.False(app.Handle == 0); + + Assert.NotEmpty(line.ExtendedData); + Assert.NotEmpty(line.ExtendedData.Get(appName).Records); + Assert.Equal(extendedDataRecord, line.ExtendedData.Get(appName).Records.First()); + } + } +} diff --git a/src/ACadSharp/CadObject.cs b/src/ACadSharp/CadObject.cs index b5a4335b..8d7ec842 100644 --- a/src/ACadSharp/CadObject.cs +++ b/src/ACadSharp/CadObject.cs @@ -7,6 +7,7 @@ using System; using ACadSharp.XData; using System.Collections.Generic; +using System.Linq; namespace ACadSharp { @@ -143,7 +144,21 @@ internal virtual void AssignDocument(CadDocument doc) this.Document = doc; if (this.XDictionary != null) + { doc.RegisterCollection(this.XDictionary); + } + + if (this.ExtendedData.Any()) + { + //Reset existing collection + var entries = this.ExtendedData.ToArray(); + this.ExtendedData.Clear(); + + foreach (var item in entries) + { + this.ExtendedData.Add(item.Key, item.Value); + } + } } internal virtual void UnassignDocument() @@ -153,6 +168,18 @@ internal virtual void UnassignDocument() this.Handle = 0; this.Document = null; + + if (this.ExtendedData.Any()) + { + //Reset existing collection + var entries = this.ExtendedData.ToArray(); + this.ExtendedData.Clear(); + + foreach (var item in entries) + { + this.ExtendedData.Add(item.Key.Clone() as AppId, item.Value); + } + } } protected T updateTable(T entry, Table table) diff --git a/src/ACadSharp/IO/DWG/DwgStreamWriters/DwgObjectWriter.Common.cs b/src/ACadSharp/IO/DWG/DwgStreamWriters/DwgObjectWriter.Common.cs index 0b9d83c3..690ef0fc 100644 --- a/src/ACadSharp/IO/DWG/DwgStreamWriters/DwgObjectWriter.Common.cs +++ b/src/ACadSharp/IO/DWG/DwgStreamWriters/DwgObjectWriter.Common.cs @@ -334,7 +334,7 @@ private void writeExtendedData(ExtendedDataDictionary data) if (this.WriteXData) { //EED size BS size of extended entity data, if any - foreach (var item in data.Entries) + foreach (var item in data) { writeExtendedDataEntry(item.Key, item.Value); } diff --git a/src/ACadSharp/IO/DXF/DxfStreamWriter/DxfSectionWriterBase.cs b/src/ACadSharp/IO/DXF/DxfStreamWriter/DxfSectionWriterBase.cs index bd877292..aa76c6b0 100644 --- a/src/ACadSharp/IO/DXF/DxfStreamWriter/DxfSectionWriterBase.cs +++ b/src/ACadSharp/IO/DXF/DxfStreamWriter/DxfSectionWriterBase.cs @@ -95,7 +95,7 @@ protected void writeExtendedData(ExtendedDataDictionary xdata) return; } - foreach (var entry in xdata.Entries) + foreach (var entry in xdata) { this._writer.Write(DxfCode.ExtendedDataRegAppName, entry.Key.Name); diff --git a/src/ACadSharp/XData/ExtendedData.cs b/src/ACadSharp/XData/ExtendedData.cs index 8c4ac785..33ff5816 100644 --- a/src/ACadSharp/XData/ExtendedData.cs +++ b/src/ACadSharp/XData/ExtendedData.cs @@ -1,22 +1,25 @@ using ACadSharp.Tables; +using System; using System.Collections.Generic; namespace ACadSharp.XData { public class ExtendedData { - public AppId AppId { get; } + public List Records { get; } = new(); - public List Records { get; } = new List(); - - public ExtendedData(AppId app) + public ExtendedData() { - this.AppId = app; } - public ExtendedData(AppId app, IEnumerable records) : this(app) + public ExtendedData(IEnumerable records) : this() { this.Records.AddRange(records); } + + public void AddControlStrings() + { + throw new NotImplementedException(); + } } } diff --git a/src/ACadSharp/XData/ExtendedDataCollection.cs b/src/ACadSharp/XData/ExtendedDataCollection.cs index a1b298dc..9b6d0a5f 100644 --- a/src/ACadSharp/XData/ExtendedDataCollection.cs +++ b/src/ACadSharp/XData/ExtendedDataCollection.cs @@ -1,14 +1,13 @@ using ACadSharp.Tables; using System; +using System.Collections; using System.Collections.Generic; using System.Linq; namespace ACadSharp.XData { - public class ExtendedDataDictionary + public class ExtendedDataDictionary : IEnumerable> { - public IEnumerable> Entries { get { return this._data; } } - public CadObject Owner { get; } private Dictionary _data = new Dictionary(); @@ -20,8 +19,29 @@ public ExtendedDataDictionary(CadObject owner) public void Add(AppId app) { - this._data.Add(app, new ExtendedData(app)); + this.Add(app, new ExtendedData()); + } + + public void Add(AppId app, ExtendedData extendedData) + { + if (this.Owner.Document != null) + { + if (this.Owner.Document.AppIds.TryGetValue(app.Name, out AppId existing)) + { + this._data.Add(existing, extendedData); + } + else + { + this.Owner.Document.AppIds.Add(app); + this._data.Add(app, extendedData); + } + } + else + { + this._data.Add(app, extendedData); + } } + /// /// Add ExtendedData for a specific AppId to the Dictionary. /// @@ -29,7 +49,21 @@ public void Add(AppId app) /// The ExtendedData records. public void Add(AppId app, IEnumerable records) { - this._data.Add(app, new ExtendedData(app, records)); + this._data.Add(app, new ExtendedData(records)); + } + + /// + /// Get the different extended data by it's name. + /// + /// + public IDictionary GetExtendedDataByName() + { + return this._data.ToDictionary(x => x.Key.Name, x => x.Value); + } + + public ExtendedData Get(string name) + { + return this.GetExtendedDataByName()[name]; } /// @@ -85,5 +119,15 @@ public void Clear() { this._data.Clear(); } + + public IEnumerator> GetEnumerator() + { + return this._data.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return this._data.GetEnumerator(); + } } }