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

            /// 
            /// 通过遍历获取装配体的全部组件
            /// 
            /// 工作部件
            /// 组件链表
            /// 选择开发的类型
            public static void TraversalComponents( List 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);
                }
            }
            /// 
            /// 用NXOPEN获得装配体的所有组件(递归算法)
            /// 
            /// 
            /// 
            public static void GetComponentsChildren(Component rootComponent, List componentsList)
            {
                Component[] componentsChildren = rootComponent.GetChildren();
                foreach (var cc in componentsChildren)
                {
                    componentsList.Add(cc);
                    GetComponentsChildren(cc, componentsList);
                }

            }


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

 

你可能感兴趣的:(NX二次开发,组件,UG,二次开发,C#,NXOPEN,UGOPEN,组件遍历)