Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: glide on July 13, 2012, 04:22:36 am

Title: Automation I/F & SQL Script Results Passing
Post by: glide on July 13, 2012, 04:22:36 am
I created a SQL script in EA that generates a list of diagrams that display a given connector based on the connector's ID.

I need to pass the EA search results out of the EA model to my external VBA script.  The external VBA script invokes EA SQL script using the Repository.GetElementsByQuery() function but the function fails to return a collection of diagrams.  

Is there some other approach that can get me the information I need?

SQL script:
------------
SELECT t_diagram.ea_guid AS CLASSGUID,t_diagram.Diagram_Type AS CLASSTYPE, t_diagram.Diagram_ID, t_diagram.Name, t_diagram.ea_guid
FROM  t_diagram, t_diagramlinks
WHERE            t_diagramlinks.ConnectorID=<Search Term>
              AND  t_diagramlinks.Hidden=FALSE
              AND  t_diagram.Diagram_ID = t_diagramlinks.DiagramID
Title: Re: Automation I/F & SQL Script Results Passin
Post by: Paulus on July 13, 2012, 04:50:59 am
Well, an Element is not a Diagram so i'm not surprised it won't work.

Perhaps what you can do is modify the SQL query to return the parent element of each diagram and subsequently use the extenal VBA script to query the Diagrams property of each element in turn to obtain the diagrams.

A bit more work but it should do the trick (provided all your diagrams are positioned below elements).

Another option is to use Reposityory.SQLQuery to obtain a list of diagram ID's and subsequently use Repository.GetDiagramByGuid to obtain a reference to each diagram.

regards,

Paulus
Title: Re: Automation I/F & SQL Script Results Passing
Post by: glide on July 13, 2012, 05:05:35 am
Thanks, I'll give that a try.
Title: Re: Automation I/F & SQL Script Results Passin
Post by: Paulus on July 13, 2012, 05:41:10 am
I've been able to get the second approach working using some stuff i've used before. Since it's fairly complex i've included the snippet below.

Create a new  script in EA, paste this in and run it. You should get a list of all diagrams in your model (perhaps you need to use another MSXML2 library, don't know about that MS stuff). Modifying this to suit your needs should be easy.

have fun!

Paulus

Quote
option explicit

!INC Local Scripts.EAConstants-VBScript
sub main
      Dim sRes
      dim SQL
      SQL = "SELECT t_diagram.ea_guid FROM  t_diagram"
            Session.output("SQL: "&SQL)
      sRes = Repository.SQLQuery(SQL)
      Session.Output("Result: "+sRes)
      
      dim xmlDom
      set xmlDom = createobject("MSXML2.DOMDocument.4.0")
      xmlDOM.validateOnParse = false
      xmlDOM.async = false

      if xmlDom.loadXML(sRes) then
            Dim nodeList  
            Set nodeList = xmlDom.selectNodes("//Dataset_0/Data/Row/ea_guid")
            
            If nodeList.length = 0 Then
                  MsgBox "No diagrams found!",0,"Querying diagrams"
            Else
                  dim  i
                  for i=0 to nodeList.length - 1
                        Dim dgm as EA.Diagram
                        Set dgm = Repository.GetDiagramByGuid(nodeList.item(i).text)
                        Session.Output("Found: "&dgm.Name&"("&dgm.DiagramGUID&")")
                  next
            end if
      else
            ' woops, error parsing xml?!
      end if
end sub

main
Title: Re: Automation I/F & SQL Script Results Passing
Post by: Geert Bellekens on July 13, 2012, 03:16:54 pm
In my Enterprise Architect Add-in Framework (https://github.com/GeertBellekens/Enterprise-Architect-Add-in-Framework) I have this operation in the  Model class (https://github.com/GeertBellekens/Enterprise-Architect-Add-in-Framework/blob/master/EAAddinFramework/EAWrappers/Model.cs):

  
Code: [Select]
//returns a list of diagrams according to the given query.
    //the given query should return a list of diagram id's
    internal List<Diagram> getDiagramsByQuery(string sqlGetDiagrams)
    {
        // get the nodes with the name "Diagram_ID"
        XmlDocument xmlDiagramIDs = this.SQLQuery(sqlGetDiagrams);
        XmlNodeList diagramIDNodes =
          xmlDiagramIDs.SelectNodes("//Diagram_ID");
        List<Diagram> diagrams = new List<Diagram>();
        foreach (XmlNode diagramIDNode in diagramIDNodes)
        {
            int diagramID;
            if (int.TryParse(diagramIDNode.InnerText, out diagramID))
            {
                Diagram diagram = this.getDiagramByID(diagramID);
                diagrams.Add(diagram);
            }
        }
        return diagrams;
    }

Geert