Skip to content

Commit cfa7583

Browse files
authored
Merge pull request #176 from DomCR/DwgWriter-entities
Dwg writer entities
2 parents 1cb0c7a + 5666ef4 commit cfa7583

29 files changed

+2352
-160
lines changed

ACadSharp.Tests/Common/Factory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private static CadObject createObject(Type type, Type original, bool randomize)
5858
return null;
5959
}
6060

61-
if (type == typeof(XRecrod)
61+
if (type == typeof(XRecord)
6262
|| type == typeof(PlotSettings)
6363
|| type == typeof(Material)
6464
|| type == typeof(MLStyle)

ACadSharp.Tests/IO/DWG/DwgWriterTests.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,27 @@ namespace ACadSharp.Tests.IO.DWG
1111
{
1212
public class DwgWriterTests : IOTestsBase
1313
{
14+
public static TheoryData<Entity> Entities { get; }
15+
1416
public DwgWriterTests(ITestOutputHelper output) : base(output) { }
1517

18+
static DwgWriterTests()
19+
{
20+
Entities = new TheoryData<Entity>
21+
{
22+
EntityFactory.Create<Point>(),
23+
EntityFactory.Create<Line>(),
24+
};
25+
}
26+
1627
[Theory]
1728
[MemberData(nameof(Versions))]
1829
public void WriteEmptyTest(ACadVersion version)
1930
{
31+
string path = Path.Combine(_samplesOutFolder, $"out_empty_sample_{version}.dwg");
2032
CadDocument doc = new CadDocument();
2133
doc.Header.Version = version;
2234

23-
string path = Path.Combine(_samplesOutFolder, $"out_empty_sample_{version}.dwg");
24-
2535
using (var wr = new DwgWriter(path, doc))
2636
{
2737
if (isSupportedVersion(version))
@@ -43,19 +53,27 @@ public void WriteEmptyTest(ACadVersion version)
4353
//this.checkDwgDocumentInAutocad(Path.GetFullPath(path));
4454
}
4555

56+
[Theory]
57+
[MemberData(nameof(Entities))]
58+
public void WriteSingleEntityFile(Entity entity)
59+
{
60+
61+
}
62+
4663
[Theory]
4764
[MemberData(nameof(Versions))]
4865
public void WriteTest(ACadVersion version)
4966
{
5067
CadDocument doc = new CadDocument();
5168
doc.Header.Version = version;
5269

53-
addEntities(doc);
70+
this.addEntities(doc);
5471

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

5774
using (var wr = new DwgWriter(path, doc))
5875
{
76+
wr.OnNotification += this.onNotification;
5977
if (isSupportedVersion(version))
6078
{
6179
wr.Write();

ACadSharp.Tests/IO/IOTests.cs

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ public void EmptyDwgToDxf()
2424
this.writeDxfFile(pathOut, doc, true);
2525
}
2626

27+
[Theory]
28+
[MemberData(nameof(DwgFilePaths))]
29+
public void DwgToDwg(string test)
30+
{
31+
CadDocument doc = DwgReader.Read(test);
32+
33+
string file = Path.GetFileNameWithoutExtension(test);
34+
string pathOut = Path.Combine(_samplesOutFolder, $"{file}_out.dwg");
35+
36+
//accoreconsole always fails because cannot recover the file
37+
this.writeDwgFile(pathOut, doc, false);
38+
}
39+
2740
[Theory]
2841
[MemberData(nameof(DwgFilePaths))]
2942
public void DwgToDxf(string test)
@@ -48,7 +61,28 @@ public void DxfToDxf(string test)
4861

4962
[Theory]
5063
[MemberData(nameof(DwgFilePaths))]
51-
public void DwgEntitiesToNewFile(string test)
64+
public void DwgEntitiesToDwgFile(string test)
65+
{
66+
CadDocument doc = DwgReader.Read(test);
67+
68+
CadDocument transfer = new CadDocument();
69+
transfer.Header.Version = doc.Header.Version;
70+
71+
List<Entity> entities = new List<Entity>(doc.Entities);
72+
foreach (var item in entities)
73+
{
74+
Entity e = doc.Entities.Remove(item);
75+
transfer.Entities.Add(e);
76+
}
77+
78+
string file = Path.GetFileNameWithoutExtension(test);
79+
string pathOut = Path.Combine(_samplesOutFolder, $"{file}_moved_out.dwg");
80+
this.writeDwgFile(pathOut, transfer, false);
81+
}
82+
83+
[Theory]
84+
[MemberData(nameof(DwgFilePaths))]
85+
public void DwgEntitiesToDxfFile(string test)
5286
{
5387
CadDocument doc = DwgReader.Read(test);
5488

@@ -67,6 +101,33 @@ public void DwgEntitiesToNewFile(string test)
67101
this.writeDxfFile(pathOut, transfer, true);
68102
}
69103

104+
private void writeCadFile(string file, CadWriterBase writer, bool check)
105+
{
106+
using (writer)
107+
{
108+
writer.OnNotification += this.onNotification;
109+
writer.Write();
110+
}
111+
112+
if (check)
113+
this.checkDxfDocumentInAutocad(Path.GetFullPath(file));
114+
}
115+
116+
private void writeDwgFile(string file, CadDocument doc, bool check)
117+
{
118+
if (doc.Header.Version < ACadVersion.AC1014 || doc.Header.Version > ACadVersion.AC1018)
119+
return;
120+
121+
using (DwgWriter writer = new DwgWriter(file, doc))
122+
{
123+
writer.OnNotification += this.onNotification;
124+
writer.Write();
125+
}
126+
127+
if (check)
128+
this.checkDwgDocumentInAutocad(Path.GetFullPath(file));
129+
}
130+
70131
private void writeDxfFile(string file, CadDocument doc, bool check)
71132
{
72133
using (DxfWriter writer = new DxfWriter(file, doc, false))
@@ -78,5 +139,17 @@ private void writeDxfFile(string file, CadDocument doc, bool check)
78139
if (check)
79140
this.checkDxfDocumentInAutocad(Path.GetFullPath(file));
80141
}
142+
143+
private void writeDwgFile(string file, CadDocument doc)
144+
{
145+
if (doc.Header.Version > ACadVersion.AC1018)
146+
return;
147+
148+
using (DwgWriter writer = new DwgWriter(file, doc))
149+
{
150+
writer.OnNotification += this.onNotification;
151+
writer.Write();
152+
}
153+
}
81154
}
82155
}

ACadSharp.Tests/IO/LocalSampleTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void ReadUserDwg(string test)
3636

3737
CadDocument doc = DwgReader.Read(test, this._dwgConfiguration, this.onNotification);
3838

39-
//return;
39+
return;
4040

4141
string outPath = Path.Combine(Path.GetDirectoryName(test), $"{Path.GetFileNameWithoutExtension(test)}.out.dxf");
4242
using (DxfWriter writer = new DxfWriter(outPath, doc, false))

ACadSharp/CadObjectCollection.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ public class CadObjectCollection<T> : IObservableCollection<T>
2323
/// </summary>
2424
public int Count { get { return this._entries.Count; } }
2525

26+
public T this[int index]
27+
{
28+
get
29+
{
30+
return this._entries.ElementAtOrDefault(index);
31+
}
32+
}
33+
2634
protected readonly HashSet<T> _entries = new HashSet<T>();
2735

2836
public CadObjectCollection(CadObject owner)

ACadSharp/Entities/Dimension.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,13 @@ public DimensionStyle Style
167167

168168
private readonly DimensionType _flags;
169169

170+
private DimensionStyle _style = DimensionStyle.Default;
171+
170172
protected Dimension(DimensionType type)
171173
{
172174
this._flags = type;
173175
}
174176

175-
private DimensionStyle _style = DimensionStyle.Default;
176-
177177
public override CadObject Clone()
178178
{
179179
Dimension clone = (Dimension)base.Clone();

ACadSharp/Entities/Insert.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,20 @@ namespace ACadSharp.Entities
1919
public class Insert : Entity
2020
{
2121
/// <inheritdoc/>
22-
public override ObjectType ObjectType => ObjectType.INSERT;
22+
public override ObjectType ObjectType
23+
{
24+
get
25+
{
26+
if (this.RowCount > 1 || this.ColumnCount > 1)
27+
{
28+
return ObjectType.MINSERT;
29+
}
30+
else
31+
{
32+
return ObjectType.INSERT;
33+
}
34+
}
35+
}
2336

2437
/// <inheritdoc/>
2538
public override string ObjectName => DxfFileToken.EntityInsert;

ACadSharp/Entities/Leader.cs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using ACadSharp.Attributes;
22
using ACadSharp.Tables;
33
using CSMath;
4+
using System;
45
using System.Collections.Generic;
56

67
namespace ACadSharp.Entities
@@ -29,7 +30,26 @@ public class Leader : Entity
2930
/// Dimension Style
3031
/// </summary>
3132
[DxfCodeValue(3)]
32-
public DimensionStyle Style { get; set; } = DimensionStyle.Default;
33+
public DimensionStyle Style
34+
{
35+
get { return this._style; }
36+
set
37+
{
38+
if (value == null)
39+
{
40+
throw new ArgumentNullException(nameof(value));
41+
}
42+
43+
if (this.Document != null)
44+
{
45+
this._style = this.updateTable(value, this.Document.DimensionStyles);
46+
}
47+
else
48+
{
49+
this._style = value;
50+
}
51+
}
52+
}
3353

3454
/// <summary>
3555
/// Arrowhead flag
@@ -112,6 +132,36 @@ public class Leader : Entity
112132
[DxfCodeValue(213, 223, 233)]
113133
public XYZ AnnotationOffset { get; set; } = XYZ.Zero;
114134

135+
private DimensionStyle _style = DimensionStyle.Default;
136+
137+
internal override void AssignDocument(CadDocument doc)
138+
{
139+
base.AssignDocument(doc);
140+
141+
this._style = this.updateTable(this.Style, doc.DimensionStyles);
142+
143+
doc.DimensionStyles.OnRemove += this.tableOnRemove;
144+
}
145+
146+
internal override void UnassignDocument()
147+
{
148+
this.Document.DimensionStyles.OnRemove -= this.tableOnRemove;
149+
150+
base.UnassignDocument();
151+
152+
this.Style = (DimensionStyle)this.Style.Clone();
153+
}
154+
155+
protected override void tableOnRemove(object sender, CollectionChangedEventArgs e)
156+
{
157+
base.tableOnRemove(sender, e);
158+
159+
if (e.Item.Equals(this.Style))
160+
{
161+
this.Style = this.Document.DimensionStyles[DimensionStyle.DefaultName];
162+
}
163+
}
164+
115165
public override CadObject Clone()
116166
{
117167
Leader clone = (Leader)base.Clone();

ACadSharp/Entities/PolyFaceMesh.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ public class PolyfaceMesh : Polyline
2424
/// <inheritdoc/>
2525
public override string SubclassMarker => DxfSubclassMarker.PolyfaceMesh;
2626

27-
public SeqendCollection<VertexFaceRecord> Faces { get; }
27+
public CadObjectCollection<VertexFaceRecord> Faces { get; }
2828

2929
public PolyfaceMesh()
3030
{
3131
this.Vertices.OnAdd += this.verticesOnAdd;
32-
this.Faces = new SeqendCollection<VertexFaceRecord>(this);
32+
this.Faces = new CadObjectCollection<VertexFaceRecord>(this);
3333
}
3434

3535
public override IEnumerable<Entity> Explode()
@@ -45,5 +45,17 @@ private void verticesOnAdd(object sender, CollectionChangedEventArgs e)
4545
throw new ArgumentException($"Wrong vertex type {e.Item.SubclassMarker} for {this.SubclassMarker}");
4646
}
4747
}
48+
49+
internal override void AssignDocument(CadDocument doc)
50+
{
51+
base.AssignDocument(doc);
52+
doc.RegisterCollection(this.Faces);
53+
}
54+
55+
internal override void UnassignDocument()
56+
{
57+
this.Document.UnregisterCollection(this.Faces);
58+
base.UnassignDocument();
59+
}
4860
}
4961
}

ACadSharp/Entities/PolyfaceMesh.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ public class PolyfaceMesh : Polyline
2424
/// <inheritdoc/>
2525
public override string SubclassMarker => DxfSubclassMarker.PolyfaceMesh;
2626

27-
public SeqendCollection<VertexFaceRecord> Faces { get; }
27+
public CadObjectCollection<VertexFaceRecord> Faces { get; }
2828

2929
public PolyfaceMesh()
3030
{
3131
this.Vertices.OnAdd += this.verticesOnAdd;
32-
this.Faces = new SeqendCollection<VertexFaceRecord>(this);
32+
this.Faces = new CadObjectCollection<VertexFaceRecord>(this);
3333
}
3434

3535
public override IEnumerable<Entity> Explode()
@@ -45,5 +45,17 @@ private void verticesOnAdd(object sender, CollectionChangedEventArgs e)
4545
throw new ArgumentException($"Wrong vertex type {e.Item.SubclassMarker} for {this.SubclassMarker}");
4646
}
4747
}
48+
49+
internal override void AssignDocument(CadDocument doc)
50+
{
51+
base.AssignDocument(doc);
52+
doc.RegisterCollection(this.Faces);
53+
}
54+
55+
internal override void UnassignDocument()
56+
{
57+
this.Document.UnregisterCollection(this.Faces);
58+
base.UnassignDocument();
59+
}
4860
}
4961
}

0 commit comments

Comments
 (0)