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 import a batch of XMI files, located on a local or networked drive, within a selected package from the browser.
Important : this script is intended to easily run a one-off import of a large number of XMI files into a single target package of an EA project.
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)
- ' 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
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.