Skip to content

Commit

Permalink
Book color readers implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
DomCR committed Nov 21, 2024
1 parent 7e49c31 commit 79977dd
Show file tree
Hide file tree
Showing 18 changed files with 78 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/ACadSharp.Tests/IO/ColorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void BookColor(FileModel test)

BookColor color = circle.BookColor;

Assert.Equal("RAL 1006", color.Name);
Assert.Equal("RAL 1006", color.ColorName);
Assert.Equal("RAL CLASSIC", color.BookName);

Assert.True(doc.Colors.ContainsKey(color.Name));
Expand Down
5 changes: 4 additions & 1 deletion src/ACadSharp/CadDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,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
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
21 changes: 20 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,29 @@ public LineType LineType
[DxfCodeValue(DxfReferenceType.Handle, 347)]
public Material Material { get; set; }

[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
13 changes: 6 additions & 7 deletions src/ACadSharp/IO/DWG/DwgStreamReaders/DwgObjectReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@
using ACadSharp.Tables;
using ACadSharp.Tables.Collections;
using CSMath;
using CSUtilities.IO;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System;
using ACadSharp.Types;
using static ACadSharp.Objects.MultiLeaderAnnotContext;
using System.Net;
using CSUtilities.Converters;
using CSUtilities.Extensions;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace ACadSharp.IO.DWG
{
Expand Down Expand Up @@ -5807,10 +5802,14 @@ private CadTemplate readDbColor()
byte flags = this._objectReader.ReadByte();

if ((flags & 1U) > 0U)
bookColor.Name = this._textReader.ReadVariableText();
{
string colorName = this._textReader.ReadVariableText();
}

if ((flags & 2U) > 0U)
bookColor.BookName = this._textReader.ReadVariableText();
{
string bookName = this._textReader.ReadVariableText();
}

byte[] arr = LittleEndianConverter.Instance.GetBytes(trueColor);

Expand Down
4 changes: 2 additions & 2 deletions src/ACadSharp/IO/DXF/DxfReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static CadDocument Read(Stream stream, NotificationEventHandler notificat
/// <returns></returns>
public static CadDocument Read(string filename, NotificationEventHandler notification = null)
{
return Read(File.OpenRead(filename));
return Read(File.OpenRead(filename), notification);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -164,7 +164,7 @@ public override CadDocument Read()
this._reader.ReadNext();
}

if(this._document.Header == null)
if (this._document.Header == null)
{
this._document.Header = new CadHeader(this._document);
}
Expand Down
19 changes: 18 additions & 1 deletion src/ACadSharp/IO/DXF/DxfStreamReader/DxfObjectsSectionReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ private CadTemplate readObject()
{
switch (this._reader.ValueAsString)
{
case DxfFileToken.ObjectDBColor:
return this.readObjectCodes<BookColor>(new CadNonGraphicalObjectTemplate(new BookColor()), this.readBookColor);
case DxfFileToken.ObjectDictionary:
return this.readObjectCodes<CadDictionary>(new CadDictionaryTemplate(), this.readDictionary);
case DxfFileToken.ObjectDictionaryWithDefault:
Expand Down Expand Up @@ -216,10 +218,25 @@ private void readXRecordEntries(XRecord recrod)
}
}

private bool readBookColor(CadTemplate template, DxfMap map)
{
CadNonGraphicalObjectTemplate tmp = template as CadNonGraphicalObjectTemplate;
BookColor color = tmp.CadObject as BookColor;

switch (this._reader.Code)
{
case 430:
color.Name = this._reader.ValueAsString;
return true;
default:
return this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.DbColor]);
}
}

private bool readDictionary(CadTemplate template, DxfMap map)
{
CadDictionary cadDictionary = new CadDictionary();
CadDictionaryTemplate tmp = template as CadDictionaryTemplate;
CadDictionary cadDictionary = tmp.CadObject;

switch (this._reader.Code)
{
Expand Down
3 changes: 3 additions & 0 deletions src/ACadSharp/IO/DXF/DxfStreamReader/DxfSectionReaderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ protected void readCommonEntityCodes(CadEntityTemplate template, out bool isExte
case 347:
template.MaterialHandle = this._reader.ValueAsHandle;
break;
case 430:
template.BookColorName = this._reader.ValueAsString;
break;
default:
if (!this.tryAssignCurrentValue(template.CadObject, map.SubClasses[DxfSubclassMarker.Entity]))
{
Expand Down
20 changes: 0 additions & 20 deletions src/ACadSharp/IO/Templates/CadBookColorTemplate.cs

This file was deleted.

15 changes: 8 additions & 7 deletions src/ACadSharp/IO/Templates/CadEntityTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using ACadSharp.Entities;
using ACadSharp.Objects;
using ACadSharp.Tables;
using CSUtilities.Extensions;

namespace ACadSharp.IO.Templates
{
Expand All @@ -24,6 +25,8 @@ internal class CadEntityTemplate : CadTemplate<Entity>

public ulong? ColorHandle { get; set; }

public string BookColorName { get; set; }

public ulong? MaterialHandle { get; set; }

public CadEntityTemplate(Entity entity) : base(entity) { }
Expand Down Expand Up @@ -58,16 +61,14 @@ public override void Build(CadDocumentBuilder builder)
this.CadObject.LineType = ltype;
}

if (this.ColorHandle.HasValue)
BookColor color;
if (builder.TryGetCadObject(this.ColorHandle, out color))
{
if (builder.TryGetCadObject(this.ColorHandle, out BookColor color))
{

}
this.CadObject.BookColor = color;
}
else
else if (!this.BookColorName.IsNullOrEmpty() && builder.DocumentToBuild.Colors.TryGetValue(this.BookColorName, out color))
{
//TODO: Set color by name, only for dxf?
this.CadObject.BookColor = color;
}
}

Expand Down
1 change: 0 additions & 1 deletion src/ACadSharp/IO/Templates/CadMLeaderTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public CadMLeaderTemplate(MultiLeader entity) : base(entity) { }

public IList<LeaderLineSubTemplate> LeaderLineSubTemplates { get; } = new List<LeaderLineSubTemplate>();


public class LeaderLineSubTemplate
{
public MultiLeaderAnnotContext.LeaderLine LeaderLine { get; }
Expand Down
21 changes: 13 additions & 8 deletions src/ACadSharp/Objects/BookColor.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using ACadSharp.Attributes;
using System.Linq;

namespace ACadSharp.Objects
{
/// <summary>
/// Represents a <see cref="Group"/> object.
/// Represents a <see cref="BookColor"/> object.
/// </summary>
/// <remarks>
/// Object name <see cref="DxfFileToken.TableGroup"/> <br/>
/// Dxf class name <see cref="DxfSubclassMarker.Group"/>
/// Object name <see cref="DxfFileToken.ObjectDBColor"/> <br/>
/// Dxf class name <see cref="DxfSubclassMarker.DbColor"/>
/// </remarks>
[DxfName(DxfFileToken.TableGroup)]
[DxfSubClass(DxfSubclassMarker.Group)]
[DxfName(DxfFileToken.ObjectDBColor)]
[DxfSubClass(DxfSubclassMarker.DbColor)]
public class BookColor : NonGraphicalObject
{
{
/// <inheritdoc/>
public override ObjectType ObjectType => ObjectType.UNLISTED;

Expand All @@ -22,7 +23,11 @@ public class BookColor : NonGraphicalObject
/// <inheritdoc/>
public override string SubclassMarker => DxfSubclassMarker.DbColor;

public string BookName { get; internal set; }
public Color Color { get; internal set; }
public string ColorName { get { return this.Name.Split('$').Last(); } }

public string BookName { get { return this.Name.Split('$').First(); } }

[DxfCodeValue(62, 420)]
public Color Color { get; set; }
}
}
3 changes: 1 addition & 2 deletions src/ACadSharp/Objects/Collections/ColorCollection.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
namespace ACadSharp.Objects.Collections
{
public class ColorCollection : ObjectDictionaryCollection<ImageDefinition>
public class ColorCollection : ObjectDictionaryCollection<BookColor>
{
public ColorCollection(CadDictionary dictionary) : base(dictionary)
{
this._dictionary = dictionary;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ public class ImageDefinitionCollection : ObjectDictionaryCollection<ImageDefinit
{
public ImageDefinitionCollection(CadDictionary dictionary) : base(dictionary)
{
this._dictionary = dictionary;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ public class MLeaderStyleCollection : ObjectDictionaryCollection<MultiLeaderStyl
{
public MLeaderStyleCollection(CadDictionary dictionary) : base(dictionary)
{
this._dictionary = dictionary;
}
}
}
1 change: 0 additions & 1 deletion src/ACadSharp/Objects/Collections/MLineStyleCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ public class MLineStyleCollection : ObjectDictionaryCollection<MLineStyle>
{
public MLineStyleCollection(CadDictionary dictionary) : base(dictionary)
{
this._dictionary = dictionary;
}
}
}
17 changes: 0 additions & 17 deletions src/ACadSharp/Objects/Collections/ScaleCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,8 @@ namespace ACadSharp.Objects.Collections
{
public class ScaleCollection : ObjectDictionaryCollection<Scale>
{
private readonly Dictionary<string, Scale> _scales = new Dictionary<string, Scale>();

public ScaleCollection(CadDictionary dictionary) : base(dictionary)
{
this._dictionary = dictionary;

foreach (Scale item in this._dictionary)
{
this._scales.Add(item.Name, item);
}
}

/// <summary>
///
/// </summary>
/// <param name="entry"></param>
public override void Add(Scale entry)
{
base.Add(entry);
}
}
}

0 comments on commit 79977dd

Please sign in to comment.