.NET下使用ufun函数取CAM操作的进给速度

UF_PARAM_ask_subobj_ptr_value,这个函数在封装的时候,给了很大一个坑啊。

NXOpen.UF.UFParam.AskSubobjPtrValue(ByVal param_tag As NXOpen.Tag,
                                    ByVal param_index As Integer,
                                    ByRef value As System.IntPtr)

如果你想使用如下的代码可以取到竟给速度,那你就错了

错误是使用方法
Dim cb As Integer = Marshal.SizeOf(GetType(NXOpen.UF.UFParam.Feedrate))
Dim ptr As IntPtr = Marshal.AllocHGlobal(cb)
theUfSession.Param.AskSubobjPtrValue(oper.tag, UFConstants.UF_PARAM_FEED_CUT, ptr)
Dim feed_cut As NXOpen.UF.UFParam.Feedrate = CType(Marshal.PtrToStructure(ptr, GetType(NXOpen.UF.UFParam.Feedrate)), NXOpen.UF.UFParam.Feedrate)

这个函数一直让我头疼了三年多,今天终于想到办法了

其实这里不能使用指针,而是直接使用结构体NXOpen.UF.UFParam.Feedrate

将UF_PARAM_ask_subobj_ptr_value函数重新封装定义一下:

<DllImport("libufun.dll", EntryPoint:="UF_PARAM_ask_subobj_ptr_value", CallingConvention:=CallingConvention.Cdecl, CharSet:=CharSet.Ansi)> _
        Friend Shared Function _AskFeedRate(ByVal param_tag As Tag, ByVal param_index As Integer, <Out> ByRef value As NXOpen.UF.UFParam.Feedrate) As Integer
        End Function

        <DllImport("libufun.dll", EntryPoint:="UF_PARAM_set_subobj_ptr_value", CallingConvention:=CallingConvention.Cdecl, CharSet:=CharSet.Ansi)> _
        Friend Shared Function _SetFeedRate(ByVal param_tag As Tag, ByVal param_index As Integer, ByVal value As NXOpen.UF.UFParam.Feedrate) As Integer
        End Function

整理了一下使用方法,代码如下:

Public Function AskFeedRate(ByVal param_index As Integer) As NXOpen.UF.UFParam.Feedrate
    Dim value As NXOpen.UF.UFParam.Feedrate
    Dim errorCode As Integer = _AskFeedRate(_camobject, param_index, value)
    If errorCode <> 0 Then
        Throw NXOpen.NXException.Create(errorCode)
    End If
    Return value
End Function


Public Sub SetFeedRate(ByVal param_index As Integer, ByVal value As NXOpen.UF.UFParam.Feedrate)
    Dim errorCode As Integer = _SetFeedRate(_camobject, param_index, value)
    If errorCode <> 0 Then
        Throw NXOpen.NXException.Create(errorCode)
    End If
End Sub
原文地址:https://www.cnblogs.com/bizca/p/4964302.html