C#调用C/C++dll的方法(近期研究的课题)

转载自https://www.cnblogs.com/bizca/p/4962559.html   bizca大神的博客

NX Open,怎样取到面的环LOOP

 在封装的ufun .NET库里面,对UF_MODL_ask_face_loops这个函数并没有封装,导致我们很多不便,那我们在.NET下怎样才能使用这个函数呢??当然是手动处理一下

using System;
using System.Collections.Generic;
using  System.Runtime.InteropServices;
 
 
    public class AskFaceLoop
    {
        public LoopList[] AskFaceLoops(NXOpen.Tag faceTag)
        {
           System.IntPtr loopT;
            NXOpen.Utilities.JAM.StartUFCall();
            int errorCode = UF_MODL_ask_face_loops(faceTag,out loopT);
            NXOpen.Utilities.JAM.EndUFCall();
            if (errorCode != 0)
            {
                throw NXOpen.NXException.Create(errorCode);
            }
            System.IntPtr ptr = loopT;
            List<LoopList> loopList = new List<LoopList>();
            while (ptr != IntPtr.Zero)
            {
                _loop_list loopListT = (_loop_list)Marshal.PtrToStructure(ptr, typeof(_loop_list));
                int count;
                errorCode = UF_MODL_ask_list_count(loopListT.edge_list,out count);
                NXOpen.Tag[] edgeArray = new NXOpen.Tag[count];
                for (int i = 0; i < count; i++)
                {
                    UF_MODL_ask_list_item(loopListT.edge_list, i,out edgeArray[i]);
                }
                //UF_MODL_delete_list(out loopListT.edge_list);
                loopList.Add(new LoopList{Type = loopListT.type,EdgeList = edgeArray});
                ptr = loopListT.next;
            }
            UF_MODL_delete_loop_list(out loopT);
            return loopList.ToArray();
        }
 
        [DllImport("libufun.dll", EntryPoint = "UF_MODL_ask_face_loops", CallingConvention = CallingConvention.Cdecl,CharSet = CharSet.Ansi)]
        internal static extern int UF_MODL_ask_face_loops(NXOpen.Tag face, out IntPtr loopList);
 
        [DllImport("libufun.dll", EntryPoint = "UF_MODL_ask_list_count", CallingConvention = CallingConvention.Cdecl,
            CharSet = CharSet.Ansi)]
        internal static extern int UF_MODL_ask_list_count(IntPtr list, out int count);
 
        [DllImport("libufun.dll", EntryPoint = "UF_MODL_ask_list_item", CallingConvention = CallingConvention.Cdecl,CharSet = CharSet.Ansi)]
        internal static extern int UF_MODL_ask_list_item(IntPtr list, int index, out NXOpen.Tag @object);
 
        [DllImport("libufun.dll", EntryPoint = "UF_MODL_delete_list", CallingConvention = CallingConvention.Cdecl,CharSet = CharSet.Ansi)]
        internal static extern int UF_MODL_delete_list(out IntPtr list);
 
        [DllImport("libufun.dll", EntryPoint = "UF_MODL_delete_loop_list", CallingConvention = CallingConvention.Cdecl,CharSet = CharSet.Ansi)]
        internal static extern int UF_MODL_delete_loop_list(out IntPtr list);
 
        internal struct _loop_list
        {
            public int type;
            public IntPtr edge_list;
            public IntPtr next;
        }
 
        public struct LoopList
        {
        /// <summary>
        /// Peripheral=1, Hole=2, Other=3
        /// </summary>
        public int Type;
            public NXOpen.Tag[] EdgeList;
        }
    }

虽然Nxopen已经封装好了面选择器 

     NXOpen.Face face = (NXOpen.Face)face_select0.SelectedObjects.First();
     NXOpen.Edge[] edges = face.GetEdges();

但是学习怎么用C#调用C++/C的dll也是非常重要的,所以接下的学习的课题就是这个了。


近期七七八八的事情太多了,也没办法静下心来写个功能,只能学学一些新的技术(对我而言),每天多了结一点进步一点才能厚积薄发,关键是一天如果从早到晚都没有看过代码的话心里总发慌,有一种莫名的害怕。

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值