Exemplo n.º 1
0
        public static bool GetHSolutionBestCasePallet(
            List <ContentItem> items,
            PalletProperties palletProperties,
            HConstraintSetPallet constraintSet,
            Vector3D cameraPosition,
            bool showCotations, float fontSizeRatio, Size sz,
            ref int palletCount,
            ref string algorithm,
            ref byte[] imageBytes,
            ref string[] errors)
        {
            List <string> lErrors = new List <string>();

            try
            {
                var analysis = new HAnalysisPallet(null)
                {
                    Pallet        = palletProperties,
                    ConstraintSet = constraintSet,
                    Content       = items,
                };
                HSolver solver    = new HSolver();
                var     solutions = solver.BuildSolutions(analysis);

                // get first solution (Sharp3DBin packing)
                if (solutions.Count > 0)
                {
                    var sol = solutions[0];
                    algorithm   = sol.Algorithm;
                    palletCount = sol.SolItemCount;

                    var tileSize  = ImageTiling.TileSize(sz, palletCount);
                    var listTiles = new List <Bitmap>();

                    for (int i = 0; i < palletCount; ++i)
                    {
                        Graphics3DImage graphics = new Graphics3DImage(tileSize)
                        {
                            FontSizeRatio  = fontSizeRatio,
                            CameraPosition = cameraPosition,
                            ShowDimensions = showCotations
                        };
                        ViewerHSolution sv = new ViewerHSolution(sol, i);
                        sv.Draw(graphics, Transform3D.Identity);
                        graphics.Flush();
                        listTiles.Add(graphics.Bitmap);
                    }

                    var            bmp       = ImageTiling.TileImage(sz, listTiles);
                    ImageConverter converter = new ImageConverter();
                    imageBytes = (byte[])converter.ConvertTo(bmp, typeof(byte[]));
                }
                else
                {
                    lErrors.Add("No valid solution found");
                }
            }
            catch (Exception ex)
            {
                lErrors.Add(ex.Message);
            }

            errors = lErrors.ToArray();
            return(0 == lErrors.Count);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            string        outputDir = @"D:\GitHub\StackBuilder\Sources\Test\treeDiM.StackBuilder.Engine.Heterogeneous3D.Test\Output";
            DirectoryInfo directory = new DirectoryInfo(outputDir);

            directory.Empty();

            var palletProperties = new PalletProperties(null, "EUR2", 1200.0, 1000.0, 140.0)
            {
                Color  = Color.Yellow,
                Weight = 22.0
            };
            bool allowXY = false;

            var items = new List <ContentItem>
            {
                new ContentItem(new BoxProperties(null, 190, 200, 420, 10.0, Color.Red), 2)
                {
                    AllowedOrientations = new bool[] { allowXY, allowXY, true }
                },
                new ContentItem(new BoxProperties(null, 250, 200, 300, 10.0, Color.Blue), 1)
                {
                    AllowedOrientations = new bool[] { allowXY, allowXY, true }
                },
                new ContentItem(new BoxProperties(null, 250, 200, 250, 10.0, Color.Green), 1)
                {
                    AllowedOrientations = new bool[] { allowXY, allowXY, true }
                },
                new ContentItem(new BoxProperties(null, 250, 200, 290, 10.0, Color.Green), 1)
                {
                    AllowedOrientations = new bool[] { allowXY, allowXY, true }
                },
                new ContentItem(new BoxProperties(null, 80, 200, 210, 10.0, Color.White), 4)
                {
                    AllowedOrientations = new bool[] { allowXY, allowXY, true }
                },                                                                                                                                            // 9

                new ContentItem(new BoxProperties(null, 360, 460, 840, 10.0, Color.Purple), 1)
                {
                    AllowedOrientations = new bool[] { allowXY, allowXY, true }
                },
                new ContentItem(new BoxProperties(null, 160, 460, 100, 10.0, Color.AliceBlue), 2)
                {
                    AllowedOrientations = new bool[] { allowXY, allowXY, true }
                },
                new ContentItem(new BoxProperties(null, 160, 460, 320, 10.0, Color.Beige), 2)
                {
                    AllowedOrientations = new bool[] { allowXY, allowXY, true }
                },
                new ContentItem(new BoxProperties(null, 200, 300, 150, 10.0, Color.Brown), 1)
                {
                    AllowedOrientations = new bool[] { allowXY, allowXY, true }
                },
                new ContentItem(new BoxProperties(null, 200, 300, 690, 10.0, Color.Chartreuse), 1)
                {
                    AllowedOrientations = new bool[] { allowXY, allowXY, true }
                },                                                                                                                                                  // 7

                new ContentItem(new BoxProperties(null, 200, 300, 210, 10.0, Color.Cyan), 4)
                {
                    AllowedOrientations = new bool[] { allowXY, allowXY, true }
                },
                new ContentItem(new BoxProperties(null, 120, 300, 70, 10.0, Color.LightPink), 12)
                {
                    AllowedOrientations = new bool[] { allowXY, allowXY, true }
                },
                new ContentItem(new BoxProperties(null, 520, 600, 420, 10.0, Color.LightGray), 2)
                {
                    AllowedOrientations = new bool[] { allowXY, allowXY, true }, PriorityLevel = 0
                },
                new ContentItem(new BoxProperties(null, 260, 360, 210, 10.0, Color.Yellow), 4)
                {
                    AllowedOrientations = new bool[] { allowXY, allowXY, true }
                },
                new ContentItem(new BoxProperties(null, 260, 360, 840, 10.0, Color.Orange), 1)
                {
                    AllowedOrientations = new bool[] { allowXY, allowXY, true }
                }                                                                                                                                              // 23
            };

            var sortedItems = items.OrderBy(i => i.Pack.OuterDimensions.X * i.Pack.OuterDimensions.Y * i.Number).ToList();

            var constraintSet = new HConstraintSetPallet()
            {
                MaximumHeight = 1300.0
            };

            var analysis = new HAnalysisPallet(null)
            {
                Pallet        = palletProperties,
                ConstraintSet = constraintSet,
                Content       = sortedItems
            };
            HSolver solver    = new HSolver();
            var     solutions = solver.BuildSolutions(analysis);

            int solIndex = 0;

            foreach (var sol in solutions)
            {
                Vector3D[] orientations = new Vector3D[] { Graphics3D.Corner_0, Graphics3D.Corner_90, Graphics3D.Corner_180, Graphics3D.Corner_270 };
                for (int iOrientation = 0; iOrientation < 4; ++iOrientation)
                {
                    List <Bitmap> images = new List <Bitmap>();
                    for (int binIndex = 0; binIndex < sol.SolItemCount; ++binIndex)
                    {
                        Vector3D bbGlob      = sol.BBoxGlobal(binIndex).DimensionsVec;
                        Vector3D bbLoad      = sol.BBoxLoad(binIndex).DimensionsVec;
                        double   weightLoad  = sol.LoadWeight(binIndex);
                        double   weightTotal = sol.Weight(binIndex);


                        Graphics3DImage graphics = new Graphics3DImage(ImageTiling.TileSize(new Size(4000, 4000), sol.SolItemCount))
                        {
                            FontSizeRatio  = 0.02f,
                            CameraPosition = orientations[iOrientation],
                            ShowDimensions = false
                        };
                        ViewerHSolution sv = new ViewerHSolution(sol, binIndex);
                        sv.Draw(graphics, Transform3D.Identity);
                        graphics.Flush();
                        images.Add(graphics.Bitmap);
                    }
                    Bitmap gBmp           = ImageTiling.TileImage(new Size(2000, 2000), images);
                    string sbSolutionName = $"sol_{sol.Algorithm}_{solIndex}_{iOrientation}.png";
                    gBmp.Save(Path.Combine(outputDir, sbSolutionName));

                    Console.WriteLine($"Saving {sbSolutionName}...");
                }
                ++solIndex;
            }
        }