Ejemplo n.º 1
0
        /// <summary>
        /// This method copies an element (for instance a class) into a target package
        /// note : the EA SDK does not provide a simple way to clone an element. It only
        /// provides a way to clone a package in the same parent package. We use this capabilities
        /// to clone an element from one package to another
        /// </summary>
        /// <param name="repository">The current repository</param>
        /// <param name="sourceElement">An element to copy</param>
        /// <param name="targetPackage">The target package</param>
        /// <returns>The newly-created element or null of the copy fails</returns>
        public static EA.Element copyElement(EA.Repository repository, EA.Element sourceElement, EA.Package targetPackage)
        {
            // we get the parent package of the source element
            EA.Package sourcePackage = repository.GetPackageByID(sourceElement.PackageID);

            // create an temporary package into the parent package
            EA.Package tmpPackage = sourcePackage.Packages.AddNew("tmp", "");
            if (!(tmpPackage.Update()))
            {
                return(null);
            }
            sourcePackage.Packages.Refresh();

            // move the source element into the temporary package
            sourceElement.PackageID = tmpPackage.PackageID;
            sourceElement.Update();
            sourcePackage.Elements.Refresh();
            tmpPackage.Elements.Refresh();

            // clone the temporary package
            EA.Package clonePackage = tmpPackage.Clone();
            clonePackage.Update();
            sourcePackage.Packages.Refresh();

            // move back the source element into it original parent package
            sourceElement.PackageID = sourcePackage.PackageID;
            sourceElement.Update();
            tmpPackage.Elements.Refresh();
            sourcePackage.Elements.Refresh();

            // getting the copy of the source element
            EA.Element targetElement = clonePackage.Elements.GetByName(sourceElement.Name);

            // move the copy into the targetPackage
            targetElement.PackageID = targetPackage.PackageID;
            targetElement.Update();
            clonePackage.Elements.Refresh();
            targetPackage.Elements.Refresh();

            // delete the temporary packages
            short tmpPackageIndex   = getIndex(sourcePackage, tmpPackage);
            short clonePackageIndex = getIndex(sourcePackage, clonePackage);

            sourcePackage.Packages.Delete(tmpPackageIndex);
            sourcePackage.Packages.Refresh();
            sourcePackage.Packages.Delete(clonePackageIndex);
            sourcePackage.Packages.Refresh();
            repository.RefreshModelView(sourcePackage.PackageID);

            return(targetElement);
        }