Author Topic: EA Template Exporting  (Read 8721 times)

Tehila1

  • EA User
  • **
  • Posts: 256
  • Karma: +0/-0
    • View Profile
EA Template Exporting
« on: December 31, 2013, 01:40:09 am »
I defined a custom template in EA and used it in my EA add in code. like:

generator.DocumentDiagram( diagranID, 0, "MyTemplate");

I want to deploy the template with the add-in.

I exported the template and added the xml result to the installation file but that does not help.

Thanks in advance.

EXploringEA

  • EA User
  • **
  • Posts: 172
  • Karma: +8/-0
    • View Profile
Re: EA Template Exporting
« Reply #1 on: December 31, 2013, 08:39:53 am »
I've not done what you want but suspect that the following approach would work.

1. Export the templates as reference data in XML format using the Project | Model Export | Export Reference data

2. Deploy the XML file containing the reference data as part of your installation

3. Import XML file as reference data
- you will need to write file handler to read the XML as a string
- then use an undocumented repository method CustomCommand which has the parameters
ClassName: String - "Repository"
MethodName: String  - "ImportRefData"
Parameters: String - this will be the XML string containing your templates

Note this last method is undocumented so tread carefully!!
It may be worth a search on the forum to see if anybody else has found another way - I didn't see anything with a quick search.

Alternatively you could produce an MDG which contains your RTF templates and include this as part of you install.  Probably a bit trickier until you are familiar with MDG's - although worth learning about some day.

Suggest you get the great books from Thomas Kilian (Scripting EA and Inside EA) if you want to start playing in these areas - really useful and saves time searching

Hope this helps.
-------

Found the following - which implies you could specify the file instead of XML string.  May be worth a check.


Re: API support for Export/Import Reference Data
Reply #6 - Nov 21st, 2007, 2:46am   Unsupported, undocumented, 7.0.818 and later:

Repository.CustomCommand("Repository", "ImportRefData", sXML)

or

Repository.CustomCommand("Repository", "ImportRefData", sFilename)


Back to top          
The Sparx Team
[email protected]
« Last Edit: December 31, 2013, 08:56:23 am by MrWappy »
EXploringEA - information, utilities and addins

Tehila1

  • EA User
  • **
  • Posts: 256
  • Karma: +0/-0
    • View Profile
Re: EA Template Exporting
« Reply #2 on: January 01, 2014, 02:58:32 am »
Thank you so much for your quick and clear reply!

I followed the instructions, but it still does not give the desired result.  :(

Code: [Select]
string filePath=@"C:\Documents\Template.xml";
                  string xmlString =  System.IO.File.ReadAllText(filePath);
                  string templateString=Repository.GetRepository.CustomCommand("Repository", "ImportRefData", xmlString);
                  generator.DocumentDiagram(diagram.DiagramID, 0, templateString);

I also tried to send as the third argument the xml file path.

Any idea?

EXploringEA

  • EA User
  • **
  • Posts: 172
  • Karma: +8/-0
    • View Profile
Re: EA Template Exporting
« Reply #3 on: January 01, 2014, 11:29:13 am »
Not clear on what you have put in the string for the 3rd parameter but would expect it to be an XML string <XML> ... As output from EA.  Have a look at the outout of reference data to check it and probably worth a quick check with a hard coded string.

If I've not understood and that doesn't help I would suggest you drop an email to Sparx to see if they can provide you wi exact details for the parameters.  Remember it is an undocumented feature with little other than a posting from Sparx on this forum so could well have changed - and of course it could change again in the future.  Hence a reason for also looking at MDGs as a longer term solution.

If you get a response that works would be interested to know.
EXploringEA - information, utilities and addins

EXploringEA

  • EA User
  • **
  • Posts: 172
  • Karma: +8/-0
    • View Profile
Re: EA Template Exporting
« Reply #4 on: January 02, 2014, 01:16:52 am »
Hi

I was curious about the custom command as I have never used this so I tested with different types of reference data and all seems to work fine.

This is the code I used
Code: [Select]
Private XMLString As String = "" ' This is where contents of the XML are read into to as a string
   Public Sub EA_MenuClick(ByVal Repository As EA.Repository, ByVal MenuName As String, ByVal ItemName As String)
    If Repository Is Nothing Then
        MsgBox("Error between EA and Extension")
        Exit Sub
    End If
    Select Case ItemName
        Case m1 ' execute Custom command string
            Dim Result As Object = Repository.CustomCommand("Repository", "ImportRefData", XMLString)
            MsgBox("Result from custom command = " & Result.ToString)
        Case m2 ' Read file command
            XMLString = readFile()
            MsgBox("XML STRING = " & vbCrLf & XMLString)


The essence of the readFile method is
Code: [Select]

....
if System.IO.File.Exists(myFilename) Then  ' can we check if file exists
      Dim sw As New StreamReader(myFilename)
      myLocalXMLString = sw.ReadToEnd()
      sw.Close()
      Return myLocalXMLString
end if
                                    

This illustrates getting the template - I didn't try your initial issue re generating diagram but at least the template should be present.

I hope this helps.


« Last Edit: January 02, 2014, 06:31:43 am by MrWappy »
EXploringEA - information, utilities and addins

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8066
  • Karma: +118/-20
    • View Profile
Re: EA Template Exporting
« Reply #5 on: January 02, 2014, 09:24:12 am »
Quote
I want to deploy the template with the add-in.

The best way to do this is to define an MDG technology and respond to EA_OnInitializeTechnologies to return the XML for your technology.

Tehila1

  • EA User
  • **
  • Posts: 256
  • Karma: +0/-0
    • View Profile
Re: EA Template Exporting
« Reply #6 on: January 02, 2014, 07:52:22 pm »
Hello MrWappy,

I appreciate your detailed answer- thanks.
you wrote:

Quote
This illustrates getting the template - I didn't try your initial issue re generating diagram but at least the template should be present.

The program converts the XML template to string perfectly, but that's the point- how to use this string for importing the template via EA API?

Here is my code (C#):

Code: [Select]
string XMLString  = "" ;
XMLString = readFile(filePath);
string Result=Repository.GetRepository.CustomCommand("Repository", "ImportRefData", XMLString);

Here is readFile method:

Code: [Select]
public string readFile(string myFileName)
            {
                  string myLocalXMLString="";
                  if (System.IO.File.Exists(myFileName))
                  {
                         System.IO.StreamReader sw= new System.IO.StreamReader(myFileName);
                              myLocalXMLString = sw.ReadToEnd();
                              sw.Close();
                  }
            return myLocalXMLString;
            }

Well,  Result value is "True", XMLString value is the XML document content as string.

Neither this nor that are the third argument (EA template) for :

Code: [Select]
generator.DocumentDiagram(diagram.DiagramID, 0, ???);
So what could it be?
Thanks in advance!

EXploringEA

  • EA User
  • **
  • Posts: 172
  • Karma: +8/-0
    • View Profile
Re: EA Template Exporting
« Reply #7 on: January 03, 2014, 02:58:19 am »
Had a quick look and the code below seems to produce a document with the selected diagram OK.  The template used is just a new version created from the standard diagram template with no changes apart from the name.

Code: [Select]
Dim result As Boolean = False ' catch result before proceeding
 Dim FilePath As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\mydoc.rtf" ' example file name only
 If Repository.GetTreeSelectedItemType = ObjectType.otDiagram Then ' I'm using a diagram selected in the project browser - so check it is a diagram
      Dim myTemplate As String = "ADiagramTemplate" ' a template I have created AND is loaded - see list of resources template
      Dim myDiagram As EA.Diagram = Repository.GetTreeSelectedObject()
      Dim myDID As Long = myDiagram.DiagramID
      Dim myGen As DocumentGenerator = Repository.CreateDocumentGenerator
      Try
            myGen.NewDocument(myTemplate)
            result = myGen.DocumentDiagram(myDID, 1, myTemplate)
            If result Then
                  result = myGen.SaveDocument(FilePath, DocumentType.dtRTF)
            End If
      Catch ex As Exception
      MsgBox("Error = " & ex.ToString)
      End Try
 End If

This is example test code so needs strengthening for production;-)
EXploringEA - information, utilities and addins

Tehila1

  • EA User
  • **
  • Posts: 256
  • Karma: +0/-0
    • View Profile
Re: EA Template Exporting
« Reply #8 on: January 03, 2014, 03:59:43 am »
Thank you MrWappy for your quick reply!
I'll take a look on your code and update you.

Tehila1

  • EA User
  • **
  • Posts: 256
  • Karma: +0/-0
    • View Profile
Re: EA Template Exporting
« Reply #9 on: January 08, 2014, 09:51:14 pm »
Hello MrWappy,

You wrote:

 
Quote
Dim myTemplate As String = "ADiagramTemplate" ' a template I have created AND is loaded - see list of resources template

My question- How did the template has been loaded to EA?
I have the xml template as string- how you get it to be loaded?

Thanks in advance!

EXploringEA

  • EA User
  • **
  • Posts: 172
  • Karma: +8/-0
    • View Profile
Re: EA Template Exporting
« Reply #10 on: January 09, 2014, 02:24:08 am »
Below is a function that will read a file and return the XML string.
No double checking performed here and you will need to change the operation to suit your application but it works.


Code: [Select]
Private Function readFile() As String
        Dim myLocalXMLString As String = ""

        Try

            Dim myFileDialog As New OpenFileDialog()
            myFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
            myFileDialog.Filter = "XML Files (*.xml)|*.xml|All Files (*.*)|*.*||"
            myFileDialog.FilterIndex = 2
            myFileDialog.RestoreDirectory = True
            myFileDialog.Title = "Select XML file"
            myFileDialog.FileName = myFilename
      
            If myFileDialog.ShowDialog = DialogResult.OK Then
                myFilename = myFileDialog.FileName

                myFileDialog.Dispose()
                If myFilename <> "" Then
                    If System.IO.File.Exists(myFilename) Then  ' can we check if file exists
                        
                        Dim sw As New StreamReader(myFilename)

                        myLocalXMLString = sw.ReadToEnd()
                        sw.Close()
                        Return myLocalXMLString
                    Else
                        MsgBox("File does not exist " & vbCrLf & "Filename : " & myFilename)
                        Return ""
                    End If
                Else
                    MsgBox("Illegal filename - please ensure filenames are legal")
                End If
            End If
        Catch ex As Exception

        End Try
        Return myLocalXMLString
    End Function

Hope that solves your problem.
« Last Edit: January 09, 2014, 02:26:23 am by MrWappy »
EXploringEA - information, utilities and addins

Tehila1

  • EA User
  • **
  • Posts: 256
  • Karma: +0/-0
    • View Profile
Re: EA Template Exporting
« Reply #11 on: January 09, 2014, 10:47:01 pm »
Well, I feel like I didn't explain myself properly.
I have the xml template which was exported from EA, as a correct string.
My Goal is to import this string to be a new  EA template.

Sending the string as the third argument to the method below is invalid as the third argument is an existing template name, and in my case this template is still not in EA- as I don't know how to import it first.

Code: [Select]
generator.DocumentDiagram(diagram.DiagramID, 0,???);
Any ideas?

EXploringEA

  • EA User
  • **
  • Posts: 172
  • Karma: +8/-0
    • View Profile
Re: EA Template Exporting
« Reply #12 on: January 09, 2014, 11:53:16 pm »
Hi,

To clarify you have an XMLString  which contains reference data that includes a template exported previously from EA

However when importing using the command Repository.CustomCommand("Repository", "ImportRefData", XMLString)  it fails as the template already exists.

If this is correct then I think the choice depends.

You could impose a naming scheme that means there wouldn't be a conflict - i.e. you manage your templates and ensure you don't create a conflict

If you have no control over the users - then you could:
  • check the reference data of the current model and get a list of templates (never done this so suggest look on forum or check out one of Thomas Kiliian's books which tend to have more information that's not readily accessible elsewhere)
  • parse the XML and check for any conflicts
  • If a conflict is found then rename in the XML ( this would probably mean a temporary rewrite to a memory stream ) and then read as per the command above.

If you look at the xml template there is a DocName Value which I assume is the template name, not sure if the template name is also embedded in the encoded data.  A quick check indicates not but would need to do more checks.

Hope this responds to the question you raised.
« Last Edit: January 09, 2014, 11:53:39 pm by MrWappy »
EXploringEA - information, utilities and addins

Tehila1

  • EA User
  • **
  • Posts: 256
  • Karma: +0/-0
    • View Profile
Re: EA Template Exporting
« Reply #13 on: January 10, 2014, 12:33:16 am »
Hello,
Thanks for the quick reply!

The following command does not fail- the returned value is True!
Code: [Select]
object result=Repository.CustomCommand("Repository", "ImportRefData", XMLString) But the program fails here- : the returned value is false!
Code: [Select]
bool thisResult=generator.DocumentDiagram(diagram.DiagramID, 0,"MyTemplateName")
Thank you!

EDIT: I have figured out what happened:   :D
 When I create a new template in EA for exporting purpose I gave it a different name from the name I gave the xml exported template.

In code I called the template as the xml file name, but EA imported the template with it first original name instead of the XML name.

In conclusion: when creating a template for exporting purpose- call its name as the appropriate XML file name.

And again- Thank you MrWappy for your great answers!
« Last Edit: January 10, 2014, 12:58:10 am by avoda234 »

Tehila1

  • EA User
  • **
  • Posts: 256
  • Karma: +0/-0
    • View Profile
Re: EA Template Exporting
« Reply #14 on: January 26, 2014, 11:01:51 pm »
Hello!

I'm developing an add-in which generates a document based on EA model.
In order to generate  diagram images I use an EA template.

When deploying the addin- It is necessary to supply that template for each model I work with, that's why I imported the EA template I use to EA model via code.

I successfully imported the template EA version 10 but when trying the addin on EA 9.3 I get  some strange symbols where a diagram image should be located. Like:  
  
Code: [Select]
[ch1468][ch8362][ch63630]¾½£ ÷P[ch63631]^[ch1494][ch1508][ch1495][ch1467]O›ƒ›[ch1520];§< ¯aY[ch1521] [ch1524]›`G[ch63635]kxm•‹PY[ch1464][š‚g G[ch1470]°ino/<‘”¡<Œ1«¢[ch1490]³†A$>"f3°£\…[ch1464]¾T÷ I      S‘[ch1468] [ch1509]Œ«÷¾[ch1463][ch1508]W š™¦[ch63630]Y ig[ch1503]@µ[ch1463]X6_
  :-/

I've checked the templates and the imported template is exist.

Thanks in advance!
« Last Edit: January 31, 2014, 12:56:06 am by avoda234 »