您的位置:首页 > 运维架构

NX Open 切削层加载

2015-06-16 14:36 288 查看
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

以下为源代码:

</pre><pre name="code" class="vb">#Region "UF_CUT_LEVELS_load"
Friend Function CreateException(ByVal errorCode As Integer) As NXOpen.NXException
Return NXOpen.NXException.Create(errorCode)
End Function

<DllImport("libufun.dll", EntryPoint:="UF_CUT_LEVELS_load", CallingConvention:=CallingConvention.Cdecl, CharSet:=CharSet.Ansi)> _
Private Function _CUT_LEVELS_load(ByVal operation_tag As Tag, ByRef cut_levels_ptr_addr As IntPtr) As Integer
End Function
Private Structure _CutLevelsStruct
Public num_levels As Integer
Public cut_levels As IntPtr
Public num_top_off_levels As Integer
Public top_off_levels As IntPtr
End Structure
''' <summary>
''' 加载切削层
''' </summary>
''' <param name="operation_tag">操作tag值</param>
''' <param name="cut_levels_ptr_addr">切削层参数结构</param>
''' <remarks>重写加载切削层结构</remarks>
Public Sub UF_CUT_LEVELS_load(ByVal operation_tag As NXOpen.Tag, ByRef cut_levels_ptr_addr As NXOpen.UF.UFCutLevels.CutLevelsStruct)
Dim ptr As IntPtr
NXOpen.Utilities.JAM.StartUFCall()
Dim errorCode As Integer = _CUT_LEVELS_load(operation_tag, ptr)
NXOpen.Utilities.JAM.EndUFCall()
If errorCode <> 0 Then
Throw CreateException(errorCode)
End If
Dim structArray As New _CutLevelsStruct
cut_levels_ptr_addr = New NXOpen.UF.UFCutLevels.CutLevelsStruct
structArray = DirectCast(Marshal.PtrToStructure(ptr, GetType(_CutLevelsStruct)), _CutLevelsStruct)
cut_levels_ptr_addr.num_levels = structArray.num_levels
Dim num As Integer = Marshal.SizeOf(GetType(NXOpen.UF.CutLevelSingle))
Dim num2 As Integer = structArray.num_levels
Dim zero As Long = structArray.cut_levels.ToInt64
If num2 > 0 Then
cut_levels_ptr_addr.cut_levels = New NXOpen.UF.CutLevelSingle(num2 - 1) {}
End If
If zero <> Nothing Then
Dim num6 As Integer = 0
Do While num6 < num2
cut_levels_ptr_addr.cut_levels(num6) = DirectCast(Marshal.PtrToStructure(CType(zero, IntPtr), GetType(NXOpen.UF.CutLevelSingle)), NXOpen.UF.CutLevelSingle)
zero = (zero + num)
num6 += 1
Loop
End If
'Marshal.FreeHGlobal(structArray.cut_levels)
structArray.cut_levels = IntPtr.Zero

cut_levels_ptr_addr.num_top_off_levels = structArray.num_top_off_levels
num2 = structArray.num_top_off_levels
num = Marshal.SizeOf(GetType(NXOpen.UF.CutLevelSingle))
zero = structArray.top_off_levels.ToInt64
If (num2 > 0) Then
cut_levels_ptr_addr.top_off_levels = New NXOpen.UF.CutLevelSingle(num2 - 1) {}
End If
If (zero <> Nothing) Then
For num6 = 0 To num2 - 1
cut_levels_ptr_addr.top_off_levels(num6) = DirectCast(Marshal.PtrToStructure(CType(zero, IntPtr), GetType(NXOpen.UF.CutLevelSingle)), NXOpen.UF.CutLevelSingle)
zero = (zero + num)
Next num6
End If
'Marshal.FreeHGlobal(structArray.top_off_levels)
structArray.top_off_levels = IntPtr.Zero
End Sub
#End Region



调用方法如下:





这样就解决了问题,不过缺陷是:由于本人技术有限,没有卸载非托管代码使用的的内存。还望大牛们赐教!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐