UG/NX二次开发(C#) 获取组件里面的部件实体

 部件一旦装配之后,UG会对部件进行封装,并生成新的tag,那我们如何去查找装配后的部件呢?

这里提供2种思路:

1、通过ufun函数获取;

2、通过NXOPEN遍历部件;

上代码:

        /// <summary>
        /// 通过ufun获取组件里的部件信息
        /// </summary>
        public static void GetBodyListFromComponet(ref List<Component> compList, ref List<Body> bodyList)
        {
            theSession = Session.GetSession();
            theUFSession = UFSession.GetUFSession();
            workPart = theSession.Parts.Work;
            compList = new List<Component>();
            bodyList = new List<Body>();
            ComponentTool.GetComponentList(workPart, compList);
            ComponentAssembly compAssembly = workPart.ComponentAssembly;
            Component rootComponent = compAssembly.RootComponent;
            foreach (Component c in compList)
            {
                SetWorkPart(c);
                workPart = theSession.Parts.Work;
                Tag objTag = Tag.Null;
                theUFSession.Obj.CycleObjsInPart(workPart.Tag, UFConstants.UF_solid_type, ref objTag);
                while (objTag != Tag.Null)
                {
                    int type, subtype;
                    theUFSession.Obj.AskTypeAndSubtype(objTag, out type, out subtype);
                    if (type == 70 && subtype == 0)
                    {
                        Body b = (Body)NXOpen.Utilities.NXObjectManager.Get(objTag);
                        bodyList.Add(b);
                    }
                    theUFSession.Obj.CycleObjsInPart(workPart.Tag, UFConstants.UF_solid_type, ref objTag);
                }
            }
            SetWorkPart(rootComponent);
        }

        /// <summary>
        /// 通过NXOPEN直接遍历
        /// </summary>
        public static void GetBodyListFromComponet1(ref List<Component> compList, ref List<Body> bodyList)
        {
            theSession = Session.GetSession();
            theUFSession = UFSession.GetUFSession();
            workPart = theSession.Parts.Work;
            compList = new List<Component>();
            bodyList = new List<Body>();
            ComponentTool.GetComponentList(workPart, compList);
            ComponentAssembly compAssembly = workPart.ComponentAssembly;
            Component rootComponent = compAssembly.RootComponent;
            foreach (Component c in compList)
            {           
                SetWorkPart(c);
                workPart = theSession.Parts.Work;
                foreach (Body bodyObj in workPart.Bodies)
                {
                    bodyList.Add(bodyObj);
                }
            }
            SetWorkPart(rootComponent);
        }

 调用方法:

            List<Component> compList = new List<Component>();
            List<Body> bodyList = new List<Body>();
            ComponentTool.GetBodyListFromComponet(ref compList, ref bodyList);

            List<Component> compList1 = new List<Component>();
            List<Body> bodyList1 = new List<Body>();
            ComponentTool.GetBodyListFromComponet1(ref compList1, ref bodyList1);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MarcoPro

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值