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.Hints;
24 import org.simantics.g2d.canvas.impl.CanvasContext;
25 import org.simantics.g2d.scenegraph.ICanvasSceneGraphProvider;
26 import org.simantics.layer0.Layer0;
27 import org.simantics.modeling.ModelingResources;
28 import org.simantics.structural.stubs.StructuralResource2;
29 import org.simantics.structural2.StructuralVariables;
30 import org.simantics.utils.DataContainer;
31 import org.simantics.utils.datastructures.Pair;
32 import org.simantics.utils.page.MarginUtils.Margins;
33 import org.simantics.utils.threads.IThreadWorkQueue;
34 import org.simantics.utils.threads.ThreadUtils;
35 import org.simantics.utils.threads.WorkerThread;
38 public class DiagramToSVG {
41 public static String diagramToSVG(Resource diagram) {
44 ImagePrinter.ImageExportPlan exportPlan = new ImagePrinter.ImageExportPlan();
45 exportPlan.margin = 0.05;
46 exportPlan.dpi = 96.0;
47 exportPlan.diagram = diagram;
48 return render(diagram,exportPlan);
49 } catch (Exception e) {
51 return "<b>error</b> "+ e.getMessage();
57 public static String diagramToSVG(Resource diagram, int width, int height) {
60 ImagePrinter.ImageExportPlan exportPlan = new ImagePrinter.ImageExportPlan();
61 exportPlan.margin = 0.05;
62 exportPlan.size = new Point(width, height);
63 exportPlan.diagram = diagram;
64 return render(diagram,exportPlan);
65 } catch (Exception e) {
67 return "<b>error</b> "+ e.getMessage();
73 public static String diagramToSVG(Resource diagram, double dpi) {
76 ImagePrinter.ImageExportPlan exportPlan = new ImagePrinter.ImageExportPlan();
77 exportPlan.margin = 0.05;
79 exportPlan.diagram = diagram;
80 return render(diagram,exportPlan);
81 } catch (Exception e) {
83 return "<b>error</b> "+ e.getMessage();
89 public static String diagramToSVG(Resource diagram, ImagePrinter.ImageExportPlan exportPlan, Margins margins, SVGGraphics2D svgGenerator) {
92 return render(diagram,exportPlan,margins,svgGenerator);
93 } catch (Exception e) {
95 return "<b>error</b> "+ e.getMessage();
100 private static String render(final Resource input, final ImagePrinter.ImageExportPlan exportPlan) throws Exception {
101 return render(input, exportPlan, null, SVGBuilder.defaultSVGGenerator());
104 private static String render(final Resource input, final ImagePrinter.ImageExportPlan exportPlan, Margins margins, SVGGraphics2D svgExporter) throws Exception {
105 Resource diagram = Simantics.getSession().syncRequest(new Read<Resource>() {
107 public Resource perform(ReadGraph graph) throws DatabaseException {
108 DiagramResource DIA = DiagramResource.getInstance(graph);
109 if (graph.isInstanceOf(input, DIA.Diagram))
111 StructuralResource2 SR = StructuralResource2.getInstance(graph);
112 ModelingResources MOD = ModelingResources.getInstance(graph);
113 Layer0 L0 = Layer0.getInstance(graph);
114 if (graph.isInstanceOf(input, SR.Composite)) {
115 Resource possibleDiagram = graph.getPossibleObject(input, MOD.CompositeToDiagram);
116 if (possibleDiagram != null)
117 return possibleDiagram;
118 for (Resource r : graph.getObjects(input, L0.ConsistsOf)) {
119 if (graph.isInstanceOf(r, SR.Composite)) {
120 possibleDiagram = graph.getPossibleObject(input, MOD.CompositeToDiagram);
121 if (possibleDiagram != null)
122 return possibleDiagram;
130 throw new DatabaseException("Input " + input + " cannot be resolved as diagram");
132 final WorkerThread thread = new WorkerThread("Diagram Image Painter");
135 final CanvasContext ctx = new CanvasContext(thread);
136 ctx.getDefaultHintContext().setHint(Hints.KEY_DISABLE_GRAPH_MODIFICATIONS, Boolean.TRUE);
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));
206 private static String resolveRVI(ReadGraph graph, Resource diagram) throws DatabaseException {
207 ModelingResources mod = ModelingResources.getInstance(graph);
208 Resource composite = graph.getSingleObject(diagram, mod.DiagramToComposite);
209 final ResourceArray compositePath = StructuralVariables.getCompositeArray(graph, composite);
210 final ResourceArray variablePath = compositePath.removeFromBeginning(1);
211 return StructuralVariables.getRVI(graph, variablePath);
214 public static String renderWithLoader(final Resource input, final ImagePrinter.ImageExportPlan exportPlan,
215 Margins margins, SVGGraphics2D svgExporter,
216 IThreadWorkQueue loaderThread,
217 IThreadWorkQueue painterThread) throws Exception {
219 if(!painterThread.currentThreadAccess()) throw new IllegalStateException("The callable should be called from the contextThread");
221 final CanvasContext ctx = new CanvasContext(loaderThread);
222 ctx.getDefaultHintContext().setHint(Hints.KEY_DISABLE_GRAPH_MODIFICATIONS, Boolean.TRUE);
223 final AtomicReference<ICanvasSceneGraphProvider> sgProvider = new AtomicReference<ICanvasSceneGraphProvider>();
224 final DataContainer<String> result = new DataContainer<String>(null);
225 final DataContainer<Exception> exception = new DataContainer<Exception>(null);
229 final ISessionContext sessionContext = Simantics.getSessionContext();
231 Pair<Resource, String> modelAndRVI = sessionContext.getSession().syncRequest(new UniqueRead<Pair<Resource, String>>() {
233 public Pair<Resource, String> perform(ReadGraph graph) throws DatabaseException {
234 return new Pair<Resource, String>( resolveModel(graph, exportPlan.diagram ), resolveRVI(graph, exportPlan.diagram) );
238 ICanvasSceneGraphProvider provider = DiagramNodeUtil.loadSceneGraphProvider(ctx, modelAndRVI.first, exportPlan.diagram, modelAndRVI.second);
239 sgProvider.set( provider );
241 final Semaphore done = new Semaphore(0);
243 ThreadUtils.asyncExec(loaderThread, new Runnable() {
247 SVGBuilder chassis = margins != null ?
248 new SVGBuilder(exportPlan.dpi,exportPlan.size,margins) :
249 new SVGBuilder(exportPlan.dpi,exportPlan.size,exportPlan.margin);
250 result.set(chassis.paint(ctx, svgExporter));
251 } catch (DatabaseException e) {
254 } catch (Throwable e) {
255 exception.set(new DatabaseException(e));
265 } catch (DatabaseException e) {
267 } catch (Throwable e) {
268 exception.set(new DatabaseException(e));
271 if (sgProvider.get() != null)
272 sgProvider.get().dispose();
277 if(exception.get() != null)
278 throw exception.get();