Skip to content

Commit 4856dec

Browse files
authored
Merge pull request DomCR#494 from DomCR/Issue-487_DBCOLOR
Issue 487 dbcolor
2 parents d11de9f + 5a3bf08 commit 4856dec

Some content is hidden

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

48 files changed

+88691
-125
lines changed
160 KB
Binary file not shown.
-705 Bytes
Binary file not shown.

samples/color_samples/sample_AC1015.dxf

Lines changed: 22256 additions & 0 deletions
Large diffs are not rendered by default.

samples/color_samples/sample_AC1021.dxf

Lines changed: 22110 additions & 0 deletions
Large diffs are not rendered by default.

samples/color_samples/sample_AC1024.dxf

Lines changed: 17302 additions & 0 deletions
Large diffs are not rendered by default.

samples/color_samples/sample_AC1027.dxf

Lines changed: 13542 additions & 0 deletions
Large diffs are not rendered by default.

samples/color_samples/sample_AC1032.dxf

Lines changed: 13038 additions & 0 deletions
Large diffs are not rendered by default.

src/ACadSharp.Tests/IO/ColorTests.cs

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Xunit;
77
using Xunit.Abstractions;
88
using ACadSharp.Tests.TestModels;
9+
using ACadSharp.Objects;
910

1011
namespace ACadSharp.Tests.IO
1112
{
@@ -15,7 +16,7 @@ public class ColorTests : IOTestsBase
1516

1617
static ColorTests()
1718
{
18-
loadSamples("color_samples", "dwg", ColorSamplesFilePaths);
19+
loadSamples("color_samples", "*", ColorSamplesFilePaths);
1920
}
2021

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

31-
//CECOLOR R 155 : G 66 : B 236
32-
Color currentEntityColor = doc.Header.CurrentEntityColor;
33-
Assert.Equal(155, currentEntityColor.R);
34-
Assert.Equal(66, currentEntityColor.G);
35-
Assert.Equal(236, currentEntityColor.B);
36-
37-
//Layer: color_125_33_79
38-
Color layerColor = doc.Layers["color_125_33_79"].Color;
39-
Assert.Equal(125, layerColor.R);
40-
Assert.Equal(33, layerColor.G);
41-
Assert.Equal(79, layerColor.B);
33+
if (doc.Header.Version <= ACadVersion.AC1015)
34+
{
35+
return;
36+
}
4237

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

46+
//Layer: color_125_33_79
47+
Color layerColor = doc.Layers["color_125_33_79"].Color;
48+
Assert.Equal(125, layerColor.R);
49+
Assert.Equal(33, layerColor.G);
50+
Assert.Equal(79, layerColor.B);
51+
52+
if (isDxf)
53+
{
54+
//True color for dimension style is not supported in dxf
55+
return;
56+
}
57+
58+
//CECOLOR R 155 : G 66 : B 236
59+
Color currentEntityColor = doc.Header.CurrentEntityColor;
60+
Assert.Equal(155, currentEntityColor.R);
61+
Assert.Equal(66, currentEntityColor.G);
62+
Assert.Equal(236, currentEntityColor.B);
63+
5164
//DimStyle: custom_dim_style
5265
DimensionStyle dimStyle = doc.DimensionStyles["custom_dim_style"];
5366
//DimensionLines: 44,136,27
@@ -74,8 +87,29 @@ public void ColorDwg(FileModel test)
7487
Assert.Equal(117, fillColor.G);
7588
Assert.Equal(66, fillColor.B);
7689
}
90+
}
91+
92+
[Theory]
93+
[MemberData(nameof(ColorSamplesFilePaths))]
94+
public void BookColor(FileModel test)
95+
{
96+
CadDocument doc = this.readDocument(test);
97+
98+
Circle circle = doc.GetCadObject<Circle>(649);
99+
100+
Assert.True(doc.Colors.ContainsKey("RAL CLASSIC$RAL 1006"));
101+
102+
if (doc.Header.Version <= ACadVersion.AC1015)
103+
{
104+
return;
105+
}
106+
107+
Assert.NotNull(circle.BookColor);
108+
109+
BookColor color = circle.BookColor;
77110

78-
DwgWriter.Write(new MemoryStream(), doc);
111+
Assert.Equal("RAL 1006", color.ColorName);
112+
Assert.Equal("RAL CLASSIC", color.BookName);
79113
}
80114
}
81115
}

src/ACadSharp.Tests/IO/IOTestsBase.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq;
77
using Xunit;
88
using Xunit.Abstractions;
9+
using static System.Net.Mime.MediaTypeNames;
910

1011
namespace ACadSharp.Tests.IO
1112
{
@@ -135,5 +136,20 @@ protected bool isSupportedVersion(ACadVersion version)
135136
return false;
136137
}
137138
}
139+
140+
protected CadDocument readDocument(FileModel test)
141+
{
142+
CadDocument doc;
143+
if (Path.GetExtension(test.FileName).Equals(".dxf"))
144+
{
145+
doc = DxfReader.Read(test.Path, this.onNotification);
146+
}
147+
else
148+
{
149+
doc = DwgReader.Read(test.Path, this.onNotification);
150+
}
151+
152+
return doc;
153+
}
138154
}
139155
}

src/ACadSharp.Tests/IO/LocalSampleTests.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
using ACadSharp.IO;
1+
using ACadSharp.Entities;
2+
using ACadSharp.IO;
23
using ACadSharp.Tests.TestModels;
34
using System.Diagnostics;
45
using System.IO;
6+
using System.Linq;
57
using Xunit;
68
using Xunit.Abstractions;
79

@@ -34,6 +36,10 @@ public void ReadUserDwg(FileModel test)
3436
return;
3537

3638
CadDocument doc = DwgReader.Read(test.Path, this._dwgConfiguration, this.onNotification);
39+
40+
var trees = doc.Entities.Where(e => e is Insert insert
41+
&& insert.Block.Name == "TREE_1"
42+
&& e.Layer.Name.Equals("VEGETATION", System.StringComparison.OrdinalIgnoreCase));
3743
}
3844

3945
[Theory]

src/ACadSharp.Tests/IO/ViewportTests.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,7 @@ public ViewportTests(ITestOutputHelper output) : base(output)
1818
[MemberData(nameof(DxfAsciiFiles))]
1919
public void ScaleInViewport(FileModel test)
2020
{
21-
CadDocument doc;
22-
if (Path.GetExtension(test.FileName).Equals(".dxf"))
23-
{
24-
doc = DxfReader.Read(test.Path);
25-
}
26-
else
27-
{
28-
doc = DwgReader.Read(test.Path);
29-
}
21+
CadDocument doc = this.readDocument(test);
3022

3123
ACadSharp.Tables.BlockRecord paper = doc.PaperSpace;
3224
foreach (Viewport v in paper.Viewports)

src/ACadSharp.Tests/IO/WriterSingleObjectTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,20 @@ public void AddCustomScale()
409409
this.Document.Scales.Add(new Scale("Hello"));
410410
}
411411

412+
public void AddCustomBookColor()
413+
{
414+
//var color = new BookColor("RAL CLASSIC$RAL 1006");
415+
var color = new BookColor("TEST BOOK$MY COLOR");
416+
color.Color = new(226, 144, 0);
417+
418+
Line line = new Line(XYZ.Zero, new XYZ(100, 100, 0));
419+
line.Color = Color.ByBlock;
420+
line.BookColor = color;
421+
422+
this.Document.Colors.Add(color);
423+
this.Document.Entities.Add(line);
424+
}
425+
412426
public void AddBlockWithAttributes()
413427
{
414428
BlockRecord record = new("my_block");
@@ -497,6 +511,7 @@ static WriterSingleObjectTests()
497511
Data.Add(new(nameof(SingleCaseGenerator.ChangedEncoding)));
498512
Data.Add(new(nameof(SingleCaseGenerator.AddBlockWithAttributes)));
499513
Data.Add(new(nameof(SingleCaseGenerator.AddCustomScale)));
514+
Data.Add(new(nameof(SingleCaseGenerator.AddCustomBookColor)));
500515
}
501516

502517
protected string getPath(string name, string ext, ACadVersion version)

src/ACadSharp/CadDocument.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ public class CadDocument : IHandledCadObject
8181
/// </summary>
8282
public VPortsTable VPorts { get; private set; }
8383

84+
/// <summary>
85+
/// The collection of all book colors in the drawing.
86+
/// </summary>
87+
/// <remarks>
88+
/// The collection is null if the <see cref="CadDictionary.AcadColor"/> doesn't exist in the root dictionary.
89+
/// </remarks>
90+
public ColorCollection Colors { get; private set; }
91+
8492
/// <summary>
8593
/// The collection of all layouts in the drawing.
8694
/// </summary>
@@ -114,8 +122,11 @@ public class CadDocument : IHandledCadObject
114122
public MLineStyleCollection MLineStyles { get; private set; }
115123

116124
/// <summary>
117-
///
125+
/// The collection of all images in the drawing.
118126
/// </summary>
127+
/// <remarks>
128+
/// The collection is null if the <see cref="CadDictionary.AcadImageDict"/> doesn't exist in the root dictionary.
129+
/// </remarks>
119130
public ImageDefinitionCollection ImageDefinitions { get; private set; }
120131

121132
/// <summary>
@@ -340,6 +351,11 @@ public void UpdateCollections(bool createDictionaries)
340351
{
341352
this.ImageDefinitions = new ImageDefinitionCollection(imageDefinitions);
342353
}
354+
355+
if (this.updateCollection(CadDictionary.AcadColor, createDictionaries, out CadDictionary colors))
356+
{
357+
this.Colors = new ColorCollection(colors);
358+
}
343359
}
344360

345361
private bool updateCollection(string dictName, bool createDictionary, out CadDictionary dictionary)

src/ACadSharp/CadObject.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using ACadSharp.Objects.Collections;
44
using ACadSharp.Tables;
55
using ACadSharp.Tables.Collections;
6+
using System;
67
using System.Collections.Generic;
78

89
namespace ACadSharp

src/ACadSharp/Classes/DxfClassCollection.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,19 @@ public static void UpdateDxfClasses(CadDocument doc)
387387
ProxyFlags = ProxyFlags.R13FormatProxy,
388388
WasZombie = false,
389389
});
390+
391+
//AcDbColor
392+
doc.Classes.AddOrUpdate(new DxfClass
393+
{
394+
CppClassName = DxfSubclassMarker.DbColor,
395+
ClassNumber = (short)(500 + doc.Classes.Count),
396+
DwgVersion = ACadVersion.AC1015,
397+
DxfName = DxfFileToken.ObjectDBColor,
398+
ItemClassId = 499,
399+
MaintenanceVersion = 14,
400+
ProxyFlags = ProxyFlags.None,
401+
WasZombie = false,
402+
});
390403
}
391404

392405
/// <summary>

src/ACadSharp/Color.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ private Color(uint trueColor)
386386
throw new ArgumentOutOfRangeException(nameof(trueColor), "True color must be a 24 bit color.");
387387

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

392392
/// <summary>

src/ACadSharp/DxfFileToken.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public static class DxfFileToken
111111
public const string ObjectDictionaryWithDefault = "ACDBDICTIONARYWDFLT";
112112
public const string ObjectAcdbPlaceHolder = "ACDBPLACEHOLDER";
113113
public const string ObjectDictionaryVar = "DICTIONARYVAR";
114+
public const string ObjectDBColor = "DBCOLOR";
114115
public const string ObjectPlotSettings = "PLOTSETTINGS";
115116
public const string ObjectPlaceholder = "ACDBPLACEHOLDER";
116117
public const string ObjectLayout = "LAYOUT";

src/ACadSharp/DxfPropertyBase.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,7 @@ public void SetValue<TCadObject>(int code, TCadObject obj, object value)
9595
case 420:
9696
byte[] b = LittleEndianConverter.Instance.GetBytes((int)value);
9797
// true color
98-
this._property.SetValue(obj, new Color(b[0], b[1], b[2]));
99-
break;
100-
case 430:
101-
// dictionary color
98+
this._property.SetValue(obj, new Color(b[2], b[1], b[0]));
10299
break;
103100
}
104101
}

src/ACadSharp/DxfSubclassMarker.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,6 @@ public static class DxfSubclassMarker
8484
public const string Scale = "AcDbScale";
8585
public const string EvalGraph = "AcDbEvalGraph";
8686
public const string BlockVisibilityParameter = "AcDbBlockVisibilityParameter";
87+
public const string DbColor = "AcDbColor";
8788
}
8889
}

src/ACadSharp/Entities/Entity.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public Layer Layer
3939
}
4040

4141
/// <inheritdoc/>
42-
[DxfCodeValue(62, 420, 430)]
42+
[DxfCodeValue(62, 420)]
4343
public Color Color { get; set; } = Color.ByLayer;
4444

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

88+
/// <summary>
89+
/// Book color for this entity.
90+
/// </summary>
91+
[DxfCodeValue(DxfReferenceType.Name, 430)]
92+
public BookColor BookColor
93+
{
94+
get { return this._bookColor; }
95+
set
96+
{
97+
if (this.Document != null)
98+
{
99+
this._bookColor = this.updateCollection(value, this.Document.Colors);
100+
}
101+
else
102+
{
103+
this._bookColor = value;
104+
}
105+
}
106+
}
107+
88108
private Layer _layer = Layer.Default;
89109

90110
private LineType _lineType = LineType.ByLayer;
91111

112+
private BookColor _bookColor = null;
113+
92114
/// <inheritdoc/>
93115
public Entity() : base() { }
94116

src/ACadSharp/Entities/MLine.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using ACadSharp.Attributes;
22
using ACadSharp.Objects;
33
using CSMath;
4+
using System;
45
using System.Collections.Generic;
56
using System.Linq;
67

0 commit comments

Comments
 (0)