Skip to content

Commit 05b1fdc

Browse files
authored
Merge pull request #38 from DomCR/DxfWriter_minor-entities
Dxf writer minor refactor
2 parents 6309d94 + a0cfaa3 commit 05b1fdc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+807
-290
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ MigrationBackup/
349349
# Ionide (cross platform F# VS Code tools) working folder
350350
.ionide/
351351

352+
/local
352353
/samples/local
353354
/local
354355
/samples/out
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using ACadSharp.IO;
2+
using System;
3+
4+
namespace ACadSharp.Examples.Common
5+
{
6+
public class NotificationHelper
7+
{
8+
public static void LogConsoleNotification(object sender, NotificationEventArgs e)
9+
{
10+
//Write in the console all the messages
11+
Console.WriteLine(e.Message);
12+
}
13+
}
14+
}

ACadSharp.Examples/IOExamples.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using ACadSharp.IO.DWG;
2+
using ACadSharp.IO.DXF;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
7+
namespace ACadSharp.Examples
8+
{
9+
/// <summary>
10+
/// Examples of conversion and saving documents
11+
/// </summary>
12+
public class IOExamples
13+
{
14+
/// <summary>
15+
/// Read a dwg and save the entities in a new file
16+
/// </summary>
17+
/// <param name="input"></param>
18+
/// <param name="output"></param>
19+
public void DwgEntitiesToNewFile(string input, string output)
20+
{
21+
/* --- ATENTION ---
22+
* ACadSharp cannot write a readed dwg/dxf file due a problem in the file structure when the dxf is writen
23+
* the workaround for now is to move the entities and save them in a new file
24+
*/
25+
CadDocument doc = DwgReader.Read(input);
26+
27+
//New document to transfer the entities
28+
CadDocument transfer = new CadDocument();
29+
doc.Header.Version = doc.Header.Version;
30+
31+
//Nove the entities to the created document
32+
List<Entities.Entity> entities = new List<Entities.Entity>(doc.Entities);
33+
foreach (var item in entities)
34+
{
35+
Entities.Entity e = doc.Entities.Remove(item);
36+
transfer.Entities.Add(e);
37+
}
38+
39+
//Save the document
40+
using (DxfWriter writer = new DxfWriter(output, doc, false))
41+
{
42+
writer.OnNotification += Common.NotificationHelper.LogConsoleNotification;
43+
writer.Write();
44+
}
45+
}
46+
}
47+
}

ACadSharp.Examples/ReaderExamples.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using ACadSharp.IO;
1+
using ACadSharp.Examples.Common;
2+
using ACadSharp.IO;
23
using ACadSharp.IO.DWG;
34
using ACadSharp.IO.DXF;
45
using System;
@@ -13,7 +14,7 @@ public static class ReaderExamples
1314
/// <param name="file">dxf file path</param>
1415
public static void ReadDxf(string file)
1516
{
16-
using (DxfReader reader = new DxfReader(file, onNotification))
17+
using (DxfReader reader = new DxfReader(file, NotificationHelper.LogConsoleNotification))
1718
{
1819
CadDocument doc = reader.Read();
1920
}
@@ -25,16 +26,10 @@ public static void ReadDxf(string file)
2526
/// <param name="file">dwg file path</param>
2627
public static void ReadDwg(string file)
2728
{
28-
using (DwgReader reader = new DwgReader(file, onNotification))
29+
using (DwgReader reader = new DwgReader(file, NotificationHelper.LogConsoleNotification))
2930
{
3031
CadDocument doc = reader.Read();
3132
}
3233
}
33-
34-
private static void onNotification(object sender, NotificationEventArgs e)
35-
{
36-
//Write in the console all the messages
37-
Console.WriteLine(e.Message);
38-
}
3934
}
4035
}

ACadSharp.Examples/WriterExamples.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using ACadSharp.Examples.Common;
2+
using ACadSharp.IO;
3+
using ACadSharp.IO.DXF;
4+
using System;
5+
6+
namespace ACadSharp.Examples
7+
{
8+
public static class WriterExamples
9+
{
10+
/// <summary>
11+
/// Write a ascii dxf file
12+
/// </summary>
13+
/// <param name="file"></param>
14+
/// <param name="doc"></param>
15+
public static void WriteAsciiDxf(string file, CadDocument doc)
16+
{
17+
using (DxfWriter writer = new DxfWriter(file, doc, false))
18+
{
19+
writer.OnNotification += NotificationHelper.LogConsoleNotification;
20+
writer.Write();
21+
}
22+
}
23+
24+
/// <summary>
25+
/// Write a binary dxf file
26+
/// </summary>
27+
/// <param name="file"></param>
28+
/// <param name="doc"></param>
29+
public static void WriteBinaryDxf(string file, CadDocument doc)
30+
{
31+
using (DxfWriter writer = new DxfWriter(file, doc, true))
32+
{
33+
writer.OnNotification += NotificationHelper.LogConsoleNotification;
34+
writer.Write();
35+
}
36+
}
37+
}
38+
}

ACadSharp.Tests/Common/DocumentIntegrity.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ public void AssertDocumentDefaults(CadDocument doc)
5959

6060
this.entryNotNull(doc.VPorts, "*Active");
6161

62-
this.notNull(doc.Layouts.FirstOrDefault(l => l.Name == "Model"), "Model");
62+
//Assert Model layout
63+
var layout = doc.Layouts.FirstOrDefault(l => l.Name == "Model");
64+
this.notNull(layout, "Layout Model is null");
65+
Assert.True(layout.AssociatedBlock == doc.ModelSpace);
6366
}
6467

6568
public void AssertBlockRecords(CadDocument doc)
@@ -199,19 +202,22 @@ private void assertCollection(IEnumerable<CadObject> collection, Node node)
199202
private void documentObjectNotNull<T>(CadDocument doc, T o)
200203
where T : CadObject
201204
{
202-
Assert.True(doc.GetCadObject(o.Handle) != null, $"Object of type {typeof(T)} | {o.Handle} not found in the doucment");
203-
205+
CadObject cobj = doc.GetCadObject(o.Handle);
206+
this.notNull(cobj, $"Object of type {typeof(T)} | {o.Handle} not found in the document");
207+
this.notNull(cobj.Document, $"Document is null for object with handle: {cobj.Handle}");
204208
}
205209

206-
private void notNull<T>(T o, string info)
210+
private void notNull<T>(T o, string info = null)
207211
{
208212
Assert.True(o != null, $"Object of type {typeof(T)} should not be null: {info}");
209213
}
210214

211215
private void entryNotNull<T>(Table<T> table, string entry)
212216
where T : TableEntry
213217
{
214-
Assert.True(table[entry] != null, $"Entry with name {entry} is null for thable {table}");
218+
var record = table[entry];
219+
Assert.True(record != null, $"Entry with name {entry} is null for thable {table}");
220+
Assert.NotNull(record.Document);
215221
}
216222
}
217223
}

ACadSharp.Tests/IO/DXF/DxfWriterTests.cs

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using ACadSharp.IO;
33
using ACadSharp.IO.DXF;
44
using System;
5+
using System.Collections.Generic;
56
using System.Diagnostics;
67
using System.IO;
78
using System.Text;
@@ -14,18 +15,24 @@ public class DxfWriterTests : IOTestsBase
1415
{
1516
public DxfWriterTests(ITestOutputHelper output) : base(output) { }
1617

17-
[Fact]
18-
public void WriteAsciiTest()
18+
[Theory]
19+
[MemberData(nameof(Versions))]
20+
public void WriteEmptyAsciiTest(ACadVersion version)
1921
{
2022
CadDocument doc = new CadDocument();
21-
string path = Path.Combine(_samplesOutFolder, "out_empty_sample_ascii.dxf");
23+
doc.Header.Version = version;
24+
25+
string path = Path.Combine(_samplesOutFolder, $"out_empty_sample_{version}_ascii.dxf");
2226

2327
using (var wr = new DxfWriter(path, doc, false))
2428
{
29+
wr.OnNotification += this.onNotification;
2530
wr.Write();
2631
}
2732

33+
this._output.WriteLine(string.Empty);
2834
this._output.WriteLine("Writer successful");
35+
this._output.WriteLine(string.Empty);
2936

3037
using (var re = new DxfReader(path, this.onNotification))
3138
{
@@ -35,18 +42,24 @@ public void WriteAsciiTest()
3542
this.checkDocumentInAutocad(Path.GetFullPath(path));
3643
}
3744

38-
[Fact]
39-
public void WriteBinaryTest()
45+
[Theory]
46+
[MemberData(nameof(Versions))]
47+
public void WriteEmptyBinaryTest(ACadVersion version)
4048
{
4149
CadDocument doc = new CadDocument();
42-
string path = Path.Combine(_samplesOutFolder, "out_empty_sample_binary.dxf");
50+
doc.Header.Version = version;
51+
52+
string path = Path.Combine(_samplesOutFolder, $"out_empty_sample_{version}_binary.dxf");
4353

4454
using (var wr = new DxfWriter(path, doc, true))
4555
{
56+
wr.OnNotification += this.onNotification;
4657
wr.Write();
4758
}
4859

60+
this._output.WriteLine(string.Empty);
4961
this._output.WriteLine("Writer successful");
62+
this._output.WriteLine(string.Empty);
5063

5164
using (var re = new DxfReader(path, this.onNotification))
5265
{
@@ -55,5 +68,44 @@ public void WriteBinaryTest()
5568

5669
this.checkDocumentInAutocad(path);
5770
}
71+
72+
[Theory]
73+
[MemberData(nameof(Versions))]
74+
public void WriteDocumentWithEntitiesTest(ACadVersion version)
75+
{
76+
CadDocument doc = new CadDocument();
77+
doc.Header.Version = version;
78+
79+
List<Entity> entities = new List<Entity>
80+
{
81+
new Point
82+
{
83+
Location = new CSMath.XYZ(0, 10, 0)
84+
},
85+
new Line
86+
{
87+
StartPoint = new CSMath.XYZ(0, 0, 0),
88+
EndPoint = new CSMath.XYZ(10, 10, 0)
89+
},
90+
new Arc
91+
{
92+
Center = new CSMath.XYZ(0, 5, 0),
93+
Radius = 20,
94+
StartAngle = 1,
95+
EndAngle = 2
96+
}
97+
};
98+
99+
100+
doc.Entities.AddRange(entities);
101+
102+
string path = Path.Combine(_samplesOutFolder, $"out_sample_{version}_ascii.dxf");
103+
104+
using (var wr = new DxfWriter(path, doc, false))
105+
{
106+
wr.OnNotification += this.onNotification;
107+
wr.Write();
108+
}
109+
}
58110
}
59111
}

ACadSharp.Tests/IO/IOTests.cs

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using ACadSharp.IO.DWG;
22
using ACadSharp.IO.DXF;
3-
using System;
43
using System.Collections.Generic;
54
using System.IO;
6-
using System.Text;
75
using Xunit;
86
using Xunit.Abstractions;
97

@@ -15,22 +13,70 @@ public IOTests(ITestOutputHelper output) : base(output)
1513
{
1614
}
1715

18-
[Theory(Skip = "Not implemented")]
16+
[Fact]
17+
public void EmptyDwgToDxf()
18+
{
19+
string inPath = Path.Combine($"{_samplesFolder}", "sample_base", "empty.dwg");
20+
CadDocument doc = DwgReader.Read(inPath);
21+
22+
string file = Path.GetFileNameWithoutExtension(inPath);
23+
string pathOut = Path.Combine(_samplesOutFolder, $"{file}_out.dxf");
24+
this.writeDxfFile(pathOut, doc, true);
25+
}
26+
27+
[Theory]
1928
[MemberData(nameof(DwgFilePaths))]
2029
public void DwgToDxf(string test)
2130
{
2231
CadDocument doc = DwgReader.Read(test);
2332

2433
string file = Path.GetFileNameWithoutExtension(test);
25-
2634
string pathOut = Path.Combine(_samplesOutFolder, $"{file}_out.dxf");
35+
this.writeDxfFile(pathOut, doc, true);
36+
}
37+
38+
[Theory]
39+
[MemberData(nameof(DxfAsciiFiles))]
40+
public void DxfToDxf(string test)
41+
{
42+
CadDocument doc = DxfReader.Read(test);
43+
44+
string file = Path.GetFileNameWithoutExtension(test);
45+
string pathOut = Path.Combine(_samplesOutFolder, $"{file}_rewrite_out.dxf");
46+
this.writeDxfFile(pathOut, doc, true);
47+
}
2748

28-
using (DxfWriter writer = new DxfWriter(pathOut, doc, false))
49+
[Theory]
50+
[MemberData(nameof(DwgFilePaths))]
51+
public void DwgEntitiesToNewFile(string test)
52+
{
53+
CadDocument doc = DwgReader.Read(test);
54+
55+
CadDocument transfer = new CadDocument();
56+
transfer.Header.Version = doc.Header.Version;
57+
58+
List<Entities.Entity> entities = new List<Entities.Entity>(doc.Entities);
59+
foreach (var item in entities)
60+
{
61+
Entities.Entity e = doc.Entities.Remove(item);
62+
transfer.Entities.Add(e);
63+
}
64+
65+
string file = Path.GetFileNameWithoutExtension(test);
66+
string pathOut = Path.Combine(_samplesOutFolder, $"{file}_moved_out.dxf");
67+
this.writeDxfFile(pathOut, transfer, true);
68+
}
69+
70+
private void writeDxfFile(string file, CadDocument doc, bool check)
71+
{
72+
using (DxfWriter writer = new DxfWriter(file, doc, false))
2973
{
74+
writer.OnNotification += this.onNotification;
3075
writer.Write();
3176
}
3277

33-
this.checkDocumentInAutocad(Path.GetFullPath(pathOut));
78+
if (check)
79+
this.checkDocumentInAutocad(Path.GetFullPath(file));
3480
}
3581
}
3682
}

0 commit comments

Comments
 (0)