Skip to content
This repository has been archived by the owner on Oct 11, 2023. It is now read-only.

Commit

Permalink
[2021/05/02]
Browse files Browse the repository at this point in the history
* Added netDxf.nuspec file.
* Ellipses with its major axis smaller than its minor axis are not allowed.
* Streams with not accessible Position property are not supported.
* (fixed) LayerState containing a non existent layer will not be added.
	This behaviour differs when the LayerState comes from an external LAS file, in the latter case the non existent layer is added to the document. This is to allow the LAS file to be used layers templates.
	Additionally if the LayerState points to a non existent linetype, the default "Standard" will be assigned.
  • Loading branch information
haplokuon committed May 2, 2021
1 parent 5060604 commit 21bcbf5
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 90 deletions.
1 change: 1 addition & 0 deletions TestDxfDocument/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using netDxf;
Expand Down
8 changes: 8 additions & 0 deletions doc/Changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
## Change history

### [2021/05/02]
* Added netDxf.nuspec file.
* Ellipses with its major axis smaller than its minor axis are not allowed.
* Streams with not accessible Position property are not supported.
* (fixed) LayerState containing a non existent layer will not be added.
This behaviour differs when the LayerState comes from an external LAS file, in the latter case the non existent layer is added to the document. This is to allow the LAS file to be used layers templates.
Additionally if the LayerState points to a non existent linetype, the default "Standard" will be assigned.

### [2021/03/27]
* The netDxf library is now licensed under the MIT License.
* Updated solution file to Visual Studio 2019.
Expand Down
30 changes: 17 additions & 13 deletions netDxf/Collections/LayerStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,23 @@ public void Export(string file, string layerStateName)
ls.Save(file);
}

/// <summary>
/// Removes all layers states.
/// </summary>
public void RemoveAll()
{
string[] names = new string[this.Names.Count];
this.Names.CopyTo(names, 0);
foreach (string name in names)
{
this.Remove(name);
}
}

#endregion

#region overrrides

/// <summary>
/// Adds a LayerState to the list.
/// </summary>
Expand Down Expand Up @@ -299,19 +314,8 @@ public override bool Remove(LayerState item)
return true;
}

/// <summary>
/// Removes all layers states.
/// </summary>
public void RemoveAll()
{
string[] names = new string[this.Names.Count];
this.Names.CopyTo(names, 0);
foreach (string name in names)
{
this.Remove(name);
}
}

#endregion

#region Layer events

private void Item_NameChanged(TableObject sender, TableObjectChangedEventArgs<string> e)
Expand Down
12 changes: 11 additions & 1 deletion netDxf/Entities/Ellipse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,23 @@ public Ellipse(Vector3 center, double majorAxis, double minorAxis)
: base(EntityType.Ellipse, DxfObjectCode.Ellipse)
{
this.center = center;

if (majorAxis <= 0)
{
throw new ArgumentOutOfRangeException(nameof(majorAxis), majorAxis, "The major axis value must be greater than zero.");
}
this.majorAxis = majorAxis;

if (minorAxis <= 0)
{
throw new ArgumentOutOfRangeException(nameof(minorAxis), minorAxis, "The minor axis value must be greater than zero.");
}

if (minorAxis > majorAxis)
{
throw new ArgumentException("The major axis must be greater than the minor axis.");
}

this.majorAxis = majorAxis;
this.minorAxis = minorAxis;
this.startAngle = 0.0;
this.endAngle = 0.0;
Expand All @@ -250,6 +258,7 @@ public Vector3 Center
/// <summary>
/// Gets or sets the ellipse mayor axis.
/// </summary>
/// <remarks>The MajorAxis value must be positive and greater than the MinorAxis.</remarks>
public double MajorAxis
{
get { return this.majorAxis; }
Expand All @@ -266,6 +275,7 @@ public double MajorAxis
/// <summary>
/// Gets or sets the ellipse minor axis.
/// </summary>
/// <remarks>The MinorAxis value must be positive and smaller than the MajorAxis.</remarks>
public double MinorAxis
{
get { return this.minorAxis; }
Expand Down
53 changes: 45 additions & 8 deletions netDxf/IO/DxfReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,22 @@ internal sealed class DxfReader
/// <param name="supportFolders">List of the document support folders.</param>
public DxfDocument Read(Stream stream, IEnumerable<string> supportFolders)
{
long startPosition = stream.Position;
long startPosition;

if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}

try
{
startPosition = stream.Position;
}
catch (NotSupportedException ex)
{
throw new ArgumentException("Streams with not accessible Position property are not supported.", nameof(stream), ex);
}

DxfVersion version = DxfDocument.CheckDxfFileVersion(stream, out this.isBinary);
stream.Position = startPosition;

Expand Down Expand Up @@ -306,7 +316,22 @@ public static bool IsBinary(Stream stream)

public static string CheckHeaderVariable(Stream stream, string headerVariable, out bool isBinary)
{
long startPosition = stream.Position;
long startPosition;

if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}

try
{
startPosition = stream.Position;
}
catch (NotSupportedException ex)
{
throw new ArgumentException("Streams with not accessible Position property are not supported.", nameof(stream), ex);
}

ICodeValueReader chunk;
isBinary = IsBinary(stream);
stream.Position = startPosition;
Expand Down Expand Up @@ -10951,6 +10976,11 @@ private void BuildLayerStateManager()
if (o is Layer layer)
{
properties = this.ReadLayerStateProperties(layer.Name, enumerator);
// if the linetype does not exist in the document, the default will be assigned
if (!this.doc.Linetypes.Contains(properties.LinetypeName))
{
properties.LinetypeName = Linetype.DefaultName;
}
layerState.Properties.Add(properties.Name, properties);
}
else
Expand All @@ -10961,7 +10991,17 @@ private void BuildLayerStateManager()
break;
case 8:
properties = this.ReadLayerStateProperties((string) recordEntry.Value, enumerator);
layerState.Properties.Add(properties.Name, properties);
// only layer properties that exist in the actual document will be added
if (this.doc.Layers.Contains(properties.Name))
{
// if the linetype does not exist in the document, the default will be assigned
if (!this.doc.Linetypes.Contains(properties.LinetypeName))
{
properties.LinetypeName = Linetype.DefaultName;
}
layerState.Properties.Add(properties.Name, properties);
}

break;
default:
enumerator.MoveNext();
Expand All @@ -10975,7 +11015,7 @@ private void BuildLayerStateManager()
private LayerStateProperties ReadLayerStateProperties(string name, IEnumerator<XRecordEntry> enumerator)
{
LayerPropertiesFlags flags = LayerPropertiesFlags.Plot;
string lineTypeName = string.Empty;
string lineTypeName = Linetype.DefaultName;
//string plotStyle = string.Empty;
AciColor color = AciColor.Default;
Lineweight lineweight = Lineweight.Default;
Expand Down Expand Up @@ -11025,8 +11065,7 @@ private LayerStateProperties ReadLayerStateProperties(string name, IEnumerator<X
}
}


LayerStateProperties properties = new LayerStateProperties(name)
return new LayerStateProperties(name)
{
Flags = flags,
Color = color,
Expand All @@ -11035,8 +11074,6 @@ private LayerStateProperties ReadLayerStateProperties(string name, IEnumerator<X
//PlotStyleName = plotStyle,
Transparency = transparency
};

return properties;
}

private void RelinkOrphanLayouts()
Expand Down
17 changes: 9 additions & 8 deletions netDxf/IO/DxfWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1894,28 +1894,29 @@ private void WriteEntity(EntityObject entity, Layout layout)
Debug.Assert(this.activeSection == DxfObjectCode.EntitiesSection || this.activeSection == DxfObjectCode.BlocksSection);
Debug.Assert(entity != null);

// hatches with zero boundaries are not allowed
if (entity.Type == EntityType.Hatch && ((Hatch) entity).BoundaryPaths.Count == 0)
{
return;
throw new ArgumentException("Hatches with zero boundaries are not allowed." + "Entity handle: " + entity.Handle, nameof(entity));
}

// leader entities with less than two vertexes are not allowed
if (entity.Type == EntityType.Leader && ((Leader) entity).Vertexes.Count < 2)
{
return;
throw new ArgumentException("Leader entities with less than two vertexes are not allowed." + "Entity handle: " + entity.Handle, nameof(entity));
}

// polyline entities with less than two vertexes are not allowed
if (entity.Type == EntityType.Polyline && ((Polyline) entity).Vertexes.Count < 2)
{
return;
throw new ArgumentException("Polyline entities with less than two vertexes are not allowed." + "Entity handle: " + entity.Handle, nameof(entity));
}

// lwPolyline entities with less than two vertexes are not allowed
if (entity.Type == EntityType.LwPolyline && ((LwPolyline) entity).Vertexes.Count < 2)
{
return;
throw new ArgumentException("LwPolyline entities with less than two vertexes are not allowed." + "Entity handle: " + entity.Handle, nameof(entity));
}

if (entity.Type == EntityType.Ellipse && ((Ellipse) entity).MajorAxis < ((Ellipse) entity).MinorAxis)
{
throw new ArgumentException("Ellipses with its major axis smaller than its minor axis are not allowed." + "Entity handle: " + entity.Handle, nameof(entity));
}

this.WriteEntityCommonCodes(entity, layout);
Expand Down
2 changes: 1 addition & 1 deletion netDxf/Objects/LayerState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ private static LayerState ReadLayerState(TextCodeValueReader chunk)
private static LayerStateProperties ReadLayerProperties(TextCodeValueReader chunk)
{
LayerPropertiesFlags flags = LayerPropertiesFlags.Plot;
string lineType = String.Empty;
string lineType = Linetype.DefaultName;
//string plotStyle = string.Empty;
AciColor color = AciColor.Default;
Lineweight lineweight = Lineweight.Default;
Expand Down
22 changes: 22 additions & 0 deletions netDxf/netDxf.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>netDxf</id>
<version>3.0.0</version>
<authors>Daniel Carvajal</authors>
<projectUrl>https://github.com/haplokuon/netDxf</projectUrl>
<license type="expression">MIT License</license>
<licenseUrl>https://github.com/haplokuon/netDxf/blob/master/LICENSE</licenseUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>netDxf is a .net library programmed in C# to read and write AutoCAD DXF files. It supports AutoCad2000, AutoCad2004, AutoCad2007, AutoCad2010, AutoCad2013, and AutoCad2018 DXF database versions, in both text and binary format.</description>
<copyright>Daniel Carvajal © 2021</copyright>
<tags>netDxf, DXF, AutoCAD</tags>
<repository url="https://github.com/haplokuon/netDxf" />
<dependencies>
<group targetFramework=".NETFramework4.5" />
<group targetFramework=".NETCoreApp3.1" />
<group targetFramework="net5.0" />
<group targetFramework=".NETStandard2.1" />
</dependencies>
</metadata>
</package>
Loading

0 comments on commit 21bcbf5

Please sign in to comment.