Example: Specifying a Script Action

The Script action lets a framework user execute a script in the node or cell. You can use the script to create a project document and attach it to the node or cell.

In the following example, we will specify an action in a framework diagram node that enables the user to execute a script that generates a model from another model. The result of this example is the same as that of a Generation action, but the script is intended to demonstrate the possibilities that you can script in your own projects. You could, for example, write a script to connect to a live database and reverse engineer it into a cell. For information about writing scripts for use in your models and projects, see "Methods (Profile)" in .

Before you can specify a script action, you have to create a method script on the FrameworkAction metaclass in an extension, and then attach it to your project. In this case, the following script is called GenerateModel, and will generate a PDM:

Sub %Method%(obj)

   ' This is a sample method that can be used in a script action
   ' It generates a PDM from a source cell CDM and attaches it to the current cell
   ' Steps:
   ' 1- Get the source cell: found by its code "mySourceCell"
   ' 2- Get the source model: first document in the source cell
   ' 3- Generate a PDM from the source model
   ' 4- Attach the generated model to the current cell
   ' 5- Set current action as source action for the new document
   ' => That will prevent generating twice if the action is not multiple
   
   If obj Is Nothing Then Exit Sub

   Dim sourceCell, targetCell, modelDoc, sourceModel, targetModel

   ' The script is defined on the action so the current cell is simply the action parent
   Set targetCell = obj.Parent

   ' First check if we can execute the action on the current cell
   ' for non multiple actions, the CanExecute should return true only the first time
   If obj.CanExecute(targetCell) Then
      ' Get Source cell
      Set sourceCell = FindCellByCode(targetCell.Parent, "mySourceCell") ' See function code below End Sub statement

      ' Get source model (supposed to be the first in its artifact document list)
      Set modelDoc = sourceCell.ArtifactDocuments.Item(0)
      Set sourceModel = modelDoc.TargetModelObject

      ' Generate PDM Model
      Set targetModel = sourceModel.GenerateModel (Nothing, PdPDM.cls_Model)

      ' Attach generated model to current cell
      Set modelDoc = targetModel.SourceModelDocument
      targetCell.AttachDocument(modelDoc)

      ' Set current action as source for the new model document
      obj.SetAsSource(modelDoc)
   Else
      ' In this sample, the action is supposed to be non-multiple
      ' Therefore, CanExecute fails if it's already a source action for an existing document
      output "The action has already been executed"
   End If
End Sub


' FindCellByCode function (Global Script function):

Function FindCellByCode (fmx, Code)
   Set FindCellByCode = Nothing
   Dim Cell
   For Each Cell In fmx.cells
      If Cell.Code = Code Then
         Set FindCellByCode = Cell
         Exit For
      End If
   Next 
End Function

>>
  1. Double-click a framework node to open its property sheet, and click the click the Actions tab.
  2. On the General tab, enter Generate Model Script in the Name field.
  3. Clear the Multiple check box to specify that exactly one generated model can be attached to the node.
  4. Click the Detail tab, and specify the following properties in the fields (see Framework Action Property Sheet Detail Tab).

    Property

    Select

    Type

    Script

    Method

    GenerateModel


    scriptaction
  5. Click OK to close the action and node property sheets and return to the framework diagram.

    The Script action is now available for use by framework diagram users (see Attaching a Document to a Framework Node and Cell).