UML & SysML modelling languages

Expertise and blog articles on UML, SysML, and Enterprise Architect modelling tool

version francaiseTwitterVideos UMLChannel SparxSystems EA YouTubeLinkedIn
Thursday, 23 January 2014 21:52

Sparx Enterprise Architect script : XMI file batch import

Written by
Rate this item
(1 Vote)

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.

enterprise architect scripting view

Step 4: open the script and copy/paste the following content (or download the VBScript text file here). 

  1. option explicit
  2. !INC Local Scripts.EAConstants-VBScript
  3. '
  4. ' Script Name: XMIImportFiles
  5. ' Author: G.Finance guillaume[at]umlchannel.com 
  6. ' Purpose: Batch import of XMI files to the selected package
  7. ' Date: 13/01/2014
  8. ' HOW TO USE  : 1. Set the target directory in the targetFolderA variable (use \\ for each backslash) ; you may enable additional folders if required
  9. '               2. Add each XMI filename to process into the "fileList" array variable
  10. '             3. Run the script
  11. '
  12. '
  13. ' [USER ACTION 1]: update the target folder where the files to process are located
  14. dim targetFolderA
  15. targetFolderA = "C:\\temp\\"
  16. ' Additional target folders to use if required
  17. 'dim targetFolderB
  18. 'targetFolderB = "D:\\"
  19. 'dim targetFolderC 
  20. 'targetFolderC = "E:\\"
  21.  
  22.  
  23. ' ArrayList Object to store the name of each file to process (including the folder name, using one of the "targetFolder" variables)
  24. dim fileList
  25. Set fileList = CreateObject("System.Collections.ArrayList")
  26. ' [USER ACTION 2]: update the following list with the name of each file to process, specifying first the associated targetFolder variable
  27. fileList.Add targetFolderA + "class.xml"
  28. fileList.Add targetFolderA + "deploy.xml"
  29. fileList.Add targetFolderA + "uc.xml"
  30. ''add new lines above if required
  31.  
  32. dim projectInterface as EA.Project
  33. dim result
  34. set projectInterface = Repository.GetProjectInterface()
  35. sub XMIImportFiles()
  36. Repository.EnsureOutputVisible "Script"
  37. Repository.ClearOutput "Script"
  38. Session.Output( "VBScript XMI Import Files" )
  39. Session.Output( "=======================================" )
  40. ' Get the selected package to import the XMI files 
  41. dim contextObjectType
  42. contextObjectType = Repository.GetContextItemType()
  43. ' Check if the selected element is a package ; if not display a message and exit
  44. if contextObjectType = otPackage then
  45. ' Get the context object as a package
  46. dim contextPackage as EA.Package
  47. set contextPackage = GetContextObject()
  48. Session.Output( "Selected Package : " & contextPackage.Name )
  49. Session.Output("TARGET PACKAGE GUID = " & contextPackage.PackageGUID & "(ID : " & contextPackage.PackageID & ")" )
  50. ' Process each XMI file set in the fileList Array Object
  51. Dim xmiFile
  52.         For Each xmiFile In fileList
  53.             Session.Output("Processing " & xmiFile & "..." )
  54. ''Import the content of the XMI file to the selected package
  55. result = projectInterface.ImportPackageXMI(projectInterface.GUIDtoXML(contextPackage.PackageGUID), xmiFile, 1, 1)
  56. Session.Output(result)
  57. Next      
  58.    }
  59. else
  60. ' Package is not currently the context item
  61. MsgBox( "This script requires a package to be selected." & vbCrLf & _
  62. "Please select a package and try again." )
  63. end if
  64. end sub
  65. 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)

scripting_enterprise-architect-batch-xmi-file-import run script

 Note: results and notification messages are displayed in the System Output view (opened automatically once the script starts running)

scripting_enterprise-architect-batch-xmi-file-import run script

 Result: the content of both XMI files is available from the selected package within the Project Browser.

scripting_enterprise-architect-batch-xmi-file-import result

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.