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

Commit 2dc2579

Browse files
committed
[2023/04/27]
* Removed the taget frameworks netstandard2.1, netcoreapp3.1, and net5.0. Manually edit the netDxf.proj if you need them. * Reworked how the references to a TableObject are tracked. Now they are internally stored in a dictionary instead of a list. While this will make adding entities to the document a little slower removing them will be much faster. See the sample TableObjectReferences(). * Add the method GetReferences to classes that inherit from TableObject. It returns the same list as the GetReferences method of its respective collection. See the sample TableObjectReferences(). * Added the method HasReferences to TableObjects and TableObject collections. It indicates if the actual table object has been referenced by other DxfObjects. TableObjects in use cannot be removed. * Added a check to avoid initializing a bezier curve with the wrong number of control points. * (fixed) Removing dimension entities with no associated block. This is the case of dimensions whick block has left null to force the program that loads the resulting DXF to generate its own dimension block. * (fixed) Error when loading a closed periodic spline from a DXF without weights.
1 parent 6b4f0ae commit 2dc2579

Some content is hidden

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

55 files changed

+1999
-496
lines changed

TestDxfDocument/Program.cs

Lines changed: 212 additions & 50 deletions
Large diffs are not rendered by default.

doc/Changelog.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
## Change history
22

3+
### [2023/04/27]
4+
* Removed the taget frameworks netstandard2.1, netcoreapp3.1, and net5.0. Manually edit the netDxf.proj if you need them.
5+
* Reworked how the references to a TableObject are tracked. Now they are internally stored in a dictionary instead of a list. While this will make adding entities to the document a little slower removing them will be much faster. See the sample TableObjectReferences().
6+
* Add the method GetReferences to classes that inherit from TableObject. It returns the same list as the GetReferences method of its respective collection. See the sample TableObjectReferences().
7+
* Added the method HasReferences to TableObjects and TableObject collections. It indicates if the actual table object has been referenced by other DxfObjects. TableObjects in use cannot be removed.
8+
* Added a check to avoid initializing a bezier curve with the wrong number of control points.
9+
* (fixed) Removing dimension entities with no associated block. This is the case of dimensions whick block has left null to force the program that loads the resulting DXF to generate its own dimension block.
10+
* (fixed) Error when loading a closed periodic spline from a DXF without weights.
11+
312
### [2023/03/05]
413
* Changed the default target framework 'net45' for 'net48'. The library is compatible will all 4.x net framework versions.
514
* Renamed the constant 'NET45' with 'NET4X' in netDxf.csproj. This constant must be applicable when compiling against any net framework 4.x versions.

netDxf/AciColor.cs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -643,25 +643,6 @@ public static byte RgbToAci(byte r, byte g, byte b)
643643
}
644644
}
645645

646-
//byte index = 0;
647-
//foreach (byte[] in IndexRgb.Keys)
648-
//{
649-
// byte[] color = IndexRgb[key];
650-
// int red = r - color[0];
651-
// int green = g - color[1];
652-
// int blue = b - color[2];
653-
// int dist = red * red + green * green + blue * blue;
654-
// if (dist == 0) // the RGB components correspond to one of the indexed colors
655-
// {
656-
// return key;
657-
// }
658-
// if (dist < prevDist)
659-
// {
660-
// prevDist = dist;
661-
// index = key;
662-
// }
663-
//}
664-
665646
return index;
666647
}
667648

netDxf/BezierCurve.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#region netDxf library licensed under the MIT License
22
//
33
// netDxf library
4-
// Copyright (c) 2019-2021 Daniel Carvajal (haplokuon@gmail.com)
4+
// Copyright (c) 2019-2023 Daniel Carvajal (haplokuon@gmail.com)
55
//
66
// Permission is hereby granted, free of charge, to any person obtaining a copy
77
// of this software and associated documentation files (the "Software"), to deal
@@ -47,18 +47,28 @@ public abstract class BezierCurve
4747
/// Initializes a new instance of the <c>BezierCurve</c> class.
4848
/// </summary>
4949
/// <param name="controlPoints">A list of control points.</param>
50+
/// <param name="degree">Bezier curve degree.</param>
5051
/// <remarks>
51-
/// The curve degree will be equal to the number of control points minus one.
52+
/// The curve degree must be equal to the number of control points minus one.
5253
/// </remarks>
53-
protected BezierCurve(IEnumerable<Vector3> controlPoints)
54+
protected BezierCurve(IEnumerable<Vector3> controlPoints, int degree)
5455
{
5556
if (controlPoints == null)
5657
{
5758
throw new ArgumentNullException(nameof(controlPoints));
5859
}
60+
if (degree < 1)
61+
{
62+
throw new ArgumentOutOfRangeException(nameof(degree), degree, "The bezier curve degree must be at least one.");
63+
}
5964

6065
this.controlPoints = controlPoints.ToArray();
61-
this.degree = this.controlPoints.Length - 1;
66+
this.degree = degree;
67+
68+
if (this.degree != this.controlPoints.Length - 1)
69+
{
70+
throw new ArgumentException("The bezier curve degree must be equal to the number of control points minus one.");
71+
}
6272
}
6373

6474
#endregion

netDxf/BezierCurveCubic.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#region netDxf library licensed under the MIT License
22
//
33
// netDxf library
4-
// Copyright (c) 2019-2021 Daniel Carvajal (haplokuon@gmail.com)
4+
// Copyright (c) 2019-2023 Daniel Carvajal (haplokuon@gmail.com)
55
//
66
// Permission is hereby granted, free of charge, to any person obtaining a copy
77
// of this software and associated documentation files (the "Software"), to deal
@@ -49,7 +49,7 @@ public class BezierCurveCubic :
4949
/// and the last the end point.
5050
/// </remarks>
5151
public BezierCurveCubic(IEnumerable<Vector3> controlPoints)
52-
: base(controlPoints)
52+
: base(controlPoints, 3)
5353
{
5454
}
5555

@@ -61,7 +61,7 @@ public BezierCurveCubic(IEnumerable<Vector3> controlPoints)
6161
/// <param name="secondControlPoint">Second control point.</param>
6262
/// <param name="endPoint">End anchor point.</param>
6363
public BezierCurveCubic(Vector3 startPoint, Vector3 firstControlPoint, Vector3 secondControlPoint, Vector3 endPoint)
64-
: base(new[]{startPoint, firstControlPoint, secondControlPoint, endPoint})
64+
: base(new[]{startPoint, firstControlPoint, secondControlPoint, endPoint}, 3)
6565
{
6666
}
6767

@@ -207,10 +207,9 @@ public List<Vector3> PolygonalVertexes(int precision)
207207
/// <param name="fitPoints">List of points.</param>
208208
/// <returns>A list of cubic bezier curves.</returns>
209209
/// <returns>
210-
/// Original https://www.codeproject.com/Articles/31859/Draw-a-Smooth-Curve-through-a-Set-of-2D-Points-wit by Oleg V. Polikarpotchkin and Peter Lee.
210+
/// Original code: https://www.codeproject.com/Articles/31859/Draw-a-Smooth-Curve-through-a-Set-of-2D-Points-wit by Oleg V. Polikarpotchkin and Peter Lee.<br />
211211
/// Modified to allow the use of 3D points, and other minor changes to accomodate the existing classes of this library.<br />
212-
/// The total number of curves returned will be equal to the number of fit points minus 1,
213-
/// therefore this method is not suitable to use over large number of fit points,
212+
/// The total number of curves returned will be equal to the number of fit points minus 1, therefore this method is not suitable to use over large number of fit points,
214213
/// where other, more computational heavy methods, like the least-squares bezier curve fitting would return a less amount of curves.
215214
/// In such cases, it is advisable to perform some method to reduce the number of points and to avoid duplicates or very close points.
216215
/// </returns>
@@ -269,7 +268,7 @@ public static List<BezierCurveCubic> CreateFromFitPoints(IEnumerable<Vector3> fi
269268
// Get first control points Y-values
270269
double[] y = GetFirstControlPoints(rhs);
271270

272-
// Set right hand side Y values
271+
// Set right hand side Z values
273272
for (int i = 1; i < n - 1; i++)
274273
{
275274
rhs[i] = 4.0 * points[i].Z + 2.0 * points[i + 1].Z;

netDxf/BezierCurveQuadratic.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#region netDxf library licensed under the MIT License
22
//
33
// netDxf library
4-
// Copyright (c) 2019-2021 Daniel Carvajal (haplokuon@gmail.com)
4+
// Copyright (c) 2019-2023 Daniel Carvajal (haplokuon@gmail.com)
55
//
66
// Permission is hereby granted, free of charge, to any person obtaining a copy
77
// of this software and associated documentation files (the "Software"), to deal
@@ -47,7 +47,7 @@ public class BezierCurveQuadratic :
4747
/// and the last the end point.
4848
/// </remarks>
4949
public BezierCurveQuadratic(IEnumerable<Vector3> controlPoints)
50-
: base(controlPoints)
50+
: base(controlPoints, 2)
5151
{
5252
}
5353

@@ -58,7 +58,7 @@ public BezierCurveQuadratic(IEnumerable<Vector3> controlPoints)
5858
/// <param name="controlPoint">Second control point.</param>
5959
/// <param name="endPoint">End anchor point.</param>
6060
public BezierCurveQuadratic(Vector3 startPoint, Vector3 controlPoint, Vector3 endPoint)
61-
: base (new []{startPoint, controlPoint, endPoint})
61+
: base (new []{startPoint, controlPoint, endPoint}, 2)
6262
{
6363
}
6464

netDxf/Blocks/Block.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#region netDxf library licensed under the MIT License
22
//
33
// netDxf library
4-
// Copyright (c) 2019-2021 Daniel Carvajal (haplokuon@gmail.com)
4+
// Copyright (c) 2019-2023 Daniel Carvajal (haplokuon@gmail.com)
55
//
66
// Permission is hereby granted, free of charge, to any person obtaining a copy
77
// of this software and associated documentation files (the "Software"), to deal
@@ -598,6 +598,41 @@ public bool Save(string file, DxfVersion version, bool isBinary)
598598

599599
#region overrides
600600

601+
/// <summary>
602+
/// Checks if this instance has been referenced by other DxfObjects.
603+
/// </summary>
604+
/// <returns>
605+
/// Returns true if this instance has been referenced by other DxfObjects, false otherwise.
606+
/// It will always return false if this instance does not belong to a document.
607+
/// </returns>
608+
/// <remarks>
609+
/// This method returns the same value as the HasReferences method that can be found in the TableObjects class.
610+
/// </remarks>
611+
public override bool HasReferences()
612+
{
613+
return this.Owner.Owner != null && this.Owner.Owner.HasReferences(this.Name);
614+
}
615+
616+
/// <summary>
617+
/// Gets the list of DxfObjects referenced by this instance.
618+
/// </summary>
619+
/// <returns>
620+
/// A list of DxfObjectReference that contains the DxfObject referenced by this instance and the number of times it does.
621+
/// It will return null if this instance does not belong to a document.
622+
/// </returns>
623+
/// <remarks>
624+
/// This method returns the same list as the GetReferences method that can be found in the TableObjects class.
625+
/// </remarks>
626+
public override List<DxfObjectReference> GetReferences()
627+
{
628+
if (this.Owner.Owner == null)
629+
{
630+
return null;
631+
}
632+
633+
return this.Owner.Owner.GetReferences(this.Name);
634+
}
635+
601636
private static TableObject Clone(Block block, string newName, bool checkName)
602637
{
603638
if (block.Record.Layout != null && !IsValidName(newName))

netDxf/Collections/ApplicationRegistries.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#region netDxf library licensed under the MIT License
22
//
33
// netDxf library
4-
// Copyright (c) 2019-2021 Daniel Carvajal (haplokuon@gmail.com)
4+
// Copyright (c) 2019-2023 Daniel Carvajal (haplokuon@gmail.com)
55
//
66
// Permission is hereby granted, free of charge, to any person obtaining a copy
77
// of this software and associated documentation files (the "Software"), to deal
@@ -25,6 +25,7 @@
2525

2626
using System;
2727
using System.Collections.Generic;
28+
using System.Diagnostics;
2829
using netDxf.Tables;
2930

3031
namespace netDxf.Collections
@@ -67,7 +68,7 @@ internal override ApplicationRegistry Add(ApplicationRegistry appReg, bool assig
6768
throw new ArgumentNullException(nameof(appReg));
6869
}
6970

70-
if (this.list.TryGetValue(appReg.Name, out ApplicationRegistry add))
71+
if (this.List.TryGetValue(appReg.Name, out ApplicationRegistry add))
7172
{
7273
return add;
7374
}
@@ -77,13 +78,14 @@ internal override ApplicationRegistry Add(ApplicationRegistry appReg, bool assig
7778
this.Owner.NumHandles = appReg.AssignHandle(this.Owner.NumHandles);
7879
}
7980

80-
this.list.Add(appReg.Name, appReg);
81-
this.references.Add(appReg.Name, new List<DxfObject>());
81+
this.List.Add(appReg.Name, appReg);
82+
this.References.Add(appReg.Name, new DxfObjectReferences());
8283

8384
appReg.Owner = this;
8485

8586
appReg.NameChanged += this.Item_NameChanged;
8687

88+
Debug.Assert(!string.IsNullOrEmpty(appReg.Handle), "The application registry handle cannot be null or empty.");
8789
this.Owner.AddedObjects.Add(appReg.Handle, appReg);
8890

8991
return appReg;
@@ -123,14 +125,14 @@ public override bool Remove(ApplicationRegistry item)
123125
return false;
124126
}
125127

126-
if (this.references[item.Name].Count != 0)
128+
if (this.HasReferences(item))
127129
{
128130
return false;
129131
}
130132

131133
this.Owner.AddedObjects.Remove(item.Handle);
132-
this.references.Remove(item.Name);
133-
this.list.Remove(item.Name);
134+
this.References.Remove(item.Name);
135+
this.List.Remove(item.Name);
134136

135137
item.Handle = null;
136138
item.Owner = null;
@@ -151,12 +153,13 @@ private void Item_NameChanged(TableObject sender, TableObjectChangedEventArgs<st
151153
throw new ArgumentException("There is already another application registry with the same name.");
152154
}
153155

154-
this.list.Remove(sender.Name);
155-
this.list.Add(e.NewValue, (ApplicationRegistry) sender);
156+
this.List.Remove(sender.Name);
157+
this.List.Add(e.NewValue, (ApplicationRegistry) sender);
156158

157-
List<DxfObject> refs = this.references[sender.Name];
158-
this.references.Remove(sender.Name);
159-
this.references.Add(e.NewValue, refs);
159+
List<DxfObjectReference> refs = this.References[sender.Name].ToList();
160+
this.References.Remove(sender.Name);
161+
this.References.Add(e.NewValue, new DxfObjectReferences());
162+
this.References[e.NewValue].Add(refs);
160163
}
161164

162165
#endregion

netDxf/Collections/BlockRecords.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#region netDxf library licensed under the MIT License
22
//
33
// netDxf library
4-
// Copyright (c) 2019-2021 Daniel Carvajal (haplokuon@gmail.com)
4+
// Copyright (c) 2019-2023 Daniel Carvajal (haplokuon@gmail.com)
55
//
66
// Permission is hereby granted, free of charge, to any person obtaining a copy
77
// of this software and associated documentation files (the "Software"), to deal
@@ -25,6 +25,7 @@
2525

2626
using System;
2727
using System.Collections.Generic;
28+
using System.Diagnostics;
2829
using netDxf.Blocks;
2930
using netDxf.Entities;
3031
using netDxf.Tables;
@@ -69,7 +70,7 @@ internal override Block Add(Block block, bool assignHandle)
6970
throw new ArgumentNullException(nameof(block));
7071
}
7172

72-
if (this.list.TryGetValue(block.Name, out Block add))
73+
if (this.List.TryGetValue(block.Name, out Block add))
7374
{
7475
return add;
7576
}
@@ -79,16 +80,16 @@ internal override Block Add(Block block, bool assignHandle)
7980
this.Owner.NumHandles = block.AssignHandle(this.Owner.NumHandles);
8081
}
8182

82-
this.list.Add(block.Name, block);
83-
this.references.Add(block.Name, new List<DxfObject>());
83+
this.List.Add(block.Name, block);
84+
this.References.Add(block.Name, new DxfObjectReferences());
8485

8586
block.Layer = this.Owner.Layers.Add(block.Layer);
8687
this.Owner.Layers.References[block.Layer.Name].Add(block);
8788

8889
//for new block definitions configure its entities
8990
foreach (EntityObject entity in block.Entities)
9091
{
91-
this.Owner.AddEntityToDocument(entity, block, assignHandle);
92+
this.Owner.AddEntityToDocument(entity, assignHandle);
9293
}
9394

9495
//for new block definitions configure its attributes
@@ -106,6 +107,7 @@ internal override Block Add(Block block, bool assignHandle)
106107
block.AttributeDefinitionAdded += this.Block_AttributeDefinitionAdded;
107108
block.AttributeDefinitionRemoved += this.Block_AttributeDefinitionRemoved;
108109

110+
Debug.Assert(!string.IsNullOrEmpty(block.Handle), "The block handle cannot be null or empty.");
109111
this.Owner.AddedObjects.Add(block.Handle, block);
110112
this.Owner.AddedObjects.Add(block.Owner.Handle, block.Owner);
111113

@@ -146,7 +148,7 @@ public override bool Remove(Block item)
146148
return false;
147149
}
148150

149-
if (this.references[item.Name].Count != 0)
151+
if (this.HasReferences(item))
150152
{
151153
return false;
152154
}
@@ -167,8 +169,8 @@ public override bool Remove(Block item)
167169
}
168170

169171
this.Owner.AddedObjects.Remove(item.Handle);
170-
this.references.Remove(item.Name);
171-
this.list.Remove(item.Name);
172+
this.References.Remove(item.Name);
173+
this.List.Remove(item.Name);
172174

173175
item.Record.Handle = null;
174176
item.Record.Owner = null;
@@ -197,12 +199,13 @@ private void Item_NameChanged(TableObject sender, TableObjectChangedEventArgs<st
197199
throw new ArgumentException("There is already another block with the same name.");
198200
}
199201

200-
this.list.Remove(sender.Name);
201-
this.list.Add(e.NewValue, (Block) sender);
202+
this.List.Remove(sender.Name);
203+
this.List.Add(e.NewValue, (Block) sender);
202204

203-
List<DxfObject> refs = this.references[sender.Name];
204-
this.references.Remove(sender.Name);
205-
this.references.Add(e.NewValue, refs);
205+
List<DxfObjectReference> refs = this.GetReferences(sender.Name);
206+
this.References.Remove(sender.Name);
207+
this.References.Add(e.NewValue, new DxfObjectReferences());
208+
this.References[e.NewValue].Add(refs);
206209
}
207210

208211
private void Block_LayerChanged(Block sender, TableObjectChangedEventArgs<Layer> e)
@@ -215,7 +218,7 @@ private void Block_LayerChanged(Block sender, TableObjectChangedEventArgs<Layer>
215218

216219
private void Block_EntityAdded(TableObject sender, BlockEntityChangeEventArgs e)
217220
{
218-
this.Owner.AddEntityToDocument(e.Item, (Block) sender, string.IsNullOrEmpty(e.Item.Handle));
221+
this.Owner.AddEntityToDocument(e.Item, string.IsNullOrEmpty(e.Item.Handle));
219222
}
220223

221224
private void Block_EntityRemoved(TableObject sender, BlockEntityChangeEventArgs e)

0 commit comments

Comments
 (0)