Author Topic: Help Correct Script  (Read 2956 times)

salayande

  • EA User
  • **
  • Posts: 224
  • Karma: +0/-0
  • I love YaBB 1 Gold!
    • View Profile
Help Correct Script
« on: December 26, 2009, 11:48:21 pm »
Dear all,

I could not get my transformation scripts to do simple tasks in EA so I decided to learn the VBScript in EA.

To learn scripting in VBScript, I created a package with two classes with stereotypes set to the "Boy Soldiers".

I then wrote the script below to change the stereotype values to "BusinessEntity" but when I ran the script, nothing happened. Could anyone suggest what I may do to improve the script. Thank you in anticipation.

Option explicit
'
' The purpose of this script is to change the stereotype of all elements  
' in a selected Package from current value to "BusinessEntity".
'
sub ChangeElementStereotype()

dim thePackage as EA.Package
set thePackage = Repository.GetTreeSelectedPackage()
      if not thePackage is nothing and thePackage.ParentID <> 0 then
      dim i
            Session.Output( "Changing the elements' stereotype" )
      'get elements
      dim elements as EA.Collection
      dim currentElement as EA.Element
      set elements = thePackage.Elements
            for i = 0 to elements.Count - 1
                  'get each element
                  set currentElement = elements.GetAt(i)
                  set currentElement.Stereotype = "BusinessEntity"
                  'update element stereotype
                  currentElement.Update()            
            Next
            'refresh elements
            elements.Refresh()
            'report element updated
            Session.Output( "Done!" )
      else
            ' No package selected in the tree
            MsgBox( "This script requires a package to be selected" )
      end if      
End sub
    



Takeshi K

  • EA User
  • **
  • Posts: 571
  • Karma: +35/-1
    • View Profile
Re: Help Correct Script
« Reply #1 on: December 28, 2009, 05:19:27 pm »
Hello,

Please add the following line before the currentElement.Update():

set currentElement.StereotypeEx = "BusinessEntity"

I think you need to set both the Stereotype and StereotypeEx.

Hope this helps,
--
t-kouno

salayande

  • EA User
  • **
  • Posts: 224
  • Karma: +0/-0
  • I love YaBB 1 Gold!
    • View Profile
Re: Help Correct Script
« Reply #2 on: December 29, 2009, 02:50:14 am »
Thank you for your quick response, T-kouno, and especially over the Xmas break.

I inserted the line as advised but nothing happened. The updated script now looks like this:

Option explicit
sub ChangeElementStereotype()

dim thePackage as EA.Package
set thePackage = Repository.GetTreeSelectedPackage()
      if not thePackage is nothing and thePackage.ParentID <> 0 then
      dim i
            Session.Output( "Working on package ")
      'get elements
      dim elements as EA.Collection
      dim currentElement as EA.Element
      set elements = thePackage.Elements
            for i = 0 to elements.Count - 1
                  'get each element
                  set currentElement = elements.GetAt(i)
                  set currentElement.Stereotype ="BusinessEntity"
                  set currentElement.StereotypeEx = "BusinessEntity"
              'update element stereotype
                  currentElement.Update
            Next
            'refresh elements
            elements.Refresh()
            'report element updated
            Session.Output( "Done!" )
      else
            ' No package selected in the tree
            MsgBox( "This script requires a package to be selected in the Project Browser.")
      end if      
End sub

Again, thank you for your help.

Segun

Takeshi K

  • EA User
  • **
  • Posts: 571
  • Karma: +35/-1
    • View Profile
Re: Help Correct Script
« Reply #3 on: December 29, 2009, 06:37:25 pm »
I have removed unnecessary lines and now it seems to work fine.
My code is:

--------------
sub ChangeElementStereotype()

      dim thePackage
      set thePackage = Repository.GetTreeSelectedPackage()
      dim i
      'get elements
      dim elements
      dim currentElement
      set elements = thePackage.Elements
      for i = 0 to elements.Count - 1
             'get each element
             set currentElement = elements.GetAt(i)
             currentElement.Stereotype ="BusinessEntity"
             currentElement.StereotypeEx = "BusinessEntity"
            'update element stereotype
             currentElement.Update
      Next
End sub

ChangeElementStereotype
--------------
Please copy the above script and paste it as EA's script (of VBScript).
My EA is 7.5.850.

Hope this helps.
--
t-kouno

salayande

  • EA User
  • **
  • Posts: 224
  • Karma: +0/-0
  • I love YaBB 1 Gold!
    • View Profile
Re: Help Correct Script
« Reply #4 on: December 29, 2009, 09:03:48 pm »
Many thanks to you, T-kouno

regards

Segun