My Profile
Help
Hot Topics
Top Community Contributors
Displaying items by tag: scripts
How to use the Enterprise Architect VBScript Library
The Enterprise Architect VBScript Library is an open source library of VBScripts written to be used in Enterprise Architect.
This article explains how to download, import and use the library in Enterprise Architect.
Initial Import
The Enterprise Architect VBScript Library contains some scripts to load and save scripts from/to your file system, but before we can use those we have to import an initial set of scripts to get started.
After downloading the file select menu option Project|Data Management|Import Reference Data and choose the downloaded file. Make sure to select Automation scriptsand click Import.
This will import the minimum set of scripts required to load other scripts from the file system
Download library from GitHub
From the Enterprise Architect VBScript Library project page on GitHub you can either choose to download the library as a zip file, or download the repository in GitHub desktop.
Load library into Enterprise Architect
One of the scripts in the initial set is the scriptLoadScripts in the group Script Management. If you execute this script you can choose the library folder from the library downloaded from GitHub.
The script will then scan the entire folder tree and it will load any .vbs file it can find.
For starters it might be interesting to only load the Frameworks scripts as they have the most chance of being useful to anyone.
If the LoadScripts script finds a script that already exists in Enterprise Architect it will ask to overwrite the existing scripts.
In order to know in which group the script belongs it will look for a the group indicator in the script.
1
|
'[group=Script Management] |
This will tell the script that this script should go in the group Script Management. If the script indicator is absent it will assume the group name is the name of the folder where it was found.
After loading the scripts into EA make sure to press the refresh button to make the scripts appear in the GUI.
Saving all your scripts
In the script management group there also a script to save all your scripts to the file system.
In order to control where the script should go you can add the path indicator to the script like this
1
|
'[path=\Framework\Tools\Script Management] |
The path indicator will control where to place the script relative to the chosen folder.
If the path indicator is absent the name of the script group will be used as name of the folder.
Being able to save and load the scripts from/to the file system now allows us to use version control on these scripts.
The library structure
The library is structured in two main parts.
- Projects
Contains an assortment of various scripts written for various projects. - Framework
Contains the framework scripts that are meant to be used by other scripts
- Utils
Contains helper scripts and classes such as TextFile, SQL, XML etc.. - Wrappers
Contains the wrapper classes for EA elements such as TaggedValue, Script, ScriptGroup
- Utils
The scripts in the projects folder can be used for inspiration, but it is mainly the scripts in the Framework part that are useful when writing scripts.
Using the library
The first thing you need to do when you want to use any of the framework classes is to include the framework in your script
1
|
!INC Wrappers.Include |
This will include the “Include” script that takes care of including all the other scripts of the framework, including those of the Utils folder.
Then you can use the classes defined in the library. For eaxample, if you want to have the user select a directory you can use following code
1
2
3
4
|
'get the folder from the user dim folder set folder = new FileSystemFolder set folder = folder.getUserSelectedFolder( "" ) |
The classes in the library contain both properties as operations. You can use the properties as you would expect from “real” classes.
1
2
3
4
5
|
'show messagebox with the name of each subfolder dim subfolders, subfolder for each subfolder in folder.SubFolders msgbox "subfolder name: " & subfolder.Name next |
Contributing
Have you written any VBScripts for Enterprise Architect you would like to share? If you would like to contribute to the Enterprise Architect VBScript library you can email me at This email address is being protected from spambots. You need JavaScript enabled to view it.
Extended Enterprise Architect Workshops from Hippo Software
Hippo Software's recently extended workshops are designed to expand and enhance delegates' knowledge of Enterprise Architect:
• EA Document Workshop – create custom document templates
• EA Excel Data Transfer Workshop – easily share requirements or other data
• EA Scripting Workshop – facilitate repetitive or complex tasks on models
• EA Profiles Workshop – define custom diagrams, toolboxes, elements and relationships
For more information, please visit the Hippo Software website below:
http://www.hippo-software.co.uk/pages/EA.htm
Enterprise Architect project browser scripts: sort elements by alias or by tagged value
I recently imported a number of requirements in my Enterprise Architect project with the following details: title, reference (stored in the requirement's alias), and description. By default Sparx Enterprise Architect sorts requirements within a given package by the element's name in the alphabetical order. When a package contains various types of elements (e.g. classes, interfaces, use cases, etc.), each group of elements from the same type is sorted separately. The following illustrates a mix of requirements, classes, and interfaces created within a package, in their default sorting order:
Going back to my requirements, I needed to sort them by the alias value e.g. REQ-TEST-001, REQ-TEST-002, REQ-TEST-003, etc. On the following screenshot, I illustrated on the left hand side the default sorting order in Enterprise Architect project browser, and on the right hand side the list of requirements in the desired alias order.
This article explains how to create and use an Enterprise Architect Project Browser script aimed at sorting elements from a selected package by the alias name. An additional script is provided at the end of the article to sort elements by a dedicated Tagged Value called "SortingOrder".
Enterprise Architect Project Browser scripts
Enterprise Architect lets you create project browser scripts. Once created in your project, these are available via a right click on a selected package from the browser, as illustrated below:
Sort by alias project browser script
To add the SortByAlias Project Browser script to your modelling project:
Step 1: open the scripting view using the Enterprise Architect menu Tools > Scripting.
Step 2: click on "New Project Browser Group" to store all user-defined scripts, e.g. Project Browser scripts.
Step 3: click on "New script > New VBScript" to define the "Sort By Alias" script, e.g. SortByAlias.
Step 4: open the script and copy/paste the following content (or download the VBScript text file here).
- option explicit
- !INC Local Scripts.EAConstants-VBScript
- !INC EAScriptLib.VBScript-Logging
- ' Script Name: SortbyAlias
- ' Author: Guillaume FINANCE, guillaume[at]umlchannel.com
- ' Purpose: Sort elements contained in the selected package from the Project Browser by the Alias name
- ' Date: 03/04/2014
- sub SortDictionary (objDict)
- ' constants
- Const dictKey = 1
- Const dictItem = 2
- ' variables
- Dim strDict()
- Dim objKey
- Dim strKey,strItem
- Dim X,Y,Z
- ' get the dictionary count
- Z = objDict.Count
- ' sorting needs more than one item
- If Z > 1 Then
- ' create an array to store dictionary information
- ReDim strDict(Z,2)
- X = 0
- ' populate the string array
- For Each objKey In objDict
- strDict(X,dictKey) = CStr(objKey)
- strDict(X,dictItem) = CStr(objDict(objKey))
- X = X + 1
- Next
- ' perform a a shell sort of the string array
- For X = 0 To (Z - 2)
- For Y = X To (Z - 1)
- If StrComp(strDict(X,1),strDict(Y,1),vbTextCompare) > 0 Then
- strKey = strDict(X,dictKey)
- strItem = strDict(X,dictItem)
- strDict(X,dictKey) = strDict(Y,dictKey)
- strDict(X,dictItem) = strDict(Y,dictItem)
- strDict(Y,dictKey) = strKey
- strDict(Y,dictItem) = strItem
- End If
- Next
- Next
- ' erase the contents of the dictionary object
- objDict.RemoveAll
- ' repopulate the dictionary with the sorted information
- For X = 0 To (Z - 1)
- objDict.Add strDict(X,dictKey), strDict(X,dictItem)
- Next
- ' sort the package elements based on the new sorting order
- dim newOrder
- newOrder = 0
- dim theItem
- dim eaelement
- for each objKey in objDict
- theItem = objDict.Item(objKey)
- Set eaelement = Repository.GetElementByGuid(theItem)
- 'change the position of the element in the package to the new sorting order value
- eaelement.TreePos = CLng(newOrder)
- eaelement.Update()
- newOrder = newOrder + 1
- next
- end if
- end sub
- sub sortElementsbyAlias (selectedPackage)
- LOGInfo("Processing selected package " & selectedPackage.Name)
- dim elements as EA.Collection
- dim i
- dim processedElements
- set processedElements = CreateObject( "Scripting.Dictionary" )
- set elements = selectedPackage.Elements
- for i = 0 to elements.Count - 1
- dim currentElement as EA.Element
- set currentElement = elements.GetAt( i )
- LOGInfo("Processing " & currentElement.Type & " no " & i & " with alias " & currentElement.Alias & "(" & currentElement.ElementGUID & ")")
- processedElements.Add currentElement.Alias, currentElement.ElementGUID
- next
- LOGInfo("Sorting package elements")
- SortDictionary processedElements
- end sub
- '
- ' Project Browser Script main function
- '
- sub OnProjectBrowserScript()
- Repository.ClearOutput "Script"
- LOGInfo( "Starting SortbyAlias script" )
- LOGInfo( "==============================" )
- ' Get the type of element selected in the Project Browser
- dim treeSelectedType
- treeSelectedType = Repository.GetTreeSelectedItemType()
- select case treeSelectedType
- case otPackage
- ' Code for when a package is selected
- dim thePackage as EA.Package
- set thePackage = Repository.GetTreeSelectedObject()
- sortElementsbyAlias thePackage
- Repository.RefreshModelView (thePackage.PackageID)
- case else
- ' Error message
- Session.Prompt "This script does not support items of this type.", promptOK
- end select
- end sub
- OnProjectBrowserScript
Step 5: save the script (Ctrl-S).
Step 6: right click on the target package from the Project Browser where elements need to be sorted by the alias, and select Scripts > SortbyAlias
Result: the requirements have been sorted by the alias value as illustrated below.
This sorting order is consistent with the requirements' alias values:
Sort by tagged value 'Sorting Order' project browser script
In some cases, the alias value may not be appropriate to sort your elements within the selected package. Based on the above SortByAlias script, I created the SortbyTaggedValue_SortingOrder script aimed at sorting elements using a tagged valued created for this purpose, SortingOrder.
To illustrate its use, I added the SortingOrder tagged value to each requirement:
To add the SortByTaggedValue Project Browser script to your project:
Step 1: open the scripting view using the Enterprise Architect menu Tools > Scripting.
Step 2: open the Project Browser scripts group.
Step 3: click on "New script > New VBScript" to define the "Sort By Tagged Value" script, e.g. SortbyTaggedValue_SortingOrder.
Step 4: open the script and copy/paste the following content from the script that can be downloaded here.
Step 5: save the script (Ctrl-S).
Step 6: right click on the target package from the Project Browser where elements need to be sorted by the alias, and select Scripts > SortbyTaggedValue_SortingOrder.
Result: the requirements have been sorted by the SortingOrder tagged value as illustrated below.
This article illustrated how Sparx Enterprise Architect can be tailored to introduce new features via user defined scripts, e.g. to apply a new sorting rule on the elements from a selected package. It could be further improved e.g. to sort elements within the sub packages and so on.
eaDocX v3.4 beta available
Additional document creation options from your Enterprise Architect model are now available with the release of the eaDocX v3.4 beta.
This latest eaDocX release allows you to:
- construct documents and document sections based on your model views
- use instance classifiers just like any other relationship to provide new document structures
- work with EA scripts to deliver even more tailored document formatting options
- plus other customer requested enhancements
eaDocX v3.4 is compatible with EA v11.
Read more and download a free trial at www.eadocx.com
Sparx Enterprise Architect script : XMI file batch import
A colleague recently enquired about a simple way to run a batch import of several XMI files into an Enterprise Architect project. The client's project required importing a rather large number of XMI files, created from various Enterprise Architect projects via the standard XMI export (note : each file store an extraction in the XMI format from a selected part of the modelling project). Having to import each XMI file is too cumbersome, and Enterprise Architect's existing "Batch XMI Import" is limited to controlled packages i.e. involving a VC repository like SVN set up with the current project.
This article explains how to create in your Enterprise Architect project a VBScript that can be used import a batch of XMI files, located on a local or networked drive, within a selected package from the browser.
Step 1: open the scripting view using the menu Tools > Scripting.
Step 2: click on "new normal group" to store all user-defined scripts, e.g. MyScripts.
Step 3: click on "new script > new VBScript" to define the Batch XMI Import script, e.g. BatchXMIImport.
Step 4: open the script and copy/paste the following content (or download the VBScript text file here).
- option explicit
- !INC Local Scripts.EAConstants-VBScript
- '
- ' Script Name: XMIImportFiles
- ' Author: G.Finance guillaume[at]umlchannel.com
- ' Purpose: Batch import of XMI files to the selected package
- ' Date: 13/01/2014
- ' HOW TO USE : 1. Set the target directory in the targetFolderA variable (use \\ for each backslash) ; you may enable additional folders if required
- ' 2. Add each XMI filename to process into the "fileList" array variable
- ' 3. Run the script
- '
- '
- ' [USER ACTION 1] : update the target folder where the files to process are located
- dim targetFolderA
- targetFolderA = "C:\\temp\\"
- ' Additional target folders to use if required
- 'dim targetFolderB
- 'targetFolderB = "D:\\"
- 'dim targetFolderC
- 'targetFolderC = "E:\\"
- '
- '
- ' ArrayList Object to store the name of each file to process (including the folder name, using one of the "targetFolder" variables)
- dim fileList
- Set fileList = CreateObject("System.Collections.ArrayList")
- ''[USER ACTION 2] : update the following list with the name of each file to process, specifying first the associated targetFolder variable
- fileList.Add targetFolderA + "class.xml"
- fileList.Add targetFolderA + "deploy.xml"
- fileList.Add targetFolderA + "uc.xml"
- ''add new lines above if required
- dim projectInterface as EA.Project
- dim result
- set projectInterface = Repository.GetProjectInterface()
- sub XMIImportFiles()
- Repository.EnsureOutputVisible "Script"
- Repository.ClearOutput "Script"
- Session.Output( "VBScript XMI Import Files" )
- Session.Output( "=======================================" )
- ' Get the selected package to import the XMI files
- dim contextObjectType
- contextObjectType = Repository.GetContextItemType()
- ' Check if the selected element is a package ; if not display a message and exit
- if contextObjectType = otPackage then
- ' Get the context object as a package
- dim contextPackage as EA.Package
- set contextPackage = GetContextObject()
- Session.Output( "Selected Package : " & contextPackage.Name )
- Session.Output("TARGET PACKAGE GUID = " & contextPackage.PackageGUID & "(ID : " & contextPackage.PackageID & ")" )
- ' Process each XMI file set in the fileList Array Object
- Dim xmiFile
- For Each xmiFile In fileList
- Session.Output("Processing " & xmiFile & "..." )
- ''Import the content of the XMI file to the selected package
- result = projectInterface.ImportPackageXMI(projectInterface.GUIDtoXML(contextPackage.PackageGUID), xmiFile, 1, 1)
- Session.Output(result)
- Next
- }
- else
- ' Package is not currently the context item
- MsgBox( "This script requires a package to be selected." & vbCrLf & _
- "Please select a package and try again." )
- end if
- end sub
- XMIImportFiles
Step 5: set the folder name(s) and filenames to process by updating the script's content.
- Let's say you need to import C:\old\usecases.xml and C:\current\class.xml XMI files, created from a separate EA project.
- Update targetFolderA variable on line 15, using \\ for each backslash: targetFolderA = "C:\\current\\"
- Uncomment targetFolderB variable on line 17: dim targetFolderB
- Update targetFolderB variable on line 18: targetFolderB = "C:\\old\\"
- Set the values in the ArrayList object (from line 27): fileList.Add targetFolderA + "class.xml", fileList.Add targetFolderB + "usecases.xml"
- Save (ctrl-S)
Step 6: select the target package (or view) from the Project Browser, and click on the "run script" to import all XMI Files (important : it is not possible to select a Model Root as the target)
Note: results and notification messages are displayed in the System Output view (opened automatically once the script starts running)
Result: the content of both XMI files is available from the selected package within the Project Browser.
Note : if your XMI file contains a model root, or a view (package at N+1 level) such as "test" from the above Project Browser illustration, it will be imported as a package within the selected package or view.
New ‘Enterprise Architect Scripting Workshop’ from Hippo Software
Hippo Software introduces a new 2-day ‘EA Scripting Workshop’ that teaches delegates how to write scripts to control and update models in Enterprise Architect. Delegates use VBScript to automate repetitive tasks such as updating tagged values, to manipulate diagrams, to perform a complex search and to transfer data to/from MS Excel.
Visit the Hippo Software website for details of our full range of training courses and workshops.
Hippo Software provides competitive prices for on-site or webinar training, for small or larger groups of delegates.