diff --git a/ACadSharp.Tests/IO/DWG/DwgWriterSingleObjectTests.cs b/ACadSharp.Tests/IO/DWG/DwgWriterSingleObjectTests.cs index 6221d2b8..2a6198fc 100644 --- a/ACadSharp.Tests/IO/DWG/DwgWriterSingleObjectTests.cs +++ b/ACadSharp.Tests/IO/DWG/DwgWriterSingleObjectTests.cs @@ -1,5 +1,6 @@ using ACadSharp.Entities; using ACadSharp.IO; +using ACadSharp.Tables; using CSMath; using System.IO; using Xunit; @@ -11,6 +12,9 @@ public class DwgWriterSingleObjectTests : IOTestsBase { public class SingleCaseGenerator : IXunitSerializable { + private const string _dxfExt = "dxf"; + private const string _dwgExt = "dwg"; + public string Name { get; private set; } public CadDocument Document { get; private set; } = new CadDocument(); @@ -29,14 +33,42 @@ public override string ToString() public void Empty() { } - public void SinglePoint() + public void DefaultLayer() { - this.Document.Entities.Add(new Point(XYZ.Zero)); + this.Document.Layers.Add(new Layer("default_layer")); } - public void DefaultLayer() + public void EntityColorByLayer() + { + Layer layer = new Layer("Test"); + layer.Color = new Color(25); + this.Document.Layers.Add(layer); + + Circle c = new Circle(); + c.Center = new XYZ(0, 0, 0); + c.Radius = 10; + c.Layer = layer; + c.Color = Color.ByLayer; + + this.Document.Entities.Add(c); + } + + public void EntityColorTrueColor() + { + Circle c = new Circle(); + c.Center = new XYZ(0, 0, 0); + c.Radius = 10; + c.Color = Color.FromTrueColor(1151726); + + this.Document.Entities.Add(c); + } + + + public void SingleLine() { - this.Document.Layers.Add(new ACadSharp.Tables.Layer("default_layer")); + Line line = new Line(XYZ.Zero, new XYZ(100, 100, 0)); + + this.Document.Entities.Add(line); } public void SingleMText() @@ -48,6 +80,11 @@ public void SingleMText() this.Document.Entities.Add(mtext); } + public void SinglePoint() + { + this.Document.Entities.Add(new Point(XYZ.Zero)); + } + public void Deserialize(IXunitSerializationInfo info) { this.Name = info.GetValue(nameof(this.Name)); @@ -74,55 +111,71 @@ static DwgWriterSingleObjectTests() } Data.Add(new(nameof(SingleCaseGenerator.Empty))); - Data.Add(new(nameof(SingleCaseGenerator.SinglePoint))); + Data.Add(new(nameof(SingleCaseGenerator.SingleLine))); + Data.Add(new(nameof(SingleCaseGenerator.EntityColorByLayer))); + Data.Add(new(nameof(SingleCaseGenerator.EntityColorTrueColor))); Data.Add(new(nameof(SingleCaseGenerator.DefaultLayer))); Data.Add(new(nameof(SingleCaseGenerator.SingleMText))); + Data.Add(new(nameof(SingleCaseGenerator.SinglePoint))); } [Theory()] [MemberData(nameof(Data))] public void WriteCasesAC1018(SingleCaseGenerator data) { - this.writeFile(data, ACadVersion.AC1018); + this.writeDwgFile(data, ACadVersion.AC1018); } [Theory()] [MemberData(nameof(Data))] public void WriteCasesAC1024(SingleCaseGenerator data) { - this.writeFile(data, ACadVersion.AC1024); + this.writeDwgFile(data, ACadVersion.AC1024); } [Theory()] [MemberData(nameof(Data))] public void WriteCasesAC1027(SingleCaseGenerator data) { - this.writeFile(data, ACadVersion.AC1027); + this.writeDwgFile(data, ACadVersion.AC1027); } [Theory()] [MemberData(nameof(Data))] public void WriteCasesAC1032(SingleCaseGenerator data) { - this.writeFile(data, ACadVersion.AC1032); + this.writeDwgFile(data, ACadVersion.AC1032); + } + + private void writeDxfFile(SingleCaseGenerator data, ACadVersion version) + { + if (!TestVariables.RunDwgWriterSingleCases) + return; + + string path = this.getPath(data.Name, "dxf", version); + + data.Document.Header.Version = version; + DxfWriter.Write(path, data.Document, false, this.onNotification); + + this.checkDxfDocumentInAutocad(path); } - private void writeFile(SingleCaseGenerator data, ACadVersion version) + private void writeDwgFile(SingleCaseGenerator data, ACadVersion version) { if (!TestVariables.RunDwgWriterSingleCases) return; - string path = this.getPath(data.Name, version); + string path = this.getPath(data.Name, "dwg", version); data.Document.Header.Version = version; - DwgWriter.Write(this.getPath(data.Name, version), data.Document, this.onNotification); + DwgWriter.Write(path, data.Document, this.onNotification); this.checkDwgDocumentInAutocad(path); } - private string getPath(string name, ACadVersion version) + private string getPath(string name, string ext, ACadVersion version) { - return Path.Combine(_singleCasesOutFolder, $"{name}_{version}.dwg"); + return Path.Combine(_singleCasesOutFolder, $"{name}_{version}.{ext}"); } } } \ No newline at end of file diff --git a/ACadSharp/Entities/Line.cs b/ACadSharp/Entities/Line.cs index 4e98be2b..42dc7e4e 100644 --- a/ACadSharp/Entities/Line.cs +++ b/ACadSharp/Entities/Line.cs @@ -51,5 +51,16 @@ public class Line : Entity /// Default constructor /// public Line() : base() { } + + /// + /// Constructor with the start and end + /// + /// + /// + public Line(XYZ start, XYZ end) : base() + { + this.StartPoint = start; + this.EndPoint = end; + } } } diff --git a/ACadSharp/IO/DWG/DwgStreamReaders/DwgStreamReaderAC18.cs b/ACadSharp/IO/DWG/DwgStreamReaders/DwgStreamReaderAC18.cs index 8c9feea5..e9e77a6d 100644 --- a/ACadSharp/IO/DWG/DwgStreamReaders/DwgStreamReaderAC18.cs +++ b/ACadSharp/IO/DWG/DwgStreamReaders/DwgStreamReaderAC18.cs @@ -93,7 +93,7 @@ public override Color ReadEnColor(out Transparency transparency, out bool flag) } //0x2000: color is followed by a transparency BL - if ((flags & 0x2000U) > 0U) + if ((flags & 0x2000) > 0U) { //The first byte represents the transparency type: diff --git a/ACadSharp/IO/DWG/DwgStreamWriters/DwgObjectWriter.Common.cs b/ACadSharp/IO/DWG/DwgStreamWriters/DwgObjectWriter.Common.cs index 8338a0f4..29b2d840 100644 --- a/ACadSharp/IO/DWG/DwgStreamWriters/DwgObjectWriter.Common.cs +++ b/ACadSharp/IO/DWG/DwgStreamWriters/DwgObjectWriter.Common.cs @@ -221,9 +221,7 @@ private void writeCommonEntityData(Entity entity) } //Color CMC(B) 62 - this._writer.WriteBitShort(0); - //TODO: Implement write en color - //this._writer.WriteEnColor(entity.Color, entity.Transparency); + this._writer.WriteEnColor(entity.Color, entity.Transparency); //R2004+: //if ((this._version >= ACadVersion.AC1018) && colorFlag) diff --git a/ACadSharp/IO/DWG/DwgStreamWriters/DwgStreamWriterAC18.cs b/ACadSharp/IO/DWG/DwgStreamWriters/DwgStreamWriterAC18.cs index a19ab5be..4570e0b5 100644 --- a/ACadSharp/IO/DWG/DwgStreamWriters/DwgStreamWriterAC18.cs +++ b/ACadSharp/IO/DWG/DwgStreamWriters/DwgStreamWriterAC18.cs @@ -40,5 +40,50 @@ public override void WriteCmColor(Color value) //(&1 => color name follows(TV), //&2 => book name follows(TV)) } + + public override void WriteEnColor(Color color, Transparency transparency) + { + //BS : color number: flags + color index + ushort size = 0; + + if (color.IsByBlock && transparency.IsByLayer) + { + base.WriteBitShort(0); + return; + } + + //0x2000: color is followed by a transparency BL + if (!transparency.IsByLayer) + { + size = (ushort)(size | 0x2000); + } + + //0x8000: complex color (rgb). + if (color.IsTrueColor) + { + size = (ushort)(size | 0x8000); + } + else + { + //Color index: if no flags were set, the color is looked up by the color number (ACI color). + size = (ushort)(size | (ushort)color.Index); + } + + base.WriteBitShort((short)size); + + if (color.IsTrueColor) + { + base.WriteBitLong(color.TrueColor); + } + + if (!transparency.IsByLayer) + { + //The first byte represents the transparency type: + //0 = BYLAYER, + //1 = BYBLOCK, + //3 = the transparency value in the last byte. + base.WriteBitLong((int)transparency.Value); + } + } } } diff --git a/ACadSharp/IO/DWG/DwgStreamWriters/DwgStreamWriterBase.cs b/ACadSharp/IO/DWG/DwgStreamWriters/DwgStreamWriterBase.cs index ab4db21d..e25a909d 100644 --- a/ACadSharp/IO/DWG/DwgStreamWriters/DwgStreamWriterBase.cs +++ b/ACadSharp/IO/DWG/DwgStreamWriters/DwgStreamWriterBase.cs @@ -366,6 +366,11 @@ public virtual void WriteCmColor(Color value) this.WriteBitShort(value.Index); } + public virtual void WriteEnColor(Color color, Transparency transparency) + { + this.WriteCmColor(color); + } + public void Write2BitDouble(XY value) { this.WriteBitDouble(value.X); diff --git a/ACadSharp/IO/DWG/DwgStreamWriters/DwgmMergedStreamWriter.cs b/ACadSharp/IO/DWG/DwgStreamWriters/DwgmMergedStreamWriter.cs index 862d3b5f..a8469046 100644 --- a/ACadSharp/IO/DWG/DwgStreamWriters/DwgmMergedStreamWriter.cs +++ b/ACadSharp/IO/DWG/DwgStreamWriters/DwgmMergedStreamWriter.cs @@ -147,6 +147,11 @@ public void WriteCmColor(Color value) this.Main.WriteCmColor(value); } + public void WriteEnColor(Color color, Transparency transparency) + { + this.Main.WriteEnColor(color, transparency); + } + public void WriteDateTime(DateTime value) { this.Main.WriteDateTime(value); diff --git a/ACadSharp/IO/DWG/DwgStreamWriters/IDwgStreamWriter.cs b/ACadSharp/IO/DWG/DwgStreamWriters/IDwgStreamWriter.cs index fc9ffbe4..bce40a8e 100644 --- a/ACadSharp/IO/DWG/DwgStreamWriters/IDwgStreamWriter.cs +++ b/ACadSharp/IO/DWG/DwgStreamWriters/IDwgStreamWriter.cs @@ -51,6 +51,8 @@ internal interface IDwgStreamWriter void WriteCmColor(Color value); + void WriteEnColor(Color color, Transparency transparency); + void Write2BitDouble(XY value); void Write3BitDouble(XYZ value);