Book a Demo

Author Topic: [solved] 'Find in all Diagrams' in a script  (Read 8881 times)

blawton

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
[solved] 'Find in all Diagrams' in a script
« on: January 18, 2011, 09:27:25 am »
I want to make a very simple script which will step through the diagrams that include a specific element and alter them. However I can't seem to find a simple API version of the 'Find in all Diagrams' GUI functionality (described here: http://www.sparxsystems.com/uml_tool_guide/modeling_tool_features/addingobjectlinks.htm), which is exactly the list of diagrams I need to get. I tried simply using the Element.Diagrams property but that didn't seem to give me what I wanted; is there a way of doing this short of parsing through every package in my script? Just trying to avoid making a long script, figured this should be doable. Thanks!
« Last Edit: January 19, 2011, 04:45:22 am by blawton »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: 'Find in all Diagrams' in a script
« Reply #1 on: January 18, 2011, 06:12:33 pm »
Blawton,

I think the only feasible option is to use Repository.SQLQuery to get a list of diagramIDs.
I've done something similar in the past.
This statement
Code: [Select]
string diagramIDs = myRepository.SQLQuery(@"select do.diagram_id  
from t_diagramobjects do
where do.object_id = " + myElement.Object_ID);
Should return all diagram_id's in an xml string.

Geert

blawton

  • EA Novice
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: 'Find in all Diagrams' in a script
« Reply #2 on: January 19, 2011, 04:44:59 am »
Geert: Thanks, that does exactly what I need!

When I posted this thread I also submitted a support query and they were kind enough to send me a full VB solution script:

Code: [Select]
option explicit

'!INC Local Scripts.EAConstants-VBScript

sub main
    Dim CurrentElement
    Dim i, DiagramIDs
    
    Set CurrentElement = GetContextObject
    DiagramIDs = FindInDiagrams(CurrentElement)
    
    If UBound(DiagramIDs) > 0 Then
        Session.Output CurrentElement.Type & " '" & CurrentElement.Name & "' appears on " & UBound(DiagramIDs) & " diagrams."
        For i = 1 To UBound(DiagramIDs)
            Dim Diagram, id
            id = DiagramIDs(i)
            Set Diagram = GetDiagramByID(id)
            Session.Output "Diagram #" & i & ": " & Diagram.Name
        Next
    Else
        Session.Output CurrentElement.Type & " '" & CurrentElement.Name & "' does not appear on any diagrams."
    End If
end sub

'returns array of diagram ids where diagram contains the specified element
Function FindInDiagrams(Element)
    Dim sql, xml, ids(), count
    Dim doc, rows, row

    Set doc = CreateObject("MSXML2.DOMDocument")

    sql = "SELECT Diagram_ID FROM t_diagramobjects WHERE Object_ID = " & Element.ElementID
    
    xml = SQLQuery(sql)
    
    count = 0
    ReDim ids(0)
    
    If doc.loadXML(xml) Then
        Set rows = doc.selectNodes("//EADATA//Dataset_0//Data//Row")
        For Each row In rows
            count = count + 1
            redim preserve ids(count)
            ids(count) = row.selectSingleNode("Diagram_ID").Text
        Next
    End If
    
    FindInDiagrams = ids
End Function


main

Problem solved!