コード例 #1
0
ファイル: Postprocess.cs プロジェクト: tourist1029/nxopenC-
    //------------------------------------------------------------------------------
    //Callback Name: update_cb
    //------------------------------------------------------------------------------
    public int update_cb(NXOpen.BlockStyler.UIBlock block)
    {
        try
        {
            if (block == list_postName)
            {
                //---------Enter your code here-----------
            }
            else if (block == nativeFileBrowser_post)
            {
                //---------Enter your code here-----------

                string env_path      = Environment.GetEnvironmentVariable("UGII_BASE_DIR"); //读取环境变量中NX的安装目录
                string env_post_path = env_path + "\\MACH\\resource\\postprocessor\\";      //进一步寻找后处理文件所在位置
                //theUfSession.Ui.DisplayMessage("测试环境变量的路径: " + env_post_path , 1);
                string post_template = env_post_path + "template_post.dat";
                //theUfSession.Ui.DisplayMessage(post_template, 1);



                string   using_path       = nativeFileBrowser_post.Path; //输出整个选择的绝对路径
                string[] post_all         = using_path.Split('\\');      //通过‘\’将绝对路径进行分割
                int      co               = post_all.Length;             //  取得分割后字符串数组的个数
                string   post_usr_all     = post_all[co - 1];            //去最后一个字符数组中的字符
                string[] post_usr_all_cut = post_usr_all.Split('.');     //用'.'进行最后文件名的分割
                string   post_usr_name    = post_usr_all_cut[0];         //去'.'分割的第一部分,即为我们要用的文件名

                string[] add_post_name = new string[1];
                add_post_name[0] = post_usr_name;
                list_postName.SetListItems(add_post_name);
                listPostNames.Add(post_usr_name);
                string[] temparray = (string[])listPostNames.ToArray(typeof(string));
                list_postName.SetListItems(temparray);



                //先读取模板文件,按条读取,然后查重
                //theUfSession.Ui.DisplayMessage(post_template, 1);
                StreamReader sr      = new StreamReader(post_template, System.Text.Encoding.Default);
                int          findstr = 0;

                while (sr.Peek() > -1)
                {
                    string sk = sr.ReadLine();

                    //theUfSession.Ui.DisplayMessage(sk, 1);
                    //st.WriteLine(sk);
                    int indexofcoma = sk.IndexOf(","); //加入判断,只有该举有“,”存在才进行分割,并对findstr++;
                    if (indexofcoma > -1)
                    {
                        string[] splitstr = sk.Split(',');
                        if (splitstr[0] == post_usr_name)
                        {
                            findstr++;
                        }
                        else
                        {
                            continue;
                        }
                    }

                    //theUfSession.Ui.DisplayMessage(splitstr[0], 1);
                    //theUfSession.Ui.DisplayMessage((post_usr_name, 1);
                }
                sr.Close();

                //theUfSession.Ui.DisplayMessage(findstr.ToString(), 1);
                StreamWriter sw = new StreamWriter(post_template, true, System.Text.Encoding.UTF8);
                if (findstr == 0)
                {
                    sw.WriteLine(post_usr_name + "," + "${UGII_CAM_POST_DIR}" + post_usr_name + ".tcl" + "," + "${UGII_CAM_POST_DIR}" + post_usr_name + ".def");
                }

                sw.Close();


                //向模板文件写入
                //using (StreamWriter sw = new StreamWriter(post_template, true, System.Text.Encoding.UTF8))
                //{
                //    //先进行判断是否有重复的项,有重复的部需要重写

                //    sw.WriteLine(post_usr_name + "," + "${UGII_CAM_POST_DIR}" + post_usr_name + ".tcl" + "," + "${UGII_CAM_POST_DIR}" + post_usr_name + ".def");
                //    sw.Close();
                //}

                //复制后处理相关文件到NX后处理默认目录
                string post_file_path = nativeFileBrowser_post.Path; //后处理文件所在位置
                //theUfSession.Ui.DisplayMessage("后处理所在位置: " + post_file_path, 1);
                int    laindex         = post_file_path.LastIndexOf(post_usr_name + "." + "pui");
                string using_post_path = post_file_path.Substring(0, laindex);
                //theUfSession.Ui.DisplayMessage("用来复制的路径" + using_post_path, 1);

                string sc1 = using_post_path + post_usr_name + ".tcl";
                string sc2 = using_post_path + post_usr_name + ".def";
                string sc3 = using_post_path + post_usr_name + ".pui";

                string aim_path = env_post_path;  //要复制到的路径
                string ncf1     = aim_path + post_usr_name + ".tcl";
                string ncf2     = aim_path + post_usr_name + ".def";
                string ncf3     = aim_path + post_usr_name + ".pui";
                //合成复制文件路径
                var ncf1_path = Path.Combine(ncf1);
                var ncf2_path = Path.Combine(ncf2);
                var ncf3_path = Path.Combine(ncf3);

                //复制文件
                File.Copy(sc1, ncf1_path, true);
                File.Copy(sc2, ncf2_path, true);
                File.Copy(sc3, ncf3_path, true);
            }
            else if (block == string_outfile)
            {
                //---------Enter your code here-----------
            }
            else if (block == string_lastName)
            {
                //---------Enter your code here-----------
            }
            else if (block == nativeFolderBrowser_outpath)
            {
                //---------Enter your code here-----------

                string_outfile.Value = nativeFolderBrowser_outpath.Path;
            }
            else if (block == string_Unit)
            {
                //---------Enter your code here-----------
            }
            else if (block == toggle_showIt)
            {
                //---------Enter your code here-----------
            }
            else if (block == list_ncfiles)
            {
                //---------Enter your code here-----------
            }
            else if (block == button_output)
            {
                //---------Enter your code here-----------

                //增加一个选项,当选取输出目录不为null时,后处理好的文件的输出目录为选取目录中的目录,当为null时,默认为打开文件的目录为输出目录

                //输出后处理
                //将选中的节点的数量及tag返回
                int   selectNodeCount;
                Tag[] selTags;
                theUfSession.UiOnt.AskSelectedNodes(out selectNodeCount, out selTags);
                string[] postlistName = new string[selectNodeCount];

                NXOpen.CAM.CAMObject[] camObjects = new NXOpen.CAM.CAMObject[selectNodeCount];

                //取得当前工作部件的目录,并将其设置在输出目录里
                string wholepath   = workPart.FullPath;
                string part_name   = workPart.Name;
                int    indexofName = wholepath.LastIndexOf(part_name);
                string workpath    = nativeFolderBrowser_outpath.Path;
                //theUfSession.Ui.DisplayMessage(workpath, 1);  //测试工作目录是否正确


                //返回选中的后处理器的名称

                string[] usingpostname = list_postName.GetSelectedItemStrings();  //获取选中的后处理器的名称,用于后处理设置中给定的machine name,每次后处理只允许选取一个后处理器,不允许多选,因此取“0”个元素
                                                                                  //theUfSession.Ui.DisplayMessage("选中的列的文本: " + usingpostname[0], 1);

                string[] all_postmachin_name = list_postName.GetListItems();
                post_machine_name = usingpostname[0];


                //开始设置循环,进行多个程序文件的后处理
                for (int i = 0; i < selectNodeCount; i++)
                {
                    camObjects[i]   = (NXOpen.CAM.CAMObject)NXObjectManager.Get(selTags[i]);
                    postlistName[i] = camObjects[i].Name + "." + string_lastName.Value;
                    //theUfSession.Ui.DisplayMessage("选中的后处理器名称" + post_machine_name, 1);
                    string testpath = string_outfile.Value + @"\" + postlistName[i];
                    //theUfSession.Ui.DisplayMessage("路径是: " + testpath, 1);
                    NXOpen.CAM.CAMObject[] tempObj = new NXOpen.CAM.CAMObject[1];
                    tempObj[0] = camObjects[i];
                    //nativeFolderBrowser_outpath.Path = string_outfile.Value + "\\" + camObjects[i].Name;
                    //theUfSession.Ui.DisplayMessage("输出文件路径: " + nativeFile_outfilepath.Path, 1);
                    //workPart.CAMSetup.OutputClsf(tempObj, enum_clsf.ValueAsString, nativeFolderBrowser_Path.Path + camObjects[i].Name, NXOpen.CAM.CAMSetup.OutputUnits.Metric);
                    if (File.Exists(string_outfile.Value + postlistName[i]))
                    {
                        //theUfSession.Ui.DisplayMessage("文件已经存在了", 1);
                        File.Delete(string_outfile.Value + postlistName[i]);
                    }
                    //theUfSession.Ui.DisplayMessage(string_outfile.Value + postlistName[i], 1);
                    workPart.CAMSetup.PostprocessWithSetting(tempObj, post_machine_name, string_outfile.Value + "\\" + postlistName[i], NXOpen.CAM.CAMSetup.OutputUnits.Metric, NXOpen.CAM.CAMSetup.PostprocessSettingsOutputWarning.PostDefined, NXOpen.CAM.CAMSetup.PostprocessSettingsReviewTool.PostDefined);
                }

                //添加选中的要后处理的程序文件到列表框中,并进行输出
                int   selpostcount;
                Tag[] selpostTags;


                //将选中的节点的数量及tag返回
                theUfSession.UiOnt.AskSelectedNodes(out selpostcount, out selpostTags);
                string[] listpostName_sec = new string[selpostcount];

                NXOpen.CAM.CAMObject[] postObjects = new NXOpen.CAM.CAMObject[selpostcount];
                for (int k = 0; k < selpostcount; k++)
                {
                    postObjects[k]      = (NXOpen.CAM.CAMObject)NXObjectManager.Get(selpostTags[k]);
                    listpostName_sec[k] = postObjects[k].Name + "." + string_lastName.Value;
                }
                list_ncfiles.SetListItems(listpostName_sec);
                //将导出文件名称转存到公共字符串数组,方便窗口显示文件调用
                posted_name_list = listpostName_sec;


                //当勾选,显示输出后置处理好的文件
                if (toggle_showIt.Value == true)
                {
                    string[] temp_output_posts = posted_name_list;
                    //string wholepath = workPart.FullPath;
                    //string part_name = workPart.Name;
                    //int indexofName = wholepath.LastIndexOf(part_name);
                    //string workpath = wholepath.Substring(0, indexofName);


                    theUfSession.Ui.OpenListingWindow();
                    for (int i = 0; i < temp_output_posts.Length; i++)
                    {
                        //打开列表窗口,只能打开一个信息窗口,因此会在一个窗口中循环显示,已经后处理的文件

                        string       using_workpath = workpath + "\\" + temp_output_posts[i];
                        StreamReader sw             = new StreamReader(using_workpath);
                        string       temp_outfile   = sw.ReadToEnd();

                        theUfSession.Ui.WriteListingWindow("已经后处理的程序文件:" + temp_output_posts[i] + "\n");
                        theUfSession.Ui.WriteListingWindow(temp_outfile + "\n");
                        sw.Close();
                    }
                }
            }
        }
        catch (Exception ex)
        {
            //---- Enter your exception handling code here -----
            theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString());
        }
        return(0);
    }
コード例 #2
0
ファイル: Postprocess1.cs プロジェクト: tourist1029/nxopenC-
    //------------------------------------------------------------------------------
    //Callback Name: apply_cb
    //------------------------------------------------------------------------------
    public int apply_cb()
    {
        int errorCode = 0;

        try
        {
            //---- Enter your callback code here -----

            //输出后处理
            //将选中的节点的数量及tag返回
            int   selectNodeCount;
            Tag[] selTags;
            theUfSession.UiOnt.AskSelectedNodes(out selectNodeCount, out selTags);
            string[] postlistName = new string[selectNodeCount];

            NXOpen.CAM.CAMObject[] camObjects = new NXOpen.CAM.CAMObject[selectNodeCount];

            //取得当前工作部件的目录,并将其设置在输出目录里
            string wholepath   = workPart.FullPath;
            string part_name   = workPart.Name;
            int    indexofName = wholepath.LastIndexOf(part_name);
            string workpath    = wholepath.Substring(0, indexofName);
            //string tempworkpath = "E:\\Work\\2018-9-19-1823-228-3";
            theUfSession.Ui.DisplayMessage(workpath, 1);

            string_outfile.Value = workpath;
            //theUfSession.Ui.DisplayMessage(list_postName.SelectedItemString, 1);



            //返回选中的后处理器的名称

            int[]    ind          = list_postName.GetSelectedItemBooleans();
            string[] temppostname = list_postName.GetSelectedItemStrings();
            theUfSession.Ui.DisplayMessage("选中的列的文本: " + temppostname[0], 1);
            string[] instr = list_postName.GetListItems();
            theUfSession.Ui.DisplayMessage("当前选中序号: " + ind.Length, 1);
            //theUfSession.Ui.DisplayMessage("当前选中序号文本: " + instr[11], 1);
            string[] all_postmachin_name = list_postName.GetListItems();
            post_machine_name = temppostname[0];
            //theUfSession.Ui.DisplayMessage("当前选中序号: " + ind, 1);
            theUfSession.Ui.DisplayMessage(post_machine_name, 1);
            string teststring = post_machine_name; //"MILL_3_AXIS";


            //开始设置循环,进行多个程序文件的后处理
            for (int i = 0; i < selectNodeCount; i++)
            {
                camObjects[i]   = (NXOpen.CAM.CAMObject)NXObjectManager.Get(selTags[i]);
                postlistName[i] = camObjects[i].Name + "." + string_lastName.Value;
                //theUfSession.Ui.DisplayMessage("选中的后处理器名称" + post_machine_name, 1);
                NXOpen.CAM.CAMObject[] tempObj = new NXOpen.CAM.CAMObject[1];
                tempObj[0] = camObjects[i];
                nativeFile_outfilepath.Path = string_outfile.Value + "\\" + camObjects[i].Name;
                string ttt = string_outfile.Value + camObjects[i].Name;
                //theUfSession.Ui.DisplayMessage("输出文件路径: " + nativeFile_outfilepath.Path, 1);
                //workPart.CAMSetup.OutputClsf(tempObj, enum_clsf.ValueAsString, nativeFolderBrowser_Path.Path + camObjects[i].Name, NXOpen.CAM.CAMSetup.OutputUnits.Metric);
                workPart.CAMSetup.PostprocessWithSetting(tempObj, teststring, string_outfile.Value + "\\" + postlistName[i], NXOpen.CAM.CAMSetup.OutputUnits.Metric, NXOpen.CAM.CAMSetup.PostprocessSettingsOutputWarning.PostDefined, NXOpen.CAM.CAMSetup.PostprocessSettingsReviewTool.PostDefined);
            }
        }
        catch (Exception ex)
        {
            //---- Enter your exception handling code here -----
            errorCode = 1;
            theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString());
        }
        return(errorCode);
    }