diff --git a/OpenNest.Engine/BestFit/BestFitFilter.cs b/OpenNest.Engine/BestFit/BestFitFilter.cs new file mode 100644 index 0000000..3cffded --- /dev/null +++ b/OpenNest.Engine/BestFit/BestFitFilter.cs @@ -0,0 +1,46 @@ +using System.Collections.Generic; + +namespace OpenNest.Engine.BestFit +{ + public class BestFitFilter + { + public double MaxPlateWidth { get; set; } + public double MaxPlateHeight { get; set; } + public double MaxAspectRatio { get; set; } = 5.0; + public double MinUtilization { get; set; } = 0.3; + + public void Apply(List results) + { + foreach (var result in results) + { + if (!result.Keep) + continue; + + if (result.ShortestSide > System.Math.Min(MaxPlateWidth, MaxPlateHeight)) + { + result.Keep = false; + result.Reason = "Exceeds plate dimensions"; + continue; + } + + var aspect = result.LongestSide / result.ShortestSide; + + if (aspect > MaxAspectRatio) + { + result.Keep = false; + result.Reason = string.Format("Aspect ratio {0:F1} exceeds max {1}", aspect, MaxAspectRatio); + continue; + } + + if (result.Utilization < MinUtilization) + { + result.Keep = false; + result.Reason = string.Format("Utilization {0:P0} below minimum", result.Utilization); + continue; + } + + result.Reason = "Valid"; + } + } + } +} diff --git a/OpenNest.Engine/BestFit/Tiling/TileEvaluator.cs b/OpenNest.Engine/BestFit/Tiling/TileEvaluator.cs new file mode 100644 index 0000000..9a7ddb0 --- /dev/null +++ b/OpenNest.Engine/BestFit/Tiling/TileEvaluator.cs @@ -0,0 +1,62 @@ +using System.Collections.Generic; +using OpenNest.Geometry; +using OpenNest.Math; + +namespace OpenNest.Engine.BestFit.Tiling +{ + public class TileEvaluator + { + public TileResult Evaluate(BestFitResult bestFit, Plate plate) + { + var plateWidth = plate.Size.Width - plate.EdgeSpacing.Left - plate.EdgeSpacing.Right; + var plateHeight = plate.Size.Height - plate.EdgeSpacing.Top - plate.EdgeSpacing.Bottom; + + var result1 = TryTile(bestFit, plateWidth, plateHeight, false); + var result2 = TryTile(bestFit, plateWidth, plateHeight, true); + return result1.PartsNested >= result2.PartsNested ? result1 : result2; + } + + private TileResult TryTile(BestFitResult bestFit, double plateWidth, double plateHeight, bool rotatePair) + { + var pairWidth = rotatePair ? bestFit.BoundingHeight : bestFit.BoundingWidth; + var pairHeight = rotatePair ? bestFit.BoundingWidth : bestFit.BoundingHeight; + var spacing = bestFit.Candidate.Spacing; + + var cols = (int)System.Math.Floor((plateWidth + spacing) / (pairWidth + spacing)); + var rows = (int)System.Math.Floor((plateHeight + spacing) / (pairHeight + spacing)); + var pairsNested = cols * rows; + var partsNested = pairsNested * 2; + + var usedArea = partsNested * (bestFit.TrueArea / 2); + var plateArea = plateWidth * plateHeight; + + var placements = new List(); + + for (var row = 0; row < rows; row++) + { + for (var col = 0; col < cols; col++) + { + placements.Add(new PairPlacement + { + Position = new Vector( + col * (pairWidth + spacing), + row * (pairHeight + spacing)), + PairRotation = rotatePair ? Angle.HalfPI : 0 + }); + } + } + + return new TileResult + { + BestFit = bestFit, + PairsNested = pairsNested, + PartsNested = partsNested, + Rows = rows, + Columns = cols, + Utilization = plateArea > 0 ? usedArea / plateArea : 0, + Placements = placements, + PairRotated = rotatePair + }; + } + } +} diff --git a/OpenNest.Engine/BestFit/Tiling/TileResult.cs b/OpenNest.Engine/BestFit/Tiling/TileResult.cs new file mode 100644 index 0000000..a3f1181 --- /dev/null +++ b/OpenNest.Engine/BestFit/Tiling/TileResult.cs @@ -0,0 +1,23 @@ +using System.Collections.Generic; +using OpenNest.Geometry; + +namespace OpenNest.Engine.BestFit.Tiling +{ + public class TileResult + { + public BestFitResult BestFit { get; set; } + public int PairsNested { get; set; } + public int PartsNested { get; set; } + public int Rows { get; set; } + public int Columns { get; set; } + public double Utilization { get; set; } + public List Placements { get; set; } + public bool PairRotated { get; set; } + } + + public class PairPlacement + { + public Vector Position { get; set; } + public double PairRotation { get; set; } + } +} diff --git a/OpenNest.Engine/OpenNest.Engine.csproj b/OpenNest.Engine/OpenNest.Engine.csproj index d89ba41..b427b7c 100644 --- a/OpenNest.Engine/OpenNest.Engine.csproj +++ b/OpenNest.Engine/OpenNest.Engine.csproj @@ -41,7 +41,10 @@ + + +