Spax EA best way to export diagrams into Word document

爷,独闯天下 提交于 2019-12-12 04:20:02

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/41116677/spax-ea-best-way-to-export-diagrams-into-word-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!