此前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循环固定数组长度的方式好像有点笨拙,有更好思路的朋友欢迎指正。