]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/scl/SCLUtil.java
Ask component rotation angle when adding components
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / scl / SCLUtil.java
1 package org.simantics.plant3d.scl;
2
3 import org.simantics.Simantics;
4 import org.simantics.db.ReadGraph;
5 import org.simantics.db.Resource;
6 import org.simantics.db.exception.DatabaseException;
7 import org.simantics.db.request.Read;
8 import org.simantics.g3d.scenegraph.base.INode;
9 import org.simantics.objmap.graph.IMapping;
10 import org.simantics.objmap.graph.Mappings;
11 import org.simantics.objmap.graph.schema.IMappingSchema;
12 import org.simantics.plant3d.ontology.Plant3D;
13 import org.simantics.plant3d.scenegraph.P3DRootNode;
14 import org.simantics.plant3d.scenegraph.SchemaBuilder;
15 import org.simantics.plant3d.scenegraph.controlpoint.ControlPointFactory;
16 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
17 import org.simantics.plant3d.utils.ComponentUtils;
18 import org.simantics.plant3d.utils.P3DUtil;
19
20 public class SCLUtil {
21
22         public static interface SchemaProvider {
23                 IMappingSchema<Resource,INode> get(ReadGraph graph) throws DatabaseException;
24         }
25
26         public static interface NodeMapProvider {
27                 P3DScriptNodeMap get(IMapping<Resource, INode> mapping, P3DRootNode rootNode);
28         }
29         
30         public static P3DScriptNodeMap load(final Resource root) throws DatabaseException {
31                 preload(Plant3D.URIs.Builtin);
32
33                 SchemaProvider schemaProvider = g -> SchemaBuilder.getSchema(g);
34                 NodeMapProvider mapProvider = (mapping, rootNode) -> new P3DScriptNodeMap(Simantics.getSession(), mapping, rootNode);
35                 return load(root, schemaProvider, mapProvider);
36         }
37
38         public static P3DRootNode loadReadOnly(final Resource root) throws DatabaseException {
39                 preload(Plant3D.URIs.Builtin);
40                 
41                 SchemaProvider schemaProvider = g -> SchemaBuilder.getSchema(g);
42                 return loadReadOnly(root, schemaProvider);
43         }
44
45         public static void preload(String symbolSet) throws DatabaseException {
46                 try {
47                         ControlPointFactory.preloadCache(symbolSet);
48                         ComponentUtils.preloadCache();
49                 } catch (Exception e) {
50                         throw new DatabaseException(e);
51                 }
52         }
53
54         public static <NodeMap extends P3DScriptNodeMap> NodeMap load(final Resource root, SchemaProvider schemaProvider, NodeMapProvider mapProvider) throws DatabaseException {
55                 return Simantics.getSession().syncRequest(new Read<NodeMap>() {
56                         @Override
57                         public NodeMap perform(ReadGraph graph) throws DatabaseException {
58                                 PipingRules.setEnabled(false);
59                                 IMappingSchema<Resource, INode> schema = schemaProvider.get(graph);
60                                 IMapping<Resource, INode> mapping = Mappings.createWithListening(schema);
61                                 P3DRootNode rootNode = (P3DRootNode) mapping.map(graph, root);
62                                 rootNode.setMapping(mapping);
63                                 try {
64                                         P3DUtil.finalizeDBLoad(rootNode);
65                                         // FIXME: Something needs to be done here...
66                                         P3DUtil.finalizeDBLoad2(rootNode);
67                                 } catch (Exception e) {
68                                         throw new DatabaseException(e);
69                                 }
70                                 
71                                 @SuppressWarnings("unchecked")
72                                 NodeMap nodeMap = (NodeMap) mapProvider.get(mapping, rootNode);
73                                 return nodeMap;
74                         }
75                 });
76         }
77
78         public static <Root extends P3DRootNode> Root loadReadOnly(final Resource root, SchemaProvider schemaProvider)
79                         throws DatabaseException {
80                 return Simantics.getSession().syncRequest(new Read<Root>() {
81                         @Override
82                         public Root perform(ReadGraph graph) throws DatabaseException {
83                                 PipingRules.setEnabled(false);
84                                 IMappingSchema<Resource, INode> schema = schemaProvider.get(graph);
85                                 IMapping<Resource, INode> mapping = Mappings.createWithoutListening(schema);
86                                 @SuppressWarnings("unchecked")
87                                 Root rootNode = (Root) mapping.map(graph, root);
88                                 rootNode.setMapping(mapping);
89                                 try {
90                                         P3DUtil.finalizeDBLoad(rootNode);
91                                         P3DUtil.finalizeDBLoad2(rootNode);
92                                 } catch (Exception e) {
93                                         throw new DatabaseException(e);
94                                 }
95                                 
96                                 return rootNode;
97                         }
98                 });
99         }
100
101 }