Skip to content

Instantly share code, notes, and snippets.

@pvlasov
Last active July 18, 2019 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pvlasov/dedeae19de02eb2750cfc7aad275bf36 to your computer and use it in GitHub Desktop.
Save pvlasov/dedeae19de02eb2750cfc7aad275bf36 to your computer and use it in GitHub Desktop.
Headless generation of diagram images from Sirius representations (.aird) with image maps

Inspired by https://github.com/gemoc/org.eclipse.sirius/blob/master/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/business/internal/dialect/DiagramDialectUIServices.java

Dependencies

 org.eclipse.sirius.ui;bundle-version="6.2.1",
 org.eclipse.sirius.common;bundle-version="6.2.1",
 org.eclipse.sirius.diagram.ui;bundle-version="6.2.1",
 org.eclipse.gmf.runtime.diagram.ui.render;bundle-version="1.7.1",
 org.eclipse.gmf.runtime.diagram.ui;bundle-version="1.8.0"

Imports

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.gmf.runtime.diagram.core.preferences.PreferencesHint;
import org.eclipse.gmf.runtime.diagram.ui.image.PartPositionInfo;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.sirius.business.api.query.DViewQuery;
import org.eclipse.sirius.business.api.session.CustomDataConstants;
import org.eclipse.sirius.business.api.session.Session;
import org.eclipse.sirius.business.api.session.factory.SessionFactory;
import org.eclipse.sirius.diagram.DNode;
import org.eclipse.sirius.diagram.business.api.refresh.CanonicalSynchronizer;
import org.eclipse.sirius.diagram.business.api.refresh.CanonicalSynchronizerFactory;
import org.eclipse.sirius.diagram.ui.tools.api.part.DiagramEditPartService;
import org.eclipse.sirius.viewpoint.DRepresentation;

Code

URI sessionResourceURI = URI.createFileURI("...representations.aird");
Session session = SessionFactory.INSTANCE.createSession(sessionResourceURI, new NullProgressMonitor());
session.open(new NullProgressMonitor());
DViewQuery query = new DViewQuery(session.getOwnedViews().iterator().next());
DRepresentation representation = query.getLoadedRepresentations().get(0);
List<PartPositionInfo> ppil = export(representation, session, new Path("...diagram-5.svg"), new NullProgressMonitor());
      
for (PartPositionInfo ppi : ppil) {
	EObject semanticElement = ppi.getSemanticElement();
	System.out.println("---");
	if (semanticElement instanceof DNode) {
		DNode dNode = (DNode) semanticElement;
		System.out.println(semanticElement.getClass());
		EObject target = dNode.getTarget();
		System.out.println(target);
		if (target instanceof Activity) {
			String name = ((Activity) target).getName();
			System.out.println(name);
			int x = ppi.getPartX();
			int y = ppi.getPartY();
			int width = ppi.getPartWidth();
			int height = ppi.getPartHeight();
			System.out.println("<area shape=\"rect\" coords=\"" + x + "," + y + "," + (x + width) + ","
					+ (y + height) + "\" href=\"sun.htm\" alt=\"" + name + "\">");
		}
	}
}

Export method

	private List<PartPositionInfo> export(final DRepresentation representation, final Session session, final IPath path, final IProgressMonitor monitor) throws Exception {
        final Collection<EObject> data = session.getServices().getCustomData(CustomDataConstants.GMF_DIAGRAMS, representation);
        for (final EObject dataElement : data) {
            if (dataElement instanceof Diagram) {
                final Diagram diagram = (Diagram) dataElement;
                
                CanonicalSynchronizer canonicalSynchronizer = CanonicalSynchronizerFactory.INSTANCE.createCanonicalSynchronizer(diagram);
                canonicalSynchronizer.storeViewsToArrange(false);
                canonicalSynchronizer.synchronize();	
                
                return new DiagramEditPartService().copyToImage(diagram, path, org.eclipse.gmf.runtime.diagram.ui.image.ImageFileFormat.SVG, monitor, PreferencesHint.USE_DEFAULTS);
            }
        }	
	    return Collections.emptyList();
	}	
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment