Book a Demo

Author Topic: Generating Elements in Different Sorted Order?  (Read 5497 times)

Rich Anderson

  • EA User
  • **
  • Posts: 142
  • Karma: +8/-0
    • View Profile
    • LinkedIn
Generating Elements in Different Sorted Order?
« on: March 21, 2018, 01:33:45 pm »
Hi There, 

I'm trying to add elements into a package in a particular order, but not having much luck. I understand the TreePos property needs to be set, but it does not appear to be having the effect.  If this code below worked as I wanted it to, it would add 3 classes to a package with the order "Charlie, Baker, Adams".  Instead it comes up "Adams, Baker Charlie" in  EA.  I can re-sort them manually, but I want this to be automatic.   

Code: [Select]
Dim thisPackage As Package
thisPackage = earepository.GetPackageByGuid("{22926A63-B111-45cf-94D4-49081E77E6A6}")

Dim newElement As Element

newElement = thisPackage.Elements.AddNew("Charlie", "Class")
newElement.TreePos = 1
newElement.Update()

newElement = thisPackage.Elements.AddNew("Baker", "Class")
newElement.TreePos = 2
newElement.Update()

newElement = thisPackage.Elements.AddNew("Adams", "Class")
newElement.TreePos = 3
newElement.Update()

I've tried various refresh and update commands on the package and the connectors, but to no avail. I've also tried multiple values for TreePos.   I feel like I must be missing something basic, so can anyone shed light on this for me?

Rich Anderson
Urgnt Limited

Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 8626
  • Karma: +259/-129
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
Re: Generating Elements in Different Sorted Order?
« Reply #1 on: March 21, 2018, 01:41:40 pm »
You may need to set a package property to allow non-alphabetic sorting.  You need to do this manually in the browser, so maybe check how that works "under the covers".

Also, you don't seem to be updating the various collections (such as the Elements collection).

Finally, you may need to insert the elements, then re-iterate over the collection to reset the TreePos property.

HTH,
Paolo
Inconsistently correct systems DON'T EXIST!
... Therefore, aim for consistency; in the expectation of achieving correctness....
-Semantica-
Helsinki Principle Rules!

Rich Anderson

  • EA User
  • **
  • Posts: 142
  • Karma: +8/-0
    • View Profile
    • LinkedIn
Re: Generating Elements in Different Sorted Order?
« Reply #2 on: March 21, 2018, 02:45:58 pm »
Thanks for responding so fast.   I had tried a number of combinations of updating the package and refreshing the collection of elements (there is no command to update the collection).   None of these had any effect.  I did try you last suggestion to sort these after inserting, and this code...

Code: [Select]

CODE
        Dim thisPos As Integer = 3
        ReportLine("Sorting...")
        For Each el As Element In thisPackage.Elements
            el.TreePos = thisPos
            ReportLine(el.Name & " " & thisPos)
            el.Update()
            thisPos = thisPos - 1
        Next

        ReportLine("Listing...")
        For Each el As Element In thisPackage.Elements
            ReportLine(el.Name & " " & thisPos)
        Next

CONSOLE

TESTING  AT 4:43:06 PM
Sorting...
Charlie 3
Baker 2
Adams 1
Listing...
Charlie 0
Baker 0
Adams 0

TESTING  AT 4:43:08 PM
Sorting...
Adams 3
Baker 2
Charlie 1
Listing...
Adams 0
Baker 0
Charlie 0


... shows the simple 3 member list reversing order when I "ReportLine" to my console, but not in the EA folder. It still stays in the original order if I exit EA an re-start.  I also cannot find the setting in the browser to turn alpha sorting off and on.  But it appears to be off because I am able to manually sort.    Any more ideas?

Rich Anderson
Urgnt Limited

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Generating Elements in Different Sorted Order?
« Reply #3 on: March 21, 2018, 04:23:32 pm »
You have to reload the package before the changes become visible.
(Repository.RefreshModelViews() ?)

Geert

Rich Anderson

  • EA User
  • **
  • Posts: 142
  • Karma: +8/-0
    • View Profile
    • LinkedIn
Re: Generating Elements in Different Sorted Order?
« Reply #4 on: March 22, 2018, 05:15:47 pm »
Thanks, Geert!   This code worked...

Code: [Select]
        Dim thisPackage As Package
        thisPackage = earepository.GetPackageByGuid("{22926A63-B111-45cf-94D4-49081E77E6A6}")
        Dim thisPos As Integer = 3
        ReportLine("Sorting...")
        For Each el As Element In thisPackage.Elements
            el.TreePos = thisPos
            ReportLine(el.Name & " " & thisPos)
            el.Update()
            thisPos = thisPos - 1
        Next
        earepository.RefreshModelView(thisPackage.PackageID)

It sort of makes the UI jump around, but it does resort the list.   
Rich Anderson
Urgnt Limited

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Generating Elements in Different Sorted Order?
« Reply #5 on: March 22, 2018, 06:12:02 pm »
It sort of makes the UI jump around, but it does resort the list.
I know, very annoying.
I think more recently a promising operation was added called ReloadPackage (long PackageID) but I think that didn't actually refresh the positions in the project browser.
I'm not sure what the purpose of that new operation actually is and how it differs from RefreshModelView()

Geert