Skip to content

Commit

Permalink
Merge pull request #30 from DomCR/ExtendedData
Browse files Browse the repository at this point in the history
Extended data
  • Loading branch information
DomCR authored Jun 2, 2022
2 parents 93be483 + 2069ad9 commit d8185e8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 14 deletions.
31 changes: 26 additions & 5 deletions ACadSharp/ExtendedDataCollection.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
using System;
using ACadSharp.Tables;
using System;
using System.Collections.Generic;
using System.Text;

namespace ACadSharp
{
public class ExtendedDataDictionary //: IDictionary<string, ExtendedData>
public class ExtendedDataDictionary
{
private Dictionary<string, ExtendedData> _data = new Dictionary<string, ExtendedData>(); //AppId, Data
private Dictionary<AppId, ExtendedData> _data = new Dictionary<AppId, ExtendedData>();

public void Add(AppId app, ExtendedData edata)
{
this._data.Add(app, edata);
}
}

public class ExtendedData
Expand All @@ -16,13 +22,28 @@ public class ExtendedData

public class ExtendedDataRecord
{
public DxfCode Code { get; set; }
public DxfCode Code
{
get { return this._code; }
set
{
if (value < DxfCode.ExtendedDataAsciiString)
{
throw new ArgumentException($"Dxf code for ExtendedDataRecord is not valid: {value}", nameof(value));
}

this._code = value;
}
}

public object Value { get; set; }

private DxfCode _code;

public ExtendedDataRecord(DxfCode dxfCode, object value)
{
//TODO: finish the dxf code throw exception for wrong values
this.Code = dxfCode;
this.Value = value;
}
}
}
31 changes: 26 additions & 5 deletions ACadSharp/IO/DXF/DxfStreamReader/DxfSectionReaderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,14 @@ protected void readMapped<T>(CadObject cadObject, CadTemplate template)
&& this._reader.LastDxfCode != DxfCode.Subclass)
{
//Check for an extended data code
if (this._reader.LastDxfCode >= DxfCode.ExtendedDataAsciiString)
if (this._reader.LastDxfCode == DxfCode.ExtendedDataRegAppName)
{
this.readExtendedData(cadObject);
this.readExtendedData(template.EDataTemplateByAppName);
continue;
}
else if (this._reader.LastDxfCode >= DxfCode.ExtendedDataAsciiString)
{
this._notification?.Invoke(null, new NotificationEventArgs($"Extended data should start witth : {DxfCode.ExtendedDataRegAppName}"));
this._reader.ReadNext();
continue;
}
Expand Down Expand Up @@ -413,9 +418,25 @@ protected void readMapped<T>(CadObject cadObject, CadTemplate template)
}
}

private void readExtendedData(CadObject cadObject)
protected void readExtendedData(Dictionary<string, ExtendedData> edata)
{
//TODO: Handle extended data
ExtendedData extendedData = new ExtendedData();
edata.Add(this._reader.LastValueAsString, extendedData);

this._reader.ReadNext();

while (this._reader.LastDxfCode >= DxfCode.ExtendedDataAsciiString)
{
if (this._reader.LastDxfCode == DxfCode.ExtendedDataRegAppName)
{
this.readExtendedData(edata);
break;
}

extendedData.Data.Add(new ExtendedDataRecord(this._reader.LastDxfCode, this._reader.LastValue));

this._reader.ReadNext();
}
}

protected void readHatch(Hatch hatch, CadHatchTemplate template)
Expand Down Expand Up @@ -520,7 +541,7 @@ protected void readHatch(Hatch hatch, CadHatchTemplate template)
}
else if (this._reader.LastDxfCode >= DxfCode.ExtendedDataAsciiString)
{
this.readExtendedData(hatch);
this.readExtendedData(template.EDataTemplateByAppName);
this._reader.ReadNext();
continue;
}
Expand Down
8 changes: 6 additions & 2 deletions ACadSharp/IO/DXF/DxfStreamReader/DxfTablesSectionReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ private void readTable()
this._reader.ReadNext();

int nentries = 0;
CadTemplate template = null;
Dictionary<string, ExtendedData> edata = new Dictionary<string, ExtendedData>();

this.readCommonObjectData(out string name, out ulong handle, out ulong? ownerHandle, out ulong? xdictHandle, out List<ulong> reactors);

Expand All @@ -68,6 +70,9 @@ private void readTable()
this._reader.ReadNext();
}
break;
case 1001:
this.readExtendedData(edata);
break;
default:
this._notification?.Invoke(null, new NotificationEventArgs($"Unhandeled dxf code {this._reader.LastCode} at line {this._reader.Position}."));
break;
Expand All @@ -79,8 +84,6 @@ private void readTable()
this._reader.ReadNext();
}

CadTemplate template = null;

switch (name)
{
case DxfFileToken.TableAppId:
Expand Down Expand Up @@ -146,6 +149,7 @@ private void readTable()
template.OwnerHandle = ownerHandle;
template.XDictHandle = xdictHandle;
template.ReactorsHandles = reactors;
template.EDataTemplateByAppName = edata;

//Add the object and the template to the builder
this._builder.AddTableTemplate((ICadTableTemplate)template);
Expand Down
6 changes: 4 additions & 2 deletions ACadSharp/IO/Templates/CadTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ internal abstract class CadTemplate : ICadObjectTemplate

public List<ulong> ReactorsHandles { get; set; } = new List<ulong>();

public Dictionary<ulong, ExtendedData> EDataTemplate { get; } = new Dictionary<ulong, ExtendedData>();
public Dictionary<ulong, ExtendedData> EDataTemplate { get; set; } = new Dictionary<ulong, ExtendedData>();

public Dictionary<string, ExtendedData> EDataTemplateByAppName { get; set; } = new Dictionary<string, ExtendedData>();

public CadTemplate(CadObject cadObject)
{
Expand Down Expand Up @@ -60,7 +62,7 @@ public virtual void Build(CadDocumentBuilder builder)
{
if (builder.TryGetCadObject(item.Key, out AppId app))
{

this.CadObject.ExtendedData.Add(app, item.Value);
}
}
}
Expand Down

0 comments on commit d8185e8

Please sign in to comment.