1 package org.simantics.diagram.svg.export;
5 import java.util.concurrent.Semaphore;
6 import java.util.concurrent.atomic.AtomicReference;
8 import org.apache.batik.svggen.SVGGraphics2D;
9 import org.simantics.Simantics;
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.Resource;
12 import org.simantics.db.common.ResourceArray;
13 import org.simantics.db.common.request.PossibleIndexRoot;
14 import org.simantics.db.common.request.UniqueRead;
15 import org.simantics.db.common.utils.NameUtils;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.exception.ValidationException;
18 import org.simantics.db.management.ISessionContext;
19 import org.simantics.db.request.Read;
20 import org.simantics.diagram.elements.DiagramNodeUtil;
21 import org.simantics.diagram.export.ImagePrinter;
22 import org.simantics.diagram.stubs.DiagramResource;
23 import org.simantics.g2d.canvas.impl.CanvasContext;
24 import org.simantics.g2d.scenegraph.ICanvasSceneGraphProvider;
25 import org.simantics.layer0.Layer0;
26 import org.simantics.modeling.ModelingResources;
27 import org.simantics.structural.stubs.StructuralResource2;
28 import org.simantics.structural2.StructuralVariables;
29 import org.simantics.utils.DataContainer;
30 import org.simantics.utils.datastructures.Pair;
31 import org.simantics.utils.page.MarginUtils.Margins;
32 import org.simantics.utils.threads.ThreadUtils;
33 import org.simantics.utils.threads.WorkerThread;
36 public class DiagramToSVG {
39 public static String diagramToSVG(Resource diagram) {
42 ImagePrinter.ImageExportPlan exportPlan = new ImagePrinter.ImageExportPlan();
43 exportPlan.margin = 0.05;
44 exportPlan.dpi = 96.0;
45 exportPlan.diagram = diagram;
46 return render(diagram,exportPlan);
47 } catch (Exception e) {
49 return "<b>error</b> "+ e.getMessage();
55 public static String diagramToSVG(Resource diagram, int width, int height) {
58 ImagePrinter.ImageExportPlan exportPlan = new ImagePrinter.ImageExportPlan();
59 exportPlan.margin = 0.05;
60 exportPlan.size = new Point(width, height);
61 exportPlan.diagram = diagram;
62 return render(diagram,exportPlan);
63 } catch (Exception e) {
65 return "<b>error</b> "+ e.getMessage();
71 public static String diagramToSVG(Resource diagram, double dpi) {
74 ImagePrinter.ImageExportPlan exportPlan = new ImagePrinter.ImageExportPlan();
75 exportPlan.margin = 0.05;
77 exportPlan.diagram = diagram;
78 return render(diagram,exportPlan);
79 } catch (Exception e) {
81 return "<b>error</b> "+ e.getMessage();
87 public static String diagramToSVG(Resource diagram, ImagePrinter.ImageExportPlan exportPlan, Margins margins, SVGGraphics2D svgGenerator) {
90 return render(diagram,exportPlan,margins,svgGenerator);
91 } catch (Exception e) {
93 return "<b>error</b> "+ e.getMessage();
98 private static String render(final Resource input, final ImagePrinter.ImageExportPlan exportPlan) throws Exception {
99 return render(input, exportPlan, null, SVGBuilder.defaultSVGGenerator());
102 private static String render(final Resource input, final ImagePrinter.ImageExportPlan exportPlan, Margins margins, SVGGraphics2D svgExporter) throws Exception {
103 Resource diagram = Simantics.getSession().syncRequest(new Read<Resource>() {
105 public Resource perform(ReadGraph graph) throws DatabaseException {
106 DiagramResource DIA = DiagramResource.getInstance(graph);
107 if (graph.isInstanceOf(input, DIA.Diagram))
109 StructuralResource2 SR = StructuralResource2.getInstance(graph);
110 ModelingResources MOD = ModelingResources.getInstance(graph);
111 Layer0 L0 = Layer0.getInstance(graph);
112 if (graph.isInstanceOf(input, SR.Composite)) {
113 Resource possibleDiagram = graph.getPossibleObject(input, MOD.CompositeToDiagram);
114 if (possibleDiagram != null)
115 return possibleDiagram;
116 for (Resource r : graph.getObjects(input, L0.ConsistsOf)) {
117 if (graph.isInstanceOf(r, SR.Composite)) {
118 possibleDiagram = graph.getPossibleObject(input, MOD.CompositeToDiagram);
119 if (possibleDiagram != null)
120 return possibleDiagram;
128 throw new DatabaseException("Input " + input + " cannot be resolved as diagram");
133 final WorkerThread thread = new WorkerThread("Diagram Image Painter");
136 final CanvasContext ctx = new CanvasContext(thread);
137 final AtomicReference<ICanvasSceneGraphProvider> sgProvider = new AtomicReference<ICanvasSceneGraphProvider>();
138 final ISessionContext sessionContext = Simantics.getSessionContext();
139 final DataContainer<String> result = new DataContainer<String>(null);
140 final DataContainer<Exception> exception = new DataContainer<Exception>(null);
142 final Semaphore done = new Semaphore(0);
143 // IMPORTANT: Load diagram in a different thread than the canvas context thread!
144 ThreadUtils.getBlockingWorkExecutor().execute(new Runnable() {
148 Pair<Resource, String> modelAndRVI = sessionContext.getSession().syncRequest(new UniqueRead<Pair<Resource, String>>() {
150 public Pair<Resource, String> perform(ReadGraph graph) throws DatabaseException {
151 return new Pair<Resource, String>( resolveModel(graph, exportPlan.diagram ), resolveRVI(graph, exportPlan.diagram) );
155 ICanvasSceneGraphProvider provider = DiagramNodeUtil.loadSceneGraphProvider(ctx, modelAndRVI.first, exportPlan.diagram, modelAndRVI.second);
156 sgProvider.set( provider );
158 ThreadUtils.asyncExec(thread, new Runnable() {
162 SVGBuilder chassis = margins != null ?
163 new SVGBuilder(exportPlan.dpi,exportPlan.size,margins) :
164 new SVGBuilder(exportPlan.dpi,exportPlan.size,exportPlan.margin);
166 result.set(chassis.paint(ctx, svgExporter));
167 } catch (Exception e) {
174 } catch (DatabaseException e) {
177 } catch (Throwable e) {
178 exception.set(new DatabaseException(e));
187 if (exception.get() != null)
188 throw exception.get();
191 if (sgProvider.get() != null)
192 sgProvider.get().dispose();
197 private static Resource resolveModel(ReadGraph graph, Resource diagram) throws DatabaseException {
198 ModelingResources mod = ModelingResources.getInstance(graph);
199 Resource composite = graph.getSingleObject(diagram, mod.DiagramToComposite);
200 Resource model = graph.syncRequest(new PossibleIndexRoot(composite));
202 throw new ValidationException("no model found for composite " + NameUtils.getSafeName(graph, composite));
208 private static String resolveRVI(ReadGraph graph, Resource diagram) throws DatabaseException {
209 ModelingResources mod = ModelingResources.getInstance(graph);
210 Resource composite = graph.getSingleObject(diagram, mod.DiagramToComposite);
211 final ResourceArray compositePath = StructuralVariables.getCompositeArray(graph, composite);
212 final ResourceArray variablePath = compositePath.removeFromBeginning(1);
213 return StructuralVariables.getRVI(graph, variablePath);