Sparx Systems Forum
Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: bilon on May 03, 2018, 12:02:22 am
-
I have simple script changing single property of elements attribute and subsequently iterating through elements attributes to display the new value. But the old value is displayed. Here is the script:
Sub Main
Dim tab AS EA.Element
Dim col AS EA.Attribute
For Each tab In Repository.GetTreeSelectedElements
For Each col In tab.Attributes
If col.Name = "TABLE_NAME" Then
Select Case col.Type
Case "varchar"
col.Type = "char"
col.Update
Session.Output "1: " & col.Name & " " & col.Type
'1: TABLE_NAME char
Case Else
End Select
End If
Next
'tab.Attributes.Refresh
For Each col In tab.Attributes
If col.Name = "TABLE_NAME" Then
Session.Output "2: " & col.Name & " " & col.Type
'2: TABLE_NAME varchar
End If
Next
Next
End Sub
The new value is correctly saved to model, so why it is not displayed in the script?
-
You need a Refresh of the collection in between the 2 loops.
q.
-
As you can see, Refresh is already there. But commented, because it doesn't help.
-
I think you need to
tab.Refresh()
as well.
Notes: Refreshes the element features in the Project Browser.
Usually called after adding or deleting attributes or methods, when the user interface is required to be updated as well.
/U
-
I might be wrong, but the refresh.line seems to be commented out.
q.
-
Yes, but he's refreshing the Collection, not the Element. According to the API documentation, you need to refresh the element after adding attributes.
/U
-
I shouldn't lean in too much without testing, but refresh is to update a collection. He's changing elements of a collection and you need to tell EA to get the updates since it's using a cache of the old values. Since the refresh is commented in his code, this must be the culprit.
q.
-
The reason why tab.Attributes.Refresh is commented, is, that tested it but it didn't work, so I commented it. Even tab.Refresh doesn't work!
-
Well, I just ran your script and it worked with NO issues.
q.
-
I shouldn't lean in too much without testing, but refresh is to update a collection.
I know, but it's implemented by other classes as well, and the user guide (as quoted in my first post) explicitly states that you need to Refresh() the Element itself after adding attributes.
Well, I just ran your script and it worked with NO issues.
.... and that's the end of this argument. :)
/U
-
What do you mean with "worked with no issues"? Does it mean, that the output looked like this:
1: TABLE_NAME char
2: TABLE_NAME char
-
Lucky you, it was still in my output window:
1: TABLE_NAME char
2: TABLE_NAME varchar
1: TABLE_NAME char
2: TABLE_NAME varchar
(output from 2 runs).
q.
-
In this case, it isn't definitely without issues. The issue is, that after setting datatype to "char" in step 1, it is displayed as "varchar" in step 2. They should be same.
-
Ah, now I see. Unfortunately you reply here in long distances so I'm out each time. I see that the 2nd loop shows the non-updated values. But EA is doing the change (this is why I posted that it works correctly) since afterwards the types ARE changed. So the question is: why does the 2nd loop show the wrong values. Hmm. Tricky indeed.
q.
-
Replacing the 2nd loop like this
dim tab1 as EA.Element
set tab1 = Repository.GetElementByID(tab.ElementID)
For Each col In tab1.Attributes
If col.Name = "TABLE_NAME" Then
Session.Output "2: " & col.Name & " " & col.Type
'2: TABLE_NAME varchar
End If
Next
fixed the issue. Honestly, I never ever used a collection more than once. This Refresh-thingy is just fishy in my opinion. And obviously it's broken too. Feel free to send a bug report.
q.
P.S. Probably the Refresh has only an effect if you added or removed elements from it. In any case it's broken.
-
in relation to Element.Update ,
How exactly does this work?
Updates the Element in the database and the model?
What happens when the Element is locked. Does it force Update?
Thank you.
-
Update save the changed properties of your EA.Element instance in memory to the database.
If the element is read-only because of locking you'll get an exception.
Geert
-
Update save the changed properties of your EA.Element instance in memory to the database.
If the element is read-only because of locking you'll get an exception.
Geert
thank you Geert , much appreciated
-
Yes, this way it is described in the documentation: "Should be called after adding a new item or after deleting an item".
-
Most times the question is "what does the documentation not say?". So it does not tell that altered elements of the collection still are shown unaltered unless you re-fetch the whole element (and its collections) anew.
q.
-
I've contacted sparx, they said it's an issue, it should be fixed sometimes :-) in the future.