Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » Export to Image - negative coordinates problem(Problems when exporting GEF to JPG or BMP)
Export to Image - negative coordinates problem [message #897985] Wed, 25 July 2012 14:48 Go to next message
Alexandre Torres is currently offline Alexandre TorresFriend
Messages: 139
Registered: July 2009
Senior Member
Hi, I want to export the entire diagram to JPG or BMP formats. I followed the tips on http://blog.eclipse-tips.com/2008/03/exporting-gef-figure-to-image.html

The first problem I faced was the clipping. This export method applied to my GraphicalEditorWithFlyoutPalette only exports the visible part of my diagram.

I find out an workaround, by setting the bounds of the graphical Viewer figure to the bounds of the PRINTABLE_LAYERS figure before exporting.
It works fine to what is beyond the printing window. But if I have any figure on negative x or y, it will not be exported.
So, what I can't figure out is how to translate the exporting window so that the entire printable area will be printed. I tried to translate the Printable layer (rootFigure variable) but with no success.
I noticied that if I scroll the figure, by hand, to (0,0) before exporting, it works fine BUT it exports the scroll bars...


ScrollingGraphicalViewer graphicalViewer= ...
String saveLocation= ...
		
ScalableFreeformRootEditPart rootEditPart = (ScalableFreeformRootEditPart) graphicalViewer.getEditPartRegistry().get(LayerManager.ID);
IFigure rootFigure = ((LayerManager) rootEditPart).getLayer( LayerConstants.PRINTABLE_LAYERS);
Rectangle rootFigureBounds = rootFigure.getBounds();
Control figureCanvas = graphicalViewer.getControl();
// ENLARGE figureCanvas
org.eclipse.swt.graphics.Rectangle scBounds = figureCanvas.getBounds(); // store original
org.eclipse.swt.graphics.Rectangle swRect = new org.eclipse.swt.graphics.Rectangle(rootFigureBounds.x,rootFigureBounds.y,rootFigureBounds.width,rootFigureBounds.height);
figureCanvas.setBounds(swRect); // I guess that negative x,y will not work for SWT control
// Back to the original code		
Image img = new Image(Display.getDefault(), rootFigureBounds.width, rootFigureBounds.height);  // tried to enlarge this with no success		
//Image img = new Image(Display.getDefault(), swRect); //this won't work either
GC imageGC = new GC(img);
figureCanvas.print(imageGC); 
ImageLoader imgLoader = new ImageLoader();
imgLoader.save(f.getAbsolutePath(), SWT.IMAGE_BMP);
// restore the bounds
figureCanvas.setBounds(scBounds);
// dispose
imageGC.dispose();
img.dispose();	


I think I'm really close... anyone knows what is missing?
Thanks
Re: Export to Image - negative coordinates problem [message #898018 is a reply to message #897985] Wed, 25 July 2012 15:43 Go to previous messageGo to next message
Jan Krakora is currently offline Jan KrakoraFriend
Messages: 477
Registered: December 2009
Location: Prague
Senior Member
Hi Alexandre,

try this:
final ScrollingGraphicalViewer viewer = ...
final String location = ...

final LayerManager layerManager = (LayerManager) viewer.getEditPartRegistry().get(LayerManager.ID);
final IFigure printableLayer = layerManager.getLayer(LayerConstants.PRINTABLE_LAYERS);
final Rectangle printableLayerBounds = printableLayer.getBounds();

final Image img = new Image(Display.getDefault(), printableLayerBounds.width, printableLayerBounds.height);

final GC imageGC = new GC(img);
final SWTGraphics swtGraphics = new SWTGraphics(imageGC);
swtGraphics.translate(printableLayerBounds.getLocation().negate());

printableLayer.paint(swtGraphics);
imageGC.dispose();

final ImageLoader imgLoader = new ImageLoader();
imgLoader.data = new ImageData[] { img.getImageData() };
imgLoader.save(location, SWT.IMAGE_BMP);

img.dispose();
Re: Export to Image - negative coordinates problem [message #898033 is a reply to message #898018] Wed, 25 July 2012 16:17 Go to previous messageGo to next message
Alexandre Torres is currently offline Alexandre TorresFriend
Messages: 139
Registered: July 2009
Senior Member
Great Jan! It works! Smile

Thank you very much!
Re: Export to Image - negative coordinates problem [message #898046 is a reply to message #898033] Wed, 25 July 2012 16:43 Go to previous message
Jan Krakora is currently offline Jan KrakoraFriend
Messages: 477
Registered: December 2009
Location: Prague
Senior Member
You're welcome. Don't forget to dispose the SWTGraphics too. I see it's missing there.
Previous Topic:Detect figure selection when CTRL key is pressed
Next Topic:CommandStack
Goto Forum:
  


Current Time: Sun Dec 21 08:39:01 GMT 2014

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

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