UF_CUT_LEVELS_load
Loads the current cut levels for the specified operation into the data
structure UF_CUT_LEVELS_t.
在.NET下面使用方法为NXOpen.UF.UFCutLevels.Load(ByVal operation_tag As NXOpen.Tag, ByRef cut_levels_ptr_addr() As NXOpen.UF.UFCutLevels.CutLevelsStruct)
可是你想加载成功,是不可能的。我觉得是在封装API库的时候出了点问题。
以下源代码会引发异常:
解决办法重新封装UF_CUT_LEVELS_load
以下为源代码:
1 #Region "UF_CUT_LEVELS_load" 2 Friend Function CreateException(ByVal errorCode As Integer) As NXOpen.NXException 3 Return NXOpen.NXException.Create(errorCode) 4 End Function 5 6 <DllImport("libufun.dll", EntryPoint:="UF_CUT_LEVELS_load", CallingConvention:=CallingConvention.Cdecl, CharSet:=CharSet.Ansi)> _ 7 Private Function _CUT_LEVELS_load(ByVal operation_tag As Tag, ByRef cut_levels_ptr_addr As IntPtr) As Integer 8 End Function 9 Private Structure _CutLevelsStruct 10 Public num_levels As Integer 11 Public cut_levels As IntPtr 12 Public num_top_off_levels As Integer 13 Public top_off_levels As IntPtr 14 End Structure 15 ''' <summary> 16 ''' 加载切削层 17 ''' </summary> 18 ''' <param name="operation_tag">操作tag值</param> 19 ''' <param name="cut_levels_ptr_addr">切削层参数结构</param> 20 ''' <remarks>重写加载切削层结构</remarks> 21 Public Sub UF_CUT_LEVELS_load(ByVal operation_tag As NXOpen.Tag, ByRef cut_levels_ptr_addr As NXOpen.UF.UFCutLevels.CutLevelsStruct) 22 Dim ptr As IntPtr 23 NXOpen.Utilities.JAM.StartUFCall() 24 Dim errorCode As Integer = _CUT_LEVELS_load(operation_tag, ptr) 25 NXOpen.Utilities.JAM.EndUFCall() 26 If errorCode <> 0 Then 27 Throw CreateException(errorCode) 28 End If 29 Dim structArray As New _CutLevelsStruct 30 cut_levels_ptr_addr = New NXOpen.UF.UFCutLevels.CutLevelsStruct 31 structArray = DirectCast(Marshal.PtrToStructure(ptr, GetType(_CutLevelsStruct)), _CutLevelsStruct) 32 cut_levels_ptr_addr.num_levels = structArray.num_levels 33 Dim num As Integer = Marshal.SizeOf(GetType(NXOpen.UF.CutLevelSingle)) 34 Dim num2 As Integer = structArray.num_levels 35 Dim zero As Long = structArray.cut_levels.ToInt64 36 If num2 > 0 Then 37 cut_levels_ptr_addr.cut_levels = New NXOpen.UF.CutLevelSingle(num2 - 1) {} 38 End If 39 If zero <> Nothing Then 40 Dim num6 As Integer = 0 41 Do While num6 < num2 42 cut_levels_ptr_addr.cut_levels(num6) = DirectCast(Marshal.PtrToStructure(CType(zero, IntPtr), GetType(NXOpen.UF.CutLevelSingle)), NXOpen.UF.CutLevelSingle) 43 zero = (zero + num) 44 num6 += 1 45 Loop 46 End If 47 'Marshal.FreeHGlobal(structArray.cut_levels) 48 structArray.cut_levels = IntPtr.Zero 49 50 cut_levels_ptr_addr.num_top_off_levels = structArray.num_top_off_levels 51 num2 = structArray.num_top_off_levels 52 num = Marshal.SizeOf(GetType(NXOpen.UF.CutLevelSingle)) 53 zero = structArray.top_off_levels.ToInt64 54 If (num2 > 0) Then 55 cut_levels_ptr_addr.top_off_levels = New NXOpen.UF.CutLevelSingle(num2 - 1) {} 56 End If 57 If (zero <> Nothing) Then 58 For num6 = 0 To num2 - 1 59 cut_levels_ptr_addr.top_off_levels(num6) = DirectCast(Marshal.PtrToStructure(CType(zero, IntPtr), GetType(NXOpen.UF.CutLevelSingle)), NXOpen.UF.CutLevelSingle) 60 zero = (zero + num) 61 Next num6 62 End If 63 'Marshal.FreeHGlobal(structArray.top_off_levels) 64 structArray.top_off_levels = IntPtr.Zero 65 End Sub 66 #End Region
调用方法如下:
这样就解决了问题,不过缺陷是:由于本人技术有限,没有卸载非托管代码使用的的内存。还望大牛们赐教!!!