Creating and Editing a Macro

www.altova.com Print this Topic Previous Page Up One Level Next page

Home >  UModel Programmer's Reference > Scripting Editor > Macros >

Creating and Editing a Macro

The following operations enable you to create a new macro and edit an existing macro.

 

 

Creating a new macro

Right-click the Macro folder in the Scripting Projects tree and select Add Macro from the context menu. (The Add Macro command can also be selected from the context menu of any item in the Scripting Projects tree.) Alternatively, click the New Macro icon in the toolbar of the Scripting Projects tree.

 

The newly created (and empty) macro document is displayed in the Main Window, and the name of the macro is displayed in the title bar of the Scripting Editor (screenshot below).

 

ScrMacroTree

 

 

Naming or renaming a macro

To name or rename a macro, click the macro name in the Scripting Project tree and press the F2 function key, or right click the name and select Rename from the context menu.

 

 

Opening a macro

To open a macro, right-click the macro in the Macros folder of the Scripting Project tree (see screenshot above), and select the Open command. The macro is displayed in the Main Window and its name is displayed in the title bar of the Scripting Editor (screenshot below). Alternatively, double-clicking a macro in the Scripting Project tree opens it in the Main Window.

 

The screenshot shows the "Find Sample" macro of the Bank_MultiLanguage.ump project file available in the ...\UmodelExamples folder.

 

scr-mac-open

 

 

 

 

Editing the macro

To edit a macro, enter or edit its code in the Main Window.

 

An example of a macro is given below. This macro finds UML elements by name and type.

 

// ############################################

// 'Find Sample' macro

// Find UML model elements by name and type

// ############################################

 

// global variables of macro

var bMatchCase                = false;

var bMatchWholeName = false;

 

var bSearchClasses                = true;

var bSearchInterfaces        = true;

var bSearchEnumerations        = true;

var bSearchPackages                  = true;

var bSearchProperties          = true;

var bSearchOperations          = true;

var bSearchEnumLiterals        = true;

var bSearchOthers                = false;

 

// import System.Windows.Forms namespace

CLR.Import( "System.Windows.Forms" );

 

// verify if UML element kind should be considered

function MatchKind( objUMLElement )

{

 if ( objUMLElement.KindName == "Class"                                 ) return bSearchClasses;

 if ( objUMLElement.KindName == "Interface"                         ) return bSearchInterfaces;

 if ( objUMLElement.KindName == "Enumeration"                ) return bSearchEnumerations;

 if ( objUMLElement.KindName == "Package"                         ) return bSearchPackages;

 if ( objUMLElement.KindName == "Property"                         ) return bSearchProperties;

 if ( objUMLElement.KindName == "Operation"                         ) return bSearchOperations;

 if ( objUMLElement.KindName == "EnumerationLiteral"        ) return bSearchEnumLiterals;

 return bSearchOthers;

}

 

// verify if UML element name matches search options

function MatchName( objUMLNamedElement, sFindText )

{

 var sName = objUMLNamedElement.Name;

 if ( !bMatchCase )

         sName = sName.toUpperCase();

 

 if ( bMatchWholeName )

         return ( sName == sFindText )

 else

         return ( sName.indexOf( sFindText ) != -1 )

}

 

// recursively search all elements

function Find( objUMLElement, sFindText )

{

 // we are only interested in named elements:

 if ( objUMLElement.IsKindOf( "NamedElement" ) )

 {

         var objUMLNamedElement = objUMLElement;

         

         // only list elements where kind and name match:

         if ( MatchKind( objUMLNamedElement ) && 

              MatchName( objUMLNamedElement, sFindText ) )

         {

                 // create a new ListViewItem class

                 var newItem = CLR.Create( "ListViewItem", objUMLNamedElement.KindName );

                 // and add it to the listview - items:

                 lastForm.lvResults.Items.Add( newItem );

                 // append name and qualified name in sub-items:

                 newItem.SubItems.Add( objUMLNamedElement.Name );

                 newItem.SubItems.Add( objUMLNamedElement.QualifiedName );

         }

 }

 

 // also iterate over all children (ownedElements):

 var ownedElements = objUMLElement.OwnedElements;

 var itr = new Enumerator( ownedElements );

 for ( ; !itr.atEnd(); itr.moveNext() )

         Find( itr.item(), sFindText );

}

 

// create and show the form:

ShowForm( "FindSample" );

 

 

Note:Macros do not support parameters or return values.

 

 

Setting a macro as an Auto-Macro

When a macro is set as an Auto-Macro it can be run automatically when: (i) UModel is started, or (ii) an Altova UModel project is loaded in UModel. To specify whether Auto-Macros should be run in each of these two events, check the Run Auto-Macros option in the Automatic Script Processing pane of the relevant dialogs:

 

When UModel is started: the Scripting tab of the UModel Options dialog (Tools | Options menu command).
When an UModel project is loaded into UModel: the Scripting dialog (screenshot below, Project | Scripting Settings menu command).

 

scr-mac-auto

 

To set a macro as an Auto-Macro, right-click the macro in the Scripting Project tree and select the command Set as Auto-Macro. This is a toggle command; so to remove the Auto-Macro setting of a macro, select the command again.

 


© 2019 Altova GmbH