public static void SetCSysInfo(ref XElement x_setup, SetupInfo setup_info, string cut_stock_csys)
        {
            IEnumerable <XElement> rows = x_setup.Descendants("CSystem");
            XElement lastrow;

            if (rows.Count() == 0)
            {
                IEnumerable <XElement> csystems = x_setup.Descendants("CSystems");
                if (csystems.Count() == 0)
                {
                    x_setup.Add(
                        new XElement("CSystems"),
                        new XAttribute("Simulation", "off"),
                        new XElement("Machine")
                        );
                    csystems = x_setup.Descendants("CSystems");
                }
                csystems.Last().Add(
                    new XElement("CSystem",
                                 new XAttribute("Name", "Global"),
                                 new XAttribute("Type", "component"),
                                 new XAttribute("Visible", "all"),
                                 new XElement("Attach", "Attach"),
                                 new XElement("Position",
                                              new XAttribute("X", 0),
                                              new XAttribute("Y", 0),
                                              new XAttribute("Z", 0)),
                                 new XElement("Rotation",
                                              new XAttribute("I", 0),
                                              new XAttribute("J", 0),
                                              new XAttribute("K", 0))
                                 ));
                rows = x_setup.Descendants("CSystem");
            }
            lastrow = rows.Last();
            lastrow.AddAfterSelf(
                new XElement("CSystem",
                             new XAttribute("Name", setup_info.attach_workplane),
                             new XAttribute("Type", "component"),
                             new XAttribute("Visible", "all"),
                             new XElement("Attach", "Attach"),
                             new XElement("Position",
                                          new XAttribute("X", 0),
                                          new XAttribute("Y", 0),
                                          new XAttribute("Z", 0)),
                             new XElement("Rotation",
                                          new XAttribute("I", 0),
                                          new XAttribute("J", 0),
                                          new XAttribute("K", 0))
                             ));
            PowerMILLAutomation.ActivateWorkplane(setup_info.attach_workplane);
            foreach (string wp in setup_info.workplanes_to_export)
            {
                if (wp == setup_info.attach_workplane)
                {
                    continue;
                }
                WorkPlaneOrigin orig = MainModule.GetWorkplaneCoords(PowerMILLAutomation.oToken, wp, true, Properties.Resources.IDS_WorkplaneFailedToGet);
                lastrow.AddAfterSelf(
                    new XElement("CSystem",
                                 new XAttribute("Name", wp),
                                 new XAttribute("Type", "component"),
                                 new XAttribute("Visible", "all"),
                                 new XAttribute("Transition", (wp.Equals(cut_stock_csys, StringComparison.OrdinalIgnoreCase) ? "on" : "off")),
                                 new XElement("Attach", (!wp.Equals(cut_stock_csys) ? setup_info.attach_workplane_to : setup_info.block_attach_to)),
                                 new XElement("Position",
                                              new XAttribute("X", orig.dX),
                                              new XAttribute("Y", orig.dY),
                                              new XAttribute("Z", orig.dZ)),
                                 new XElement("Rotation",
                                              new XAttribute("I", orig.dXangle),
                                              new XAttribute("J", orig.dYangle),
                                              new XAttribute("K", orig.dZangle))
                                 ));
            }
        }
        /// <summary>
        /// Create the different Output Files
        /// </summary>
        /// <remarks>nxf File,stl, iso, ...</remarks>
        public static string CreateOutputFiles(ProjectData proj_data, string output_dir, PluginSettings plugin_settings)
        {
            string project_fpath = Path.Combine(output_dir, ProjectData.ProjectName + ".vcproject");

            XDocument main_doc;

            try
            {
                main_doc = XDocument.Load(proj_data.proj_template);
            }
            catch
            {
                Messages.ShowError(Properties.Resources.IDS_VericutTemplateReadFailure, proj_data.proj_template);
                return("*");
            }

            main_doc.Descendants("Setup").Remove();
            if (main_doc.Descendants("InfoUserName").Count() > 0)
            {
                main_doc.Descendants("InfoUserName").First().Value =
                    String.Format("PowerMill to Vericut plugin v{0}.{1}.{2}",
                                  System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.Major,
                                  System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.Minor,
                                  System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.Build);
            }

            for (int i = 0; i < proj_data.setup_infos.Count; i++)
            {
                XDocument doc;
                XElement  setup;
                SetupInfo setup_info = proj_data.setup_infos[i];

                PowerMILLAutomation.CreateTransformWorkplane("Transform_Vericut");
                PowerMILLAutomation.ActivateWorkplane(setup_info.attach_workplane);
                //WorkPlaneOrigin model_origin = MainModule.GetWorkplaneCoords(PowerMILLAutomation.oToken, setup_info.model_workplane, false, Properties.Resources.IDS_WorkplaneFailedToGet);
                WorkPlaneOrigin model_origin = MainModule.GetWorkplaneCoords(PowerMILLAutomation.oToken, "Transform_Vericut", true, Properties.Resources.IDS_WorkplaneFailedToGet);//Changed relation to true and look for the Transform position from the model location to align correctly in Vericut
                PowerMILLAutomation.DeleteTransformWorkplane("Transform_Vericut");


                string template = null;
                if (String.IsNullOrEmpty(setup_info.template) || !File.Exists(setup_info.template))
                {
                    template = proj_data.proj_template;
                }
                else
                {
                    template = setup_info.template;
                }
                doc = XDocument.Load(template);

                // Add path to items from the template
                IEnumerable <XElement> rows = doc.Descendants("File");
                foreach (XElement file in rows.ToList())
                {
                    string file_name = (file.FirstNode != null) ? file.FirstNode.ToString() : null;
                    if (!String.IsNullOrEmpty(file_name) && !String.IsNullOrEmpty(Path.GetExtension(file_name)) &&
                        String.IsNullOrEmpty(Path.GetDirectoryName(file_name)))
                    {
                        string full_path   = Path.Combine(Path.GetDirectoryName(template), file_name);
                        string new_element = file.ToString().Replace(file_name, full_path);

                        file.ReplaceWith(XElement.Parse(new_element));
                    }
                }

                setup = doc.Descendants("Setup").First();
                setup.Attribute("Name").Value = setup_info.name;

                PowerMILLExporter.Tools.Tools setup_tools;
                GetListOfTools(setup_info, plugin_settings, proj_data.output_dir, out setup_tools);
                setup_info.oTools = setup_tools;

                if (setup_info.tools_to_use == 0)
                {
                    EventLogger.WriteToEvengLog("Write tool info to tools.xml");
                    WriteTools.WriteToolsToXML(setup_info.oTools, output_dir, setup_info.name + "_tools.tls", plugin_settings.tool_id_use_num, plugin_settings.tool_id_use_name);

                    //output_dir, setup_info.name + "_tools.tls"
                    SetTools(ref setup, Path.Combine(output_dir, setup_info.name + "_tools.tls"));
                }
                else
                {
                    List <string> lib_tools     = GetListOfToolsFromLibrary(setup_info.tls_fpath);
                    List <string> missing_tools = WriteTools.FindToolsMissingFromLibrary(setup_info.oTools, lib_tools);
                    if (missing_tools.Count > 0)
                    {
                        if (setup_info.tools_to_use == 1)
                        {
                            Messages.ShowWarning(String.Format("Setup {0}: the following tools are missing from the library:\r\n {1}.", setup_info.name, String.Join(Environment.NewLine, missing_tools.ToArray())));
                        }
                        else if (setup_info.tools_to_use == 2)
                        {
                            EventLogger.WriteToEvengLog("Write tool info to tools.xml");
                            WriteTools.AppendToolsToXML(setup_info.oTools, output_dir, setup_info.tls_fpath, missing_tools);

                            //output_dir, setup_info.name + "_tools.tls"
                            SetTools(ref setup, Path.Combine(output_dir, setup_info.tls_fpath));
                        }
                    }
                }

                SetWorkOffsets(ref setup, setup_info);

                if (setup_info.tools_to_use == 1 || setup_info.tools_to_use == 2)
                {
                    SetNCPrograms(ref setup, setup_info, false, true);
                }
                else
                {
                    SetNCPrograms(ref setup, setup_info, plugin_settings.tool_id_use_num, plugin_settings.tool_id_use_name);
                }

                SetCSysInfo(ref setup, setup_info, proj_data.cut_stock_csys);

                /* Don't set block info for 2nd and other setups if using cut stock transition */
                if (i == 0 || String.IsNullOrEmpty(proj_data.cut_stock_csys))
                {
                    SetBlockInfo(ref setup, output_dir, setup_info, model_origin, plugin_settings.export_options.block_tol);
                }

                SetFixturesInfo(ref setup, output_dir, setup_info, model_origin, plugin_settings.export_options.fixture_tol);

                /* Don't set part info for 2nd and other setups if using cut stock transition */
                if (i == 0 || String.IsNullOrEmpty(proj_data.cut_stock_csys))
                {
                    SetPartsInfo(ref setup, output_dir, setup_info, model_origin, plugin_settings.export_options.model_tol);
                }

                if (i == 0)
                {
                    main_doc.Elements("VcProject").First().Add(setup);
                }
                else
                {
                    if (main_doc.Descendants("Setup").Count() > 0)
                    {
                        main_doc.Descendants("Setup").Last().AddAfterSelf(setup);
                    }
                }
            }

            main_doc.Save(project_fpath);


            return(project_fpath);
        }