Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » [Resolved] Diagram To Image
[Resolved] Diagram To Image [message #527311] Wed, 14 April 2010 18:50 Go to next message
Fabricio Pellegrini is currently offline Fabricio PellegriniFriend
Messages: 36
Registered: July 2009
Member
Hi,

I need to save my diagram files as image. I saw that I can use CopyToImageUtils but I didn't have much sucess. Can someone help me? Very Happy

[Updated on: Wed, 05 May 2010 14:42]

Report message to a moderator

Re: Diagram To Image [message #527383 is a reply to message #527311] Thu, 15 April 2010 06:49 Go to previous messageGo to next message
Marc Mising name is currently offline Marc Mising nameFriend
Messages: 193
Registered: July 2009
Location: Valencia, Spain
Senior Member
Hi Fabrizio, you're lucky cause I had the same 'problem' last week, and I solve it basing on the class you referenced, CopyToImageUtils. I think is what you're finding. To get the Image of a Diagram, but not to save it into a file. The following code I attach to you, converts an existing Diagram to an image (GIF format). Please feel free to copy and try it.

public class CreateDiagramImage {
	public static Image getDiagramImage(Diagram diagram,
			PreferencesHint preferencesHint) {
		Image image = null;
		DiagramEditor openedDiagramEditor = DiagramEditorUtil.findOpenedDiagramEditorForID(ViewUtil.getIdStr(diagram));

		if (openedDiagramEditor != null) {
			image = getDiagramImage(openedDiagramEditor.getDiagramEditPart());
		} else {
			DiagramEditPart diagramEditPart = OffscreenEditPartFactory
					.getInstance().createDiagramEditPart(diagram, new Shell(),
							preferencesHint);
			Assert.isNotNull(diagramEditPart);
			image = getDiagramImage(diagramEditPart);
			diagramEditPart.deactivate();
		}

		return image;
	}

	public static Image getDiagramImage(DiagramEditPart diagramEP) {
		DiagramGenerator gen = new DiagramImageGenerator(diagramEP);
		List<?> editParts = diagramEP.getPrimaryEditParts();
		Rectangle rectangle = gen.calculateImageRectangle(editParts);
		ImageDescriptor descriptor = gen.createSWTImageDescriptorForParts(
				editParts, rectangle);
		return descriptor.createImage();
	}
}


If you has the DiagramEditPart, call directly the getDiagramImage(DiagramEditPart), but if you only has the Diagram, you must to call getDiagramImage(Diagram, PreferenceHint) that will get the DiagramEditPart from the editor if it's opened, or will create it (only in memory) with the OffscreenEditPartFactory factory.

Good luck Wink
Marc

[Updated on: Thu, 15 April 2010 06:51]

Report message to a moderator

Re: Diagram To Image [message #527385 is a reply to message #527311] Thu, 15 April 2010 06:51 Go to previous messageGo to next message
Robert Wloch is currently offline Robert WlochFriend
Messages: 109
Registered: July 2009
Senior Member
Hi Fabricio,

you can always right click the diagram and use the save as image function. If you need a customized image export then you may find this post interesting: http://dev.eclipse.org/newslists/news.eclipse.modeling.gmf/m sg16811.html

Regards
Rob
Re: Diagram To Image [message #527511 is a reply to message #527383] Thu, 15 April 2010 13:50 Go to previous messageGo to next message
Fabricio Pellegrini is currently offline Fabricio PellegriniFriend
Messages: 36
Registered: July 2009
Member
Thank you,

So I tried to do this:
IEditorPart editor = null
if (editorDescriptor == null) {
  editor = IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), file, activate);
} else {
  editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().openEditor(new FileEditorInput(file),
	                            editorDescriptor.getId(), activate);
}
try {
  Image image = CreateDiagramImage.getDiagramImage((DiagramEditPart) editor);
  ImageData data = image.getImageData();
  IFile imageFile = project.getFile("Imagem.gif");
  byte [] bytes = data.data;
  InputStream source = new ByteArrayInputStream(bytes);
  imageFile.create(source, IResource.NONE, null);
} catch (CoreException e) {
  e.printStackTrace();
} catch (Exception exc)
{
  exc.printStackTrace();
}


I used "IDE.openEditor();" and " PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage().openEditor(); " because my application already open diagrams editors using it.
But this methods returns an EditorPart that is instance of my "ProcessActivityDiagramEditor".
When I cast it to a "DiagramEditPart", it didn't work.

But it was only a test. What I really want is to generate a Image for each one of my Diagrams on my diagram's folder. So a started to look how could get the Diagram from the File,but i didn't find anything usefully.
Any tip?
Re: Diagram To Image [message #527527 is a reply to message #527511] Thu, 15 April 2010 14:09 Go to previous messageGo to next message
Marc Mising name is currently offline Marc Mising nameFriend
Messages: 193
Registered: July 2009
Location: Valencia, Spain
Senior Member
You can browse the Diagrams throught the resources placed in the Editing Domain.

If you have an EObject, you can get its eResource, and from this eResource you can get the ResourceSet. This ResourceSet has all the Resources loaded in the Editing Domain. You can browse all the Resources and check that the type is of GMFResource, and every content of this resource will be a Diagram.

Normally, when you're working with a GMF Diagram, you have loaded in the ResourceSet at least 2 resources: the one that contains the Diagrams, and the one that contains the Model.

Good Luck Surprised
Marc
Re: Diagram To Image [message #531035 is a reply to message #527311] Mon, 03 May 2010 13:05 Go to previous messageGo to next message
Fabricio Pellegrini is currently offline Fabricio PellegriniFriend
Messages: 36
Registered: July 2009
Member
Hi again,

I found how to handle with diagrams and sucefully generated their image when I open the diagram in a Editor. Thanks for the help.

But when i tried to generate driagrams that aren't opened, i got this exception:
java.lang.NullPointerException
	at org.eclipse.gmf.runtime.diagram.core.listener.DiagramEventBroker.initializeDiagramEventBroker(DiagramEventBroker.java:243)
	at org.eclipse.gmf.runtime.diagram.core.listener.DiagramEventBroker.startListening(DiagramEventBroker.java:230)
	at org.eclipse.gmf.runtime.diagram.ui.OffscreenEditPartFactory.createDiagramEditPart(OffscreenEditPartFactory.java:126)
	at bi.transforms.mdaprocesseditor2.actions.CreateDiagramImage.getDiagramImage(CreateDiagramImage.java:38)



So I tried to Debug and found that the Editing Domain of my diagram was null.
Sad
Re: Diagram To Image [message #531072 is a reply to message #527311] Mon, 03 May 2010 15:31 Go to previous messageGo to next message
Aurélien Pupier is currently offline Aurélien PupierFriend
Messages: 637
Registered: July 2009
Location: Grenoble, FRANCE
Senior Member

Hi,

I suggest you to take a look at OffscreenEditPart


Aurélien Pupier - Red Hat
Senior Software Engineer in Fuse Tooling team
Re: Diagram To Image [message #531136 is a reply to message #531072] Mon, 03 May 2010 20:16 Go to previous message
Fabricio Pellegrini is currently offline Fabricio PellegriniFriend
Messages: 36
Registered: July 2009
Member
I looked at OffscreenEditPart and found where the null object was comming.

 DiagramEventBroker.startListening(TransactionUtil.getEditingDomain(diagram));


The TransactionUtil.getEditingDomain(diagram) was returnig null.

The problem is that I didn't understand well how to registry the Editing Domain of my gmf diagram.


SOLUTION!!
//Method was added to Marc's CreateDiagramImage Class... It create the driagram's image from IFile and save it in JPEG format  
public static void createImageFrom(IFile file, String target)
	{
		try {
                        EMFLoader loader = new EMFLoader();
			EObject object = loader.loadDomain(file.getLocation()+"");
			Resource resc = object.eResource();
			
			// This create the Editing Domain 
                        ResourceSet rset = resc.getResourceSet();
			TransactionalEditingDomain domain = TransactionalEditingDomain.Factory.INSTANCE.createEditingDomain(rset);
			
			Image image = CreateDiagramImage.getDiagramImage((Diagram) resc.getContents().get(0),null);
			
			ImageLoader imageLoader = new ImageLoader();
			imageLoader.data = new ImageData[] {image.getImageData()};
			imageLoader.save(target+".jpg",SWT.IMAGE_JPEG);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}





[Updated on: Wed, 05 May 2010 14:40]

Report message to a moderator

Previous Topic:[GenGMF] new project proposal
Next Topic:get a handle to the document provider
Goto Forum:
  


Current Time: Thu Apr 25 01:48:19 GMT 2024

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

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

Back to the top