Create GMF diagram programmatically

2019-04-16 23:14发布

问题:

I trying to create one simple diagram using EMF & GMF in Eclipse plug-ins. I have created one Ecore with two node & connection. I am able to generate GMF out of that. When I run my application I am getting the palate and with the help of that I am able to draw the GMF diagram. But I want that programmatically.

I have created one XML out of that Ecore and successfully able to load that XML into the EMF object. So my EObject is having that XML values. Now I want a GMF diagram out of that automatically. like with the opening of one sample RCP view it should load the GMF content.

The problem is that I am new to this GMF and EMF, so I don't know which class I have to modify or what I have to write.

回答1:

I have a solution:

    ResourceSet resourceSet = new ResourceSetImpl();
    Diagram diagram = NotationFactory.eINSTANCE.createDiagram();
    diagram.setMeasurementUnit(MeasurementUnit.PIXEL_LITERAL);
    diagram.getStyles().add(NotationFactory.eINSTANCE.createDiagramStyle());
    diagram.setElement(object); //your EObject that should be referenced to this diagram (probably an ecore file)
    diagram.setType("Ecore");
    URI diagUri = URI.createFileURI(yourPathToECoreFile + "_diagram");
    Resource diagramResource = resourceSet.createResource(diagUri);
    diagramResource.getContents().add(diagram);

It works for me... after many hours of research

PS: The diagram should be at the same place as your ecore file