Author Topic: How do I set just the stereotype of an element in scripting?  (Read 517 times)

mse

  • EA User
  • **
  • Posts: 97
  • Karma: +1/-0
    • View Profile
How do I set just the stereotype of an element in scripting?
« on: August 31, 2019, 03:02:18 am »
I'm trying to make a selection of elements and set the stereotype field of those elements using scripting. I started off with the MultiSelectionExample script. I thought I could add just this one line:

   
Code: [Select]
set theElement.Stereotype = "requirement"
That gives me an error stating that an object is required.

How can I set the stereotype of multiple elements in a package?

Here is the source code:

Code: [Select]
option explicit

!INC Local Scripts.EAConstants-VBScript

'
' Outputs the details of elements selected in the Project Browser
'
' Related APIs
' =================================================================================
' Repository API - http://www.sparxsystems.com/enterprise_architect_user_guide/12.1/automation_and_scripting/repository3.html
'
sub MultiSelectionExample()

' Show the script output window
Repository.EnsureOutputVisible "Script"

Session.Output( "VBScript Project Browser Multi Selection Example" )
Session.Output( "=======================================" )

' Get the set of selected items as a collection
dim selectedElements as EA.Collection
set selectedElements = Repository.GetTreeSelectedElements()

dim selectedElementCount, i
selectedElementCount = selectedElements.Count

if selectedElementCount > 0 then
' Iterate over all of the selected elements

for i = 0 to selectedElementCount - 1
dim theElement as EA.Element
set theElement = selectedElements.GetAt( i )

' Output the details of the current selected element
dim message
message = "Element Name: " & theElement.Name
message = message & vbCrLf & "GUID: " & theElement.ElementGUID
message = message & vbCrLf & "Type: " & theElement.Type
message = message & vbCrLf & "Stereotype: " & theElement.StereotypeEx
message = message & vbCrLf & "ID: " & theElement.ElementID
message = message & vbCrLf & vbCrLf

set theElement.Stereotype = "requirement"

Session.Output( message )
next

Session.Output( "Done!" )
else
' No item selected in the tree, or the item selected was not an element
Session.Prompt "This script requires one or more elements to be selected in the Project Browser." & _
vbCrLf & "Please select a range of elements in the Project Browser and try again.", promptOK
end if

end sub

MultiSelectionExample


qwerty

  • EA Guru
  • *****
  • Posts: 10497
  • Karma: +231/-190
  • I'm no guru at all
    • View Profile
Re: How do I set just the stereotype of an element in scripting?
« Reply #1 on: August 31, 2019, 04:44:02 am »
Use StereotypeEx instead of Stereotype.

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 9377
  • Karma: +258/-27
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: How do I set just the stereotype of an element in scripting?
« Reply #2 on: August 31, 2019, 03:02:15 pm »
You may only use the "set" keyword when setting objects.
Code: [Select]
set myObjectVariable = someObjectPrimitive types like string or integer you should just use
Code: [Select]
myPrimitiveVariable = somePrimitiveValue
Geert

mse

  • EA User
  • **
  • Posts: 97
  • Karma: +1/-0
    • View Profile
Re: How do I set just the stereotype of an element in scripting?
« Reply #3 on: September 02, 2019, 05:05:31 pm »
I made a small change based on everyone's comments:

Code: [Select]
theElement.StereotypeEx = "requirement"
That has no affect however when running the script. Clearly the script is going through each selected element in the project browswer, but the stereotype does not change.

qwerty

  • EA Guru
  • *****
  • Posts: 10497
  • Karma: +231/-190
  • I'm no guru at all
    • View Profile
Re: How do I set just the stereotype of an element in scripting?
« Reply #4 on: September 02, 2019, 05:14:04 pm »
You never update anything in your script.

q.

mse

  • EA User
  • **
  • Posts: 97
  • Karma: +1/-0
    • View Profile
Re: How do I set just the stereotype of an element in scripting?
« Reply #5 on: September 02, 2019, 05:21:32 pm »
Apparently I have the wrong idea of how to update. I thought a simple assignment would do the trick. How do I update the stereotype then?

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 9377
  • Karma: +258/-27
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: How do I set just the stereotype of an element in scripting?
« Reply #6 on: September 02, 2019, 05:34:17 pm »
After changing stuff on an API object you have to call Update() to save those changes to the database.

Geert

mse

  • EA User
  • **
  • Posts: 97
  • Karma: +1/-0
    • View Profile
Re: How do I set just the stereotype of an element in scripting?
« Reply #7 on: September 02, 2019, 05:44:49 pm »
That worked perfectly. All requirements are now converted to SysML requirements. Thanks!  :)

One more follow-up question. I would like to transfer some text from two tagged values to the standard SysML .id and .text fields. However, just writing

Code: [Select]
theElement.id = ...
theElement.text = ...

is not enough, the fields are not recognized even though they are part of the SysML requirement element. How can I update these fields?

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 9377
  • Karma: +258/-27
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: How do I set just the stereotype of an element in scripting?
« Reply #8 on: September 02, 2019, 06:06:12 pm »
I guess these are stored as tagged values (or is text stored in the notes field)

If tagged values then you'll have to loop the tagged values and find the one with name ID.
You can then set it's value and save it (by calling update())

Geert

mse

  • EA User
  • **
  • Posts: 97
  • Karma: +1/-0
    • View Profile
Re: How do I set just the stereotype of an element in scripting?
« Reply #9 on: September 02, 2019, 09:38:04 pm »
Looping through tagged values and identifying by name did the job just fine. And of course do not forget to Update(). Thanks again for your help!  :)