剛開通博客園,隨手寫一篇簡單的獲取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 }