Archiv der Kategorie: Modellierung

Sparx Systems Enterprise Architect + C#: Get all EA Elements of a package

Problem

All Elements of a package should be retrieved.

Approach – Ansatz

The method getElementsOfElement, which has been defined in the previous article is used to add all subelements of a package.

Lösung – Solution

public List<EA.Element> getElementsOfPackage(EA.Package package, string packageName)
{
    List<EA.Element> elementsOfPackage = new List<EA.Element>();
    // Only list the elements if the actual package is connected to a DOORS module
    if (package.Name == packageName)
    {
        foreach (EA.Element element in package.Elements)
        {
            elementsOfPackage.Add(element);
            elementsOfPackage.AddRange(getElementsOfElement(element));
        }
    }

    foreach (EA.Package subPackage in package.Packages)
    {
        elementsOfPackage.AddRange(getElementsOfPackage(subPackage, packageName));
    }
    return elementsOfPackage;
}

private List<EA.Element> getElementsOfElement(EA.Element actualElement)
{
    List<EA.Element> elementsOfElement = new List<EA.Element>();
    EA.Collection elements;
    elements = actualElement.Elements;

    foreach (EA.Element element in elements)
    {
        elementsOfElement.Add(element);
        elementsOfElement.AddRange(getElementsOfElement(element));
    }
    return elementsOfElement;
}

Sparx Systems Enterprise Architect + C#: Recursively get all elements and sub elements of an EA Element

Problem

All elements of an subelement should be catched

Approach – Solution

Recursively iterate through all subelements and AddRange (already a list) to the List.
The interuption of the recursion is, when an empty List is used as parameter for AddRange, because the loop will not be called where the AddRange-Method is in on next iteration.

Solution – Approach

private List<EA.Element> getElementsOfElement(EA.Element actualElement)
{
    List<EA.Element> elementsOfElement = new List<EA.Element>();
    EA.Collection elements;
    elements = actualElement.Elements;

    foreach (EA.Element element in elements)
    {
        elementsOfElement.Add(element);
        elementsOfElement.AddRange(getElementsOfElement(element));
    }
    return elementsOfElement;
}

Sparx Systems Enterprise Architect + C#: Get Top Package from selected Package

Problem

The top package of an Enterprise Architect Project EAP should be retrieved

Approach

Navigate to the Parent ID until the package return null.

Solution – Lösung

public EA.Package getTopPackage(EA.Package selectedPackage)
{
    EA.Package package = null;
    try
    {
        // If it has a parent it won't throw an error
        package = repository.GetPackageByID(selectedPackage.ParentID);
    }
    catch (System.Runtime.InteropServices.COMException)
    {
        // The selectedPackage is the top package
        package = selectedPackage;
    }
    // If this is not the top package, recall this function
    if (package != selectedPackage)
    {
        package = getTopPackage(package);
    }
    return package;
}

IBM Doors DXL: Import from Sparx Systems Enterprise Architect

Problem

To refresh all UML Diagrams within an Formal Doors Module, a script is needed that imports all actual versions of a UML Diagram.

Approach

* Copy the GUID of the Diagram / Note
** right-click the project menu on the right side.
** Select „Copy Reference -> Copy Node GUID to clipboard“
* If not done before, create an Module attribute (here ATTRIBUT_WITH_GUID)
* Paste the GUID to the place in your doors module, where the diagram should be created

Solution

// Recommended as module plugin
/**
 * Run through all objects of the current module 
 * and watch for the attribute ATTRIBUT_WITH_GUID.
 * If it is not empty, fetch the diagram/note
 * from this attribute an load it into the 
 * Object Text of Sparx Systems Enterprise Architect
 */

Object o
Filter currentFilter, f
Module m
DB updateWin

string guid
string projectPath = "DP_ENTARCHITECT --- DBType=1;" //-
"Connect=Provider=SQLOLEDB.1;Integrated Security=SSPI;" //-
"Persist Security Info=False;Initial Catalog=DBNAME;" //-
"Data Source=EA_DB_SERVER"

OleAutoObj eaProject, eaRepository, diaObj, actObj
OleAutoArgs autoArgs = create
string diagramName, elementNotes

void establishInterface()
{
 eaProject = oleGetAutoObject("EA.Project")
 if(null(eaProject))
 { 
   eaProject = oleCreateAutoObject("EA.Project")
 }

 if(null(eaProject))
 {
   ack "Creating OLE-Object 'EA.Project' not possible."
   progressStop
   halt
 }

 eaRepository = oleGetAutoObject("EA.Project")

 if(null(eaRepository))
 {
   eaRepository = oleCreateAutoObject("EA.Repository")
 }

 if(null(eaRepository))
 {
   ack "Creating OLE-Object 'EA.Repository' not possible."
  progressStop
  halt
 }

 put(autoArgs,projectPath)
 oleMethod(eaRepository,"OpenFile",autoArgs)
 clear(autoArgs)
}

void closeInterface()
{
  oleMethod(eaRepository,"CloseFile",autoArgs)
  oleMethod(eaRepository,"Exit",autoArgs)
	
  delete(autoArgs)
}

void updateObjects()
{
 int progessbarStep = 0
 bool diagramCopied
 string fileName

 for o in m do
 {
  guid = o."BB_ImageFileName"
  diagramCopied = false
	
  if(guid != "" && guid[0:0] == "{")
  {
   progressMessage("Updating " identifier(o))
   put(autoArgs, guid)
		
   /**************************************************
    * Gets a pointer to a diagram using an absolute 
    * reference number (local ID). This is usually 
    * found using the DiagramID property of an 
    * element, and stored for later use to open a diagram 
    * without using the 
    * collection GetAt() function.
    **************************************************/
    oleMethod(eaRepository, "GetDiagramByGuid", autoArgs, diaObj)
		
    /*************************************************
     * An Element is the main modeling unit. 
     * It corresponds to (for example) a Class, 
     * Use Case, Node or Component. 
     * You create new elements by adding to the 
     * Package Elements collection. 
     * Once you have created an element, you can add it
     * to the DiagramObject 
     * Class of a diagram to include it in the diagram.
     ****************************************************/
     oleMethod(eaRepository, "GetElementByGuid", autoArgs, actObj)
     clear(autoArgs)
		
     // Wenn das Diagramm mit der GUID nicht gefunden wurde
     if(null(diaObj))
     {
      // ... und auch kein Element mit der GUID gefunden wurde
      if(null(actObj))
      {
         ack "GUID " guid " of object " identifier(o) "" //-
            " not found in EA as diagram or element."
      }
      else
      {
        // Es wurde kein Diagramm, aber ein Element 
        // mit der GUID gefunden
        oleGet(actObj, "Notes", elementNotes)
        if(null(elementNotes))
        {
          ack "Getting Notes of GUID " guid " failed."
        }
        else
        {
	 o."Object Text" = elementNotes
	 progressStep(++progessbarStep)
        }
      }
    }
    else // Es wurde ein Diagramm mit der GUID gefunden
    {					
      /***************************************************
       *  EA.Project.PutDiagramImageOnClipboard: 
       *  ======================================
       *  Copies an image of the specified diagram to the 
       *  clipboard.
       *	
       *  Parameters:
       *	•	DiagramGUID: String - the GUID 
       *        (in XML format) 
       *        of the diagram 
       *        to copy
       *	•	Type: Long - the file type
       *		•	If Type = 0 then it is a metafile
       *		•	If Type = 1 then it is a Device 
       *            Independent Bitmap
       *
       *****************************************************/
       put(autoArgs, guid)
       put(autoArgs, 0)				
       oleMethod(eaProject, "PutDiagramImageOnClipboard", autoArgs, 
	             diagramCopied)
       clear(autoArgs)
		
       if(diagramCopied)
       {
         while(oleDelete(o));
         if(olePaste(o))					
         {
	       oleSetMaxWidth(o."Object Text", 400)
	        progressStep(++progessbarStep)
         }
         else
         {
	       ack "Pasting from clipboard of GUID " guid " failed."
         }
       }
       else
       {
         ack "Copying to clipboard of GUID " guid " failed."
       }
     }
	 diaObj = null
     actObj = null
   }
  }
}

void main(DB updateWin)
{
  int accepted, rejected

  currentFilter = current	
  f = contains(attribute("ATTRIBUT_WITH_GUID"),"{", false)
  set(m, f, accepted, rejected)
  unApplyFiltering(m)
  progressStart(updateWin, "Executing update", "Processing...", accepted)
  establishInterface()
  updateObjects()
  if(!null(currentFilter)) set(m, currentFilter, accepted, rejected)
  closeInterface()
  progressStop
  refresh(m)
  hide(updateWin)
  destroy(updateWin)
  updateWin = null
}

m = current
if(null(m))
{
  ack "Update can only be started within a module."
}
else
{
 if(isEdit(m))
 {
   updateWin = create("Update EA ", styleCentered|styleFloating)
   label(updateWin, "Do you wish to update?")
   ok(updateWin, main)
   show updateWin
 }
 else
 {
  ack "Module must be opend in 'Exclusive Edit'."
 }
}