Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » Export a GEF diagram as a PNG(Please help! )
Export a GEF diagram as a PNG [message #1385472] Fri, 06 June 2014 16:17 Go to next message
Ken Keefe is currently offline Ken KeefeFriend
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);

			}
		});

	}
Re: Export a GEF diagram as a PNG [message #1385757 is a reply to message #1385472] Wed, 11 June 2014 04:10 Go to previous messageGo to next message
Ken Keefe is currently offline Ken KeefeFriend
Messages: 38
Registered: September 2009
Member
Has anyone seen something like this in an example?
Re: Export a GEF diagram as a PNG [message #1386677 is a reply to message #1385472] Thu, 19 June 2014 12:05 Go to previous messageGo to next message
Phil Beauvoir is currently offline Phil BeauvoirFriend
Messages: 62
Registered: October 2012
Member
Try putting:

viewer.flush();

after viewer.createControl(new Shell());
Re: Export a GEF diagram as a PNG [message #1398933 is a reply to message #1386677] Fri, 11 July 2014 20:27 Go to previous message
Ken Keefe is currently offline Ken KeefeFriend
Messages: 38
Registered: September 2009
Member
I tried Phillipus' suggestion and still get an all white PNG file.

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

					imgLoader.save(pngFile.getAbsolutePath(), SWT.IMAGE_PNG);
Previous Topic:How do I copy a part of Zest Graph to SWT Canvas
Next Topic:Hit-testing for overlapping figures in refreshVisuals() or elsewhere?
Goto Forum:
  


Current Time: Thu Apr 18 17:52:50 GMT 2024

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

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

Back to the top