Skip to content

Commit

Permalink
Merge pull request #537 from DomCR/issue-534_XData-link-cadDocument
Browse files Browse the repository at this point in the history
Issue 534 XData link cad document
  • Loading branch information
DomCR authored Jan 20, 2025
2 parents 8b7e447 + 47dc2e2 commit 8de195e
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 13 deletions.
6 changes: 6 additions & 0 deletions src/ACadSharp.Tests/IO/IOTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using ACadSharp.Tests.TestModels;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -100,6 +101,11 @@ public void DwgEntitiesToDxfFile(FileModel test)
List<Entity> entities = new List<Entity>(doc.Entities);
foreach (var item in entities)
{
if (item.ExtendedData.Any())
{

}

Entity e = doc.Entities.Remove(item);
transfer.Entities.Add(e);
}
Expand Down
35 changes: 35 additions & 0 deletions src/ACadSharp.Tests/XData/ExtendedDataTests.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
27 changes: 27 additions & 0 deletions src/ACadSharp/CadObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using ACadSharp.XData;
using System.Collections.Generic;
using System.Linq;

namespace ACadSharp
{
Expand Down Expand Up @@ -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()
Expand All @@ -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>(T entry, Table<T> table)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
15 changes: 9 additions & 6 deletions src/ACadSharp/XData/ExtendedData.cs
Original file line number Diff line number Diff line change
@@ -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<ExtendedDataRecord> Records { get; } = new();

public List<ExtendedDataRecord> Records { get; } = new List<ExtendedDataRecord>();

public ExtendedData(AppId app)
public ExtendedData()
{
this.AppId = app;
}

public ExtendedData(AppId app, IEnumerable<ExtendedDataRecord> records) : this(app)
public ExtendedData(IEnumerable<ExtendedDataRecord> records) : this()
{
this.Records.AddRange(records);
}

public void AddControlStrings()
{
throw new NotImplementedException();
}
}
}
54 changes: 49 additions & 5 deletions src/ACadSharp/XData/ExtendedDataCollection.cs
Original file line number Diff line number Diff line change
@@ -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<KeyValuePair<AppId, ExtendedData>>
{
public IEnumerable<KeyValuePair<AppId, ExtendedData>> Entries { get { return this._data; } }

public CadObject Owner { get; }

private Dictionary<AppId, ExtendedData> _data = new Dictionary<AppId, ExtendedData>();
Expand All @@ -20,16 +19,51 @@ 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);
}
}

/// <summary>
/// Add ExtendedData for a specific AppId to the Dictionary.
/// </summary>
/// <param name="app">The AppId object.</param>
/// <param name="records">The ExtendedData records.</param>
public void Add(AppId app, IEnumerable<ExtendedDataRecord> records)
{
this._data.Add(app, new ExtendedData(app, records));
this._data.Add(app, new ExtendedData(records));
}

/// <summary>
/// Get the different extended data by it's name.
/// </summary>
/// <returns></returns>
public IDictionary<string, ExtendedData> GetExtendedDataByName()
{
return this._data.ToDictionary(x => x.Key.Name, x => x.Value);
}

public ExtendedData Get(string name)
{
return this.GetExtendedDataByName()[name];
}

/// <summary>
Expand Down Expand Up @@ -85,5 +119,15 @@ public void Clear()
{
this._data.Clear();
}

public IEnumerator<KeyValuePair<AppId, ExtendedData>> GetEnumerator()
{
return this._data.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return this._data.GetEnumerator();
}
}
}

0 comments on commit 8de195e

Please sign in to comment.