#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 Joseph Helm and Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using HeuristicLab.Core.Views;
using HeuristicLab.MainForm;
using HeuristicLab.Encodings.PackingEncoding.PackingPlan;
using HeuristicLab.Problems.BinPacking.PackingItem;
using HeuristicLab.Problems.BinPacking.PackingBin;
using HeuristicLab.Problems.BinPacking.Dimensions;
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";
int i = 0;
foreach (var bp in Content.BinPackings)
binSelection.Items.Add(i++ + " (" + Math.Round(bp.PackingDensity * 100, 2) + "%)");
binSelection.SelectedIndex = 0;
UpdateModel();
packingPlan3D.StartRendering();
}
}
private void UpdateModel() {
int currentBin = (binSelection != null) ? (int)(binSelection.SelectedIndex) : 0;
var bin = Content.BinPackings[currentBin].BinMeasures;
packingPlan3D.InitializeContainer(bin.Width, bin.Height, bin.Depth);
foreach (var entry in Content.BinPackings[currentBin].ItemMeasures) {
var position = Content.BinPackings[currentBin].ItemPositions[entry.Key];
packingPlan3D.AddItemToContainer(
position.Rotated ? entry.Value.Depth : entry.Value.Width,
entry.Value.Height,
position.Rotated ? entry.Value.Width : entry.Value.Depth,
position.X, position.Y, position.Z, entry.Key, entry.Value.Material);
}
}
private void binSelection_SelectedIndexChanged(object sender, EventArgs e) {
UpdateModel();
itemSelection.SelectedIndex = -1;
itemSelection.Items.Clear();
int currentBin = (binSelection != null) ? (int)(binSelection.SelectedIndex) : 0;
//for (int i = 0; i < Content.PackingItemPositions.Count; i++) {
// if (Content.PackingItemPositions[i].AssignedBin == currentBin)
// itemSelection.Items.Add(i);
//}
foreach (var entry in Content.BinPackings[currentBin].ItemMeasures) {
itemSelection.Items.Add(entry.Key);
}
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";
int i = 0;
foreach (var bp in Content.BinPackings)
binSelection.Items.Add(i++ + " (" + Math.Round(bp.PackingDensity * 100, 2) + "%)");
binSelection.SelectedIndex = 0;
UpdateModel();
packingPlan3D.StartRendering();
}
}
}
}
}