C#实现NX组件替换功能


        /// 
        /// 替换组件
        /// 
        /// 被替换的组件实例
        /// 目标组件
        static void ReplaceComponent(Tag replacedInstance, string part)
        {
            if ((NXOpen.Tag.Null == replacedInstance) || string.IsNullOrEmpty(part.Trim()))
            {
                return;
            }
            
            string part_name = "";
            string refset_name = "";
            string instance_name = "";
            double[] origin = new double[3];
            double[] csys_matrix = new double[9];
            double[,] transform = new double[4, 4];

            //获取被替换组件相关数据
            theUfSession.Assem.AskComponentData(replacedInstance, out part_name, out refset_name, out instance_name, origin, csys_matrix, transform);

            //获取被替换组件父
            Tag replacedParentInstance = theUfSession.Assem.AskParentOfInstance(replacedInstance);

            //移除组件
            theUfSession.Assem.RemoveInstance(replacedInstance);

            Tag outInstance = NXOpen.Tag.Null;
            NXOpen.UF.UFPart.LoadStatus error_status;

            //添加目标组件
            theUfSession.Assem.AddPartToAssembly(replacedParentInstance, part, refset_name, instance_name, origin, csys_matrix, -1, out outInstance, out error_status);

            //保存修改
            bool anyPartsModified = false;
            PartSaveStatus saveStatus;
            theSession.Parts.SaveAll(out anyPartsModified, out saveStatus);
        }