Skip to content

Commit

Permalink
dynamic blocks for dxf
Browse files Browse the repository at this point in the history
  • Loading branch information
DomCR committed Nov 13, 2024
1 parent 908f9a0 commit 2ab475e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 21 deletions.
29 changes: 21 additions & 8 deletions src/ACadSharp.Tests/IO/DynamicBlockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,41 @@ namespace ACadSharp.Tests.IO
{
public class DynamicBlockTests : IOTestsBase
{
public static TheoryData<FileModel> DynamicBlocksPaths { get; } = new();
public static TheoryData<FileModel> DwgDynamicBlocksPaths { get; } = new();

static DynamicBlockTests()
{
loadSamples("dynamic-blocks", "dwg", DynamicBlocksPaths);
loadSamples("dynamic-blocks", "*", DwgDynamicBlocksPaths);
}

public DynamicBlockTests(ITestOutputHelper output) : base(output)
{
}

[Theory]
[MemberData(nameof(DynamicBlocksPaths))]
[MemberData(nameof(DwgDynamicBlocksPaths))]
public void DynamicBlocksTest(FileModel test)
{
//"my-dynamic-block" handle = 570
CadDocument doc;

if (test.Extension == ".dxf")
{
DxfReaderConfiguration configuration = new();
configuration.KeepUnknownEntities = true;
configuration.KeepUnknownNonGraphicalObjects = true;

DwgReaderConfiguration configuration = new DwgReaderConfiguration();
configuration.KeepUnknownEntities = true;
configuration.KeepUnknownNonGraphicalObjects = true;
doc = DxfReader.Read(test.Path, configuration, this.onNotification);
}
else
{
DwgReaderConfiguration configuration = new DwgReaderConfiguration();
configuration.KeepUnknownEntities = true;
configuration.KeepUnknownNonGraphicalObjects = true;

CadDocument doc = DwgReader.Read(test.Path, configuration, this.onNotification);
doc = DwgReader.Read(test.Path, configuration, this.onNotification);
}

//"my-dynamic-block" handle = 570

BlockRecord blk = doc.BlockRecords["my-dynamic-block"];

Expand Down
10 changes: 7 additions & 3 deletions src/ACadSharp.Tests/TestModels/FileModel.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using Xunit.Abstractions;
using ACadSharp.IO;
using System.IO;
using Xunit.Abstractions;

namespace ACadSharp.Tests.TestModels
{
public class FileModel : IXunitSerializable
{
public string FileName { get; set; }
public string Extension { get { return System.IO.Path.GetExtension(Path); } }

public string Path { get; set; }
public string FileName { get; private set; }

public string Path { get; private set; }

public FileModel()
{
Expand Down
15 changes: 14 additions & 1 deletion src/ACadSharp/IO/DXF/DxfReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ public static bool IsBinary(Stream stream, bool resetPos = false)
return isBinary;
}

public static CadDocument Read(string filename, DxfReaderConfiguration configuration, NotificationEventHandler notification = null)
{
CadDocument doc = null;

using (DxfReader reader = new DxfReader(filename, notification))
{
reader.Configuration = configuration;
doc = reader.Read();
}

return doc;
}

/// <summary>
/// Read a dxf document in a stream
/// </summary>
Expand Down Expand Up @@ -164,7 +177,7 @@ public override CadDocument Read()
this._reader.ReadNext();
}

if(this._document.Header == null)
if (this._document.Header == null)
{
this._document.Header = new CadHeader(this._document);
}
Expand Down
12 changes: 12 additions & 0 deletions src/ACadSharp/IO/Templates/CadTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ public virtual void Build(CadDocumentBuilder builder)
builder.Notify($"AppId in extended data with handle {item.Key} not found", NotificationType.Warning);
}
}

foreach (KeyValuePair<string, ExtendedData> item in this.EDataTemplateByAppName)
{
if (builder.TryGetTableEntry(item.Key, out AppId app))
{
this.CadObject.ExtendedData.Add(app, item.Value);
}
else
{
builder.Notify($"AppId in extended data with handle {item.Key} not found", NotificationType.Warning);
}
}
}

protected IEnumerable<T> getEntitiesCollection<T>(CadDocumentBuilder builder, ulong firstHandle, ulong endHandle)
Expand Down
14 changes: 5 additions & 9 deletions src/ACadSharp/Objects/Evaluations/EvaluationExpression.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using ACadSharp.Attributes;
using CSMath;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ACadSharp.Objects.Evaluations
{
Expand Down Expand Up @@ -32,13 +28,13 @@ public abstract class EvaluationExpression : CadObject
public override string SubclassMarker => DxfSubclassMarker.EvalGraphExpr;

[DxfCodeValue(90)]
public int Value90 { get; set; }
internal int Value90 { get; set; }

[DxfCodeValue(98)]
public int Value98 { get; set; }
internal int Value98 { get; set; }

[DxfCodeValue(99)]
public int Value99 { get; set; }
internal int Value99 { get; set; }
}

public abstract class BlockElement : EvaluationExpression
Expand All @@ -61,10 +57,10 @@ public abstract class BlockParameter : BlockElement
public override string SubclassMarker => DxfSubclassMarker.BlockParameter;

[DxfCodeValue(280)]
public bool Value280 { get; set; }
internal bool Value280 { get; set; }

[DxfCodeValue(281)]
public bool Value281 { get; set; }
internal bool Value281 { get; set; }
}

public abstract class Block2PtParameter : BlockParameter
Expand Down

0 comments on commit 2ab475e

Please sign in to comment.