Skip to content

Commit

Permalink
Merge pull request #40 from DomCR/DwgWriter
Browse files Browse the repository at this point in the history
Dwg writer
  • Loading branch information
DomCR authored Dec 23, 2022
2 parents acea93a + 2edad31 commit afbaf68
Show file tree
Hide file tree
Showing 77 changed files with 7,427 additions and 455 deletions.
2 changes: 1 addition & 1 deletion ACadSharp.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace ACadSharp.Examples
{
class Program
{
const string file = "../../../../samples/sample_AC1021.dwg";
const string file = "../../../../samples/sample_AC1032.dwg";

static void Main(string[] args)
{
Expand Down
13 changes: 12 additions & 1 deletion ACadSharp.Tests/Common/EntityFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using ACadSharp.Entities;
using CSMath;
using System;

namespace ACadSharp.Tests.Common
Expand Down Expand Up @@ -53,6 +52,9 @@ public static T Create<T>(bool randomize = true)
case Line line:
RandomizeLine(line);
break;
case Point point:
RandomizePoint(point);
break;
case Polyline2D pl2d:
RandomizePolyline(pl2d);
break;
Expand Down Expand Up @@ -81,6 +83,15 @@ public static void RandomizeLine(Line line)
line.EndPoint = _random.NextXYZ();
}

public static void RandomizePoint(Point point)
{
RandomizeEntity(point);

point.Thickness = _random.NextDouble();
// line.Normal = _random.NextXYZ(); //Entity becomes invisible if has a different value
point.Location = _random.NextXYZ();
}

public static void RandomizePolyline(Polyline pline)
{
RandomizeEntity(pline);
Expand Down
7 changes: 1 addition & 6 deletions ACadSharp.Tests/IO/DWG/DwgReaderTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using ACadSharp.Header;
using ACadSharp.IO;
using ACadSharp.Tests.Common;
using System;
using System.Collections.Generic;
using System.IO;
using ACadSharp.IO;
using Xunit;
using Xunit.Abstractions;

Expand Down
192 changes: 192 additions & 0 deletions ACadSharp.Tests/IO/DWG/DwgWriterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
using ACadSharp.Entities;
using ACadSharp.Exceptions;
using ACadSharp.IO;
using ACadSharp.Tests.Common;
using System;
using System.IO;
using Xunit;
using Xunit.Abstractions;

namespace ACadSharp.Tests.IO.DWG
{
public class DwgWriterTests : IOTestsBase
{
public DwgWriterTests(ITestOutputHelper output) : base(output) { }

[Theory]
[MemberData(nameof(Versions))]
public void WriteEmptyTest(ACadVersion version)
{
CadDocument doc = new CadDocument();
doc.Header.Version = version;

string path = Path.Combine(_samplesOutFolder, $"out_empty_sample_{version}.dwg");

using (var wr = new DwgWriter(path, doc))
{
if (isSupportedVersion(version))
{
wr.Write();
}
else
{
Assert.Throws<DwgNotSupportedException>(() => wr.Write());
return;
}
}

using (var re = new DwgReader(path, this.onNotification))
{
CadDocument readed = re.Read();
}

//this.checkDwgDocumentInAutocad(Path.GetFullPath(path));
}

[Theory]
[MemberData(nameof(Versions))]
public void WriteTest(ACadVersion version)
{
CadDocument doc = new CadDocument();
doc.Header.Version = version;

addEntities(doc);

string path = Path.Combine(_samplesOutFolder, $"out_sample_{version}.dwg");

using (var wr = new DwgWriter(path, doc))
{
if (isSupportedVersion(version))
{
wr.Write();
}
else
{
Assert.Throws<DwgNotSupportedException>(() => wr.Write());
return;
}
}

using (var re = new DwgReader(path, this.onNotification))
{
CadDocument readed = re.Read();
}

//this.checkDwgDocumentInAutocad(Path.GetFullPath(path));
}

[Theory]
[MemberData(nameof(Versions))]
public void WriteSummaryTest(ACadVersion version)
{
if (version <= ACadVersion.AC1015)
return;

CadDocument doc = new CadDocument();
doc.Header.Version = version;
doc.SummaryInfo = new CadSummaryInfo
{
Title = "This is a random title",
Subject = "This is a subject",
Author = "ACadSharp",
Keywords = "My Keyworks",
Comments = "This is my comment"
};

MemoryStream stream = new MemoryStream();

using (var wr = new DwgWriter(stream, doc))
{
if (isSupportedVersion(version))
{
wr.Write();
}
else
{
Assert.Throws<DwgNotSupportedException>(() => wr.Write());
return;
}
}

stream = new MemoryStream(stream.ToArray());

using (var re = new DwgReader(stream, this.onNotification))
{
CadSummaryInfo info = re.ReadSummaryInfo();

Assert.Equal(doc.SummaryInfo.Title, info.Title);
Assert.Equal(doc.SummaryInfo.Subject, info.Subject);
Assert.Equal(doc.SummaryInfo.Author, info.Author);
Assert.Equal(doc.SummaryInfo.Keywords, info.Keywords);
Assert.Equal(doc.SummaryInfo.Comments, info.Comments);
}
}

[Theory]
[MemberData(nameof(Versions))]
public void WriteHeaderTest(ACadVersion version)
{
CadDocument doc = new CadDocument();
doc.Header.Version = version;

MemoryStream stream = new MemoryStream();

using (var wr = new DwgWriter(stream, doc))
{
if (isSupportedVersion(version))
{
wr.Write();
}
else
{
Assert.Throws<DwgNotSupportedException>(() => wr.Write());
return;
}
}

stream = new MemoryStream(stream.ToArray());

using (var re = new DwgReader(stream, this.onNotification))
{
Header.CadHeader header = re.ReadHeader();
}
}

private void addEntities(CadDocument doc)
{
doc.Entities.Add(EntityFactory.Create<Point>());
doc.Entities.Add(EntityFactory.Create<Line>());
}

private bool isSupportedVersion(ACadVersion version)
{
switch (version)
{
case ACadVersion.MC0_0:
case ACadVersion.AC1_2:
case ACadVersion.AC1_4:
case ACadVersion.AC1_50:
case ACadVersion.AC2_10:
case ACadVersion.AC1002:
case ACadVersion.AC1003:
case ACadVersion.AC1004:
case ACadVersion.AC1006:
case ACadVersion.AC1009:
case ACadVersion.AC1012:
return false;
case ACadVersion.AC1014:
case ACadVersion.AC1015:
case ACadVersion.AC1018:
return true;
case ACadVersion.AC1021:
case ACadVersion.AC1024:
case ACadVersion.AC1027:
case ACadVersion.AC1032:
return false;
case ACadVersion.Unknown:
default:
return false;
}
}
}
}
7 changes: 3 additions & 4 deletions ACadSharp.Tests/IO/DXF/DxfWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -40,7 +39,7 @@ public void WriteEmptyAsciiTest(ACadVersion version)
CadDocument readed = re.Read();
}

this.checkDocumentInAutocad(Path.GetFullPath(path));
this.checkDxfDocumentInAutocad(Path.GetFullPath(path));
}

[Theory]
Expand All @@ -67,7 +66,7 @@ public void WriteEmptyBinaryTest(ACadVersion version)
CadDocument readed = re.Read();
}

this.checkDocumentInAutocad(path);
this.checkDxfDocumentInAutocad(path);
}

[Theory]
Expand Down Expand Up @@ -111,7 +110,7 @@ public void WriteDocumentWithEntitiesTest(ACadVersion version)
wr.Write();
}

this.checkDocumentInAutocad(path);
this.checkDxfDocumentInAutocad(path);
}
}
}
2 changes: 1 addition & 1 deletion ACadSharp.Tests/IO/IOTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private void writeDxfFile(string file, CadDocument doc, bool check)
}

if (check)
this.checkDocumentInAutocad(Path.GetFullPath(file));
this.checkDxfDocumentInAutocad(Path.GetFullPath(file));
}
}
}
63 changes: 62 additions & 1 deletion ACadSharp.Tests/IO/IOTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ static IOTestsBase()
}

Versions = new TheoryData<ACadVersion>();
Versions.Add(ACadVersion.AC1012);
Versions.Add(ACadVersion.AC1014);
Versions.Add(ACadVersion.AC1015);
Versions.Add(ACadVersion.AC1018);
Versions.Add(ACadVersion.AC1021);
Versions.Add(ACadVersion.AC1024);
Versions.Add(ACadVersion.AC1027);
Expand Down Expand Up @@ -108,7 +112,7 @@ protected void onNotification(object sender, NotificationEventArgs e)
_output.WriteLine(e.Message);
}

protected void checkDocumentInAutocad(string path)
protected void checkDxfDocumentInAutocad(string path)
{
if (Environment.GetEnvironmentVariable("GITHUB_WORKFLOW") != null)
return;
Expand Down Expand Up @@ -168,6 +172,63 @@ protected void checkDocumentInAutocad(string path)
}
}

protected void checkDwgDocumentInAutocad(string path)
{
if (Environment.GetEnvironmentVariable("GITHUB_WORKFLOW") != null)
return;

System.Diagnostics.Process process = new System.Diagnostics.Process();

try
{
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo.FileName = "\"D:\\Programs\\Autodesk\\AutoCAD 2023\\accoreconsole.exe\"";
process.StartInfo.Arguments = $"/i \"{path}\" /l en - US";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.StandardOutputEncoding = Encoding.ASCII;

Assert.True(process.Start());

string l = process.StandardOutput.ReadLine();
bool testPassed = true;
while (!process.StandardOutput.EndOfStream)
{
string li = l.Replace("\0", "");
if (!string.IsNullOrEmpty(li))
{
if (li.Contains("Invalid or incomplete DXF input -- drawing discarded.")
|| li.Contains("error", StringComparison.OrdinalIgnoreCase))
{
testPassed = false;
}

_output.WriteLine(li);
}

var t = process.StandardOutput.ReadLineAsync();

//The last line gets into an infinite loop
if (t.Wait(1000))
{
l = t.Result;
}
else
{
break;
}
}

if (!testPassed)
throw new Exception("File loading with accoreconsole failed");
}
finally
{
process.Kill();
}
}

protected static void loadSamples(string folder, string ext, TheoryData<string> files)
{
string path = Path.Combine(_samplesFolder, "local", folder);
Expand Down
Loading

0 comments on commit afbaf68

Please sign in to comment.