Skip to content

Commit afbaf68

Browse files
authored
Merge pull request #40 from DomCR/DwgWriter
Dwg writer
2 parents acea93a + 2edad31 commit afbaf68

File tree

77 files changed

+7427
-455
lines changed

Some content is hidden

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

77 files changed

+7427
-455
lines changed

ACadSharp.Examples/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace ACadSharp.Examples
1010
{
1111
class Program
1212
{
13-
const string file = "../../../../samples/sample_AC1021.dwg";
13+
const string file = "../../../../samples/sample_AC1032.dwg";
1414

1515
static void Main(string[] args)
1616
{

ACadSharp.Tests/Common/EntityFactory.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using ACadSharp.Entities;
2-
using CSMath;
32
using System;
43

54
namespace ACadSharp.Tests.Common
@@ -53,6 +52,9 @@ public static T Create<T>(bool randomize = true)
5352
case Line line:
5453
RandomizeLine(line);
5554
break;
55+
case Point point:
56+
RandomizePoint(point);
57+
break;
5658
case Polyline2D pl2d:
5759
RandomizePolyline(pl2d);
5860
break;
@@ -81,6 +83,15 @@ public static void RandomizeLine(Line line)
8183
line.EndPoint = _random.NextXYZ();
8284
}
8385

86+
public static void RandomizePoint(Point point)
87+
{
88+
RandomizeEntity(point);
89+
90+
point.Thickness = _random.NextDouble();
91+
// line.Normal = _random.NextXYZ(); //Entity becomes invisible if has a different value
92+
point.Location = _random.NextXYZ();
93+
}
94+
8495
public static void RandomizePolyline(Polyline pline)
8596
{
8697
RandomizeEntity(pline);

ACadSharp.Tests/IO/DWG/DwgReaderTests.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using ACadSharp.Header;
2-
using ACadSharp.IO;
3-
using ACadSharp.Tests.Common;
4-
using System;
5-
using System.Collections.Generic;
6-
using System.IO;
1+
using ACadSharp.IO;
72
using Xunit;
83
using Xunit.Abstractions;
94

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
using ACadSharp.Entities;
2+
using ACadSharp.Exceptions;
3+
using ACadSharp.IO;
4+
using ACadSharp.Tests.Common;
5+
using System;
6+
using System.IO;
7+
using Xunit;
8+
using Xunit.Abstractions;
9+
10+
namespace ACadSharp.Tests.IO.DWG
11+
{
12+
public class DwgWriterTests : IOTestsBase
13+
{
14+
public DwgWriterTests(ITestOutputHelper output) : base(output) { }
15+
16+
[Theory]
17+
[MemberData(nameof(Versions))]
18+
public void WriteEmptyTest(ACadVersion version)
19+
{
20+
CadDocument doc = new CadDocument();
21+
doc.Header.Version = version;
22+
23+
string path = Path.Combine(_samplesOutFolder, $"out_empty_sample_{version}.dwg");
24+
25+
using (var wr = new DwgWriter(path, doc))
26+
{
27+
if (isSupportedVersion(version))
28+
{
29+
wr.Write();
30+
}
31+
else
32+
{
33+
Assert.Throws<DwgNotSupportedException>(() => wr.Write());
34+
return;
35+
}
36+
}
37+
38+
using (var re = new DwgReader(path, this.onNotification))
39+
{
40+
CadDocument readed = re.Read();
41+
}
42+
43+
//this.checkDwgDocumentInAutocad(Path.GetFullPath(path));
44+
}
45+
46+
[Theory]
47+
[MemberData(nameof(Versions))]
48+
public void WriteTest(ACadVersion version)
49+
{
50+
CadDocument doc = new CadDocument();
51+
doc.Header.Version = version;
52+
53+
addEntities(doc);
54+
55+
string path = Path.Combine(_samplesOutFolder, $"out_sample_{version}.dwg");
56+
57+
using (var wr = new DwgWriter(path, doc))
58+
{
59+
if (isSupportedVersion(version))
60+
{
61+
wr.Write();
62+
}
63+
else
64+
{
65+
Assert.Throws<DwgNotSupportedException>(() => wr.Write());
66+
return;
67+
}
68+
}
69+
70+
using (var re = new DwgReader(path, this.onNotification))
71+
{
72+
CadDocument readed = re.Read();
73+
}
74+
75+
//this.checkDwgDocumentInAutocad(Path.GetFullPath(path));
76+
}
77+
78+
[Theory]
79+
[MemberData(nameof(Versions))]
80+
public void WriteSummaryTest(ACadVersion version)
81+
{
82+
if (version <= ACadVersion.AC1015)
83+
return;
84+
85+
CadDocument doc = new CadDocument();
86+
doc.Header.Version = version;
87+
doc.SummaryInfo = new CadSummaryInfo
88+
{
89+
Title = "This is a random title",
90+
Subject = "This is a subject",
91+
Author = "ACadSharp",
92+
Keywords = "My Keyworks",
93+
Comments = "This is my comment"
94+
};
95+
96+
MemoryStream stream = new MemoryStream();
97+
98+
using (var wr = new DwgWriter(stream, doc))
99+
{
100+
if (isSupportedVersion(version))
101+
{
102+
wr.Write();
103+
}
104+
else
105+
{
106+
Assert.Throws<DwgNotSupportedException>(() => wr.Write());
107+
return;
108+
}
109+
}
110+
111+
stream = new MemoryStream(stream.ToArray());
112+
113+
using (var re = new DwgReader(stream, this.onNotification))
114+
{
115+
CadSummaryInfo info = re.ReadSummaryInfo();
116+
117+
Assert.Equal(doc.SummaryInfo.Title, info.Title);
118+
Assert.Equal(doc.SummaryInfo.Subject, info.Subject);
119+
Assert.Equal(doc.SummaryInfo.Author, info.Author);
120+
Assert.Equal(doc.SummaryInfo.Keywords, info.Keywords);
121+
Assert.Equal(doc.SummaryInfo.Comments, info.Comments);
122+
}
123+
}
124+
125+
[Theory]
126+
[MemberData(nameof(Versions))]
127+
public void WriteHeaderTest(ACadVersion version)
128+
{
129+
CadDocument doc = new CadDocument();
130+
doc.Header.Version = version;
131+
132+
MemoryStream stream = new MemoryStream();
133+
134+
using (var wr = new DwgWriter(stream, doc))
135+
{
136+
if (isSupportedVersion(version))
137+
{
138+
wr.Write();
139+
}
140+
else
141+
{
142+
Assert.Throws<DwgNotSupportedException>(() => wr.Write());
143+
return;
144+
}
145+
}
146+
147+
stream = new MemoryStream(stream.ToArray());
148+
149+
using (var re = new DwgReader(stream, this.onNotification))
150+
{
151+
Header.CadHeader header = re.ReadHeader();
152+
}
153+
}
154+
155+
private void addEntities(CadDocument doc)
156+
{
157+
doc.Entities.Add(EntityFactory.Create<Point>());
158+
doc.Entities.Add(EntityFactory.Create<Line>());
159+
}
160+
161+
private bool isSupportedVersion(ACadVersion version)
162+
{
163+
switch (version)
164+
{
165+
case ACadVersion.MC0_0:
166+
case ACadVersion.AC1_2:
167+
case ACadVersion.AC1_4:
168+
case ACadVersion.AC1_50:
169+
case ACadVersion.AC2_10:
170+
case ACadVersion.AC1002:
171+
case ACadVersion.AC1003:
172+
case ACadVersion.AC1004:
173+
case ACadVersion.AC1006:
174+
case ACadVersion.AC1009:
175+
case ACadVersion.AC1012:
176+
return false;
177+
case ACadVersion.AC1014:
178+
case ACadVersion.AC1015:
179+
case ACadVersion.AC1018:
180+
return true;
181+
case ACadVersion.AC1021:
182+
case ACadVersion.AC1024:
183+
case ACadVersion.AC1027:
184+
case ACadVersion.AC1032:
185+
return false;
186+
case ACadVersion.Unknown:
187+
default:
188+
return false;
189+
}
190+
}
191+
}
192+
}

ACadSharp.Tests/IO/DXF/DxfWriterTests.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Collections.Generic;
77
using System.Diagnostics;
88
using System.IO;
9-
using System.Text;
109
using Xunit;
1110
using Xunit.Abstractions;
1211

@@ -40,7 +39,7 @@ public void WriteEmptyAsciiTest(ACadVersion version)
4039
CadDocument readed = re.Read();
4140
}
4241

43-
this.checkDocumentInAutocad(Path.GetFullPath(path));
42+
this.checkDxfDocumentInAutocad(Path.GetFullPath(path));
4443
}
4544

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

70-
this.checkDocumentInAutocad(path);
69+
this.checkDxfDocumentInAutocad(path);
7170
}
7271

7372
[Theory]
@@ -111,7 +110,7 @@ public void WriteDocumentWithEntitiesTest(ACadVersion version)
111110
wr.Write();
112111
}
113112

114-
this.checkDocumentInAutocad(path);
113+
this.checkDxfDocumentInAutocad(path);
115114
}
116115
}
117116
}

ACadSharp.Tests/IO/IOTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private void writeDxfFile(string file, CadDocument doc, bool check)
7676
}
7777

7878
if (check)
79-
this.checkDocumentInAutocad(Path.GetFullPath(file));
79+
this.checkDxfDocumentInAutocad(Path.GetFullPath(file));
8080
}
8181
}
8282
}

ACadSharp.Tests/IO/IOTestsBase.cs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ static IOTestsBase()
5050
}
5151

5252
Versions = new TheoryData<ACadVersion>();
53+
Versions.Add(ACadVersion.AC1012);
54+
Versions.Add(ACadVersion.AC1014);
55+
Versions.Add(ACadVersion.AC1015);
56+
Versions.Add(ACadVersion.AC1018);
5357
Versions.Add(ACadVersion.AC1021);
5458
Versions.Add(ACadVersion.AC1024);
5559
Versions.Add(ACadVersion.AC1027);
@@ -108,7 +112,7 @@ protected void onNotification(object sender, NotificationEventArgs e)
108112
_output.WriteLine(e.Message);
109113
}
110114

111-
protected void checkDocumentInAutocad(string path)
115+
protected void checkDxfDocumentInAutocad(string path)
112116
{
113117
if (Environment.GetEnvironmentVariable("GITHUB_WORKFLOW") != null)
114118
return;
@@ -168,6 +172,63 @@ protected void checkDocumentInAutocad(string path)
168172
}
169173
}
170174

175+
protected void checkDwgDocumentInAutocad(string path)
176+
{
177+
if (Environment.GetEnvironmentVariable("GITHUB_WORKFLOW") != null)
178+
return;
179+
180+
System.Diagnostics.Process process = new System.Diagnostics.Process();
181+
182+
try
183+
{
184+
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
185+
process.StartInfo.FileName = "\"D:\\Programs\\Autodesk\\AutoCAD 2023\\accoreconsole.exe\"";
186+
process.StartInfo.Arguments = $"/i \"{path}\" /l en - US";
187+
process.StartInfo.UseShellExecute = false;
188+
process.StartInfo.RedirectStandardOutput = true;
189+
process.StartInfo.RedirectStandardInput = true;
190+
process.StartInfo.StandardOutputEncoding = Encoding.ASCII;
191+
192+
Assert.True(process.Start());
193+
194+
string l = process.StandardOutput.ReadLine();
195+
bool testPassed = true;
196+
while (!process.StandardOutput.EndOfStream)
197+
{
198+
string li = l.Replace("\0", "");
199+
if (!string.IsNullOrEmpty(li))
200+
{
201+
if (li.Contains("Invalid or incomplete DXF input -- drawing discarded.")
202+
|| li.Contains("error", StringComparison.OrdinalIgnoreCase))
203+
{
204+
testPassed = false;
205+
}
206+
207+
_output.WriteLine(li);
208+
}
209+
210+
var t = process.StandardOutput.ReadLineAsync();
211+
212+
//The last line gets into an infinite loop
213+
if (t.Wait(1000))
214+
{
215+
l = t.Result;
216+
}
217+
else
218+
{
219+
break;
220+
}
221+
}
222+
223+
if (!testPassed)
224+
throw new Exception("File loading with accoreconsole failed");
225+
}
226+
finally
227+
{
228+
process.Kill();
229+
}
230+
}
231+
171232
protected static void loadSamples(string folder, string ext, TheoryData<string> files)
172233
{
173234
string path = Path.Combine(_samplesFolder, "local", folder);

0 commit comments

Comments
 (0)