private void Del_Picture_Click(object sender, RoutedEventArgs e)
        {
            if (listNCProgsSelected.Items.Count == 0)
            {
                MessageBox.Show("Please select at least 1 NC Program");
            }
            else
            {
                string NCProg      = listNCProgsSelected.Items[0].ToString();
                string PictureName = "";
                if (Toolpath_Pics.IsChecked == true)
                {
                    PictureName = Picture_List.Text;
                    ConvertToUsableName(PictureName, out PictureName);
                    PictureName = PowerMILLAutomation.ExecuteEx("print $project_pathname(0)") + "\\Excel_Setupsheet\\" + "TP-" + PictureName + ".png";
                }
                else if (NCProg_Pics.IsChecked == true)
                {
                    PictureName = NCProg + "-" + Picture_List.Text;
                    ConvertToUsableName(PictureName, out PictureName);
                    PictureName = PowerMILLAutomation.ExecuteEx("print $project_pathname(0)") + "\\Excel_Setupsheet\\" + "NCP-" + PictureName + ".png";
                }
                else if (Project_Pics.IsChecked == true)
                {
                    PictureName = Picture_List.Text;
                    ConvertToUsableName(PictureName, out PictureName);
                    PictureName = PowerMILLAutomation.ExecuteEx("print $project_pathname(0)") + "\\Excel_Setupsheet\\" + "PRJ-" + PictureName + ".png";
                }

                Setup_Picture.Source = null;
                File.Delete(PictureName);
            }
        }
        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 UpdatePicture()
        {
            string PictureName = "";
            string Path        = "";


            if (listNCProgsSelected.Items.Count > 0)
            {
                Path = PowerMILLAutomation.ExecuteEx("print $project_pathname(0)") + "\\Excel_Setupsheet\\";
                string NCProg = NCProg = listNCProgsSelected.Items[0].ToString();

                if (Toolpath_Pics.IsChecked == true)
                {
                    PictureName = Picture_List.Text;
                    ConvertToUsableName(PictureName, out PictureName);
                    PictureName = Path + "TP-" + PictureName + ".png";
                }
                else if (NCProg_Pics.IsChecked == true)
                {
                    PictureName = NCProg + "-" + Picture_List.Text;
                    ConvertToUsableName(PictureName, out PictureName);
                    PictureName = Path + "NCP-" + PictureName + ".png";
                }
                else if (Project_Pics.IsChecked == true)
                {
                    PictureName = Picture_List.Text;
                    ConvertToUsableName(PictureName, out PictureName);
                    PictureName = Path + "PRJ-" + PictureName + ".png";
                }

                if (File.Exists(PictureName))
                {
                    BitmapImage image = new BitmapImage();
                    using (var stream = File.OpenRead(PictureName))
                    {
                        image.BeginInit();
                        image.CacheOption  = BitmapCacheOption.OnLoad;
                        image.StreamSource = stream;
                        image.EndInit();
                    }
                    Setup_Picture.Source = image;
                }
                else
                {
                    Setup_Picture.Source = null;
                }
            }
        }
        //public ToolSheetPaneWPF()
        //{
        //    InitializeComponent();

        //}


        public ToolSheetPaneWPF(IPluginCommunicationsInterface comms)
        {
            InitializeComponent();

            oComm = comms;
            PowerMILLAutomation.SetVariables(oComm);

            // subscribe to some events
            comms.EventUtils.Subscribe(new EventSubscription("EntityCreated", EntityCreated));
            comms.EventUtils.Subscribe(new EventSubscription("EntityDeleted", EntityDeleted));
            comms.EventUtils.Subscribe(new EventSubscription("ProjectClosed", ProjectClosed));
            comms.EventUtils.Subscribe(new EventSubscription("ProjectOpened", ProjectOpened));
            comms.EventUtils.Subscribe(new EventSubscription("EntityRenamed", EntityRenamed));

            LoadProjectComponents();
        }
        private void Detailed_Checked(object sender, RoutedEventArgs e)
        {
            List <string> NCProg_Toolpaths = new List <string>();

            Picture_List.Items.Clear();
            if (listNCProgsSelected.Items.Count > 0)
            {
                foreach (String NCProg in listNCProgsSelected.Items)
                {
                    NCProg_Toolpaths = PowerMILLAutomation.GetNCProgToolpathes(NCProg);
                    for (int i = 0; i <= NCProg_Toolpaths.Count - 1; i++)
                    {
                        Picture_List.Items.Add(NCProg_Toolpaths[i]);
                    }
                }
            }
            Picture_List.SelectedIndex = 0;
            UpdatePicture();
        }
        public static List <string> GetNCProgToolpathes(string NCProgName)
        {
            List <string> sToolpathesList = new List <string>();

            // Get NC prog Infos
            PowerMILLAutomation.Execute("DIALOGS MESSAGE OFF");
            string sProgCN = PowerMILLAutomation.ExecuteEx("EDIT NCPROGRAM '" + NCProgName + "' LIST");

            PowerMILLAutomation.Execute("DIALOGS MESSAGE ON");

            // Split the Line
            string[] sTabCNInfos = Regex.Split(sProgCN, Environment.NewLine);// sProgCN.Split(Convert.ToChar("\n\r"));

            // Add NC prog to the List
            for (int i = 3; i <= sTabCNInfos.Length - 3; i++)
            {
                if (!string.IsNullOrEmpty(sTabCNInfos[i].Trim()) && sTabCNInfos[i].IndexOf(".pmoptz") < 0 && !string.IsNullOrEmpty(sTabCNInfos[i].Trim()) && sTabCNInfos[i].IndexOf(".pmopt") < 0 && !string.IsNullOrEmpty(sTabCNInfos[i].Trim()) && sTabCNInfos[i].IndexOf(".opt") < 0)
                {
                    sToolpathesList.Add(sTabCNInfos[i]);
                }
            }

            return(sToolpathesList);
        }
        public static List <string> GetListOf(enumEntity Entity)
        {
            List <string> oList = new List <string>();

            switch (Entity)
            {
            case enumEntity.MachineTools:
            {
                foreach (PowerMILL.MachineTool oEntity in oPServices.Project.MachineTools)
                {
                    oList.Add(oEntity.Name);
                }
                return(oList);
            }

            case enumEntity.NCPrograms:
            {
                foreach (PowerMILL.NCProgram oEntity in oPServices.Project.NCPrograms)
                {
                    oList.Add(oEntity.Name);
                }
                return(oList);
            }

            case enumEntity.Toolpaths:
            {
                foreach (PowerMILL.Toolpath oEntity in oPServices.Project.Toolpaths)
                {
                    oList.Add(oEntity.Name);
                }
                return(oList);
            }

            case enumEntity.Tools:
            {
                foreach (PowerMILL.Tool oEntity in oPServices.Project.Tools)
                {
                    oList.Add(oEntity.Name);
                }
                return(oList);
            }

            case enumEntity.Boundaries:
            {
                foreach (PowerMILL.Boundary oEntity in oPServices.Project.Boundaries)
                {
                    oList.Add(oEntity.Name);
                }
                return(oList);
            }

            case enumEntity.Patterns:
            {
                foreach (PowerMILL.Pattern oEntity in oPServices.Project.Patterns)
                {
                    oList.Add(oEntity.Name);
                }
                return(oList);
            }

            case enumEntity.FeatureSets:
            {
                foreach (PowerMILL.FeatureSet oEntity in oPServices.Project.FeatureSets)
                {
                    oList.Add(oEntity.Name);
                }
                return(oList);
            }

            case enumEntity.Workplanes:
            {
                foreach (PowerMILL.Workplane oEntity in oPServices.Project.Workplanes)
                {
                    oList.Add(oEntity.Name);
                }
                return(oList);
            }

            case enumEntity.Models:
            {
                foreach (PowerMILL.Model oEntity in oPServices.Project.Models)
                {
                    oList.Add(oEntity.Name);
                }
                return(oList);
            }

            case enumEntity.MachinableModels:
            {
                foreach (PowerMILL.Model oEntity in oPServices.Project.Models)
                {
                    string isReference = PowerMILLAutomation.ExecuteEx("print $entity('model';'" + oEntity.Name + "').IsReferenceModel");
                    if (isReference == "0")
                    {
                        oList.Add(oEntity.Name);
                    }
                }
                return(oList);
            }

            case enumEntity.StockModels:
            {
                foreach (PowerMILL.StockModel oEntity in oPServices.Project.StockModels)
                {
                    oList.Add(oEntity.Name);
                }
                return(oList);
            }

            default:
            {
                throw new Exception("Unknown entity (GetListOf): " + Entity.ToString());
            }
            }
        }
        public static void GetModelsLimits(List <string> ModelList, out double MinX, out double MinY, out double MinZ, out double MaxX, out double MaxY, out double MaxZ)
        {
            bool FoundX = false;
            bool FoundY = false;

            // Get NC prog Infos
            PowerMILLAutomation.Execute("DIALOGS MESSAGE OFF");

            MinX = 10000;
            MinY = 10000;
            MinZ = 10000;
            MaxX = -10000;
            MaxY = -10000;
            MaxZ = -10000;

            foreach (string Model in ModelList)
            {
                string sSize = PowerMILLAutomation.ExecuteEx("SIZE MODEL '" + Model + "'");

                // Split the Line
                string[] sSizeInfos = Regex.Split(sSize, Environment.NewLine);// sProgCN.Split(Convert.ToChar("\n\r"));

                string[] sMinInfos = sSizeInfos[2].Split(new Char[] { ' ' });
                string[] sMaxInfos = sSizeInfos[3].Split(new Char[] { ' ' });
                string   Temp_Dim  = "";
                FoundX = false;
                FoundY = false;

                for (int i = 0; i <= sMinInfos.Length - 1; i++)
                {
                    if (!string.IsNullOrEmpty(sMinInfos[i].Trim()) && sMinInfos[i].Trim() != "Minimum:")
                    {
                        if (double.Parse(sMinInfos[i].Trim()) < MinX && FoundX == false)
                        {
                            MinX   = double.Parse(sMinInfos[i].Trim());
                            FoundX = true;
                        }
                        else if (double.Parse(sMinInfos[i].Trim()) < MinY && FoundX && FoundY == false)
                        {
                            MinY   = double.Parse(sMinInfos[i].Trim());
                            FoundY = true;
                        }
                        else if (double.Parse(sMinInfos[i].Trim()) < MinZ && FoundX && FoundY)
                        {
                            MinZ = double.Parse(sMinInfos[i].Trim());
                        }
                    }
                }

                FoundX = false;
                FoundY = false;
                for (int i = 0; i <= sMaxInfos.Length - 1; i++)
                {
                    if (!string.IsNullOrEmpty(sMaxInfos[i].Trim()) && sMaxInfos[i].Trim() != "Maximum:")
                    {
                        if (double.Parse(sMaxInfos[i].Trim()) > MaxX && FoundX == false)
                        {
                            MaxX   = double.Parse(sMaxInfos[i].Trim());
                            FoundX = true;
                        }
                        else if (double.Parse(sMaxInfos[i].Trim()) > MaxY && FoundX && FoundY == false)
                        {
                            MaxY   = double.Parse(sMaxInfos[i].Trim());
                            FoundY = true;
                        }
                        else if (double.Parse(sMaxInfos[i].Trim()) > MaxZ && FoundX && FoundY)
                        {
                            MaxZ = double.Parse(sMaxInfos[i].Trim());
                        }
                    }
                }
            }



            PowerMILLAutomation.Execute("DIALOGS MESSAGE ON");
        }
        public static void GetToolpathLimits(string Toolpath, out double MinX, out double MinY, out double MinZ, out double MaxX, out double MaxY, out double MaxZ)
        {
            char Appos = (char)34;

            // Get NC prog Infos
            PowerMILLAutomation.Execute("DIALOGS MESSAGE OFF");
            string sSize = PowerMILLAutomation.ExecuteEx("SIZE TOOLPATH " + Appos + Toolpath + Appos);

            PowerMILLAutomation.Execute("DIALOGS MESSAGE ON");

            // Split the Line
            string[] sSizeInfos = Regex.Split(sSize, Environment.NewLine);// sProgCN.Split(Convert.ToChar("\n\r"));

            string[] sMinInfos = sSizeInfos[1].Split(new Char[] { ' ' });
            string[] sMaxInfos = sSizeInfos[2].Split(new Char[] { ' ' });

            MinX = 0;
            MinY = 0;
            MinZ = 0;

            for (int i = 0; i <= sMinInfos.Length - 1; i++)
            {
                if (!string.IsNullOrEmpty(sMinInfos[i].Trim()) && sMinInfos[i].Trim() != "Minimum:")
                {
                    if (MinX == 0)
                    {
                        MinX = double.Parse(sMinInfos[i].Trim());
                    }
                    else if (MinY == 0)
                    {
                        MinY = double.Parse(sMinInfos[i].Trim());
                    }
                    else if (MinZ == 0)
                    {
                        MinZ = double.Parse(sMinInfos[i].Trim());
                    }
                }
            }

            MaxX = 0;
            MaxY = 0;
            MaxZ = 0;

            for (int i = 0; i <= sMaxInfos.Length - 1; i++)
            {
                if (!string.IsNullOrEmpty(sMaxInfos[i].Trim()) && sMaxInfos[i].Trim() != "Maximum:")
                {
                    if (MaxX == 0)
                    {
                        MaxX = double.Parse(sMaxInfos[i].Trim());
                    }
                    else if (MaxY == 0)
                    {
                        MaxY = double.Parse(sMaxInfos[i].Trim());
                    }
                    else if (MaxZ == 0)
                    {
                        MaxZ = double.Parse(sMaxInfos[i].Trim());
                    }
                }
            }
        }
        private void Get_Picture_Click(object sender, RoutedEventArgs e)
        {
            string New_PictureName = "";
            string Path            = PowerMILLAutomation.ExecuteEx("print $project_pathname(0)") + "\\SetupSheets_files\\snapshots";

            if (listNCProgsSelected.Items.Count == 0)
            {
                MessageBox.Show("Please select at least 1 NC Program");
            }
            else
            {
                string NCProg = NCProg = listNCProgsSelected.Items[0].ToString();

                PowerMILLAutomation.ExecuteEx("KEEP SNAPSHOT NCPROGRAM '" + NCProg + "' CURRENT");

                FileInfo Picture = GetNewestFile(new DirectoryInfo(@Path));

                Path = PowerMILLAutomation.ExecuteEx("print $project_pathname(0)") + "\\Excel_Setupsheet";

                bool DirectoryExist = Directory.Exists(Path);

                if (DirectoryExist == false)
                {
                    Directory.CreateDirectory(Path);
                }

                Path = Path + "//";

                if (Toolpath_Pics.IsChecked == true)
                {
                    New_PictureName = Picture_List.Text;
                    ConvertToUsableName(New_PictureName, out New_PictureName);
                    New_PictureName = Path + "TP-" + New_PictureName + ".png";
                }
                else if (NCProg_Pics.IsChecked == true)
                {
                    New_PictureName = NCProg + "-" + Picture_List.Text;
                    ConvertToUsableName(New_PictureName, out New_PictureName);
                    New_PictureName = Path + "NCP-" + New_PictureName + ".png";
                }
                else if (Project_Pics.IsChecked == true)
                {
                    New_PictureName = Picture_List.Text;
                    ConvertToUsableName(New_PictureName, out New_PictureName);
                    New_PictureName = Path + "PRJ-" + New_PictureName + ".png";
                }


                if (File.Exists(New_PictureName))
                {
                    Setup_Picture.Source = null;
                    File.Delete(New_PictureName);
                }
                File.Move(Picture.FullName, New_PictureName);

                BitmapImage image = new BitmapImage();
                using (var stream = File.OpenRead(New_PictureName))
                {
                    image.BeginInit();
                    image.CacheOption  = BitmapCacheOption.OnLoad;
                    image.StreamSource = stream;
                    image.EndInit();
                }
                Setup_Picture.Source = image;
            }
        }
        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");
            }
        }