其他分享
首页 > 其他分享> > NX二次开发--NXOPEN方式获取部件的prt文件名以及全路径

NX二次开发--NXOPEN方式获取部件的prt文件名以及全路径

作者:互联网

刚开通博客园,随手写一篇简单的获取prt文件名以及全路径的方法。

  1 using System;
  2 using System.IO;
  3 using NXOpen;
  4 using NXOpen.UF;
  5 
  6 public class Program
  7 {
  8     // class members
  9     private static Session theSession;
 10     private static UI theUI;
 11     private static UFSession theUfSession;
 12     public static Program theProgram;
 13     public static bool isDisposeCalled;
 14 
 15     //------------------------------------------------------------------------------
 16     // Constructor
 17     //------------------------------------------------------------------------------
 18     public Program()
 19     {
 20         try
 21         {
 22             theSession = Session.GetSession();
 23             theUI = UI.GetUI();
 24             theUfSession = UFSession.GetUFSession();
 25             isDisposeCalled = false;
 26         }
 27         catch (NXOpen.NXException ex)
 28         {
 29             // ---- Enter your exception handling code here -----
 30             // UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message);
 31         }
 32     }
 33 
 34     //------------------------------------------------------------------------------
 35     //  Explicit Activation
 36     //      This entry point is used to activate the application explicitly
 37     //------------------------------------------------------------------------------
 38     public static int Main(string[] args)
 39     {
 40         int retValue = 0;
 41         try
 42         {
 43             theProgram = new Program();
 44 
 45             //获取部件文件的全路径
 46             string fullPath = theSession.Parts.Work.FullPath;
 47 
 48             //获取部件名 方法1   通过Parts.Work或Part.Display的Name属性来获取
 49             string path1 = theSession.Parts.Work.Name;
 50 
 51             //获取部件名 方法2   通过C#Path类提供的函数来获取( 需using System.IO )
 52             string path2 = Path.GetFileNameWithoutExtension(fullPath); ;
 53 
 54             //获取部件名 方法3   通过截取fullPath来获取
 55             string path3 = "";
 56             int index = fullPath.LastIndexOf('\\') + 1;  //找到fullpath中最末尾反斜杠的下标,下标从0开始,故+1
 57             if (fullPath.Substring(fullPath.Length - 4).ToUpper() == ".PRT")
 58             {
 59                 path3 = fullPath.Substring(index, fullPath.Length - index - 4);
 60             }
 61 
 62             theUfSession.Ui.IsListingWindowOpen(out bool res);
 63             if (!res)
 64             {
 65                 theUfSession.Ui.OpenListingWindow();
 66             }
 67             theUfSession.Ui.WriteListingWindow("当前部件文件的路径是:" + fullPath + "\r\n");
 68             theUfSession.Ui.WriteListingWindow("方法1获取到的结果:" + path1 + "\r\n");
 69             theUfSession.Ui.WriteListingWindow("方法2获取到的结果:" + path2 + "\r\n");
 70             theUfSession.Ui.WriteListingWindow("方法3获取到的结果:" + path3 + "\r\n");
 71 
 72             //TODO: Add your application code here 
 73 
 74             theProgram.Dispose();
 75         }
 76         catch (NXOpen.NXException ex)
 77         {
 78             // ---- Enter your exception handling code here -----
 79 
 80         }
 81         return retValue;
 82     }
 83 
 84     //------------------------------------------------------------------------------
 85     // Following method disposes all the class members
 86     //------------------------------------------------------------------------------
 87     public void Dispose()
 88     {
 89         try
 90         {
 91             if (isDisposeCalled == false)
 92             {
 93                 //TODO: Add your application code here 
 94             }
 95             isDisposeCalled = true;
 96         }
 97         catch (NXOpen.NXException ex)
 98         {
 99             // ---- Enter your exception handling code here -----
100 
101         }
102     }
103 
104     public static int GetUnloadOption(string arg)
105     {
106         //Unloads the image explicitly, via an unload dialog
107         //return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly);
108 
109         //Unloads the image immediately after execution within NX
110         return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);
111 
112         //Unloads the image when the NX session terminates
113         // return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination);
114     }
115 
116 }

 

标签:string,获取,--,theUfSession,prt,static,二次开发,fullPath,public
来源: https://www.cnblogs.com/fxp888/p/15417338.html