UG NX二次开发中的组件遍历(C#)

            /// <summary>
            /// 通过遍历获取装配体的全部组件
            /// </summary>
            /// <param name="part">工作部件</param>
            /// <param name="componentsList">组件链表</param>
            /// <param name="developmentType">选择开发的类型</param>
            public static void TraversalComponents( List<NXOpen.Assemblies.Component> componentsList,DevelopmentType developmentType)
            {
                
                if (developmentType == DevelopmentType.UGOPEN)
                {
                    Tag componentTag = Tag.Null;
                    uFObj.CycleObjsInPart(uFAssem.AskWorkPart(), 63, ref componentTag);                     //根据类型遍历装配体中的组件,按次序读取,一次读取一个
                    while (componentTag != Tag.Null)                                                         //以读取的组件为空为结束条件
                    {
                        NXObjectManager nXObjectManager = new NXObjectManager();
                        TaggedObject taggedObject = nXObjectManager.GetTaggedObject(componentTag);        //将tag转化为TaggedObject对象
                        Component newComponent = (Component)taggedObject;                                  //将taggedObject转化为Component对象
                        componentsList.Add(newComponent);

                        uFObj.CycleObjsInPart(uFAssem.AskWorkPart(), 63, ref componentTag);                      //读取下一个组件    
                    }
                }
                else if(developmentType==DevelopmentType.NXOPEN)
                {
                    ComponentAssembly componentAssembly = workPart.ComponentAssembly;
                    Component rootComponent = componentAssembly.RootComponent;
                    GetComponentsChildren(rootComponent, componentsList);
                }
            }
            /// <summary>
            /// 用NXOPEN获得装配体的所有组件(递归算法)
            /// </summary>
            /// <param name="rootComponent"></param>
            /// <param name="componentsList"></param>
            public static void GetComponentsChildren(Component rootComponent, List<Component> componentsList)
            {
                Component[] componentsChildren = rootComponent.GetChildren();
                foreach (var cc in componentsChildren)
                {
                    componentsList.Add(cc);
                    GetComponentsChildren(cc, componentsList);
                }

            }


      /// <summary>
      /// 定义开发类型的枚举
      /// </summary>
        public enum DevelopmentType
        {
            /// <summary>
            /// 采用OPEN C/C++
            /// </summary>
            UGOPEN,
            /// <summary>
            /// 采用NXOPEN
            /// </summary>
            NXOPEN,
        }

猜你喜欢

转载自blog.csdn.net/yang19861007/article/details/103082482
今日推荐