此前c#調用Grip,本人是采用的抽取libufun內部函數的方式(因為c#UFUN本身沒有對“UF_call_grip”封裝),使用相對比較麻煩,今天剛發現c#的nxopen中也是有調
用Grip的函數的。並且調用方式也簡單許多。
官方幫助文檔:
參數列表 1:需要調用的Grip程序 2:Object基類數組
需要傳遞的參數全部塞進數組里就可以。數組長度必須與Grip中ufargs接收數量一致。
以設定孔加工工序幾何體舉例:
Grip代碼:
Grip代碼邏輯此處不做解釋。
調用代碼:
1 using System; 2 using System.Runtime.InteropServices; 3 using NXOpen; 4 using NXOpen.UF; 5 using NXOpen.Utilities; 6 7 public class Program 8 { 9 // class members 10 private static Session theSession; 11 private static UI theUI; 12 private static UFSession theUfSession; 13 public static Program theProgram; 14 public static bool isDisposeCalled; 15 16 [DllImport("libufun.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, EntryPoint = "UF_call_grip")] 17 18 internal static extern int _CallGrip(string grip_executable, int argument_count, ref Args[] gruf_arg_list); 19 public static void CallGrip(string grip_executable, int argument_count, ref Args[] gruf_arg_list) 20 { 21 JAM.StartUFCall(); 22 int num = _CallGrip(grip_executable, argument_count, ref gruf_arg_list); 23 JAM.EndUFCall(); 24 } 25 26 27 //------------------------------------------------------------------------------ 28 // Constructor 29 //------------------------------------------------------------------------------ 30 public Program() 31 { 32 try 33 { 34 theSession = Session.GetSession(); 35 theUI = UI.GetUI(); 36 theUfSession = UFSession.GetUFSession(); 37 isDisposeCalled = false; 38 } 39 catch (NXOpen.NXException ex) 40 { 41 // ---- Enter your exception handling code here ----- 42 // UI.GetUI().NXMessageBox.Show("Message", NXMessageBox.DialogType.Error, ex.Message); 43 } 44 } 45 46 //------------------------------------------------------------------------------ 47 // Explicit Activation 48 // This entry point is used to activate the application explicitly 49 //------------------------------------------------------------------------------ 50 public static int Main(string[] args) 51 { 52 int retValue = 0; 53 try 54 { 55 theProgram = new Program(); 56 57 Selection.SelectionType[] type = new Selection.SelectionType[1]; 58 type[0] = Selection.SelectionType.All; 59 theUI.SelectionManager.SelectTaggedObjects("選擇", "選擇對象", Selection.SelectionScope.WorkPart, false, type, out TaggedObject[] objtmp); 60 TaggedObject[] obj = new TaggedObject[200]; 61 TaggedObject tagged = theSession.Parts.Work as TaggedObject; 62 if (objtmp.Length == 0) 63 { 64 return retValue; 65 } 66 67 //為確保傳遞數組的長度與Grip一致,所以給數組中的空元素 68 //賦值Grip中無法使用的類型,Grip運行時會遇錯跳轉。 69 for (int i = 0; i < 200; i++) 70 { 71 try 72 { 73 obj[i] = objtmp[i]; 74 } 75 catch (Exception) 76 { 77 obj[i] = tagged; 78 } 79 } 80 81 try 82 { 83 string operName = "TTTTT"; 84 string gripName = @"D:\FXP-wg\Grx\testhole.grx"; 85 object[] inputToGrip = new object[3]; 86 inputToGrip[0] = operName; //操作名 87 inputToGrip[1] = obj; //幾何體數組 88 inputToGrip[2] = Convert.ToDouble(obj.Length); //數組長度 89 object[] getedRet = Session.GetSession().ExecuteGrip(gripName, inputToGrip); //Call 90 } 91 catch (Exception exInfo) 92 { 93 theUfSession.Ui.OpenListingWindow(); 94 theUfSession.Ui.WriteListingWindow("出錯,錯誤原因:" + exInfo.ToString()); 95 } 96 97 98 //TODO: Add your application code here 99 100 theProgram.Dispose(); 101 } 102 catch (NXOpen.NXException ex) 103 { 104 // ---- Enter your exception handling code here ----- 105 theUfSession.Ui.OpenListingWindow(); 106 theUfSession.Ui.WriteListingWindow("出錯,錯誤原因:" + ex.ToString()); 107 } 108 return retValue; 109 } 110 111 //------------------------------------------------------------------------------ 112 // Following method disposes all the class members 113 //------------------------------------------------------------------------------ 114 public void Dispose() 115 { 116 try 117 { 118 if (isDisposeCalled == false) 119 { 120 //TODO: Add your application code here 121 } 122 isDisposeCalled = true; 123 } 124 catch (NXOpen.NXException ex) 125 { 126 // ---- Enter your exception handling code here ----- 127 128 } 129 } 130 131 public static int GetUnloadOption(string arg) 132 { 133 //Unloads the image explicitly, via an unload dialog 134 //return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly); 135 136 //Unloads the image immediately after execution within NX 137 return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately); 138 139 //Unloads the image when the NX session terminates 140 // return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination); 141 } 142 143 internal static IntPtr ArrayToIntptr(Tag[] source) 144 { 145 if (source == null) 146 return IntPtr.Zero; 147 unsafe 148 { 149 fixed (Tag* point = source) 150 { 151 IntPtr ptr = new IntPtr(point); 152 return ptr; 153 } 154 } 155 } 156 }
需要注意的是,Grip中所需要的Number類型,是對應C#double類型的,所以傳遞參
數時必須以double類型來傳遞。
inputToGrip[2] = Convert.ToDouble(obj.Length);
演示:
補充一句, 使用for循環固定數組長度的方式好像有點笨拙,有更好思路的朋友歡迎指正。