Skip to content

Commit

Permalink
Merge pull request #494 from DomCR/Issue-487_DBCOLOR
Browse files Browse the repository at this point in the history
Issue 487 dbcolor
  • Loading branch information
DomCR authored Nov 25, 2024
2 parents d11de9f + 5a3bf08 commit 4856dec
Show file tree
Hide file tree
Showing 48 changed files with 88,691 additions and 125 deletions.
Binary file added samples/color_samples/color_sample_AC1015.dwg
Binary file not shown.
Binary file modified samples/color_samples/color_sample_AC1018.dwg
Binary file not shown.
Binary file not shown.
22,256 changes: 22,256 additions & 0 deletions samples/color_samples/sample_AC1015.dxf

Large diffs are not rendered by default.

22,110 changes: 22,110 additions & 0 deletions samples/color_samples/sample_AC1021.dxf

Large diffs are not rendered by default.

17,302 changes: 17,302 additions & 0 deletions samples/color_samples/sample_AC1024.dxf

Large diffs are not rendered by default.

13,542 changes: 13,542 additions & 0 deletions samples/color_samples/sample_AC1027.dxf

Large diffs are not rendered by default.

13,038 changes: 13,038 additions & 0 deletions samples/color_samples/sample_AC1032.dxf

Large diffs are not rendered by default.

62 changes: 48 additions & 14 deletions src/ACadSharp.Tests/IO/ColorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Xunit;
using Xunit.Abstractions;
using ACadSharp.Tests.TestModels;
using ACadSharp.Objects;

namespace ACadSharp.Tests.IO
{
Expand All @@ -15,7 +16,7 @@ public class ColorTests : IOTestsBase

static ColorTests()
{
loadSamples("color_samples", "dwg", ColorSamplesFilePaths);
loadSamples("color_samples", "*", ColorSamplesFilePaths);
}

public ColorTests(ITestOutputHelper output) : base(output)
Expand All @@ -26,19 +27,13 @@ public ColorTests(ITestOutputHelper output) : base(output)
[MemberData(nameof(ColorSamplesFilePaths))]
public void ColorDwg(FileModel test)
{
CadDocument doc = DwgReader.Read(test.Path);
bool isDxf = Path.GetExtension(test.FileName).Equals(".dxf");
CadDocument doc = this.readDocument(test);

//CECOLOR R 155 : G 66 : B 236
Color currentEntityColor = doc.Header.CurrentEntityColor;
Assert.Equal(155, currentEntityColor.R);
Assert.Equal(66, currentEntityColor.G);
Assert.Equal(236, currentEntityColor.B);

//Layer: color_125_33_79
Color layerColor = doc.Layers["color_125_33_79"].Color;
Assert.Equal(125, layerColor.R);
Assert.Equal(33, layerColor.G);
Assert.Equal(79, layerColor.B);
if (doc.Header.Version <= ACadVersion.AC1015)
{
return;
}

//Entity Line: R 52 : G 201 : B 24
Line line = doc.Entities.OfType<Line>().FirstOrDefault();
Expand All @@ -48,6 +43,24 @@ public void ColorDwg(FileModel test)
Assert.Equal(201, lcolor.G);
Assert.Equal(24, lcolor.B);

//Layer: color_125_33_79
Color layerColor = doc.Layers["color_125_33_79"].Color;
Assert.Equal(125, layerColor.R);
Assert.Equal(33, layerColor.G);
Assert.Equal(79, layerColor.B);

if (isDxf)
{
//True color for dimension style is not supported in dxf
return;
}

//CECOLOR R 155 : G 66 : B 236
Color currentEntityColor = doc.Header.CurrentEntityColor;
Assert.Equal(155, currentEntityColor.R);
Assert.Equal(66, currentEntityColor.G);
Assert.Equal(236, currentEntityColor.B);

//DimStyle: custom_dim_style
DimensionStyle dimStyle = doc.DimensionStyles["custom_dim_style"];
//DimensionLines: 44,136,27
Expand All @@ -74,8 +87,29 @@ public void ColorDwg(FileModel test)
Assert.Equal(117, fillColor.G);
Assert.Equal(66, fillColor.B);
}
}

[Theory]
[MemberData(nameof(ColorSamplesFilePaths))]
public void BookColor(FileModel test)
{
CadDocument doc = this.readDocument(test);

Circle circle = doc.GetCadObject<Circle>(649);

Assert.True(doc.Colors.ContainsKey("RAL CLASSIC$RAL 1006"));

if (doc.Header.Version <= ACadVersion.AC1015)
{
return;
}

Assert.NotNull(circle.BookColor);

BookColor color = circle.BookColor;

DwgWriter.Write(new MemoryStream(), doc);
Assert.Equal("RAL 1006", color.ColorName);
Assert.Equal("RAL CLASSIC", color.BookName);
}
}
}
16 changes: 16 additions & 0 deletions src/ACadSharp.Tests/IO/IOTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using Xunit;
using Xunit.Abstractions;
using static System.Net.Mime.MediaTypeNames;

namespace ACadSharp.Tests.IO
{
Expand Down Expand Up @@ -135,5 +136,20 @@ protected bool isSupportedVersion(ACadVersion version)
return false;
}
}

protected CadDocument readDocument(FileModel test)
{
CadDocument doc;
if (Path.GetExtension(test.FileName).Equals(".dxf"))
{
doc = DxfReader.Read(test.Path, this.onNotification);
}
else
{
doc = DwgReader.Read(test.Path, this.onNotification);
}

return doc;
}
}
}
8 changes: 7 additions & 1 deletion src/ACadSharp.Tests/IO/LocalSampleTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using ACadSharp.IO;
using ACadSharp.Entities;
using ACadSharp.IO;
using ACadSharp.Tests.TestModels;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -34,6 +36,10 @@ public void ReadUserDwg(FileModel test)
return;

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

var trees = doc.Entities.Where(e => e is Insert insert
&& insert.Block.Name == "TREE_1"
&& e.Layer.Name.Equals("VEGETATION", System.StringComparison.OrdinalIgnoreCase));
}

[Theory]
Expand Down
10 changes: 1 addition & 9 deletions src/ACadSharp.Tests/IO/ViewportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,7 @@ public ViewportTests(ITestOutputHelper output) : base(output)
[MemberData(nameof(DxfAsciiFiles))]
public void ScaleInViewport(FileModel test)
{
CadDocument doc;
if (Path.GetExtension(test.FileName).Equals(".dxf"))
{
doc = DxfReader.Read(test.Path);
}
else
{
doc = DwgReader.Read(test.Path);
}
CadDocument doc = this.readDocument(test);

ACadSharp.Tables.BlockRecord paper = doc.PaperSpace;
foreach (Viewport v in paper.Viewports)
Expand Down
15 changes: 15 additions & 0 deletions src/ACadSharp.Tests/IO/WriterSingleObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,20 @@ public void AddCustomScale()
this.Document.Scales.Add(new Scale("Hello"));
}

public void AddCustomBookColor()
{
//var color = new BookColor("RAL CLASSIC$RAL 1006");
var color = new BookColor("TEST BOOK$MY COLOR");
color.Color = new(226, 144, 0);

Line line = new Line(XYZ.Zero, new XYZ(100, 100, 0));
line.Color = Color.ByBlock;
line.BookColor = color;

this.Document.Colors.Add(color);
this.Document.Entities.Add(line);
}

public void AddBlockWithAttributes()
{
BlockRecord record = new("my_block");
Expand Down Expand Up @@ -497,6 +511,7 @@ static WriterSingleObjectTests()
Data.Add(new(nameof(SingleCaseGenerator.ChangedEncoding)));
Data.Add(new(nameof(SingleCaseGenerator.AddBlockWithAttributes)));
Data.Add(new(nameof(SingleCaseGenerator.AddCustomScale)));
Data.Add(new(nameof(SingleCaseGenerator.AddCustomBookColor)));
}

protected string getPath(string name, string ext, ACadVersion version)
Expand Down
18 changes: 17 additions & 1 deletion src/ACadSharp/CadDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ public class CadDocument : IHandledCadObject
/// </summary>
public VPortsTable VPorts { get; private set; }

/// <summary>
/// The collection of all book colors in the drawing.
/// </summary>
/// <remarks>
/// The collection is null if the <see cref="CadDictionary.AcadColor"/> doesn't exist in the root dictionary.
/// </remarks>
public ColorCollection Colors { get; private set; }

/// <summary>
/// The collection of all layouts in the drawing.
/// </summary>
Expand Down Expand Up @@ -114,8 +122,11 @@ public class CadDocument : IHandledCadObject
public MLineStyleCollection MLineStyles { get; private set; }

/// <summary>
///
/// The collection of all images in the drawing.
/// </summary>
/// <remarks>
/// The collection is null if the <see cref="CadDictionary.AcadImageDict"/> doesn't exist in the root dictionary.
/// </remarks>
public ImageDefinitionCollection ImageDefinitions { get; private set; }

/// <summary>
Expand Down Expand Up @@ -340,6 +351,11 @@ public void UpdateCollections(bool createDictionaries)
{
this.ImageDefinitions = new ImageDefinitionCollection(imageDefinitions);
}

if (this.updateCollection(CadDictionary.AcadColor, createDictionaries, out CadDictionary colors))
{
this.Colors = new ColorCollection(colors);
}
}

private bool updateCollection(string dictName, bool createDictionary, out CadDictionary dictionary)
Expand Down
1 change: 1 addition & 0 deletions src/ACadSharp/CadObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using ACadSharp.Objects.Collections;
using ACadSharp.Tables;
using ACadSharp.Tables.Collections;
using System;
using System.Collections.Generic;

namespace ACadSharp
Expand Down
13 changes: 13 additions & 0 deletions src/ACadSharp/Classes/DxfClassCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,19 @@ public static void UpdateDxfClasses(CadDocument doc)
ProxyFlags = ProxyFlags.R13FormatProxy,
WasZombie = false,
});

//AcDbColor
doc.Classes.AddOrUpdate(new DxfClass
{
CppClassName = DxfSubclassMarker.DbColor,
ClassNumber = (short)(500 + doc.Classes.Count),
DwgVersion = ACadVersion.AC1015,
DxfName = DxfFileToken.ObjectDBColor,
ItemClassId = 499,
MaintenanceVersion = 14,
ProxyFlags = ProxyFlags.None,
WasZombie = false,
});
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/ACadSharp/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ private Color(uint trueColor)
throw new ArgumentOutOfRangeException(nameof(trueColor), "True color must be a 24 bit color.");

// Shift to set the 30th bit indicating a true color.
this._color = (uint)(trueColor | _trueColorFlag); //Is this correct?
this._color = (uint)(trueColor | _trueColorFlag);
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions src/ACadSharp/DxfFileToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public static class DxfFileToken
public const string ObjectDictionaryWithDefault = "ACDBDICTIONARYWDFLT";
public const string ObjectAcdbPlaceHolder = "ACDBPLACEHOLDER";
public const string ObjectDictionaryVar = "DICTIONARYVAR";
public const string ObjectDBColor = "DBCOLOR";
public const string ObjectPlotSettings = "PLOTSETTINGS";
public const string ObjectPlaceholder = "ACDBPLACEHOLDER";
public const string ObjectLayout = "LAYOUT";
Expand Down
5 changes: 1 addition & 4 deletions src/ACadSharp/DxfPropertyBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,7 @@ public void SetValue<TCadObject>(int code, TCadObject obj, object value)
case 420:
byte[] b = LittleEndianConverter.Instance.GetBytes((int)value);
// true color
this._property.SetValue(obj, new Color(b[0], b[1], b[2]));
break;
case 430:
// dictionary color
this._property.SetValue(obj, new Color(b[2], b[1], b[0]));
break;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/ACadSharp/DxfSubclassMarker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,6 @@ public static class DxfSubclassMarker
public const string Scale = "AcDbScale";
public const string EvalGraph = "AcDbEvalGraph";
public const string BlockVisibilityParameter = "AcDbBlockVisibilityParameter";
public const string DbColor = "AcDbColor";
}
}
24 changes: 23 additions & 1 deletion src/ACadSharp/Entities/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Layer Layer
}

/// <inheritdoc/>
[DxfCodeValue(62, 420, 430)]
[DxfCodeValue(62, 420)]
public Color Color { get; set; } = Color.ByLayer;

/// <inheritdoc/>
Expand Down Expand Up @@ -85,10 +85,32 @@ public LineType LineType
[DxfCodeValue(DxfReferenceType.Handle, 347)]
public Material Material { get; set; }

/// <summary>
/// Book color for this entity.
/// </summary>
[DxfCodeValue(DxfReferenceType.Name, 430)]
public BookColor BookColor
{
get { return this._bookColor; }
set
{
if (this.Document != null)
{
this._bookColor = this.updateCollection(value, this.Document.Colors);
}
else
{
this._bookColor = value;
}
}
}

private Layer _layer = Layer.Default;

private LineType _lineType = LineType.ByLayer;

private BookColor _bookColor = null;

/// <inheritdoc/>
public Entity() : base() { }

Expand Down
1 change: 1 addition & 0 deletions src/ACadSharp/Entities/MLine.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using ACadSharp.Attributes;
using ACadSharp.Objects;
using CSMath;
using System;
using System.Collections.Generic;
using System.Linq;

Expand Down
Loading

0 comments on commit 4856dec

Please sign in to comment.