]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/utils/ComponentUtils.java
Merge "First version of fixed nozzle positions"
[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.Equipment;
18 import org.simantics.plant3d.scenegraph.InlineComponent;
19 import org.simantics.plant3d.scenegraph.Nozzle;
20 import org.simantics.plant3d.scenegraph.P3DRootNode;
21 import org.simantics.plant3d.scenegraph.PipeRun;
22 import org.simantics.plant3d.scenegraph.PipelineComponent;
23 import org.simantics.plant3d.scenegraph.TurnComponent;
24
25 public class ComponentUtils {
26
27         
28         private static Map<String,Class<? extends PipelineComponent>> clazzes = new HashMap<String, Class<? extends PipelineComponent>>();
29         private static Map<String,GeometryProvider> providers = new HashMap<String,GeometryProvider>();
30         
31         public static void preloadCache() {
32                 Simantics.getSession().asyncRequest(new ReadRequest() {
33                         
34                         @Override
35                         public void run(ReadGraph graph) throws DatabaseException {
36                                 List<String> types = new ArrayList<String>();
37                                 types.add(Plant3D.URIs.Builtin_Straight);
38                                 types.add(Plant3D.URIs.Builtin_Elbow);
39                                 types.add(Plant3D.URIs.Builtin_ConcentricReducer);
40                                 types.add(Plant3D.URIs.Builtin_BranchSplitComponent);
41                                 types.add(Plant3D.URIs.Builtin_EccentricReducer);
42                                 
43                                 for (String typeURI : types) {
44                                         load(graph, typeURI);
45                                 }
46                         }
47                 });
48         }
49         
50         private static GeometryProvider getProvider(ReadGraph graph, Resource type) throws DatabaseException {
51                 
52                 Layer0 l0 = Layer0.getInstance(graph);
53                 Plant3D p3d = Plant3D.getInstance(graph);
54                 Resource geom = graph.getPossibleObject(type,p3d.hasGeometry);
55                 if (geom == null) {
56                         for (Resource a : graph.getObjects(type, l0.Asserts)) {
57                                 if (p3d.hasGeometry.equals(graph.getPossibleObject(a, l0.HasPredicate))) {
58                                         geom = graph.getPossibleObject(a, l0.HasObject);
59                                         break;
60                                 }
61                         }
62                 }
63                 if (geom != null) {
64                         GeometryProvider provider = graph.adapt(geom, GeometryProvider.class);
65                         return provider;
66                 }
67                 return null;
68         }
69         
70         private static Class<? extends PipelineComponent> getClazz(ReadGraph graph, Resource type) throws DatabaseException {
71                 Plant3D p3d = Plant3D.getInstance(graph);
72                 if (graph.isInheritedFrom(type, p3d.InlineComponent))
73                         return InlineComponent.class;
74                 if (graph.isInheritedFrom(type, p3d.TurnComponent))
75                         return TurnComponent.class;
76                 if (graph.isInheritedFrom(type, p3d.EndComponent))
77                         return EndComponent.class;
78                 if (graph.isInheritedFrom(type, p3d.Nozzle))
79                         return Nozzle.class;
80                 return null;
81         }
82         
83         private static void load(ReadGraph graph, String typeURI) throws DatabaseException {
84                 Plant3D p3d = Plant3D.getInstance(graph);
85                 Resource type = graph.getResource(typeURI);
86                 
87                 GeometryProvider provider = getProvider(graph, type);
88                 if (provider != null || graph.hasStatement(type,p3d.NonVisibleComponent)) {
89                         providers.put(typeURI, provider);
90                         clazzes.put(typeURI,getClazz(graph, type));
91                         return;
92                 }
93                 throw new DatabaseException("Cannot find component for " + typeURI);
94         }
95         
96         private static void load(final String typeURI) throws DatabaseException {
97                 Simantics.getSession().syncRequest(new ReadRequest() {
98                         
99                         @Override
100                         public void run(ReadGraph graph) throws DatabaseException {
101                                 load(graph,typeURI);    
102                         }
103                 });
104         }
105         
106         public static PipelineComponent createComponent(P3DRootNode root, String typeURI) throws Exception {
107                 Class<? extends PipelineComponent> type = clazzes.get(typeURI);
108                 GeometryProvider provider = providers.get(typeURI);
109                 if (type == null || provider == null) {
110                         load(typeURI);
111                         type = clazzes.get(typeURI);
112                         provider = providers.get(typeURI);
113                 }
114                 //PipelineComponent component = type.newInstance();
115                 PipelineComponent component = null;
116                 if (type == InlineComponent.class) {
117                         component = root.createInline();
118                 } else if (type == TurnComponent.class) {
119                         component = root.createTurn();
120                 } else if (type == EndComponent.class) {
121                         component = root.createTurn();
122                 } else if (type == Nozzle.class) {
123                         component = root.createNozzle();
124                 }
125                 component.setType(typeURI);
126                 component.setGeometry(provider);
127                 return component;
128         }
129         
130         public static InlineComponent createStraight(P3DRootNode root) throws Exception{
131                 InlineComponent component = root.createInline();
132                 component.setType(Plant3D.URIs.Builtin_Straight);
133                 component.setGeometry(providers.get(Plant3D.URIs.Builtin_Straight));
134                 return component;
135         }
136         
137         public static TurnComponent createTurn(P3DRootNode root) throws Exception {
138                 TurnComponent elbow = root.createTurn();
139                 elbow.setType(Plant3D.URIs.Builtin_Elbow);
140                 elbow.setGeometry(providers.get(Plant3D.URIs.Builtin_Elbow));
141                 return elbow;
142         }
143         
144         public static InlineComponent createReducer(P3DRootNode root) throws Exception {
145                 InlineComponent component = root.createInline();
146                 component.setType(Plant3D.URIs.Builtin_ConcentricReducer);
147                 component.setGeometry(providers.get(Plant3D.URIs.Builtin_ConcentricReducer));
148                 return component;
149         }
150         
151         public static InlineComponent createBranchSplit(P3DRootNode root) throws Exception {
152                 InlineComponent component = root.createInline();
153                 component.setType(Plant3D.URIs.Builtin_BranchSplitComponent);
154                 return component;
155         }
156         
157         public static Equipment createEquipment(P3DRootNode root, Item equipmentType) throws Exception {
158             Equipment equipment = root.createEquipment();
159         equipment.setType(equipmentType.getUri());
160         String n = root.getUniqueName(equipmentType.getName());
161         equipment.setName(n);
162         root.addChild(equipment);
163         return equipment;
164         }
165         
166         public static Nozzle createDefaultNozzle(P3DRootNode root, Equipment equipment) throws Exception {
167             return createNozzle(root, equipment, new Item(Plant3D.URIs.Builtin_Nozzle, "Nozzle"));
168         }
169         
170         public static Nozzle createNozzle(P3DRootNode root, Equipment equipment, Item nozzleType) throws Exception {
171             Nozzle nozzle = root.createNozzle();
172         nozzle.setType(nozzleType.getUri());
173         String n = root.getUniqueName(nozzleType.getName());
174         nozzle.setName(n);
175         PipeRun pipeRun = new PipeRun();
176         n = root.getUniqueName("PipeRun");
177         pipeRun.setName(n);
178         nozzle.setPipeRun(pipeRun);
179         
180         equipment.addChild(nozzle);
181         root.addChild(pipeRun);
182         // root.getNodeMap().commit("Add nozzle " + n);
183         return nozzle;
184         }
185 }