Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [gef-dev] Trying to export a GEF diagram as a PNG without displaying it

Hi Ken,

this is a method I use to convert a GraphicalPart to an Image:

public static Image createImage(GraphicalEditPart part) {

IFigure figure = part.getFigure();

Rectangle figureBounds = figure.getBounds();

Control figureCanvas = part.getViewer().getControl();

double scale = ((ScalableRootEditPart) part.getViewer().getRootEditPart()).getZoomManager().getZoom();


// calculate feedback maximum dimension

Dimension dimension = new Dimension((int) Math.round(figureBounds.width*scale),

(int) Math.round(figureBounds.height*scale));


Image img = new Image(null, dimension.width, dimension.height);

img.setBackground(ColorConstants.white);

GC imageGC = new GC(img);


Graphics imgGraphics = new SWTGraphics(imageGC);

imgGraphics.scale(scale);

imgGraphics.translate(-figureBounds.x, -figureBounds.y);

figure.paint(imgGraphics);


ImageData data = img.getImageData();


Image feedbackImage = new Image(figureCanvas.getDisplay(), data);


imageGC.dispose();

img.dispose();


return feedbackImage;

}


you can save it using this one:


public static void saveImageFile(File file, Image image) throws IOException, FileNotFoundException {

ImageLoader loader = new ImageLoader();

loader.data = new ImageData[] {image.getImageData()};

OutputStream os = new FileOutputStream(file);

loader.save(os, SWT.IMAGE_PNG);

os.close();

}


Regards,

Enrico Persiani



2014-07-11 22:40 GMT+02:00 Ken Keefe <kjkeefe@xxxxxxxxx>:
I've been trying to figure out a GEF issue for about a month now and I'm not getting anywhere. I have tried other discussion venues first, but I'm hoping someone here can assist. I have a GEF diagram that I want to save directly to a PNG file. I need to do this without displaying the diagram graphically (actually, it needs to work when the RCP app is running headlessly). 

Here is the code I have so far:

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());
viewer.flush();
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 = "" ImageData[] { img.getImageData() };

imgLoader.save(pngFile.getAbsolutePath(), SWT.IMAGE_PNG);

}
});

I get a 1000x1000 white PNG file out of it. Any suggestions would be VERY welcome. I've run out of ideas at this point.

Thanks,
Ken Keefe



--
Forti et Fordeli nihil difficile – Nothing is difficult to the brave and faithful.

_______________________________________________
gef-dev mailing list
gef-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/gef-dev



--
Enrico Persiani
Software Engineer
T: +39 339 3475220
F: +39 051 19902276
http://www.metatis.com/



Back to the top