Home » Modeling » Graphiti » Generate an image from a diagram(without opening the associated editor...)
| |
Re: Generate an image from a diagram [message #1209716 is a reply to message #1200947] |
Mon, 25 November 2013 17:11 |
Juan Pedro Silva Messages: 258 Registered: July 2009 |
Senior Member |
|
|
Hi Laurent and Michael,
We have developed such an SVG exporter for Graphiti without the need to open the different diagrams, as part of the results of a research project within the Center for Open Middleware (http://www.centeropenmiddleware.com/). It works directly from the Graphiti metamodel making use of Batik and Draw2d, and only holds a small indirect dependency from GEF (via Graphiti).
We are currently setting up the project in our Github account. We are planning to make it available under EPL License, so that it could be pulled into Graphiti if desired. If you care to wait for a couple more days, we will make the code available in our Github account. I'll get back to you and let you know when it is ready.
Regards,
JP
[Updated on: Mon, 25 November 2013 17:29] Report message to a moderator
|
|
| | | | | |
Re: Generate an image from a diagram [message #1219324 is a reply to message #1219310] |
Tue, 03 December 2013 10:49 |
Juan Pedro Silva Messages: 258 Registered: July 2009 |
Senior Member |
|
|
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 11:08] Report message to a moderator
|
|
| | |
Re: Generate an image from a diagram [message #1221847 is a reply to message #1221803] |
Wed, 18 December 2013 14:44 |
Juan Pedro Silva Messages: 258 Registered: July 2009 |
Senior Member |
|
|
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 21:52 |
Laurent Le Moux Messages: 184 Registered: September 2011 |
Senior Member |
|
|
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 11:53 |
Juan Pedro Silva Messages: 258 Registered: July 2009 |
Senior Member |
|
|
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: Sat Apr 20 02:08:00 GMT 2024
Powered by FUDForum. Page generated in 0.04403 seconds
|