Book a Demo

Author Topic: How to fetch elements and diagrams under the packages and sub packages in c#  (Read 3028 times)

ea0921

  • EA User
  • **
  • Posts: 104
  • Karma: +0/-1
    • View Profile
How to fetch not only the elements and diagrams under the package as well as it's sub packages along the hierarchy in C#

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
In EA searches you can use the #Branch# macro, to get a list of all packageID's under a certain branch.

You can then use those packageID's so select elements, diagrams, etc... using an SQL Query.

here's the method I use in C# to get the this packageTreeIDList (from my Package class)

Code: [Select]
    private List<string> getPackageTreeIDs(List<string> parentIDs = null)
    {
        List<string> allPackageIDs = new List<string>();
        List<string> subPackageIDs = new List<string>();
        if (parentIDs == null)
        {
            parentIDs = new List<string>() { this.packageID.ToString() };
        }
        //add the current parentID's to the list of all ID's
        allPackageIDs.AddRange(parentIDs);
        //get the id's from the subpackages
        string parentIDString = string.Join(",", parentIDs);
        string getSubpackageSQL = "select p.Package_ID from t_package p where p.Parent_ID in (" + parentIDString + ")";
        var queryResult = this.EAModel.SQLQuery(getSubpackageSQL);
        foreach (XmlNode packageIdNode in queryResult.SelectNodes(this.EAModel.formatXPath("//Package_ID")))
        {
            subPackageIDs.Add(packageIdNode.InnerText);
        }
        //if subpackages found then go a level deeper
        if (subPackageIDs.Any())
        {
            allPackageIDs.AddRange(this.getPackageTreeIDs(subPackageIDs));
        }
        return allPackageIDs;
    }

Geert