import org.simantics.utils.DataContainer;
import org.simantics.utils.datastructures.Pair;
import org.simantics.utils.page.MarginUtils.Margins;
+import org.simantics.utils.threads.IThreadWorkQueue;
import org.simantics.utils.threads.ThreadUtils;
import org.simantics.utils.threads.WorkerThread;
if (diagram == null)
throw new DatabaseException("Input " + input + " cannot be resolved as diagram");
-
-
-
final WorkerThread thread = new WorkerThread("Diagram Image Painter");
thread.start();
return model;
}
-
-
private static String resolveRVI(ReadGraph graph, Resource diagram) throws DatabaseException {
ModelingResources mod = ModelingResources.getInstance(graph);
Resource composite = graph.getSingleObject(diagram, mod.DiagramToComposite);
return StructuralVariables.getRVI(graph, variablePath);
}
+ public static String renderWithLoader(final Resource input, final ImagePrinter.ImageExportPlan exportPlan,
+ Margins margins, SVGGraphics2D svgExporter,
+ IThreadWorkQueue loaderThread,
+ IThreadWorkQueue painterThread) throws Exception {
+
+ if(!painterThread.currentThreadAccess()) throw new IllegalStateException("The callable should be called from the contextThread");
+
+ final CanvasContext ctx = new CanvasContext(loaderThread);
+ final AtomicReference<ICanvasSceneGraphProvider> sgProvider = new AtomicReference<ICanvasSceneGraphProvider>();
+ final DataContainer<String> result = new DataContainer<String>(null);
+ final DataContainer<Exception> exception = new DataContainer<Exception>(null);
+
+ try {
+
+ final ISessionContext sessionContext = Simantics.getSessionContext();
+
+ Pair<Resource, String> modelAndRVI = sessionContext.getSession().syncRequest(new UniqueRead<Pair<Resource, String>>() {
+ @Override
+ public Pair<Resource, String> perform(ReadGraph graph) throws DatabaseException {
+ return new Pair<Resource, String>( resolveModel(graph, exportPlan.diagram ), resolveRVI(graph, exportPlan.diagram) );
+ }
+ });
+
+ ICanvasSceneGraphProvider provider = DiagramNodeUtil.loadSceneGraphProvider(ctx, modelAndRVI.first, exportPlan.diagram, modelAndRVI.second);
+ sgProvider.set( provider );
+
+ final Semaphore done = new Semaphore(0);
+
+ ThreadUtils.asyncExec(loaderThread, new Runnable() {
+ @Override
+ public void run() {
+ try {
+ SVGBuilder chassis = margins != null ?
+ new SVGBuilder(exportPlan.dpi,exportPlan.size,margins) :
+ new SVGBuilder(exportPlan.dpi,exportPlan.size,exportPlan.margin);
+ result.set(chassis.paint(ctx, svgExporter));
+ } catch (DatabaseException e) {
+ exception.set(e);
+ done.release();
+ } catch (Throwable e) {
+ exception.set(new DatabaseException(e));
+ done.release();
+ } finally {
+ done.release();
+ }
+ }
+ });
+
+ done.acquire();
+
+ } catch (DatabaseException e) {
+ exception.set(e);
+ } catch (Throwable e) {
+ exception.set(new DatabaseException(e));
+ } finally {
+
+ if (sgProvider.get() != null)
+ sgProvider.get().dispose();
+ ctx.dispose();
+
+ }
+
+ if(exception.get() != null)
+ throw exception.get();
+
+ return result.get();
+
+ }
+
}