]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/ControlPointFactory.java
824f74d6741e9e07643fdc197b10b63b9d03e190
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / scenegraph / controlpoint / ControlPointFactory.java
1 package org.simantics.plant3d.scenegraph.controlpoint;
2
3 import java.util.HashMap;
4 import java.util.List;
5 import java.util.Map;
6
7 import org.simantics.Simantics;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.request.Read;
12 import org.simantics.plant3d.ontology.Plant3D;
13 import org.simantics.plant3d.scenegraph.PipelineComponent;
14 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.Type;
15 import org.simantics.plant3d.utils.Item;
16 import org.simantics.plant3d.utils.P3DUtil;
17
18 public class ControlPointFactory {
19         
20         private static Map<String,Instruction> cache = new HashMap<String, Instruction>();
21         
22         
23         public static void preloadCache() throws Exception {
24                 List<Item> items = P3DUtil.getEnds();
25                 items.addAll(P3DUtil.getInlines());
26                 items.addAll(P3DUtil.getNozzles());
27                 items.addAll(P3DUtil.getTurns());
28                 
29                 for (Item item : items) {
30                         Instruction inst = createInstruction(item.getUri());
31                         cache.put(item.getUri(), inst);
32                 }
33         }
34         
35         public static PipeControlPoint create(PipelineComponent component) throws Exception {
36                 Instruction inst = cache.get(component.getType());
37                 if (inst == null) {
38                         inst = createInstruction(component.getType());
39                         cache.put(component.getType(), inst);
40                 }
41                 if (inst == null) {
42                         return null;
43                 }
44                 
45                 PipeControlPoint pcp = new PipeControlPoint(component);
46                 pcp.setType(inst.type);
47                 pcp.setFixed(inst.fixed);
48                 pcp.setRotate(inst.isRotate);
49                 switch(inst.type) {
50                 case END:
51                         
52                         break;
53                 case INLINE:
54                         if (inst.isOffset || inst.isSizeChange) {
55                                 PipeControlPoint sub = new PipeControlPoint(component);
56                                 pcp.children.add(sub);
57                                 sub.parent = pcp;
58                                 sub.setType(inst.type);
59                                 sub.setFixed(inst.fixed);
60                                 sub.setSub(true);
61                                 sub.setDeletable(false);
62 //                              pcp.setOffset(0.0);
63                                 if (inst.isOffset)
64                                         pcp.setOffset(0.0);
65                         }
66                         
67                         break;
68                 case TURN:
69                         
70                         break;
71                 }
72                 return pcp;
73         }
74         
75         
76         private static class Instruction {
77                 Type type;
78                 boolean fixed;
79                 boolean isOffset;
80                 boolean isSizeChange;
81                 boolean isRotate;
82                 
83         }
84         
85         private static Instruction createInstruction(final String type) throws Exception {
86                 return Simantics.getSession().syncRequest(new Read<Instruction>() {
87                         @Override
88                         public Instruction perform(ReadGraph graph) throws DatabaseException {
89                                 Resource res = graph.getResource(type);
90                                 Plant3D p3d = Plant3D.getInstance(graph);
91                                 Instruction i = new Instruction();
92                                 i.fixed = false;
93                                 i.isOffset = false;
94                                 i.isSizeChange = false;
95                                 i.isRotate = false;
96                                 i.type = Type.INLINE;
97                                 if (graph.isInheritedFrom(res, p3d.Nozzle)) {
98                                         i.fixed = true;
99                                         i.isOffset = false;
100                                         i.isSizeChange = false;
101                                         i.type = Type.END;
102                                 } else if (graph.isInheritedFrom(res, p3d.InlineComponent)){
103                                         
104                                         i.type = Type.INLINE;
105                                         if (graph.hasStatement(res,p3d.VariableLengthInlineComponent)) {
106                                                 i.fixed = false;
107                                         } else if (graph.hasStatement(res,p3d.FixedLengthInlineComponent)) {
108                                                 i.fixed = true;
109                                         }
110                                         
111                                         if (graph.hasStatement(res,p3d.SizeChangeComponent)) {
112                                                 i.isSizeChange = true;
113                                         }
114                                         
115                                         if (graph.hasStatement(res,p3d.OffsetComponent)) {
116                                                 i.isOffset = true;
117                                         }
118                                         if (graph.hasStatement(res,p3d.RotateComponent)) {
119                                                 i.isRotate = true;
120                                         }
121                                         
122                                 } else if (graph.isInheritedFrom(res, p3d.TurnComponent)) {
123                                         i.type = Type.TURN;
124                                         if (graph.hasStatement(res,p3d.VariableAngleTurnComponent)) {
125                                                 i.fixed = false;
126                                         } else if (graph.hasStatement(res,p3d.FixedAngleTurnComponent)) {
127                                                 i.fixed = true; 
128                                         }
129                                 } else if (graph.isInheritedFrom(res, p3d.EndComponent)) {
130                                         i.fixed = false;
131                                         i.type = Type.END;
132                                 } else {
133                                         return null;
134                                 }
135                                 return i;
136                         }
137                 });
138         }
139
140 }