private void Load_List_Click(object sender, RoutedEventArgs e)
        {
            listNCProgs.Items.Clear();

            List <string> ncprogs = PowerMILLAutomation.GetListOf(PowerMILLAutomation.enumEntity.NCPrograms);

            foreach (string ncprog in ncprogs)
            {
                listNCProgs.Items.Add(ncprog);
            }
        }
        private void LoadProjectComponents()
        {
            listNCProgs.Items.Clear();



            List <string> ncprogs = PowerMILLAutomation.GetListOf(PowerMILLAutomation.enumEntity.NCPrograms);

            foreach (string ncprog in ncprogs)
            {
                listNCProgs.Items.Add(ncprog);
            }
        }
        private void Generate_Click(object sender, RoutedEventArgs e)
        {
            List <string> NCProg_Tools = new List <string>();

            Microsoft.Office.Interop.Excel.Application oXL;
            _Workbook  oWB;
            _Worksheet oSheetProjectSummary     = null;
            _Worksheet oSheetProjectSummaryFull = null;
            _Worksheet oSheetNCProgSummary      = null;
            _Worksheet oSheetNewNCProgSummary   = null;
            _Worksheet oSheetToolpathDetails    = null;
            _Worksheet oSheetToolList           = null;
            _Worksheet oSheetNewToolList        = null;
            Dictionary <string, string> VarsListProjectSummary     = new Dictionary <string, string>();
            Dictionary <string, string> VarsListProjectSummaryFull = new Dictionary <string, string>();
            Dictionary <string, string> VarsListNCProgSummary      = new Dictionary <string, string>();
            Dictionary <string, string> VarsListToolpathDetails    = new Dictionary <string, string>();
            Dictionary <string, string> VarsListToolList           = new Dictionary <string, string>();
            bool   Has_ProjectSummary     = false;
            bool   Has_ProjectSummaryFull = false;
            bool   Has_NCProgSummary      = false;
            bool   Has_ToolList           = false;
            bool   Has_ToolpathDetails    = false;
            string Project_Path           = PowerMILLAutomation.ExecuteEx("print $project_pathname(0)");
            string NCProgDetails          = "";
            string MergedModelList        = "";
            int    FileQty = 0;

            if (listNCProgsSelected.Items.Count == 0)
            {
                //No NC Program selected, do nothing...
                MessageBox.Show("Please select at least one NC Program");
            }
            else
            {
                List <ToolInfo>     ToolData            = new List <ToolInfo>();
                List <ToolpathInfo> ToolpathData        = new List <ToolpathInfo>();
                List <ToolInfo>     ProjectToolData     = new List <ToolInfo>();
                List <ToolpathInfo> ProjectToolpathData = new List <ToolpathInfo>();
                ProjectInfo         Project             = new ProjectInfo();

                //Start Excel and get Application object.

                oXL               = new Microsoft.Office.Interop.Excel.Application();
                oXL.Visible       = true;
                oXL.DisplayAlerts = false;

                //Get the template from the option page
                string Path         = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\ExcellSetupSheet\\Template.ini";
                string TemplateFile = "";
                if (File.Exists(Path))
                {
                    const Int32 BufferSize = 128;
                    using (var fileStream = File.OpenRead(Path))
                        using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true, BufferSize))
                        {
                            String line;
                            while ((line = streamReader.ReadLine()) != null)
                            {
                                TemplateFile = line;
                            }
                        }
                }
                else
                {
                    Path = System.Reflection.Assembly.GetExecutingAssembly().CodeBase.Substring(8, System.Reflection.Assembly.GetExecutingAssembly().CodeBase.Length - 8);
                    string directory = System.IO.Path.GetDirectoryName(Path);
                    TemplateFile = directory + "\\Tool_List.xlsx";
                }

                if (!Directory.Exists(Project_Path + "\\Excel_Setupsheet\\"))
                {
                    Directory.CreateDirectory(Project_Path + "\\Excel_Setupsheet\\");
                }

                string[] fileArrayXLSX = Directory.GetFiles(Project_Path + "\\Excel_Setupsheet\\", "*.xlsx");
                string[] fileArrayXLS  = Directory.GetFiles(Project_Path + "\\Excel_Setupsheet\\", "*.xls");
                foreach (string File in fileArrayXLSX)
                {
                    if (File.IndexOf("~$") < 0)
                    {
                        FileQty = FileQty + 1;
                    }
                }

                foreach (string File in fileArrayXLS)
                {
                    if (File.IndexOf("~$") < 0)
                    {
                        FileQty = FileQty + 1;
                    }
                }

                if (FileQty > 0)
                {
                    File.Copy(TemplateFile, Project_Path + "\\Excel_Setupsheet\\SetupSheet_" + FileQty + ".xlsx");
                    TemplateFile = Project_Path + "\\Excel_Setupsheet\\SetupSheet_" + FileQty + ".xlsx";
                }
                else
                {
                    File.Copy(TemplateFile, Project_Path + "\\Excel_Setupsheet\\SetupSheet.xlsx");
                    TemplateFile = Project_Path + "\\Excel_Setupsheet\\SetupSheet.xlsx";
                }


                //Get a new workbook.
                oWB = (_Workbook)(oXL.Workbooks.Open(TemplateFile));

                List <string> ModelList = PowerMILLAutomation.GetListOf(PowerMILLAutomation.enumEntity.MachinableModels);
                PowerMILLAutomation.GetModelsLimits(ModelList, out double ModelsMinX, out double ModelsMinY, out double ModelsMinZ, out double ModelsMaxX, out double ModelsMaxY, out double ModelsMaxZ);

                Project = new ProjectInfo();
                Project.MachModelsMaxX = ModelsMaxX.ToString();
                Project.MachModelsMaxY = ModelsMaxY.ToString();
                Project.MachModelsMaxZ = ModelsMaxZ.ToString();
                Project.MachModelsMinX = ModelsMinX.ToString();
                Project.MachModelsMinY = ModelsMinY.ToString();
                Project.MachModelsMinZ = ModelsMinZ.ToString();
                Project.Name           = PowerMILLAutomation.ExecuteEx("print $project_pathname(1)");
                Project.Path           = PowerMILLAutomation.ExecuteEx("print $project_pathname(0)");
                Project.OrderNumber    = PowerMILLAutomation.ExecuteEx("print $project.orderNumber");
                Project.Programmer     = PowerMILLAutomation.ExecuteEx("print $project.programmer");
                Project.PartName       = PowerMILLAutomation.ExecuteEx("print $project.partname");
                Project.Customer       = PowerMILLAutomation.ExecuteEx("print $project.customer");
                Project.Date           = DateTime.Now.ToString();
                Project.Notes          = PowerMILLAutomation.ExecuteEx("print $project.notes");
                Project.ExcelTemplate  = TemplateFile;
                foreach (string Model in ModelList)
                {
                    MergedModelList = MergedModelList + Environment.NewLine + Model;
                }
                Project.ModelsList       = MergedModelList;
                Project.CombinedNCTPList = "";

                foreach (Worksheet worksheet in oWB.Worksheets)
                {
                    if (worksheet.Name == "Project_Summary")
                    {
                        oSheetProjectSummary = worksheet;
                        //Extract template keywords and cell adresses
                        VarsListProjectSummary = WriteFiles.ExtractTemplateData(oSheetProjectSummary);
                        Has_ProjectSummary     = true;
                    }
                    else if (worksheet.Name == "Project_Summary_Full")
                    {
                        oSheetProjectSummaryFull = worksheet;
                        //Extract template keywords and cell adresses
                        VarsListProjectSummaryFull = WriteFiles.ExtractTemplateData(oSheetProjectSummaryFull);
                        Has_ProjectSummaryFull     = true;
                    }
                    else if (worksheet.Name == "NCProg_Summary")
                    {
                        oSheetNCProgSummary = worksheet;
                        //Extract template keywords and cell adresses
                        VarsListNCProgSummary = WriteFiles.ExtractTemplateData(oSheetNCProgSummary);
                        Has_NCProgSummary     = true;
                    }
                    else if (worksheet.Name == "Toolpath_Details")
                    {
                        oSheetToolpathDetails = worksheet;
                        //Extract template keywords and cell adresses
                        VarsListToolpathDetails = WriteFiles.ExtractTemplateData(oSheetToolpathDetails);
                        Has_ToolpathDetails     = true;
                    }
                    else if (worksheet.Name == "ToolList")
                    {
                        oSheetToolList = worksheet;
                        //Extract template keywords and cell adresses
                        VarsListToolList = WriteFiles.ExtractTemplateData(oSheetToolList);
                        Has_ToolList     = true;
                    }
                }


                List <string> NCProg_Toolpaths = new List <string>();
                foreach (String NCProg in listNCProgsSelected.Items)
                {
                    if (NCProgDetails == "")
                    {
                        NCProgDetails = NCProg;
                    }
                    else
                    {
                        NCProgDetails = NCProgDetails + Environment.NewLine + NCProg;
                    }
                    NCProg_Toolpaths = PowerMILLAutomation.GetNCProgToolpathes(NCProg);
                    foreach (string Toolpath in NCProg_Toolpaths)
                    {
                        NCProgDetails = NCProgDetails + Environment.NewLine + "    " + Toolpath;
                    }
                    Project.TotalTime = Project.TotalTime + double.Parse(PowerMILLAutomation.ExecuteEx("print $entity('ncprogram';'" + NCProg + "').Statistics.TotalTime"));

                    //Extract the PowerMILL parameters found in the template from the current NCProgram toolapths
                    WriteFiles.ExtractData(NCProg, out ToolData, out ToolpathData);

                    if (Has_ProjectSummaryFull)
                    {
                        foreach (ToolpathInfo Toolpath in ToolpathData)
                        {
                            ProjectToolpathData.Add(new ToolpathInfo
                            {
                                Name               = Toolpath.Name,
                                Description        = Toolpath.Description,
                                Notes              = Toolpath.Notes,
                                ToolName           = Toolpath.ToolName,
                                ToolNumber         = Toolpath.ToolNumber,
                                ToolDiameter       = Toolpath.ToolDiameter,
                                ToolType           = Toolpath.ToolType,
                                ToolCutterLength   = Toolpath.ToolCutterLength,
                                ToolHolderName     = Toolpath.ToolHolderName,
                                ToolOverhang       = Toolpath.ToolOverhang,
                                ToolNumberOfFlutes = Toolpath.ToolNumberOfFlutes,
                                ToolLengthOffset   = Toolpath.ToolLengthOffset,
                                ToolRadOffset      = Toolpath.ToolRadOffset,
                                ToolpathType       = Toolpath.ToolpathType,
                                ToolTipRadius      = Toolpath.ToolTipRadius,
                                ToolDescription    = Toolpath.ToolDescription,
                                Thickness          = Toolpath.Thickness,
                                AxialThickness     = Toolpath.AxialThickness,
                                CutterComp         = Toolpath.CutterComp,
                                Feed               = Toolpath.Feed,
                                Speed              = Toolpath.Speed,
                                IPT               = Toolpath.IPT,
                                SFM               = Toolpath.SFM,
                                PlungeFeed        = Toolpath.PlungeFeed,
                                SkimFeed          = Toolpath.SkimFeed,
                                Coolant           = Toolpath.Coolant,
                                Stepover          = Toolpath.Stepover,
                                DOC               = Toolpath.DOC,
                                GeneralAxisType   = Toolpath.GeneralAxisType,
                                Statistic_Time    = Toolpath.Statistic_Time,
                                TPWorkplane       = Toolpath.TPWorkplane,
                                Tolerance         = Toolpath.Tolerance,
                                RapidHeight       = Toolpath.RapidHeight,
                                SkimHeight        = Toolpath.SkimFeed,
                                ToolpathMinX      = Toolpath.ToolpathMinX,
                                ToolpathMinY      = Toolpath.ToolpathMinY,
                                ToolpathMinZ      = Toolpath.ToolpathMinZ,
                                ToolpathMaxX      = Toolpath.ToolpathMaxX,
                                ToolpathMaxY      = Toolpath.ToolpathMaxY,
                                ToolpathMaxZ      = Toolpath.ToolpathMaxZ,
                                FirstLeadInType   = Toolpath.FirstLeadInType,
                                SecondLeadInType  = Toolpath.SecondLeadInType,
                                FirstLeadOutType  = Toolpath.FirstLeadOutType,
                                SecondLeadOutType = Toolpath.SecondLeadOutType,
                                CuttingDistance   = Toolpath.CuttingDistance,
                                NCProgName        = NCProg
                            });
                        }
                    }

                    if (Has_NCProgSummary)
                    {
                        //Write the Excel document
                        int Index = oSheetNCProgSummary.Index;
                        oSheetNCProgSummary.Copy(oSheetNCProgSummary, Type.Missing);

                        if (NCProg.Length > 31)
                        {
                            oWB.Sheets[Index].Name = NCProg.Replace("*", "").Substring(0, 30);
                        }
                        else
                        {
                            oWB.Sheets[Index].Name = NCProg;
                        }
                        oSheetNewNCProgSummary = oWB.Sheets[Index];

                        if (Has_ToolpathDetails)
                        {
                            WriteFiles.CreateExcelFile(NCProg, ToolpathData, ToolData, Project, oSheetNewNCProgSummary, VarsListNCProgSummary, oWB, true, Project_Path, oSheetNewNCProgSummary, NCProgDetails);
                        }
                        else
                        {
                            WriteFiles.CreateExcelFile(NCProg, ToolpathData, ToolData, Project, oSheetNewNCProgSummary, VarsListNCProgSummary, oWB, false, Project_Path, oSheetNewNCProgSummary, NCProgDetails);
                        }
                    }
                    if (Has_ToolpathDetails)
                    {
                        //Write the Excel document
                        WriteFiles.CreateExcelFile(NCProg, ToolpathData, ToolData, Project, oSheetToolpathDetails, VarsListToolpathDetails, oWB, false, Project_Path, oSheetNewNCProgSummary, NCProgDetails);
                    }
                    if (Has_ToolList)
                    {
                        //Write the Excel document
                        int Index = oSheetToolList.Index;
                        oSheetToolList.Copy(oSheetToolList, Type.Missing);
                        if (NCProg.Length > 25)
                        {
                            oWB.Sheets[Index].Name = NCProg.Replace("*", "").Substring(0, 24) + "-Tools";
                        }
                        else
                        {
                            oWB.Sheets[Index].Name = NCProg + "-Tools";
                        }
                        oWB.Sheets[Index].Name = NCProg + "-Tools";
                        oSheetNewToolList      = oWB.Sheets[Index];

                        WriteFiles.CreateExcelFile(NCProg, ToolpathData, ToolData, Project, oSheetNewToolList, VarsListToolList, oWB, false, Project_Path, oSheetNewNCProgSummary, NCProgDetails);
                    }
                }
                Project.CombinedNCTPList = NCProgDetails;
                if (Has_ProjectSummary)
                {
                    WriteFiles.CreateExcelFile("None", ToolpathData, ToolData, Project, oSheetProjectSummary, VarsListProjectSummary, oWB, true, Project_Path, oSheetProjectSummary, NCProgDetails);
                }
                if (Has_ProjectSummaryFull)
                {
                    WriteFiles.CreateExcelFile("None", ProjectToolpathData, ToolData, Project, oSheetProjectSummaryFull, VarsListProjectSummaryFull, oWB, true, Project_Path, oSheetProjectSummaryFull, NCProgDetails);
                }

                if (Has_NCProgSummary)
                {
                    oSheetNCProgSummary.Delete();
                }
                if (Has_ToolpathDetails)
                {
                    oSheetToolpathDetails.Delete();
                }
                if (Has_ToolList)
                {
                    oSheetToolList.Delete();
                }
                oXL.DisplayAlerts = true;
                oWB.Sheets[1].Activate();
                oWB.Save();
                MessageBox.Show("SetupSheet exported successfully");
            }
        }