]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/utils/ComponentUtils.java
Support for inline component rotations
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / utils / ComponentUtils.java
1 package org.simantics.plant3d.utils;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 import org.simantics.Simantics;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.common.request.ReadRequest;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.g3d.scenegraph.GeometryProvider;
14 import org.simantics.layer0.Layer0;
15 import org.simantics.plant3d.ontology.Plant3D;
16 import org.simantics.plant3d.scenegraph.EndComponent;
17 import org.simantics.plant3d.scenegraph.InlineComponent;
18 import org.simantics.plant3d.scenegraph.Nozzle;
19 import org.simantics.plant3d.scenegraph.P3DRootNode;
20 import org.simantics.plant3d.scenegraph.PipelineComponent;
21 import org.simantics.plant3d.scenegraph.TurnComponent;
22
23 public class ComponentUtils {
24
25         
26         private static Map<String,Class<? extends PipelineComponent>> clazzes = new HashMap<String, Class<? extends PipelineComponent>>();
27         private static Map<String,GeometryProvider> providers = new HashMap<String,GeometryProvider>();
28         
29         public static void preloadCache() {
30                 Simantics.getSession().asyncRequest(new ReadRequest() {
31                         
32                         @Override
33                         public void run(ReadGraph graph) throws DatabaseException {
34                                 List<String> types = new ArrayList<String>();
35                                 types.add(Plant3D.URIs.Builtin_Straight);
36                                 types.add(Plant3D.URIs.Builtin_Elbow);
37                                 types.add(Plant3D.URIs.Builtin_ConcentricReducer);
38                                 types.add(Plant3D.URIs.Builtin_BranchSplitComponent);
39                                 types.add(Plant3D.URIs.Builtin_EccentricReducer);
40                                 
41                                 for (String typeURI : types) {
42                                         load(graph, typeURI);
43                                 }
44                         }
45                 });
46         }
47         
48         private static GeometryProvider getProvider(ReadGraph graph, Resource type) throws DatabaseException {
49                 
50                 Layer0 l0 = Layer0.getInstance(graph);
51                 Plant3D p3d = Plant3D.getInstance(graph);
52                 Resource geom = graph.getPossibleObject(type,p3d.hasGeometry);
53                 if (geom == null) {
54                         for (Resource a : graph.getObjects(type, l0.Asserts)) {
55                                 if (p3d.hasGeometry.equals(graph.getPossibleObject(a, l0.HasPredicate))) {
56                                         geom = graph.getPossibleObject(a, l0.HasObject);
57                                         break;
58                                 }
59                         }
60                 }
61                 if (geom != null) {
62                         GeometryProvider provider = graph.adapt(geom, GeometryProvider.class);
63                         return provider;
64                 }
65                 return null;
66         }
67         
68         private static Class<? extends PipelineComponent> getClazz(ReadGraph graph, Resource type) throws DatabaseException {
69                 Plant3D p3d = Plant3D.getInstance(graph);
70                 if (graph.isInheritedFrom(type, p3d.InlineComponent))
71                         return InlineComponent.class;
72                 if (graph.isInheritedFrom(type, p3d.TurnComponent))
73                         return TurnComponent.class;
74                 if (graph.isInheritedFrom(type, p3d.EndComponent))
75                         return EndComponent.class;
76                 if (graph.isInheritedFrom(type, p3d.Nozzle))
77                         return Nozzle.class;
78                 return null;
79         }
80         
81         private static void load(ReadGraph graph, String typeURI) throws DatabaseException {
82                 Plant3D p3d = Plant3D.getInstance(graph);
83                 Resource type = graph.getResource(typeURI);
84                 
85                 GeometryProvider provider = getProvider(graph, type);
86                 if (provider != null || graph.hasStatement(type,p3d.NonVisibleComponent)) {
87                         providers.put(typeURI, provider);
88                         ComponentUtils.clazzes.put(typeURI,getClazz(graph, type));
89                         return;
90                 }
91                 throw new DatabaseException("Cannot find component for " + typeURI);
92         }
93         
94         private static void load(final String typeURI) throws DatabaseException {
95                 Simantics.getSession().syncRequest(new ReadRequest() {
96                         
97                         @Override
98                         public void run(ReadGraph graph) throws DatabaseException {
99                                 load(graph,typeURI);    
100                         }
101                 });
102         }
103         
104         public static PipelineComponent createComponent(P3DRootNode root, String typeURI) throws Exception {
105                 Class<? extends PipelineComponent> type = clazzes.get(typeURI);
106                 GeometryProvider provider = providers.get(typeURI);
107                 if (type == null || provider == null) {
108                         load(typeURI);
109                         type = clazzes.get(typeURI);
110                         provider = providers.get(typeURI);
111                 }
112                 //PipelineComponent component = type.newInstance();
113                 PipelineComponent component = null;
114                 if (type == InlineComponent.class) {
115                         component = root.createInline();
116                 } else if (type == TurnComponent.class) {
117                         component = root.createTurn();
118                 } else if (type == EndComponent.class) {
119                         component = root.createTurn();
120                 } else if (type == Nozzle.class) {
121                         component = root.createNozzle();
122                 }
123                 component.setType(typeURI);
124                 component.setGeometry(provider);
125                 return component;
126         }
127         
128         public static InlineComponent createStraight(P3DRootNode root) throws Exception{
129                 InlineComponent component = root.createInline();
130                 component.setType(Plant3D.URIs.Builtin_Straight);
131                 component.setGeometry(providers.get(Plant3D.URIs.Builtin_Straight));
132                 return component;
133         }
134         
135         public static TurnComponent createTurn(P3DRootNode root) throws Exception {
136                 TurnComponent elbow = root.createTurn();
137                 elbow.setType(Plant3D.URIs.Builtin_Elbow);
138                 elbow.setGeometry(providers.get(Plant3D.URIs.Builtin_Elbow));
139                 return elbow;
140         }
141         
142         public static InlineComponent createReducer(P3DRootNode root) throws Exception {
143                 InlineComponent component = root.createInline();
144                 component.setType(Plant3D.URIs.Builtin_ConcentricReducer);
145                 component.setGeometry(providers.get(Plant3D.URIs.Builtin_ConcentricReducer));
146                 return component;
147         }
148         
149         public static InlineComponent createBranchSplit(P3DRootNode root) throws Exception {
150                 InlineComponent component = root.createInline();
151                 component.setType(Plant3D.URIs.Builtin_BranchSplitComponent);
152                 return component;
153         }
154 }