From 5bb00d95f57e85358022cf619e45e8114dd516e6 Mon Sep 17 00:00:00 2001 From: DomCR Date: Fri, 17 Jan 2025 14:07:22 +0100 Subject: [PATCH 1/5] fix --- src/ACadSharp/CadObject.cs | 15 +++++++++++ src/ACadSharp/XData/ExtendedData.cs | 9 +++---- src/ACadSharp/XData/ExtendedDataCollection.cs | 25 +++++++++++++++++-- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/ACadSharp/CadObject.cs b/src/ACadSharp/CadObject.cs index b5a4335b..46bf6c47 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 != null) + { + //Reset existing collection + var entries = this.ExtendedData.Entries; + this.ExtendedData.Clear(); + + foreach (var item in entries) + { + this.ExtendedData.Add(item.Key, item.Value); + } + } } internal virtual void UnassignDocument() diff --git a/src/ACadSharp/XData/ExtendedData.cs b/src/ACadSharp/XData/ExtendedData.cs index 8c4ac785..ddf0e7f8 100644 --- a/src/ACadSharp/XData/ExtendedData.cs +++ b/src/ACadSharp/XData/ExtendedData.cs @@ -5,16 +5,13 @@ 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); } diff --git a/src/ACadSharp/XData/ExtendedDataCollection.cs b/src/ACadSharp/XData/ExtendedDataCollection.cs index a1b298dc..048fa3f1 100644 --- a/src/ACadSharp/XData/ExtendedDataCollection.cs +++ b/src/ACadSharp/XData/ExtendedDataCollection.cs @@ -20,8 +20,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 +50,7 @@ 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)); } /// From aaaec97b9cf2a82afefa27a0829a432a2d0a24fb Mon Sep 17 00:00:00 2001 From: DomCR Date: Mon, 20 Jan 2025 09:12:57 +0100 Subject: [PATCH 2/5] test setup --- .../XData/ExtendedDataTests.cs | 31 +++++++++++++++++++ src/ACadSharp/CadObject.cs | 2 +- src/ACadSharp/XData/ExtendedData.cs | 6 ++++ src/ACadSharp/XData/ExtendedDataCollection.cs | 28 ++++++++++++++++- 4 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 src/ACadSharp.Tests/XData/ExtendedDataTests.cs diff --git a/src/ACadSharp.Tests/XData/ExtendedDataTests.cs b/src/ACadSharp.Tests/XData/ExtendedDataTests.cs new file mode 100644 index 00000000..493749b4 --- /dev/null +++ b/src/ACadSharp.Tests/XData/ExtendedDataTests.cs @@ -0,0 +1,31 @@ +using ACadSharp.Entities; +using ACadSharp.Tables; +using ACadSharp.XData; +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(); + extendedData.Records.Add(new ExtendedDataString("extended data record")); + + + line.ExtendedData.Add(app, extendedData); + + doc.Entities.Add(line); + + Assert.Contains(app, doc.AppIds); + Assert.NotEmpty(line.ExtendedData); + Assert.NotEmpty(line.ExtendedData.Get(appName).Records); + } + } +} diff --git a/src/ACadSharp/CadObject.cs b/src/ACadSharp/CadObject.cs index 46bf6c47..be103646 100644 --- a/src/ACadSharp/CadObject.cs +++ b/src/ACadSharp/CadObject.cs @@ -151,7 +151,7 @@ internal virtual void AssignDocument(CadDocument doc) if (this.ExtendedData != null) { //Reset existing collection - var entries = this.ExtendedData.Entries; + var entries = this.ExtendedData.ToArray(); this.ExtendedData.Clear(); foreach (var item in entries) diff --git a/src/ACadSharp/XData/ExtendedData.cs b/src/ACadSharp/XData/ExtendedData.cs index ddf0e7f8..33ff5816 100644 --- a/src/ACadSharp/XData/ExtendedData.cs +++ b/src/ACadSharp/XData/ExtendedData.cs @@ -1,4 +1,5 @@ using ACadSharp.Tables; +using System; using System.Collections.Generic; namespace ACadSharp.XData @@ -15,5 +16,10 @@ 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 048fa3f1..4a5a750c 100644 --- a/src/ACadSharp/XData/ExtendedDataCollection.cs +++ b/src/ACadSharp/XData/ExtendedDataCollection.cs @@ -1,12 +1,14 @@ 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> { + [Obsolete] public IEnumerable> Entries { get { return this._data; } } public CadObject Owner { get; } @@ -53,6 +55,20 @@ public void Add(AppId app, IEnumerable 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]; + } + /// /// Get ExtendedData for a specific AppId from the Dictionary. /// @@ -106,5 +122,15 @@ public void Clear() { this._data.Clear(); } + + public IEnumerator> GetEnumerator() + { + return this._data.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return this._data.GetEnumerator(); + } } } From c7b5caa4ad25a9d6c527c42fe478f883ebe2d838 Mon Sep 17 00:00:00 2001 From: DomCR Date: Mon, 20 Jan 2025 10:49:37 +0100 Subject: [PATCH 3/5] test refined --- src/ACadSharp.Tests/XData/ExtendedDataTests.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ACadSharp.Tests/XData/ExtendedDataTests.cs b/src/ACadSharp.Tests/XData/ExtendedDataTests.cs index 493749b4..7483a8f1 100644 --- a/src/ACadSharp.Tests/XData/ExtendedDataTests.cs +++ b/src/ACadSharp.Tests/XData/ExtendedDataTests.cs @@ -1,6 +1,7 @@ using ACadSharp.Entities; using ACadSharp.Tables; using ACadSharp.XData; +using System.Linq; using Xunit; namespace ACadSharp.Tests.XData @@ -16,16 +17,19 @@ public void AddObjectWithXData() AppId app = new AppId(appName); Line line = new Line(); ExtendedData extendedData = new ExtendedData(); - extendedData.Records.Add(new ExtendedDataString("extended data record")); - + 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()); } } } From e122cb537fda56b16e7c684f0167677654d45692 Mon Sep 17 00:00:00 2001 From: DomCR Date: Mon, 20 Jan 2025 11:09:16 +0100 Subject: [PATCH 4/5] unassign extended data --- src/ACadSharp.Tests/IO/IOTests.cs | 6 ++++++ src/ACadSharp/CadObject.cs | 14 +++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) 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/CadObject.cs b/src/ACadSharp/CadObject.cs index be103646..8d7ec842 100644 --- a/src/ACadSharp/CadObject.cs +++ b/src/ACadSharp/CadObject.cs @@ -148,7 +148,7 @@ internal virtual void AssignDocument(CadDocument doc) doc.RegisterCollection(this.XDictionary); } - if (this.ExtendedData != null) + if (this.ExtendedData.Any()) { //Reset existing collection var entries = this.ExtendedData.ToArray(); @@ -168,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) From 47dc2e2491e32fe4b9122bde4af78db4b107913c Mon Sep 17 00:00:00 2001 From: DomCR Date: Mon, 20 Jan 2025 11:12:34 +0100 Subject: [PATCH 5/5] remove obsolete --- .../IO/DWG/DwgStreamWriters/DwgObjectWriter.Common.cs | 2 +- src/ACadSharp/IO/DXF/DxfStreamWriter/DxfSectionWriterBase.cs | 2 +- src/ACadSharp/XData/ExtendedDataCollection.cs | 3 --- 3 files changed, 2 insertions(+), 5 deletions(-) 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/ExtendedDataCollection.cs b/src/ACadSharp/XData/ExtendedDataCollection.cs index 4a5a750c..9b6d0a5f 100644 --- a/src/ACadSharp/XData/ExtendedDataCollection.cs +++ b/src/ACadSharp/XData/ExtendedDataCollection.cs @@ -8,9 +8,6 @@ namespace ACadSharp.XData { public class ExtendedDataDictionary : IEnumerable> { - [Obsolete] - public IEnumerable> Entries { get { return this._data; } } - public CadObject Owner { get; } private Dictionary _data = new Dictionary();