Skip to content

Commit

Permalink
Merge pull request #150 from DomCR/DxfWriter-Full-refactor
Browse files Browse the repository at this point in the history
Dxf writer full refactor
  • Loading branch information
DomCR authored Sep 21, 2023
2 parents 5f271f1 + d532c3c commit 7e34698
Show file tree
Hide file tree
Showing 57 changed files with 1,964 additions and 682 deletions.
110 changes: 56 additions & 54 deletions ACadSharp.Tests/ColorTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using ACadSharp.Entities;
using ACadSharp.Tests.Common;
using System;
using System.Collections.Generic;
using System.Text;
using System;
using Xunit;

namespace ACadSharp.Tests
Expand All @@ -12,59 +8,65 @@ public class ColorTests
[Fact]
public void IndexedColorProperties()
{
Assert.True(new Color(256).IsByLayer);
Assert.True(new Color(0).IsByBlock);
}
Assert.True(new Color(0).IsByBlock);
Assert.True(new Color(256).IsByLayer);

Assert.True(new Color(2705).IsTrueColor);
Assert.True(new Color(2706).IsTrueColor);
Assert.True(new Color(2707).IsTrueColor);
Assert.True(new Color(2708).IsTrueColor);
}

private int Int32FromInt24(byte[] array)
{
if (BitConverter.IsLittleEndian)
return array[0] | array[1] << 8 | array[2] << 16;
else
return array[0] << 16 | array[1] << 8 | array[2];
}
[Fact]
public void ParsesTrueColors()
{
var random = new Random();
byte[] colors = new byte[3];
for (int i = 0; i < 1000; i++)
{
random.NextBytes(colors);
[Fact]
public void ParsesTrueColors()
{
var random = new Random();
byte[] colors = new byte[3];
for (int i = 0; i < 1000; i++)
{
random.NextBytes(colors);

var intColor = Int32FromInt24(colors);
var color = new Color(colors);
Assert.True(color.IsTrueColor, color.TrueColor.ToString() + $" Bytes:{colors[0]},{colors[1]},{colors[2]},");
Assert.Equal(intColor, color.TrueColor);
Assert.Equal(-1, color.Index);
var intColor = this.int32FromInt24(colors);
var color = new Color(colors);
Assert.True(color.IsTrueColor, color.TrueColor.ToString() + $" Bytes:{colors[0]},{colors[1]},{colors[2]},");
Assert.Equal(intColor, color.TrueColor);
Assert.Equal(-1, color.Index);

var rgb = color.GetTrueColorRgb();
Assert.Equal(colors[0], rgb[0]);
Assert.Equal(colors[1], rgb[1]);
Assert.Equal(colors[2], rgb[2]);
}
}
var rgb = color.GetTrueColorRgb();
Assert.Equal(colors[0], rgb[0]);
Assert.Equal(colors[1], rgb[1]);
Assert.Equal(colors[2], rgb[2]);
}
}

[Fact]
public void Handles000TrueColor()
{
var color = new Color(new byte[] { 0, 0, 0 });

[Fact]
public void Handles000TrueColor()
{
var color = new Color(new byte[] { 0, 0, 0 });
Assert.True(color.IsTrueColor);
Assert.Equal(0, color.TrueColor);
}

Assert.True(color.IsTrueColor);
Assert.Equal(0, color.TrueColor);
}
[Fact]
public void HandlesIndexedColors()
{
for (short i = 0; i <= 256; i++)
{
var color = new Color(i);
Assert.False(color.IsTrueColor);
Assert.Equal(i, color.Index);
Assert.Equal(-1, color.TrueColor);
Assert.True(ReadOnlySpan<byte>.Empty.SequenceEqual(color.GetTrueColorRgb()));
}
}

[Fact]
public void HandlesIndexedColors()
{
for (short i = 0; i <= 256; i++)
{
var color = new Color(i);
Assert.False(color.IsTrueColor);
Assert.Equal(i, color.Index);
Assert.Equal(-1, color.TrueColor);
Assert.True(ReadOnlySpan<byte>.Empty.SequenceEqual(color.GetTrueColorRgb()));
}
}
}
private int int32FromInt24(byte[] array)
{
if (BitConverter.IsLittleEndian)
return array[0] | array[1] << 8 | array[2] << 16;
else
return array[0] << 16 | array[1] << 8 | array[2];
}
}
}
9 changes: 7 additions & 2 deletions ACadSharp.Tests/Common/CSMathRandom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ public CSMathRandom(int seed) : base(seed) { }

public short NextShort()
{
return (short)Next(short.MinValue, short.MaxValue);
return NextShort(short.MinValue, short.MaxValue);
}

public short NextShort(short min, short max)
{
return (short)Next(min, max);
}

public object Next(Type t)
Expand Down Expand Up @@ -55,7 +60,7 @@ public XYZ NextXYZ()

public Color NextColor()
{
return new Color(this.NextShort());
return new Color(this.NextShort(0, 256));
}

public string RandomString(int length)
Expand Down
4 changes: 2 additions & 2 deletions ACadSharp/ACadSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net48'">
<PackageReference Include="System.Memory" Version="4.5.5"/>
<PackageReference Include="System.Memory" Version="4.5.5" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
<PackageReference Include="System.Memory" Version="4.5.5"/>
<PackageReference Include="System.Memory" Version="4.5.5" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="7.0.0" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion ACadSharp/Blocks/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public string Name
/// Gets the path of the block, document, application, or external reference.
/// </summary>
[DxfCodeValue(1)]
public string XrefPath { get; internal set; }
public string XrefPath { get; set; }

/// <summary>
/// Specifies the comments for the block or drawing.
Expand Down
38 changes: 16 additions & 22 deletions ACadSharp/CadDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ private void onRemove(object sender, CollectionChangedEventArgs e)
}
}

internal void RegisterCollection<T>(IObservableCollection<T> collection, bool addElements = true)
internal void RegisterCollection<T>(IObservableCollection<T> collection)
where T : CadObject
{
switch (collection)
Expand Down Expand Up @@ -444,23 +444,20 @@ internal void RegisterCollection<T>(IObservableCollection<T> collection, bool ad
}
}

if (addElements)
foreach (T item in collection)
{
foreach (T item in collection)
if (item is CadDictionary dictionary)
{
if (item is CadDictionary dictionary)
{
this.RegisterCollection(dictionary);
}
else
{
this.addCadObject(item);
}
this.RegisterCollection(dictionary);
}
else
{
this.addCadObject(item);
}
}
}

internal void UnregisterCollection<T>(IObservableCollection<T> collection, bool removeElements = true)
internal void UnregisterCollection<T>(IObservableCollection<T> collection)
where T : CadObject
{
switch (collection)
Expand Down Expand Up @@ -496,18 +493,15 @@ internal void UnregisterCollection<T>(IObservableCollection<T> collection, bool
}
}

if (removeElements)
foreach (T item in collection)
{
foreach (T item in collection)
if (item is CadDictionary dictionary)
{
if (item is CadDictionary dictionary)
{
this.UnregisterCollection(dictionary);
}
else
{
this.removeCadObject(item);
}
this.UnregisterCollection(dictionary);
}
else
{
this.removeCadObject(item);
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion ACadSharp/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,13 @@ public bool IsByBlock
/// <summary>
/// True if the stored color is a true color. False if the color is an indexed color.
/// </summary>
public bool IsTrueColor => _color >= (1 << 30);
public bool IsTrueColor
{
get
{
return _color > 256 || _color < 0;
}
}

/// <summary>
/// Represents the actual stored color. Either a True Color or an indexed color.
Expand Down
15 changes: 15 additions & 0 deletions ACadSharp/DxfFileToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ public static class DxfFileToken
public const string ClassesSection = "CLASSES";
public const string ClassEntry = "CLASS";

/// <summary>
/// Object reactors, list of handles
/// </summary>
public const string ReactorsToken = "{ACAD_REACTORS";

/// <summary>
/// Handle for the xdictionary
/// </summary>
public const string DictionaryToken = "{ACAD_XDICTIONARY";

/// <summary>
/// Block references
/// </summary>
public const string BlkRefToken = "{BLKREFS";

#region Tables

public const string TablesSection = "TABLES";
Expand Down
18 changes: 16 additions & 2 deletions ACadSharp/Entities/Dimension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ public abstract class Dimension : Entity
/// Dimension type
/// </summary>
[DxfCodeValue(70)]
public DimensionType DimensionType { get; set; }
public DimensionType Flags
{
get
{
var flags = this._flags | DimensionType.BlockReference;
return flags;
}
}

/// <summary>
/// Attachment point
Expand Down Expand Up @@ -125,7 +132,7 @@ public string Text
/// All dimension types have an optional 51 group code, which indicates the horizontal direction for the dimension entity.The dimension entity determines the orientation of dimension text and lines for horizontal, vertical, and rotated linear dimensions
/// This group value is the negative of the angle between the OCS X axis and the UCS X axis. It is always in the XY plane of the OCS
/// </summary>
[DxfCodeValue(DxfReferenceType.Optional| DxfReferenceType.IsAngle, 51)]
[DxfCodeValue(DxfReferenceType.Optional | DxfReferenceType.IsAngle, 51)]
public double HorizontalDirection { get; set; }

//This group value is the negative of the angle between the OCS X axis and the UCS X axis.It is always in the XY plane of the OCS
Expand All @@ -138,6 +145,13 @@ public string Text

private string _text;

private readonly DimensionType _flags;

protected Dimension(DimensionType type)
{
this._flags = type;
}

public override CadObject Clone()
{
Dimension clone = (Dimension)base.Clone();
Expand Down
6 changes: 5 additions & 1 deletion ACadSharp/Entities/DimensionAligned.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ public class DimensionAligned : Dimension
/// When added to the rotation angle of the linear dimension(group code 50),
/// it gives the angle of the extension lines
/// </summary>
[DxfCodeValue(52)]
[DxfCodeValue(DxfReferenceType.Optional, 52)]
public double ExtLineRotation { get; set; }

protected DimensionAligned(DimensionType type) : base(type) { }

public DimensionAligned() : base(DimensionType.Aligned) { }
}
}
4 changes: 4 additions & 0 deletions ACadSharp/Entities/DimensionAngular2Line.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,9 @@ public class DimensionAngular2Line : Dimension
/// </summary>
[DxfCodeValue(16, 26, 36)]
public XYZ DimensionArc { get; set; }

public DimensionAngular2Line() : base(DimensionType.Angular)
{
}
}
}
2 changes: 2 additions & 0 deletions ACadSharp/Entities/DimensionAngular3Pt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ public class DimensionAngular3Pt : Dimension
/// </summary>
[DxfCodeValue(15, 25, 35)]
public XYZ AngleVertex { get; set; }

public DimensionAngular3Pt() : base(DimensionType.Angular3Point) { }
}
}
2 changes: 2 additions & 0 deletions ACadSharp/Entities/DimensionDiameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ public class DimensionDiameter : Dimension
/// </summary>
[DxfCodeValue(40)]
public double LeaderLength { get; set; }

public DimensionDiameter() : base(DimensionType.Diameter) { }
}
}
2 changes: 2 additions & 0 deletions ACadSharp/Entities/DimensionLinear.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ public class DimensionLinear : DimensionAligned
/// </summary>
[DxfCodeValue(DxfReferenceType.IsAngle, 50)]
public double Rotation { get; set; }

public DimensionLinear() : base(DimensionType.Linear) { }
}
}
2 changes: 2 additions & 0 deletions ACadSharp/Entities/DimensionOrdinate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ public class DimensionOrdinate : Dimension
/// </summary>
[DxfCodeValue(14, 24, 34)]
public XYZ LeaderEndpoint { get; set; }

public DimensionOrdinate() : base(DimensionType.Ordinate) { }
}
}
2 changes: 2 additions & 0 deletions ACadSharp/Entities/DimensionRadius.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ public class DimensionRadius : Dimension
/// </summary>
[DxfCodeValue(40)]
public double LeaderLength { get; set; }

public DimensionRadius() : base(DimensionType.Radius) { }
}
}
1 change: 1 addition & 0 deletions ACadSharp/Entities/DimensionType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/// <summary>
/// Dimension type
/// </summary>
[System.Flags]
public enum DimensionType
{
/// <summary>
Expand Down
Loading

0 comments on commit 7e34698

Please sign in to comment.