0

Got an old module which is generates reports with data from sparx ea project.

There is a part where you need to insert diagrams as pictures in the document.

Now it looks like that

 public static void copyDiagram(
            EA.Diagram diagram,
            EA.Repository eaRepository)
        {
            eaRepository.App.Project.PutDiagramImageOnClipboard(diagram.DiagramGUID, 0);
            eaRepository.CloseDiagram(diagram.DiagramID);
        }

copying it to clipboard, and after that there goes something like currentDocumentRange.Paste()

Looks strange for me. I think it's not really good to use clipboard like that, so I want to rewrite it in future.

So, only other function I found there looks like that PutDiagramImageToFile(diagrammGUID, path, type)

If there are no better option is it okay to create new file, after that get it by it's path insert into word document, and then delete it?

Or, maybe there are some other SparxEA function, which get image from diagram in byte[] format or like Image format?

What way is better?

DanilGholtsman
  • 2,354
  • 4
  • 38
  • 69
  • Why working via files if you could do the same in-memory via the clipboard? There is nothing wrong with that approach. – qwerty_so Dec 13 '16 at 09:21
  • @ThomasKilian well, in my case the problem with clipboard appears here (1st comment under the question) http://stackoverflow.com/questions/41055211/task-run-from-ui-thread-throws-sta-error – DanilGholtsman Dec 13 '16 at 09:40
  • @ThomasKilian thats why I want to reduce of all clipboard calls in code I work with – DanilGholtsman Dec 13 '16 at 09:41
  • Which seems to be the wrong place if your threading model it broken. But well, if you want to export files use the `PutDiagramImageToFile`. – qwerty_so Dec 13 '16 at 10:03
  • @ThomasKilian well, I'm not sure that is really broken but, as I understood, threading with COM objects could be slightly not the same as, let say, when you do the same in ASP.net MVC project – DanilGholtsman Dec 13 '16 at 13:00

1 Answers1

3

I'm using this code (on a diagram wrapper class) to get the image of a diagram without having to use the clipboard.
This code is used primarily in a custom written document generator and is surprisingly fast.

/// <summary>
/// returns diagram image
/// </summary>
public Image image
{
    get 
    {
        EA.Project projectInterface = this.model.getWrappedModel().GetProjectInterface();
        string diagramGUID = projectInterface.GUIDtoXML(this.wrappedDiagram.DiagramGUID);
        string filename = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".png";
        //save diagram image to file (format ".png")
        projectInterface.PutDiagramImageToFile(diagramGUID, filename, 1);
        //load the contents of the file into a memorystream
        MemoryStream imageStream = new MemoryStream(File.ReadAllBytes(filename));
        //then create the image from the memorystream.
        //this allows us to delete the temporary file right after loading it.
        //When using Image.FromFile the file would have been locked for the lifetime of the Image
        Image diagramImage = Image.FromStream(imageStream);
        //delete the temorary file
        System.IO.File.Delete(filename);

        return diagramImage;
    }
}
Geert Bellekens
  • 12,788
  • 2
  • 23
  • 50
  • Wow, mr. Bellekens (saw many arctiles of yours about the EA) : ) Thanks for your answer, exactly what I need! – DanilGholtsman Dec 14 '16 at 12:24
  • Well, hi again, used this code but there are one problem. All diagrams looks like that http://i.imgur.com/mw4m1gD.png – DanilGholtsman Dec 16 '16 at 11:25
  • In projects there actors and use cases and stuff but it doesn't appear on pictures – DanilGholtsman Dec 16 '16 at 11:25
  • Well, I found out that I used `Guid` type instaed of `string`, and when I used something like `myItemGuid.toString()` it was cast like `3d0b5f50-3461-4eec-9f6e-c1a7ccfaabfb` when it suppose to be like `{3d0b5f50-3461-4eec-9f6e-c1a7ccfaabfb}`. Just used like `string.Format("{{{0}}}", eaDiagramGuid.ToString())` there – DanilGholtsman Dec 16 '16 at 11:54
  • 1
    Instead you better guid myGuid.ToString("B"). See https://msdn.microsoft.com/nl-be/library/97af8hh4(v=vs.110).aspx for more info on formatting GUID's – Geert Bellekens Dec 16 '16 at 12:42