Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
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 13:30 Go to next message
Laurent Le Moux is currently offline Laurent Le Moux
Messages: 153
Registered: September 2011
Senior Member
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 #1200947 is a reply to message #1198723] Thu, 21 November 2013 13:54 Go to previous messageGo to next message
Michael Wenz is currently offline Michael Wenz
Messages: 1595
Registered: July 2009
Location: Walldorf, Germany
Senior Member
Laurent,

sorry, but all it seems that currently there is no option to export a
diagram without having the diagram open in a view or an editor. I looked in
that direction as well when we implemented the save image feature API for
last release, but it seems not to be possible on GEF basis without having
the diagram.

Michael
Re: Generate an image from a diagram [message #1209716 is a reply to message #1200947] Mon, 25 November 2013 17:11 Go to previous messageGo to next message
Juan Pedro Silva is currently offline 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 #1214225 is a reply to message #1209716] Wed, 27 November 2013 15:52 Go to previous messageGo to next message
Laurent Le Moux is currently offline Laurent Le Moux
Messages: 153
Registered: September 2011
Senior Member
Hi Juan and Michael,

I just read Juan's message. It sounds promising. I'm looking forward to see it !

I believe the Graphiti SVGExporter class could be extended to produce an image directly from the diagram based on SVG and Batik.

Mapping Graphiti graphical algorithms to their corresponding SVG elements seems not to be too difficult.

I wrote the attached code corresponding to my (simplistic) diagrams.
It could may be be extended to cover all graphical algorithms and their many options but I don't have the opportunity to achieve this, nor to ensure a full test coverage.

What about filling a request for a new Graphiti feature in Bugzilla based on our inputs ?

Kind regards,

Laurent
Re: Generate an image from a diagram [message #1219200 is a reply to message #1209716] Mon, 02 December 2013 15:28 Go to previous messageGo to next message
Michael Wenz is currently offline Michael Wenz
Messages: 1595
Registered: July 2009
Location: Walldorf, Germany
Senior Member
JP,

that sounds really interesting! If you would like to see it as part of
Graphiti and are willing to contribute please file an extension Bugzilla.

Looking forward to that.

Michael
Re: Generate an image from a diagram [message #1219300 is a reply to message #1219200] Tue, 03 December 2013 08:47 Go to previous messageGo to next message
Juan Pedro Silva is currently offline Juan Pedro Silva
Messages: 258
Registered: July 2009
Senior Member
Dear Laurent and Michael,

you may find the repository here and a web page describing its use and differences with current GEF-based exporter. I hope you like it.

Michael, I will file that extension Bugzilla, as you suggest.
Regards,
JP
Re: Generate an image from a diagram [message #1219303 is a reply to message #1219300] Tue, 03 December 2013 09:03 Go to previous messageGo to next message
Juan Pedro Silva is currently offline Juan Pedro Silva
Messages: 258
Registered: July 2009
Senior Member
Enhancement bug opened in https://bugs.eclipse.org/bugs/show_bug.cgi?id=423018

[Updated on: Tue, 03 December 2013 09:03]

Report message to a moderator

Re: Generate an image from a diagram [message #1219310 is a reply to message #1219300] Tue, 03 December 2013 09:34 Go to previous messageGo to next message
Andreas Graf is currently offline Andreas Graf
Messages: 151
Registered: July 2009
Senior Member
Hi Juan,

I would expect that one of the major differences is in the treatment of decorators. I would assume that a file-based export will not be able to work with decorators. Is that correct?

Regards,

Andreas
Re: Generate an image from a diagram [message #1219324 is a reply to message #1219310] Tue, 03 December 2013 10:49 Go to previous messageGo to next message
Juan Pedro Silva is currently offline 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 #1220530 is a reply to message #1219324] Thu, 12 December 2013 11:23 Go to previous messageGo to next message
Juan Pedro Silva is currently offline Juan Pedro Silva
Messages: 258
Registered: July 2009
Senior Member
Andreas,
we created a new branch in github (named "decorator") to include the decorators, as a proof of concept of what I mentioned in my last post.
It required one new class, es.com.upm.graphiti.exporter.svg.DecoratorConfigurationSVGElement, which implements the IConfigurationSVGElement interface, and a couple of changes in the es.com.upm.graphiti.exporter.svg.DiagramGraphicsAdaptor class. This code exports all decorators which are images; we didn't try to make it exhaustive.

You don't need to change any code to define a new Configuration. For that, an extension point was defined in the first place:

Identifier: es.com.upm.graphiti.exporter.svg.configurationSVGElement
Example:
<extension
    point="es.com.upm.graphiti.exporter.svg.configurationSVGElement">
     <configurator
class="es.com.upm.graphiti.exporter.svg.DecoratorConfigurationSVGElement"
diagramTypeID="testtool.sketch">
     </configurator>
</extension> 


I hope it suits your needs.
Regards,
Juan Pedro
Re: Generate an image from a diagram [message #1221803 is a reply to message #1220530] Wed, 18 December 2013 13:13 Go to previous messageGo to next message
Laurent Le Moux is currently offline Laurent Le Moux
Messages: 153
Registered: September 2011
Senior Member
Hi Juan,

I try to use your code. I get an access restriction for org.eclipse.graphiti.export.batik.GraphicsToGraphics2DAdaptor in DiagramGraphicsAdaptor.
The comment is clear about the fact that this Graphiti class should be made public and I can change my Eclipse IDE settings to get rid of this error.

But, when I try to execute my RCP application, I get the following ClassNotFoundException exception :
org.eclipse.graphiti.export.batik.GraphicsToGraphics2DAdaptor cannot be found by... (my rcp application).

How do you solve this at runtime ?

Kind regards,

Laurent
Re: Generate an image from a diagram [message #1221847 is a reply to message #1221803] Wed, 18 December 2013 14:44 Go to previous messageGo to next message
Juan Pedro Silva is currently offline 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 Go to previous messageGo to next message
Laurent Le Moux is currently offline Laurent Le Moux
Messages: 153
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 Smile

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 Go to previous message
Juan Pedro Silva is currently offline 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
Previous Topic:I can create a graph editor [GMF] with graphiti instead of GEF??
Next Topic:Button icon disabled
Goto Forum:
  


Current Time: Thu Sep 18 01:45:21 GMT 2014

Powered by FUDForum. Page generated in 0.02403 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software