Export a GEF diagram as a PNG [message #1385472] |
Fri, 06 June 2014 16:17 |
Ken Keefe Messages: 38 Registered: September 2009 |
Member |
|
|
I am trying to export a GEF diagram to a PNG file without displaying the diagram first. Basically, my users are filling out a documentation wizard that prints all the project details in an HTML file. Of course, I don't want to require them to open each of the models graphically to export the images.
Check out my code so far. Notice the comment in the middle where it says that the bounds are not being captured correctly, so I override the values just to see what gets exported. In the end, this code gives me no errors, and creates a PNG file that is solid white and 1000x1000 pixels.
Here's my code so far:
private void documentImage(HTML html) {
File docDir = new File(html.getFileName()).getParentFile();
final File pngFile = new File(docDir, getParent().getName()+".png");
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
GraphicalViewer viewer = new ScrollingGraphicalViewer();
viewer.setEditPartFactory(new AttackExecutionModelEditPartFactory());
ScalableFreeformRootEditPart rootPart = new ScalableFreeformRootEditPart();
viewer.setRootEditPart(rootPart);
viewer.setContents(getCastedModel().getAeg());
IFigure rootFigure = ((LayerManager)rootPart).getLayer(LayerConstants.PRINTABLE_LAYERS);
Rectangle rootFigureBounds = rootFigure.getBounds();
viewer.createControl(new Shell());
Control figureCanvas = viewer.getControl();
// The bounds are (0,0) at this point, so let's change them and see if the rest of the code works...
rootFigureBounds.height = 1000;
rootFigureBounds.width = 1000;
Image img = new Image(Display.getDefault(), rootFigureBounds.width, rootFigureBounds.height);
GC imageGC = new GC(img);
figureCanvas.print(imageGC);
ImageLoader imgLoader = new ImageLoader();
imgLoader.data = new ImageData[] { img.getImageData() };
imgLoader.save(pngFile.getAbsolutePath(), SWT.IMAGE_PNG);
}
});
}
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02528 seconds