using System; using System.Windows.Forms; using HeuristicLab.Core.Views; using HeuristicLab.MainForm; using HeuristicLab.Encodings.PackingEncoding.PackingPlan; using HeuristicLab.Problems.BinPacking.Interfaces; using HeuristicLab.Problems.BinPacking.PackingItem; using HeuristicLab.Problems.BinPacking.PackingBin; using HeuristicLab.Problems.BinPacking.Dimensions; using HeuristicLab.Core; using HeuristicLab.Data; namespace HeuristicLab.Problems.BinPacking.Views { [View("3-dimensional packing plan View")] [Content(typeof(PackingPlan), true)] public partial class PackingPlan3DView : NamedItemView { public PackingPlan3DView() { InitializeComponent(); this.nameTextBox.Text = "Packing Plan"; } protected override void DeregisterContentEvents() { Content.QualityChanged -= new EventHandler(Content_QualityChanged); base.DeregisterContentEvents(); } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.QualityChanged += new EventHandler(Content_QualityChanged); } public new PackingPlan Content { get { return (PackingPlan)base.Content; } set { base.Content = value; } } protected override void OnContentChanged() { base.OnContentChanged(); binSelection.Items.Clear(); if (Content == null) { packingPlan3D.InitializeContainer(0, 0, 0); } else { Content.Name = "Packing Plan"; for (int i = 0; i < Content.NrOfBins; i++) binSelection.Items.Add(i); binSelection.SelectedIndex = 0; UpdateModel(); packingPlan3D.StartRendering(); } } private void UpdateModel() { int currentBin = (binSelection != null && binSelection.SelectedItem != null) ? (int)(binSelection.SelectedItem) : 0; var bin = Content.GetPackingBinMeasuresForBinNr(currentBin); packingPlan3D.InitializeContainer(bin.Width, bin.Height, bin.Depth); int i = 0; foreach (var item in Content.PackingItemMeasures) { var position = Content.PackingItemPositions[i]; if (position.AssignedBin == currentBin) packingPlan3D.AddItemToContainer( position.Rotated ? item.Depth : item.Width, item.Height, position.Rotated ? item.Width : item.Depth, position.X, position.Y, position.Z, i); i++; } } private void binSelection_SelectedIndexChanged(object sender, EventArgs e) { UpdateModel(); itemSelection.SelectedIndex = -1; itemSelection.Items.Clear(); int currentBin = (binSelection != null && binSelection.SelectedItem != null) ? (int)(binSelection.SelectedItem) : 0; for (int i = 0; i < Content.PackingItemPositions.Count; i++) { if (Content.PackingItemPositions[i].AssignedBin == currentBin) itemSelection.Items.Add(i); } packingPlan3D.Invalidate(); packingPlan3D.Update(); } private void itemSelection_SelectedIndexChanged(object sender, EventArgs e) { UpdateModel(); int selectedItem = -1; if ((itemSelection != null && itemSelection.SelectedItem != null) && Int32.TryParse(itemSelection.SelectedItem.ToString(), out selectedItem)) { packingPlan3D.SelectItem(selectedItem); } else packingPlan3D.UnselectItem(); packingPlan3D.Invalidate(); packingPlan3D.Update(); } private void Content_QualityChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_QualityChanged), sender, e); else { binSelection.Items.Clear(); if (Content == null) { packingPlan3D.InitializeContainer(0, 0, 0); } else { Content.Name = "Packing Plan"; for (int i = 0; i < Content.NrOfBins; i++) binSelection.Items.Add(i); binSelection.SelectedIndex = 0; UpdateModel(); packingPlan3D.StartRendering(); } } } } }