DanilGholtsman
DanilGholtsman

Reputation: 2374

Spax EA best way to export diagrams into Word document

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?

Upvotes: 0

Views: 702

Answers (1)

Geert Bellekens
Geert Bellekens

Reputation: 13784

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;
    }
}

Upvotes: 3

Related Questions