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("2-dimensional packing plan View")] [Content(typeof(PackingPlan), true)] public partial class PackingPlan2DView : NamedItemView { public PackingPlan2DView() { InitializeComponent(); } 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) { Redraw(); } else { for (int i = 0; i < Content.NrOfBins; i++) binSelection.Items.Add(i); Redraw(Content); } } private void Redraw() { packingPlan2D.InitializeContainer(0, 0); } private void Redraw(PackingPlan plan) { int currentBin = (binSelection != null && binSelection.SelectedItem != null) ? (int)(binSelection.SelectedItem) : 0; var bin = plan.BinPackings[currentBin].BinMeasures; packingPlan2D.InitializeContainer(bin.Width, bin.Height); foreach (var entry in plan.BinPackings[currentBin].ItemMeasures) { var position = plan.BinPackings[currentBin].ItemPositions[entry.Key]; packingPlan2D.AddItemToContainer( position.Rotated ? entry.Value.Height : entry.Value.Width, position.Rotated ? entry.Value.Width : entry.Value.Height, position.X, position.Y, entry.Key.ToString()); } } private void binSelection_SelectedIndexChanged(object sender, EventArgs e) { Redraw(Content); packingPlan2D.Refresh(); packingPlan2D.Refresh(); packingPlan2D.Refresh(); packingPlan2D.Refresh(); packingPlan2D.Refresh(); } private void Content_QualityChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Content_QualityChanged), sender, e); else { binSelection.Items.Clear(); if (Content == null) { Redraw(); } else { for (int i = 0; i < Content.NrOfBins; i++) binSelection.Items.Add(i); Redraw(Content); } packingPlan2D.Refresh(); packingPlan2D.Refresh(); packingPlan2D.Refresh(); packingPlan2D.Refresh(); packingPlan2D.Refresh(); } } } }