Home » Modeling » Graphiti » Generate an image from a diagram(without opening the associated editor...)
Generate an image from a diagram [message #1198723] |
Wed, 20 November 2013 08:30  |
Eclipse User |
|
|
|
Hi all,
I would like to display a Graphiti diagram in a generated BIRT report.
So I would like to convert it into an image without having to display the diagram in a view.
I had a look at DefaultSaveImageFeature but it relies on an opened editor.
I also had a look at Batik documentation and Graphiti SVGExporter.
It looks much more like what I need but I didn't get it all right.
My question is : How do I get the SVG document corresponding to my diagram so that I could run the Batik transcoder to get my report image ?
Kind regards,
Laurent
|
|
| | | | | | | |
Re: Generate an image from a diagram [message #1219324 is a reply to message #1219310] |
Tue, 03 December 2013 05:49   |
Eclipse User |
|
|
|
Hi Andreas,
you are right, decorators are not persisted in the model file, so that information is not available to be exported if you take the file as exclusive input. We discussed this issue, and there were different visions as whether decorators did belong to the diagram or not (for instance, validation decorators, or unresolved elements). Some decorators may change depending on the context where you open your diagram, although the diagram itself is the same.
On the other hand, we included some hooks to be able to pre and post process the different model elements being exported. I believe that, by using the appropriate code, those hooks (or, at least, the approach) may be used to add such decorators to the resulting image file.
Regards,
JP
[Updated on: Tue, 03 December 2013 06:08] by Moderator
|
|
| | |
Re: Generate an image from a diagram [message #1221847 is a reply to message #1221803] |
Wed, 18 December 2013 09:44   |
Eclipse User |
|
|
|
Hi Laurent,
to solve it you have to get Graphiti plugin's sources and export that package in the manifest (make that class public).
You could do that by importing the plugin (with source code) in your workspace, change what I just told you, and try to use the exporter in a runtime instance. Alternatively, you may install the modified plugin into your Eclipse.
Nevertheless, if you are not using any PlatformGraphicsAlgorithm elements in your graphics (it is a means Graphiti provides to extend it and create your own graphical elements, sort of), probably the simplest way is to comment out that piece of code, and it should work OK for all standard Graphiti elements.
I hope I helped you.
Regards,
Juan Pedro
|
|
|
Re: Generate an image from a diagram [message #1222194 is a reply to message #1221847] |
Thu, 19 December 2013 16:52   |
Eclipse User |
|
|
|
Hi Juan,
Thanks for the tip. Decompiling GraphicsToGraphics2DAdaptor with Eclipse shows the class and the constructor is already public. So I manually added : Export-Package: org.eclipse.graphiti.export.batik;version="0.10.1", in org.eclipse.graphiti.export.batik_0.10.1.v20130918-0838.jar.
I also changed the corresponding access rule in my rcp project properties.
The class is then reachable.
So, I can now provide you some feedback
Let me first describe my use case.
My Graphiti diagram is stored in a CDO repository.
I want to convert it to an SVG document and - from there - generate an image as a byte array to be injected in a BIRT report as a BIRT dynamic image.
As you can see, there's no file at all in the whole process.
Concerning your code, unless I'm wrong, it seems an extension point declaration is mandatory (the one for placing hooks in the SVG document). I would suggest to make it optionnal.
I tried to use the DefaultConfigurationSVGElement but it internally relies on files.
So I finally changed the code to remove any reference to this and I finally kept the 3 useful files in my case : AwtService, DiagramGraphicsAdaptor and GradientExtensionHandler.
I then used the following code :
/*
* Convert the diagram to an image array using code provided by Universidad Politécnica de Madrid
*/
public static byte[] produceImageUPMBased(Diagram d, Transcoder t) throws TranscoderException, SVGGraphics2DIOException {
// Get a DOMImplementation.
DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
// Create an instance of org.w3c.dom.Document.
String svgNS = "http://www.w3.org/2000/svg";
Document document = domImpl.createDocument(svgNS, "svg", null);
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(document);
ctx.setExtensionHandler(new GradientExtensionHandler());
GenericImageHandler ihandler = new CachedImageHandlerBase64Encoder();
ctx.setGenericImageHandler(ihandler);
ctx.setEmbeddedFontsOn(false);
// Create an instance of the SVG Generator.
DiagramGraphicsAdaptor svgGenerator = new DiagramGraphicsAdaptor(ctx, false);
// Launch diagram conversion to SVG
svgGenerator.analyze(d);
StringWriter writer = new StringWriter();
svgGenerator.stream(writer);
// Run the transcoder and return the resulting image
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Document xmlDoc = null;
try {
xmlDoc = builder.parse(new ByteArrayInputStream(writer.toString().getBytes()));
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
TranscoderInput input = new TranscoderInput(xmlDoc);
ByteArrayOutputStream image = new ByteArrayOutputStream();
TranscoderOutput output = new TranscoderOutput(image);
t.transcode(input, output);
return image.toByteArray();
}
But the result differs from what I get with my own implementation (see attached libre office result files, the three java classes and my code from the previous post).
Did I do something wrong while modifying your classes ?
Kind regards,
Laurent
|
|
|
Re: Generate an image from a diagram [message #1222431 is a reply to message #1222194] |
Fri, 20 December 2013 06:53  |
Eclipse User |
|
|
|
Laurent, a couple of thoughts.
First, the extension point should have always been optional, you are right, not mandatory; it is an implementation error. The code looks for anything that uses it to use in those hooks. If there is no extension on the extension point, it exports normally, with no extra processing (for instance, without the decorators).
Now, on the difference between what you expected and what you got. You didn't provide the transcoder code, so we cannot be precise on this, but it seems that what you are getting depends on the screen resolution in some way. We don't know if what is truncated is the input, or the result of the "transcode" method. I would suggest that you also get the regular file output, to check if the resulting file contains all elements, or if it is also truncated.
Finally, if you also don't want to have the grid exported, you can comment out the call to the paintDefaultGrid(diagram) within the paint(shape, checkstyles) method in DiagramGraphicsAdaptor.
Today is our last day before holidays, so, if you have more doubts, I don't know when we can get back at you.
Regards, and marry Christmas to you all,
JP
|
|
|
Goto Forum:
Current Time: Wed Jul 23 17:08:07 EDT 2025
Powered by FUDForum. Page generated in 0.06816 seconds
|