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.IThreadWorkQueue;
33 import org.simantics.utils.threads.ThreadUtils;
34 import org.simantics.utils.threads.WorkerThread;
37 public class DiagramToSVG {
40 public static String diagramToSVG(Resource diagram) {
43 ImagePrinter.ImageExportPlan exportPlan = new ImagePrinter.ImageExportPlan();
44 exportPlan.margin = 0.05;
45 exportPlan.dpi = 96.0;
46 exportPlan.diagram = diagram;
47 return render(diagram,exportPlan);
48 } catch (Exception e) {
50 return "<b>error</b> "+ e.getMessage();
56 public static String diagramToSVG(Resource diagram, int width, int height) {
59 ImagePrinter.ImageExportPlan exportPlan = new ImagePrinter.ImageExportPlan();
60 exportPlan.margin = 0.05;
61 exportPlan.size = new Point(width, height);
62 exportPlan.diagram = diagram;
63 return render(diagram,exportPlan);
64 } catch (Exception e) {
66 return "<b>error</b> "+ e.getMessage();
72 public static String diagramToSVG(Resource diagram, double dpi) {
75 ImagePrinter.ImageExportPlan exportPlan = new ImagePrinter.ImageExportPlan();
76 exportPlan.margin = 0.05;
78 exportPlan.diagram = diagram;
79 return render(diagram,exportPlan);
80 } catch (Exception e) {
82 return "<b>error</b> "+ e.getMessage();
88 public static String diagramToSVG(Resource diagram, ImagePrinter.ImageExportPlan exportPlan, Margins margins, SVGGraphics2D svgGenerator) {
91 return render(diagram,exportPlan,margins,svgGenerator);
92 } catch (Exception e) {
94 return "<b>error</b> "+ e.getMessage();
99 private static String render(final Resource input, final ImagePrinter.ImageExportPlan exportPlan) throws Exception {
100 return render(input, exportPlan, null, SVGBuilder.defaultSVGGenerator());
103 private static String render(final Resource input, final ImagePrinter.ImageExportPlan exportPlan, Margins margins, SVGGraphics2D svgExporter) throws Exception {
104 Resource diagram = Simantics.getSession().syncRequest(new Read<Resource>() {
106 public Resource perform(ReadGraph graph) throws DatabaseException {
107 DiagramResource DIA = DiagramResource.getInstance(graph);
108 if (graph.isInstanceOf(input, DIA.Diagram))
110 StructuralResource2 SR = StructuralResource2.getInstance(graph);
111 ModelingResources MOD = ModelingResources.getInstance(graph);
112 Layer0 L0 = Layer0.getInstance(graph);
113 if (graph.isInstanceOf(input, SR.Composite)) {
114 Resource possibleDiagram = graph.getPossibleObject(input, MOD.CompositeToDiagram);
115 if (possibleDiagram != null)
116 return possibleDiagram;
117 for (Resource r : graph.getObjects(input, L0.ConsistsOf)) {
118 if (graph.isInstanceOf(r, SR.Composite)) {
119 possibleDiagram = graph.getPossibleObject(input, MOD.CompositeToDiagram);
120 if (possibleDiagram != null)
121 return possibleDiagram;
129 throw new DatabaseException("Input " + input + " cannot be resolved as diagram");
131 final WorkerThread thread = new WorkerThread("Diagram Image Painter");
134 final CanvasContext ctx = new CanvasContext(thread);
135 final AtomicReference<ICanvasSceneGraphProvider> sgProvider = new AtomicReference<ICanvasSceneGraphProvider>();
136 final ISessionContext sessionContext = Simantics.getSessionContext();
137 final DataContainer<String> result = new DataContainer<String>(null);
138 final DataContainer<Exception> exception = new DataContainer<Exception>(null);
140 final Semaphore done = new Semaphore(0);
141 // IMPORTANT: Load diagram in a different thread than the canvas context thread!
142 ThreadUtils.getBlockingWorkExecutor().execute(new Runnable() {
146 Pair<Resource, String> modelAndRVI = sessionContext.getSession().syncRequest(new UniqueRead<Pair<Resource, String>>() {
148 public Pair<Resource, String> perform(ReadGraph graph) throws DatabaseException {
149 return new Pair<Resource, String>( resolveModel(graph, exportPlan.diagram ), resolveRVI(graph, exportPlan.diagram) );
153 ICanvasSceneGraphProvider provider = DiagramNodeUtil.loadSceneGraphProvider(ctx, modelAndRVI.first, exportPlan.diagram, modelAndRVI.second);
154 sgProvider.set( provider );
156 ThreadUtils.asyncExec(thread, new Runnable() {
160 SVGBuilder chassis = margins != null ?
161 new SVGBuilder(exportPlan.dpi,exportPlan.size,margins) :
162 new SVGBuilder(exportPlan.dpi,exportPlan.size,exportPlan.margin);
164 result.set(chassis.paint(ctx, svgExporter));
165 } catch (Exception e) {
172 } catch (DatabaseException e) {
175 } catch (Throwable e) {
176 exception.set(new DatabaseException(e));
185 if (exception.get() != null)
186 throw exception.get();
189 if (sgProvider.get() != null)
190 sgProvider.get().dispose();
195 private static Resource resolveModel(ReadGraph graph, Resource diagram) throws DatabaseException {
196 ModelingResources mod = ModelingResources.getInstance(graph);
197 Resource composite = graph.getSingleObject(diagram, mod.DiagramToComposite);
198 Resource model = graph.syncRequest(new PossibleIndexRoot(composite));
200 throw new ValidationException("no model found for composite " + NameUtils.getSafeName(graph, composite));
204 private static String resolveRVI(ReadGraph graph, Resource diagram) throws DatabaseException {
205 ModelingResources mod = ModelingResources.getInstance(graph);
206 Resource composite = graph.getSingleObject(diagram, mod.DiagramToComposite);
207 final ResourceArray compositePath = StructuralVariables.getCompositeArray(graph, composite);
208 final ResourceArray variablePath = compositePath.removeFromBeginning(1);
209 return StructuralVariables.getRVI(graph, variablePath);
212 public static String renderWithLoader(final Resource input, final ImagePrinter.ImageExportPlan exportPlan,
213 Margins margins, SVGGraphics2D svgExporter,
214 IThreadWorkQueue loaderThread,
215 IThreadWorkQueue painterThread) throws Exception {
217 if(!painterThread.currentThreadAccess()) throw new IllegalStateException("The callable should be called from the contextThread");
219 final CanvasContext ctx = new CanvasContext(loaderThread);
220 final AtomicReference<ICanvasSceneGraphProvider> sgProvider = new AtomicReference<ICanvasSceneGraphProvider>();
221 final DataContainer<String> result = new DataContainer<String>(null);
222 final DataContainer<Exception> exception = new DataContainer<Exception>(null);
226 final ISessionContext sessionContext = Simantics.getSessionContext();
228 Pair<Resource, String> modelAndRVI = sessionContext.getSession().syncRequest(new UniqueRead<Pair<Resource, String>>() {
230 public Pair<Resource, String> perform(ReadGraph graph) throws DatabaseException {
231 return new Pair<Resource, String>( resolveModel(graph, exportPlan.diagram ), resolveRVI(graph, exportPlan.diagram) );
235 ICanvasSceneGraphProvider provider = DiagramNodeUtil.loadSceneGraphProvider(ctx, modelAndRVI.first, exportPlan.diagram, modelAndRVI.second);
236 sgProvider.set( provider );
238 final Semaphore done = new Semaphore(0);
240 ThreadUtils.asyncExec(loaderThread, new Runnable() {
244 SVGBuilder chassis = margins != null ?
245 new SVGBuilder(exportPlan.dpi,exportPlan.size,margins) :
246 new SVGBuilder(exportPlan.dpi,exportPlan.size,exportPlan.margin);
247 result.set(chassis.paint(ctx, svgExporter));
248 } catch (DatabaseException e) {
251 } catch (Throwable e) {
252 exception.set(new DatabaseException(e));
262 } catch (DatabaseException e) {
264 } catch (Throwable e) {
265 exception.set(new DatabaseException(e));
268 if (sgProvider.get() != null)
269 sgProvider.get().dispose();
274 if(exception.get() != null)
275 throw exception.get();