Skip to content

Commit

Permalink
Start of 2D Packing Node following Issue #11
Browse files Browse the repository at this point in the history
  • Loading branch information
thomascorrie committed Nov 8, 2018
1 parent 059174a commit 4f7fa37
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
91 changes: 91 additions & 0 deletions Miscellany/ContainerPacking/PackingService/PackRectangle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Linq;
using CromulentBisgetti.ContainerPacking.Entities; //added to interact with ContainerPacking
using Autodesk.DesignScript.Runtime;

namespace Miscellany.ContainerPacking
{
/// <summary>
/// PackingService
/// </summary>
public static partial class PackingService
{
#region PackRectangle
/// <summary>
/// Packs in Two Dimensions, ignoring dim3 for the Items and Z for the Container
/// </summary>
/// <param name="container">Container</param>
/// <param name="itemsToPack">Items to pack</param>
/// <param name="algorithm">Algorithm ID</param>
/// <returns name="packedItems">Items that were successfully packed</returns>
/// <returns name="unpackedItems">Items that were not packed</returns>
/// <returns name="isCompletePack">Are all items packed?</returns>
/// <returns name="packTimeInMilliseconds">Total pack time</returns>
/// <returns name="percentContainerVolumePacked">Percentage of the container that is packed</returns>
/// <returns name="percentItemVolumePacked">Percentage of items packed</returns>
/// <search>
/// containerpacking
/// </search>
[MultiReturn(new[] { "packedItems", "unpackedItems", "isCompletePack", "packTimeInMilliseconds", "percentContainerVolumePacked", "percentItemVolumePacked" })]
public static Dictionary<string, object> PackRectangle(Miscellany.ContainerPacking.Entities.Container container, List<Miscellany.ContainerPacking.Entities.Item> itemsToPack, int algorithm = 1)
{
//Create CromulentBisgetti Container
Container con = ContainerToCB(container);

//Create CromulentBisgetti Items
List<Item> items = new List<Item>();
foreach (Miscellany.ContainerPacking.Entities.Item i in itemsToPack)
{
Item cbItem = ItemToCB(i);
items.Add(cbItem);
}

//Create list with single container
List<Container> containers = new List<Container> { con };

//Select algorithm using integer
List<int> algorithms = new List<int> { algorithm };

//Get container packing result
ContainerPackingResult containerPackingResult = CromulentBisgetti.ContainerPacking.PackingService.Pack(containers, items, algorithms).FirstOrDefault();

//Get the single algorthim packing result from the container packing result
AlgorithmPackingResult algorithmPackingResult = containerPackingResult.AlgorithmPackingResults.FirstOrDefault();
bool IsCompletePack = algorithmPackingResult.IsCompletePack;
int PackTimeInMilliseconds = Convert.ToInt32(algorithmPackingResult.PackTimeInMilliseconds); //Max limit of int32 for milliseconds is596 hours
double PercentContainerVolumePacked = Miscellany.Math.Functions.ToDouble(algorithmPackingResult.PercentContainerVolumePacked);
double PercentItemVolumePacked = Miscellany.Math.Functions.ToDouble(algorithmPackingResult.PercentItemVolumePacked);
//int BestFitOrientation = algorithmPackingResult.best

//Convert CromulentBisgetti items to Miscellany Items
//Packed Items
List<Miscellany.ContainerPacking.Entities.Item> itemsPacked = new List<Miscellany.ContainerPacking.Entities.Item>();
foreach (Item i in algorithmPackingResult.PackedItems)
{
Miscellany.ContainerPacking.Entities.Item mItem = ItemToMiscellany(i);
itemsPacked.Add(mItem);
}
//Unpacked Items
List<Miscellany.ContainerPacking.Entities.Item> itemsUnpacked = new List<Miscellany.ContainerPacking.Entities.Item>();
foreach (Item i in algorithmPackingResult.UnpackedItems)
{
Miscellany.ContainerPacking.Entities.Item mItem = ItemToMiscellany(i);
itemsUnpacked.Add(mItem);
}

//Return values
var d = new Dictionary<string, object>();
d.Add("packedItems", itemsPacked);
d.Add("unpackedItems", itemsUnpacked);
d.Add("isCompletePack", IsCompletePack);
d.Add("packTimeInMilliseconds", PackTimeInMilliseconds);
d.Add("percentContainerVolumePacked", PercentContainerVolumePacked);
d.Add("percentItemVolumePacked", PercentItemVolumePacked);
d.Add("orientation", PercentItemVolumePacked);
return d;
}

#endregion
}
}
1 change: 1 addition & 0 deletions Miscellany/Miscellany.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<Compile Include="ContainerPacking\PackingService\PackContainers.cs" />
<Compile Include="ContainerPacking\PackingService\PackContainersWithGroups.cs" />
<Compile Include="ContainerPacking\PackingService\PackContainersWithGroupsContinuously.cs" />
<Compile Include="ContainerPacking\PackingService\PackRectangle.cs" />
<Compile Include="List\List.cs" />
<Compile Include="Math\Functions.cs" />
<Compile Include="Geometry\Abstract\Abstract.cs" />
Expand Down

4 comments on commit 4f7fa37

@Vanman989
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! looking forward to the release

@Vanman989
Copy link

@Vanman989 Vanman989 commented on 4f7fa37 Feb 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Thomas, is this node ready to go? Do you have a link that would show me how to turn this code into a node with dynamo or do I require visual studio?

@thomascorrie
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Jeremy, it isn't ready yet as I've been waylaid by other things but I will get back to it. Apologies for the delay. I spent some time refactoring the main code but I haven't released that properly yet

@Vanman989
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries. Look forward to it :)

Please sign in to comment.