Book a Demo

Author Topic: Finding Parent Lane, Pool  (Read 7752 times)

djdejong

  • EA User
  • **
  • Posts: 32
  • Karma: +2/-0
    • View Profile
Finding Parent Lane, Pool
« on: December 23, 2016, 05:07:43 am »
(VBScript)

Is there a way to find the lane an element is located in?  I've been trying to use this through the diagram object,s but perhaps looking through the packages is better?  I hesitated to do that because they're elements and not packages.

To be clear, for something like this in Project Browser:

Pool 1
   Lane 1
      Element 1
      Element 2
   Lane 2
      Element 3
      Element 4
Pool 2
    Lane 3
      Element 5
      Element 6
   Lane 4
      Element 7
      Element 8
     

Want to be able to determine that, for example, Element 5 is located in Lane 3 in Pool 2.
« Last Edit: December 23, 2016, 05:11:03 am by djdejong »

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Finding Parent Lane, Pool
« Reply #1 on: December 23, 2016, 06:51:57 am »
Just look into the element's ParentID, provided you have placed it in the lane not only graphically.

q.

djdejong

  • EA User
  • **
  • Posts: 32
  • Karma: +2/-0
    • View Profile
Re: Finding Parent Lane, Pool
« Reply #2 on: December 23, 2016, 07:12:55 am »
This is what I tried right away, but I get an internal application error here so I assumed ParentID didn't actually get the parent element:

Code: [Select]
set tempElement = getElementByID(tempElement.ParentID)
You're saying that it should? I suspected that ParentID returned a package ID and that's where I was going wrong. If it's indeed an element, any clues as to why my code fails?

djdejong

  • EA User
  • **
  • Posts: 32
  • Karma: +2/-0
    • View Profile
Re: Finding Parent Lane, Pool
« Reply #3 on: December 23, 2016, 07:14:45 am »
Also I did place it graphically in the lane, but that automatically put it in the lane within Project Browser as well.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Finding Parent Lane, Pool
« Reply #4 on: December 23, 2016, 07:17:30 am »
What do you seen when printing the ParentID. You can do a search in the SQL browser like
Code: [Select]
SELECT * FROM t_object WHERE object_id = <thenumber>
Also: aren't you missing Repository.getElementById ?

q.

djdejong

  • EA User
  • **
  • Posts: 32
  • Karma: +2/-0
    • View Profile
Re: Finding Parent Lane, Pool
« Reply #5 on: December 23, 2016, 07:35:34 am »
It seems to work without using "Repository." first. What's the distinction?

Also I think I've figured it out; the error was because earlier code was setting tempElement to an element that had no parent element. I've done a test it am able to get an element and it's parent element using the ParentID.

djdejong

  • EA User
  • **
  • Posts: 32
  • Karma: +2/-0
    • View Profile
Re: Finding Parent Lane, Pool
« Reply #6 on: December 23, 2016, 08:51:39 am »
BTW anyone that read this interested in the solution. Probably not most efficient way but it works. Not sure if this is even a super useful functionality, but I was unsure how to filter well within the section of document editor so this makes it easy to filter by tagged values. (EDIT: just realized it'll tell you that the tags are reset even if you cancel, but it won't do anything if you do cancel)

Code: [Select]
option explicit

!INC Local Scripts.EAConstants-VBScript

'
' Script Name:
' Author:
' Purpose: At tags to elements located inside swimlanes with name "Actor" and value of the swimlane name (and remove "Actor" tags from elements not located in a swimlane)
' Date:
'
'
sub OnDiagramScript()

'GET DIAGRAM REFERENCE
dim currentDiagram as EA.Diagram
set currentDiagram = Repository.GetCurrentDiagram()

if not currentDiagram is nothing then
' Get a reference to any selected connector/objects
dim selectedConnector as EA.Connector
dim selectedObjects as EA.Collection
set selectedConnector = currentDiagram.SelectedConnector
set selectedObjects = currentDiagram.SelectedObjects

if not selectedConnector is nothing then
' A connector is selected
elseif selectedObjects.Count > 0 then
' One or more diagram objects are selected
else
' Nothing is selected
end if
else
Session.Prompt "This script requires a diagram to be visible", promptOK
end if


'ADD THE ACTOR TAGS BY SWIM LANE

'ask user
dim result : dim clearTags
result = MsgBox("This script will reset and add 'Actor' tags for all diagram objects. Do you wish to proceed?", vbYesNo + vbQuestion, "Reset Actor Tags")

Select Case result
Case vbYes
MsgBox "Script initiated..."
clearTags = true
Case vbNo
MsgBox "The script has been terminated by user.",vbinformation,"Script cancelled"
clearTags = false
End Select

'clear and reset actor tags
if clearTags then
'get a list of the lanes in diagram
dim arrListLanes
set arrListLanes = CreateObject("System.Collections.ArrayList")
set arrListLanes = getDiagramObjects(currentDiagram, "Lane")

'tag elements by lane
tagActors currentDiagram, arrListLanes
end if

msgbox "Actor tags have been reset.", vbinformation, "Script complete"

end sub




'removes actor tags then re-tags
function tagActors(currentDiagram, arrListLanes)

dim poolArrList, i, poolCount, laneCount, tagUpdated
dim tempElement as EA.Element
dim parentElement as EA.Element
dim laneElement as EA.Element
dim tag as EA.TaggedValue
dim diagramObject as EA.DiagramObject

poolCount = 0 : laneCount = 0 : tagUpdated = false

'clear any existing actor tags from diagram objects
for each diagramObject in currentDiagram.DiagramObjects
set tempElement = getElementByID(diagramObject.ElementID)
dim tags as EA.Collection
set tags = tempElement.TaggedValues

for i = tags.Count - 1 to 0 step -1
dim theTag as EA.TaggedValue
set theTag = tags.GetAt(i)
if theTag.Name = "Actor" then
call tempElement.TaggedValues.DeleteAt(i, FALSE)
end if
next
next
'tag the desired elements
for each diagramObject in currentDiagram.DiagramObjects
set tempElement = getElementByID(diagramObject.ElementID)
for i = 0 to arrListLanes.count-1
dim laneID : laneID = arrListLanes(i).ElementID
if tempElement.ParentID = laneID then
set parentElement = getElementByID(tempElement.ParentID)
if parentElement.Stereotype = "Lane" then
for each tag in tempElement.TaggedValues
if tag.Name = "Actor" and tag.Value <> parentElement.Name then
tag.Value = parentElement.Name
tag.Update
parentElement.TaggedValues.Refresh
tagUpdated = true
end if
next

if not tagUpdated then
set tag = tempElement.TaggedValues.AddNew("Actor","")
tag.Value = parentElement.Name
tag.Update
parentElement.TaggedValues.Refresh
end if
end if
end if
next
next

end function


'returns array list of diagram objects of specified stereotype
function getDiagramObjects(diagram, elementStereoType)

dim selectedObjectsList
set selectedObjectsList = CreateObject("System.Collections.ArrayList")
dim diagramObject as EA.DiagramObject
dim diagramConnector as EA.Connector
dim element as EA.Element

for each diagramObject in diagram.DiagramObjects
set element = Repository.GetElementByID(diagramObject.ElementID)
if element.StereoType = elementStereoType then
selectedObjectsList.Add diagramObject 'why don't I just add the element instead of the object..?
end if
next

'return selected Elements
set getDiagramObjects = selectedObjectsList

end function 'end getDiagramObjects

OnDiagramScript
« Last Edit: December 23, 2016, 09:01:50 am by djdejong »

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Finding Parent Lane, Pool
« Reply #7 on: December 23, 2016, 09:31:58 am »
It seems to work without using "Repository." first. What's the distinction?
Repository is the object providing the operation. I guess it is a convenience in the internal VB interpreter (I was not aware of).

q.