diff --git a/source/SixFourThree.BoxPacker.Tests/SixFourThree.BoxPacker.Tests.csproj b/source/SixFourThree.BoxPacker.Tests/SixFourThree.BoxPacker.Tests.csproj index e9fbb51..2f5c9a8 100644 --- a/source/SixFourThree.BoxPacker.Tests/SixFourThree.BoxPacker.Tests.csproj +++ b/source/SixFourThree.BoxPacker.Tests/SixFourThree.BoxPacker.Tests.csproj @@ -1,68 +1,79 @@ - - - - - Debug - AnyCPU - {2FA2C22B-A489-4D5B-8B90-FA5E5CAD4E43} - Library - Properties - SixFourThree.BoxPacker.Tests - SixFourThree.BoxPacker.Tests - v4.5 - 512 - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - ..\packages\NUnit.3.0.1\lib\net45\nunit.framework.dll - True - - - - - - - - - - - - - - - - - - - - - {99625C49-A44D-4A69-B50E-C872885ACA2E} - SixFourThree.BoxPacker - - - + + + + + + + Debug + AnyCPU + {2FA2C22B-A489-4D5B-8B90-FA5E5CAD4E43} + Library + Properties + SixFourThree.BoxPacker.Tests + SixFourThree.BoxPacker.Tests + v4.8 + 512 + + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\NUnit.3.13.3\lib\net45\nunit.framework.dll + + + + + + + + + + + + + + + + + + {99625c49-a44d-4a69-b50e-c872885aca2e} + SixFourThree.BoxPacker + + + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + --> \ No newline at end of file diff --git a/source/SixFourThree.BoxPacker.Tests/packages.config b/source/SixFourThree.BoxPacker.Tests/packages.config index b183023..1978917 100644 --- a/source/SixFourThree.BoxPacker.Tests/packages.config +++ b/source/SixFourThree.BoxPacker.Tests/packages.config @@ -1,4 +1,5 @@ - - - + + + + \ No newline at end of file diff --git a/source/SixFourThree.BoxPacker/Interfaces/IPacker.cs b/source/SixFourThree.BoxPacker/Interfaces/IPacker.cs index 7ca9796..3611216 100644 --- a/source/SixFourThree.BoxPacker/Interfaces/IPacker.cs +++ b/source/SixFourThree.BoxPacker/Interfaces/IPacker.cs @@ -37,8 +37,8 @@ public interface IPacker /// /// Add a pre-prepared set of boxes all at once /// - /// - void AddBoxes(BoxList items); + /// + void AddBoxes(BoxList boxes); /// /// Add a pre-prepared set of boxes all at once diff --git a/source/SixFourThree.BoxPacker/Packer.cs b/source/SixFourThree.BoxPacker/Packer.cs index 5d39e6a..335f30c 100644 --- a/source/SixFourThree.BoxPacker/Packer.cs +++ b/source/SixFourThree.BoxPacker/Packer.cs @@ -4,7 +4,11 @@ using System.Text; using System.Threading; using System.Threading.Tasks; +#if NET6_0_OR_GREATER +using Microsoft.Extensions.Logging; +#else using NLog; +#endif using SixFourThree.BoxPacker.Exceptions; using SixFourThree.BoxPacker.Helpers.Extensions; using SixFourThree.BoxPacker.Interfaces; @@ -14,7 +18,11 @@ namespace SixFourThree.BoxPacker { public class Packer : IPacker { +#if NET6_0_OR_GREATER + private readonly ILogger _logger; +#else private static Logger _logger; +#endif protected ItemList Items { get; set; } protected bool StartWithLargerBoxes { get; set; } @@ -39,24 +47,18 @@ public class Packer : IPacker /// protected Boolean CreateBoxesForOversizedItems { get; set; } - public Packer() - { - Items = new ItemList(); - Boxes = new BoxList(); - CreateBoxesForOversizedItems = false; - StartWithLargerBoxes = false; - _logger = LogManager.GetCurrentClassLogger(); - WidthPadding = 0.1; - LengthPadding = 0.1; - } - public Packer(Boolean createBoxesForOverizedItems = false, bool startWithLargerBoxes = false) { Items = new ItemList(); Boxes = new BoxList(); CreateBoxesForOversizedItems = createBoxesForOverizedItems; StartWithLargerBoxes = startWithLargerBoxes; +#if NET6_0_OR_GREATER + LoggerFactory lf = new (); + _logger = lf.CreateLogger(); +#else _logger = LogManager.GetCurrentClassLogger(); +#endif WidthPadding = 0.1; LengthPadding = 0.1; } @@ -79,7 +81,11 @@ public void AddItem(Item item, int quantity) Items.Insert(item); } +#if NET6_0_OR_GREATER + _logger.LogInformation("Added {quantity} x {Description} (id: {Id})", quantity, item.Description, item.Id); +#else _logger.Log(LogLevel.Info, String.Format("Added {0} x {1} (id: {2})", quantity, item.Description, item.Id)); +#endif } /// @@ -89,7 +95,7 @@ public void AddItem(Item item, int quantity) public void AddItems(ItemList items) { if (items == null) - throw new ArgumentNullException(); + throw new ArgumentNullException("items"); var tmpItems = items.GetContent().Cast(); foreach (var item in tmpItems) @@ -103,7 +109,7 @@ public void AddItems(ItemList items) public void AddItems(IList items) { if (items == null) - throw new ArgumentNullException(); + throw new ArgumentNullException("items"); foreach (var item in items) AddItem(item, 1); @@ -122,7 +128,11 @@ public void AddBox(Box box) Boxes.Insert(box); +#if NET6_0_OR_GREATER + _logger.LogInformation("Added box {Description}", box.Description); +#else _logger.Log(LogLevel.Info, String.Format("Added box {0}", box.Description)); +#endif } /// @@ -132,7 +142,7 @@ public void AddBox(Box box) public void AddBoxes(BoxList boxes) { if (boxes == null) - throw new ArgumentNullException(); + throw new ArgumentNullException("boxes"); var tmpBoxes = boxes.GetContent().Cast(); foreach (var box in tmpBoxes) @@ -146,7 +156,7 @@ public void AddBoxes(BoxList boxes) public void AddBoxes(IList boxes) { if (boxes == null) - throw new ArgumentNullException(); + throw new ArgumentNullException("boxes"); foreach (var box in boxes) AddBox(box); @@ -164,7 +174,11 @@ public PackedBoxList Pack() if (packedBoxes.GetCount() > 1) packedBoxes = RedistributeWeight(packedBoxes); +#if NET6_0_OR_GREATER + _logger.LogInformation("Packing completed, {Count} boxes", packedBoxes.GetCount()); +#else _logger.Log(LogLevel.Info, String.Format("Packing completed, {0} boxes", packedBoxes.GetCount())); +#endif return packedBoxes; } @@ -236,8 +250,13 @@ public PackedBoxList PackByVolume() OuterWidth = oversizedItem.Width }; Boxes.Insert(box); +#if NET6_0_OR_GREATER + _logger.LogDebug("Item {Description} (id: {Id}) is too large to fit into any box, creating custom box for it.", + oversizedItem.Description, oversizedItem.Id); +#else _logger.Log(LogLevel.Debug, "Item {0} (id: {1}) is too large to fit into any box, creating custom box for it.", oversizedItem.Description, oversizedItem.Id); +#endif } else { @@ -289,8 +308,11 @@ public PackedBoxList PackByVolume() public PackedBoxList RedistributeWeight(PackedBoxList originalPackedBoxList) { var targetWeight = originalPackedBoxList.GetMeanWeight(); - +#if NET6_0_OR_GREATER + _logger.LogDebug("Repacking for weight distribution, weight variance {WeightVariance}, target weight {targetWeight}", originalPackedBoxList.GetWeightVariance(), targetWeight); +#else _logger.Log(LogLevel.Debug, "Repacking for weight distribution, weight variance {0}, target weight {1}", originalPackedBoxList.GetWeightVariance(), targetWeight); +#endif var packedBoxes = new PackedBoxList(); var overWeightBoxes = new List(); @@ -312,8 +334,12 @@ public PackedBoxList RedistributeWeight(PackedBoxList originalPackedBoxList) // Keep moving items from most overweight box to most underweight box var tryRepack = false; - do { + do { +#if NET6_0_OR_GREATER + _logger.LogDebug("Boxes under/over target: {underWeightBoxes}/{overWeightBoxes}", underWeightBoxes.Count, overWeightBoxes.Count); +#else _logger.Log(LogLevel.Debug, "Boxes under/over target: {0}/{1}", underWeightBoxes.Count, overWeightBoxes.Count); +#endif for (var underWeightBoxIndex = 0; underWeightBoxIndex < underWeightBoxes.Count; underWeightBoxIndex++) { @@ -356,7 +382,11 @@ public PackedBoxList RedistributeWeight(PackedBoxList originalPackedBoxList) // Found an edge case in packing algorithm that *increased* box count if (newHeavierBoxes.GetCount() > 1) { +#if NET6_0_OR_GREATER + _logger.LogInformation("[REDISTRIBUTING WEIGHT] Abandoning redistribution, because new packing is less effciient than original"); +#else _logger.Log(LogLevel.Info, "[REDISTRIBUTING WEIGHT] Abandoning redistribution, because new packing is less effciient than original"); +#endif return originalPackedBoxList; } @@ -376,7 +406,12 @@ public PackedBoxList RedistributeWeight(PackedBoxList originalPackedBoxList) } } - MOVINGON: _logger.Log(LogLevel.Info, "Trying to repack"); + MOVINGON: +#if NET6_0_OR_GREATER + _logger.LogInformation("Trying to repack"); +#else + _logger.Log(LogLevel.Info, "Trying to repack"); +#endif } } while (tryRepack); @@ -394,7 +429,11 @@ public PackedBoxList RedistributeWeight(PackedBoxList originalPackedBoxList) /// public PackedBox PackIntoBox(Box box, ItemList items) { +#if NET6_0_OR_GREATER + _logger.LogDebug("[EVALUATING BOX] {Description}", box.Description); +#else _logger.Log(LogLevel.Debug, "[EVALUATING BOX] {0}", box.Description); +#endif var packedItems = new ItemList(); var remainingDepth = box.InnerDepth; @@ -416,9 +455,15 @@ public PackedBox PackIntoBox(Box box, ItemList items) continue; } +#if NET6_0_OR_GREATER + _logger.LogDebug("Evaluating item {0}", itemToPack.Description); + _logger.LogDebug("Remaining width: {0}, length: {1}, depth: {2}", remainingWidth, remainingLength, remainingDepth); + _logger.LogDebug("LayerWidth: {0}, layerLength: {1}, layerDepth: {2}", layerWidth, layerLength, layerDepth); +#else _logger.Log(LogLevel.Debug, "Evaluating item {0}", itemToPack.Description); _logger.Log(LogLevel.Debug, "Remaining width: {0}, length: {1}, depth: {2}", remainingWidth, remainingLength, remainingDepth); _logger.Log(LogLevel.Debug, "LayerWidth: {0}, layerLength: {1}, layerDepth: {2}", layerWidth, layerLength, layerDepth); +#endif var itemWidth = itemToPack.Width; var itemLength = itemToPack.Length; @@ -436,14 +481,22 @@ public PackedBox PackIntoBox(Box box, ItemList items) (itemWidth <= remainingWidth && !items.IsEmpty() && items.GetMax() == itemToPack && remainingLength >= 2 * itemLength)) { +#if NET6_0_OR_GREATER + _logger.LogDebug("Fits (better) unrotated."); +#else _logger.Log(LogLevel.Debug, "Fits (better) unrotated."); +#endif remainingLength -= itemLength; layerLength += itemLength; layerWidth = Math.Max(itemWidth, layerWidth); } else { +#if NET6_0_OR_GREATER + _logger.LogDebug("Fits (better) rotated."); +#else _logger.Log(LogLevel.Debug, "Fits (better) rotated."); +#endif remainingLength -= itemWidth; layerLength += itemWidth; layerWidth = Math.Max(itemLength, layerWidth); @@ -477,7 +530,11 @@ public PackedBox PackIntoBox(Box box, ItemList items) if (remainingWidth >= Math.Min(itemWidth, itemLength) && layerDepth > 0 && layerWidth > 0 && layerLength > 0) { +#if NET6_0_OR_GREATER + _logger.LogDebug("No more fit in lengthwise, resetting for new row."); +#else _logger.Log(LogLevel.Debug, "No more fit in lengthwise, resetting for new row."); +#endif remainingLength += layerLength; remainingWidth -= layerWidth; layerWidth = layerLength = 0; @@ -486,7 +543,11 @@ public PackedBox PackIntoBox(Box box, ItemList items) if (remainingLength < Math.Min(itemWidth, itemLength) || layerDepth == 0) { +#if NET6_0_OR_GREATER + _logger.LogDebug("Doesn't fit on layer even when empty."); +#else _logger.Log(LogLevel.Debug, "Doesn't fit on layer even when empty."); +#endif items.ExtractMax(); continue; } @@ -502,11 +563,19 @@ public PackedBox PackIntoBox(Box box, ItemList items) remainingDepth -= layerDepth; layerWidth = layerLength = layerDepth = 0; +#if NET6_0_OR_GREATER + _logger.LogDebug("Doesn't fit, so starting next vertical layer."); +#else _logger.Log(LogLevel.Debug, "Doesn't fit, so starting next vertical layer."); +#endif } } +#if NET6_0_OR_GREATER + _logger.LogDebug("Done with this box."); +#else _logger.Log(LogLevel.Debug, "Done with this box."); +#endif return new PackedBox(box, packedItems, remainingWidth, remainingLength, remainingDepth, remainingWeight); } diff --git a/source/SixFourThree.BoxPacker/Properties/AssemblyInfo.cs b/source/SixFourThree.BoxPacker/Properties/AssemblyInfo.cs deleted file mode 100644 index cd57320..0000000 --- a/source/SixFourThree.BoxPacker/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("SixFourThree.BoxPacker")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("SixFourThree.BoxPacker")] -[assembly: AssemblyCopyright("Copyright © 2016")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("ea8f7608-30e7-43cf-ac11-6ab6b6709e6f")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/source/SixFourThree.BoxPacker/SixFourThree.BoxPacker.csproj b/source/SixFourThree.BoxPacker/SixFourThree.BoxPacker.csproj index 5769ec4..d54ab3f 100644 --- a/source/SixFourThree.BoxPacker/SixFourThree.BoxPacker.csproj +++ b/source/SixFourThree.BoxPacker/SixFourThree.BoxPacker.csproj @@ -1,7 +1,6 @@ - - - + + net452;net48;net6.0 Debug AnyCPU {99625C49-A44D-4A69-B50E-C872885ACA2E} @@ -9,61 +8,48 @@ Properties SixFourThree.BoxPacker SixFourThree.BoxPacker - v4.5 - 512 + true + $(DefaultItemExcludes) + Library + True + 1.1.0 + + + none - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 + + none - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 + + none + + + none - + + ..\packages\NLog.4.2.3\lib\net45\NLog.dll + True + + ..\packages\NLog.4.2.3\lib\net45\NLog.dll True - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + - -