Skip to content

Commit

Permalink
Merge pull request #219 from DomCR/Issue-217_DwgWriter-entity-color
Browse files Browse the repository at this point in the history
Issue 217 dwg writer entity color
  • Loading branch information
DomCR authored Dec 5, 2023
2 parents ecbbdb5 + 838f6a7 commit 7f6413d
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 18 deletions.
81 changes: 67 additions & 14 deletions ACadSharp.Tests/IO/DWG/DwgWriterSingleObjectTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ACadSharp.Entities;
using ACadSharp.IO;
using ACadSharp.Tables;
using CSMath;
using System.IO;
using Xunit;
Expand All @@ -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();
Expand All @@ -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()
Expand All @@ -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<string>(nameof(this.Name));
Expand All @@ -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}");
}
}
}
11 changes: 11 additions & 0 deletions ACadSharp/Entities/Line.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,16 @@ public class Line : Entity
/// Default constructor
/// </summary>
public Line() : base() { }

/// <summary>
/// Constructor with the start and end
/// </summary>
/// <param name="start"></param>
/// <param name="end"></param>
public Line(XYZ start, XYZ end) : base()
{
this.StartPoint = start;
this.EndPoint = end;
}
}
}
2 changes: 1 addition & 1 deletion ACadSharp/IO/DWG/DwgStreamReaders/DwgStreamReaderAC18.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 1 addition & 3 deletions ACadSharp/IO/DWG/DwgStreamWriters/DwgObjectWriter.Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
45 changes: 45 additions & 0 deletions ACadSharp/IO/DWG/DwgStreamWriters/DwgStreamWriterAC18.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
5 changes: 5 additions & 0 deletions ACadSharp/IO/DWG/DwgStreamWriters/DwgStreamWriterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions ACadSharp/IO/DWG/DwgStreamWriters/DwgmMergedStreamWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions ACadSharp/IO/DWG/DwgStreamWriters/IDwgStreamWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 7f6413d

Please sign in to comment.