]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram.svg/src/org/simantics/diagram/svg/export/DiagramToSVG.java
3c110976294a86bf1aa2414ea50eda567881365b
[simantics/platform.git] / bundles / org.simantics.diagram.svg / src / org / simantics / diagram / svg / export / DiagramToSVG.java
1 package org.simantics.diagram.svg.export;
2
3
4 import java.awt.Point;
5 import java.util.concurrent.Semaphore;
6 import java.util.concurrent.atomic.AtomicReference;
7
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;
34
35
36 public class DiagramToSVG {
37         
38         
39         public static String diagramToSVG(Resource diagram) {
40
41                 try {
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) {
48                         e.printStackTrace();
49                         return "<b>error</b> "+ e.getMessage();
50                 }
51  
52                 
53         }
54         
55         public static String diagramToSVG(Resource diagram, int width, int height) {
56
57                 try {
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) {
64                         e.printStackTrace();
65                         return "<b>error</b> "+ e.getMessage();
66                 }
67  
68                 
69         }
70         
71         public static String diagramToSVG(Resource diagram, double dpi) {
72
73                 try {
74                         ImagePrinter.ImageExportPlan exportPlan = new ImagePrinter.ImageExportPlan();
75                         exportPlan.margin = 0.05;
76                         exportPlan.dpi = dpi;
77                         exportPlan.diagram = diagram;
78                         return render(diagram,exportPlan);
79                 } catch (Exception e) {
80                         e.printStackTrace();
81                         return "<b>error</b> "+ e.getMessage();
82                 }
83  
84                 
85         }
86         
87         public static String diagramToSVG(Resource diagram, ImagePrinter.ImageExportPlan exportPlan, Margins margins, SVGGraphics2D svgGenerator) {
88
89                 try {
90                         return render(diagram,exportPlan,margins,svgGenerator);
91                 } catch (Exception e) {
92                         e.printStackTrace();
93                         return "<b>error</b> "+ e.getMessage();
94                 }
95                 
96         }
97         
98         private static String render(final Resource input, final ImagePrinter.ImageExportPlan exportPlan) throws Exception {
99                 return render(input, exportPlan, null, SVGBuilder.defaultSVGGenerator()); 
100         }
101         
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>() {
104                         @Override
105                         public Resource perform(ReadGraph graph) throws DatabaseException {
106                                 DiagramResource DIA = DiagramResource.getInstance(graph);
107                                 if (graph.isInstanceOf(input, DIA.Diagram))
108                                         return input;
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;
121                                                 }
122                                         }
123                                 }
124                                 return null;
125                         }
126                 });
127                 if (diagram == null)
128                         throw new DatabaseException("Input " + input + " cannot be resolved as diagram");
129                 
130                 
131         
132                 
133                 final WorkerThread thread = new WorkerThread("Diagram Image Painter");
134                 thread.start();
135                 
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);
141         try {
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() {
145                 @Override
146                 public void run() {
147                     try {
148                         Pair<Resource, String> modelAndRVI = sessionContext.getSession().syncRequest(new UniqueRead<Pair<Resource, String>>() {
149                             @Override
150                             public Pair<Resource, String> perform(ReadGraph graph) throws DatabaseException {
151                                 return new Pair<Resource, String>( resolveModel(graph, exportPlan.diagram ), resolveRVI(graph, exportPlan.diagram) );
152                             }
153                         });
154
155                         ICanvasSceneGraphProvider provider = DiagramNodeUtil.loadSceneGraphProvider(ctx, modelAndRVI.first, exportPlan.diagram, modelAndRVI.second);
156                         sgProvider.set( provider );
157                         
158                         ThreadUtils.asyncExec(thread, new Runnable() {
159                             @Override
160                             public void run() {
161                                 try {
162                                     SVGBuilder chassis = margins != null ?
163                                                 new SVGBuilder(exportPlan.dpi,exportPlan.size,margins) :
164                                                 new SVGBuilder(exportPlan.dpi,exportPlan.size,exportPlan.margin);
165                                     
166                                     result.set(chassis.paint(ctx, svgExporter));
167                                 } catch (Exception e) {
168                                         exception.set(e);
169                                 } finally {
170                                     done.release();
171                                 }
172                             }
173                         });
174                     } catch (DatabaseException e) {
175                         exception.set(e);
176                         done.release();
177                     } catch (Throwable e) {
178                         exception.set(new DatabaseException(e));
179                         done.release();
180                     } finally {
181                         done.release();
182                     }
183                 }
184             });
185
186             done.acquire(2);
187             if (exception.get() != null)
188                 throw exception.get();
189             return result.get();
190         } finally {
191             if (sgProvider.get() != null)
192                 sgProvider.get().dispose();
193             ctx.dispose();
194         }
195         }
196         
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));
201         if (model == null)
202             throw new ValidationException("no model found for composite " + NameUtils.getSafeName(graph, composite));
203         return model;
204     }
205
206
207
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);
214     }
215
216 }