public static void StartCaseOptimization(string name, double length, double width, double height)
        {
            FormDefineCaseOptimization form = new FormDefineCaseOptimization();
            form.BoxName = name;
            form.BoxLength = length;
            form.BoxWidth = width;
            form.BoxHeight = height;

            if (DialogResult.OK != form.ShowDialog())
                return;

            // create document
            Document doc = new Document(name, name + "_on_" + form.SelectedPallet.Name, "from extension component", DateTime.Now, null);
            // create case in document
            BoxProperties boxInDoc = doc.CreateNewBox(form.SelectedBox);
            // create pallet in document
            PalletProperties palletInDoc = doc.CreateNewPallet(form.SelectedPallet);
        }
        private bool GenerateProject(out Document doc, out CasePalletAnalysis analysis, out CasePalletSolution casePalletSolution)
        {
            doc = null;
            analysis = null;
            casePalletSolution = null;

            try
            {
                // build solution
                doc = new Document(
                    _boxName,
                    string.Format(Properties.Resources.ID_OPTDOCUMENTDESCRIPTION, _boxName),
                    string.Empty,
                    DateTime.Now,
                    null);
                // box
                BoxProperties boxProperties = doc.CreateNewBox(SelectedBox);
                // pallet
                PalletProperties palletProperties = doc.CreateNewPallet(SelectedPallet);
                // get selected caseOptimSolution
                CaseOptimSolution sol = SelectedSolution;
                PackArrangement arrangement = sol.CaseDefinition.Arrangement;
                // build new case name
                string arrangName = string.Format("{0}_{1}x{2}x{3}_{4}{5}"
                    , boxProperties.Name
                    , arrangement._iLength
                    , arrangement._iWidth
                    , arrangement._iHeight
                    , sol.CaseDefinition.Dim0
                    , sol.CaseDefinition.Dim1);
                // build new case description
                string description = string.Format(
                    Properties.Resources.ID_OPTCASEDESCRIPTION
                    , boxProperties.Name
                    , palletProperties.Name);
                // add new case
                CaseOfBoxesProperties caseProperties = doc.CreateNewCaseOfBoxes(
                    arrangName, description
                    , boxProperties
                    , sol.CaseDefinition
                    , BuildCaseOptimConstraintSet());
                // set color
                caseProperties.SetColor(Color.Chocolate);
                // add new pallet analysis
                string analysisName = string.Format(
                    Properties.Resources.ID_OPTANALYSISNAME
                    , boxProperties.Name
                    , boxProperties.Name
                    , arrangement._iLength
                    , arrangement._iWidth
                    , arrangement._iHeight
                    , sol.CaseDefinition.Dim0
                    , sol.CaseDefinition.Dim1);
                string analysisDescription = string.Format(
                    Properties.Resources.ID_OPTANALYSISDESCRIPTION
                    , boxProperties.Name
                    , palletProperties.Name);
                List<CasePalletSolution> palletSolutionList = new List<CasePalletSolution>();
                palletSolutionList.Add(sol.PalletSolution);
                analysis = doc.CreateNewCasePalletAnalysis(
                    analysisName
                    , analysisDescription
                    , caseProperties
                    , palletProperties
                    , null
                    , null
                    , null
                    , null
                    , null
                    , BuildPalletConstraintSet()
                    , palletSolutionList);
            }
            catch (Exception ex)
            {
                _log.Error(ex.ToString());
                return false;
            }
            return true;
        }
        public bool onFileNew(ref string fileName)
        {
            // INTEX data are in cms
            UnitsManager.CurrentUnitSystem = UnitsManager.UnitSystem.UNIT_METRIC2;

            string dbPath = Properties.Settings.Default.DatabasePathINTEX;
            if (string.IsNullOrWhiteSpace(dbPath) || !File.Exists(dbPath))
            {
                OpenFileDialog fd = new OpenFileDialog();
                fd.DefaultExt = "xls";
                fd.AddExtension = false;
                fd.Filter = "Microsoft Excel File (*.xls)|*.xls|All files (*.*)|*.*";
                fd.FilterIndex = 0;
                fd.RestoreDirectory = true;
                fd.CheckFileExists = true;
                if (DialogResult.OK != fd.ShowDialog())
                    return false;

                dbPath = fd.FileName;
                Properties.Settings.Default.DatabasePathINTEX = dbPath;
                Properties.Settings.Default.Save();
            }
            // load INTEX excel file
            List<DataItemINTEX> listItems = null;
            List<DataPalletINTEX> listPallets = null;
            List<DataCaseINTEX> listCases = null;
            try
            {
                // Set cursor as hourglass
                Cursor.Current = Cursors.WaitCursor;
                // load excel file
                if (!ExcelDataReader.ExcelDataReader.LoadIntexFile(dbPath, ref listItems, ref listPallets, ref listCases))
                {
                    Cursor.Current = Cursors.Default;
                    MessageBox.Show(
                        string.Format(Properties.Resources.ID_ERROR_INVALIDFILE, dbPath)
                        , Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    ex.Message
                    , Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                _log.Error(ex.Message);
            }
            finally
            { Cursor.Current = Cursors.Default; }
            // do we have a valid list
            if (null == listItems || 0 == listItems.Count)
                return false;
            // proceed : buid project file
            try
            {
                FormNewINTEX form = new FormNewINTEX();
                form._listItems = listItems;
                form._listPallets = listPallets;
                form._listCases = listCases;
                if (DialogResult.OK != form.ShowDialog())
                    return false;
                // create document
                DataItemINTEX item = form._currentItem;
                Document document = new Document(item._ref, item._description, "INTEX", DateTime.Now, null);
                // create box properties
                Color[] colorsCase = new Color[6];
                for (int i = 0; i < 6; ++i) {   colorsCase[i] = Color.Chocolate; }
                BoxProperties itemProperties = null;
                if (!form.UseIntermediatePacking)
                {
                    itemProperties = document.CreateNewCase(
                        item._ref
                        , string.Format("{0};EAN14 : {1};UPC : {2};PCB : {3}", item._description, item._gencode, item._UPC, item._PCB)
                        , UnitsManager.ConvertLengthFrom(item._length, UnitsManager.UnitSystem.UNIT_METRIC2)
                        , UnitsManager.ConvertLengthFrom(item._width, UnitsManager.UnitSystem.UNIT_METRIC2)
                        , UnitsManager.ConvertLengthFrom(item._height, UnitsManager.UnitSystem.UNIT_METRIC2)
                        , UnitsManager.ConvertLengthFrom(item._length - 0.6, UnitsManager.UnitSystem.UNIT_METRIC2)
                        , UnitsManager.ConvertLengthFrom(item._width - 0.6, UnitsManager.UnitSystem.UNIT_METRIC2)
                        , UnitsManager.ConvertLengthFrom(item._height - 0.6, UnitsManager.UnitSystem.UNIT_METRIC2)
                        , UnitsManager.ConvertMassFrom(item._weight, UnitsManager.UnitSystem.UNIT_METRIC2)
                        , colorsCase);
                }
                else
                {
                    itemProperties = document.CreateNewBox(
                        item._ref
                        , string.Format("{0};EAN14 : {1};UPC : {2};PCB : {3}", item._description, item._gencode, item._UPC, item._PCB)
                        , UnitsManager.ConvertLengthFrom(item._length, UnitsManager.UnitSystem.UNIT_METRIC2)
                        , UnitsManager.ConvertLengthFrom(item._width, UnitsManager.UnitSystem.UNIT_METRIC2)
                        , UnitsManager.ConvertLengthFrom(item._height, UnitsManager.UnitSystem.UNIT_METRIC2)
                        , UnitsManager.ConvertMassFrom(item._weight, UnitsManager.UnitSystem.UNIT_METRIC2)
                        , colorsCase);
                }
                itemProperties.ShowTape = true;
                itemProperties.TapeColor = Color.Beige;
                itemProperties.TapeWidth = 5.0;
                InsertPictogram(ref itemProperties);

                BoxProperties currentCase = null;
                if (form.UseIntermediatePacking)
                {
                    // create cases
                    foreach (DataCaseINTEX dataCase in listCases)
                    {
                        double lengthInt = dataCase._lengthInt > 0 ? dataCase._lengthInt : dataCase._lengthExt - 2* form.DefaultCaseThickness;
                        double widthInt = dataCase._widthInt > 0 ? dataCase._widthInt : dataCase._widthExt - 2 * form.DefaultCaseThickness;
                        double heightInt = dataCase._heightInt > 0 ? dataCase._heightInt : dataCase._heightExt - 2 * form.DefaultCaseThickness;

                        BoxProperties intercaseProperties = document.CreateNewCase(
                            dataCase._ref
                            , string.Format("{0:0.0}*{1:0.0}*{2:0.0}", dataCase._lengthExt, dataCase._widthExt, dataCase._heightExt)
                            , UnitsManager.ConvertLengthFrom(dataCase._lengthExt, UnitsManager.UnitSystem.UNIT_METRIC2)
                            , UnitsManager.ConvertLengthFrom(dataCase._widthExt, UnitsManager.UnitSystem.UNIT_METRIC2)
                            , UnitsManager.ConvertLengthFrom(dataCase._heightExt, UnitsManager.UnitSystem.UNIT_METRIC2)
                            , UnitsManager.ConvertLengthFrom(lengthInt, UnitsManager.UnitSystem.UNIT_METRIC2)
                            , UnitsManager.ConvertLengthFrom(widthInt, UnitsManager.UnitSystem.UNIT_METRIC2)
                            , UnitsManager.ConvertLengthFrom(heightInt, UnitsManager.UnitSystem.UNIT_METRIC2)
                            , UnitsManager.ConvertMassFrom(dataCase._weight, UnitsManager.UnitSystem.UNIT_METRIC2)
                            , colorsCase);
                        intercaseProperties.ShowTape = true;
                        intercaseProperties.TapeColor = Color.Beige;
                        intercaseProperties.TapeWidth = 5.0;

                        if (string.Equals( form._currentCase._ref, intercaseProperties.Name))
                            currentCase = intercaseProperties;
                    }
                }

                if (form.UseIntermediatePacking)
                {
                    // Case constraint set
                    BoxCaseConstraintSet boxCaseConstraintSet = new BoxCaseConstraintSet();
                    boxCaseConstraintSet.SetAllowedOrthoAxisAll();
                    if (boxCaseConstraintSet.IsValid)
                    {
                        // create case analysis
                        BoxCaseAnalysis analysis = document.CreateNewBoxCaseAnalysis(
                            string.Format(Properties.Resources.ID_PACKING, item._ref)
                            , item._description
                            , itemProperties
                            , currentCase
                            , boxCaseConstraintSet
                            , new BoxCaseSolver());
                    }
                }

                // create pallets
                PalletProperties currentPallet = null;
                foreach (DataPalletINTEX pallet in listPallets)
                {
                    PalletProperties palletProperties = document.CreateNewPallet(
                        pallet._type, pallet._type, "EUR"
                        , UnitsManager.ConvertLengthFrom(pallet._length, UnitsManager.UnitSystem.UNIT_METRIC2)
                        , UnitsManager.ConvertLengthFrom(pallet._width, UnitsManager.UnitSystem.UNIT_METRIC2)
                        , UnitsManager.ConvertLengthFrom(pallet._height, UnitsManager.UnitSystem.UNIT_METRIC2)
                        , UnitsManager.ConvertMassFrom(pallet._weight, UnitsManager.UnitSystem.UNIT_METRIC2));
                    if (string.Equals(form._currentPallet._type, pallet._type))
                        currentPallet = palletProperties;
                }

                // *** pallet analysis ***
                // constraint set
                CasePalletConstraintSet constraintSet = new CasePalletConstraintSet();
                constraintSet.UseMaximumHeight = true;
                constraintSet.MaximumHeight = UnitsManager.ConvertLengthFrom(form.PalletHeight, UnitsManager.UnitSystem.UNIT_METRIC2);
                constraintSet.SetAllowedOrthoAxis(HalfAxis.HAxis.AXIS_Z_P, true);
                constraintSet.AllowAlignedLayers = true;
                constraintSet.AllowAlternateLayers = true;
                constraintSet.SetAllowedPattern("Column");
                constraintSet.SetAllowedPattern("Diagonale");
                constraintSet.SetAllowedPattern("Interlocked");
                constraintSet.SetAllowedPattern("Trilock");
                constraintSet.SetAllowedPattern("Spirale");
                constraintSet.SetAllowedPattern("Enlarged spirale");
                if (constraintSet.IsValid)
                {
                    // create analysis
                    CasePalletSolver solver = new CasePalletSolver();
                    CasePalletAnalysis palletAnalysis = document.CreateNewCasePalletAnalysis(
                        item._ref, item.ToString()
                        , form.UseIntermediatePacking ? currentCase : itemProperties
                        , currentPallet,
                        null, null,
                        null, null, null,
                        constraintSet, solver);
                }
                // save document
                fileName = form.FilePath;
                document.Write(form.FilePath);
                // return true to let application open
                return File.Exists(fileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }
        private void Compute()
        {
            try
            {
                _document = new Document(_name, _name, "", DateTime.Now, null);
                _analysis = null;
                // bundle
                BundleProperties bundleProperties = _document.CreateNewBundle(_name, _name, FlatLength, FlatWidth, FlatThickness, FlatWeight, Color.Beige, NoFlats);
                // pallet
                PalletProperties palletProp = _document.CreateNewPallet(CurrentPallet);
                // constraint set
                BundlePalletConstraintSet constraintSet = new BundlePalletConstraintSet();
                constraintSet.OverhangX = 0.0;
                constraintSet.OverhangY = 0.0;
                constraintSet.UseMaximumNumberOfCases = false;
                constraintSet.UseMaximumPalletWeight = false;
                constraintSet.UseMaximumWeightOnBox = false;
                constraintSet.UseMaximumHeight = true;
                constraintSet.MaximumHeight = MaximumPalletHeight;
                constraintSet.AllowAlignedLayers = true;
                constraintSet.AllowAlternateLayers = true;
                constraintSet.AllowedPatternString = "Column,Diagonale,Interlocked,Trilock,Spirale";

                _analysis = _document.CreateNewCasePalletAnalysis(
                    _name, _name
                    ,bundleProperties , palletProp
                    , null /*interlayer */, null /* interlayerAntiSlip */
                    , null /* palletCorners */, null  /*palletCap */, null /* palletFilm */
                    , constraintSet
                    , new CasePalletSolver()
                    );
                // fill grid
                FillGrid();
            }
            catch (Exception ex)
            {
                _log.Error( ex.ToString());
            }
        }
        public static void StartPalletization(string name, double length, double width, double height)
        {
            // show analysis details dialog
            FormDefineAnalysis formDefinition = new FormDefineAnalysis();
            formDefinition.CaseName = name;
            formDefinition.CaseLength = length;
            formDefinition.CaseWidth = width;
            formDefinition.CaseHeight = height;
            formDefinition.Pallets = Pallets;

            if (DialogResult.OK != formDefinition.ShowDialog())
                return;

            // create document
            Document doc = new Document(name, name + "_on_" + formDefinition.Pallet.Name, "from extension component", DateTime.Now, null);
            // create case in document
            BoxProperties caseInDoc = doc.CreateNewCase(formDefinition.Case);
            // create pallet in document
            PalletProperties palletInDoc = doc.CreateNewPallet(formDefinition.Pallet);
            // create interlayer in document
            InterlayerProperties interlayerInDoc = formDefinition.Interlayer == null ? null : doc.CreateNewInterlayer(formDefinition.Interlayer);

            // solver
            CasePalletSolver solver = new CasePalletSolver();
            // build list of solutions
            CasePalletAnalysis analysis = doc.CreateNewCasePalletAnalysis(
                string.Format(Resources.ID_ANALYSISNAME, name, formDefinition.Pallet.Name)
                , string.Format(Resources.ID_ANALYSISDESCRIPTION, name, formDefinition.Pallet.Name)
                , caseInDoc
                , palletInDoc
                , interlayerInDoc
                , null
                , null
                , null
                , null
                , formDefinition.Constraints
                , solver);

            // select solution / generate report / generate StackBuilder file
            FormSelectSolution formSolutions = new FormSelectSolution(doc, analysis);
            if (DialogResult.OK == formSolutions.ShowDialog())
                return;
        }