]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/ControlPointFactory.java
Yesterdays change introduced infinite loop for directed path leg updates
[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.PointType;
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(String libUri) throws Exception {
24                 List<Item> items = P3DUtil.getEnds(libUri);
25                 items.addAll(P3DUtil.getInlines(libUri));
26                 items.addAll(P3DUtil.getNozzles(libUri));
27                 items.addAll(P3DUtil.getTurns(libUri));
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.setMod(inst.mod);
49                 pcp.setRotate(inst.isRotate);
50                 pcp.setReverse(inst.isReverse);
51                 switch(inst.type) {
52                 case END:
53                         
54                         break;
55                 case INLINE:
56                         if (inst.isOffset || inst.isSizeChange) {
57                                 PipeControlPoint sub = new PipeControlPoint(component);
58                                 pcp.children.add(sub);
59                                 sub.parent = pcp;
60                                 sub.setType(inst.type);
61                                 sub.setFixed(inst.fixed);
62                                 sub.setMod(inst.mod);
63                                 sub.setSub(true);
64                                 sub.setDeletable(false);
65                                 if (inst.isOffset)
66                                         pcp.setOffset(0.0);
67                                 if (inst.isSizeChange)
68                                         pcp.setSizeChange(true);
69                         }
70                         
71                         break;
72                 case TURN:
73                         
74                         break;
75                 }
76                 return pcp;
77         }
78         
79         
80         private static class Instruction {
81                 PointType type;
82                 boolean fixed;
83                 boolean mod;
84                 boolean isOffset;
85                 boolean isSizeChange;
86                 boolean isRotate;
87                 boolean isReverse;
88                 
89         }
90         
91         private static Instruction createInstruction(final String type) throws Exception {
92                 return Simantics.getSession().syncRequest(new Read<Instruction>() {
93                         @Override
94                         public Instruction perform(ReadGraph graph) throws DatabaseException {
95                                 Resource res = graph.getResource(type);
96                                 Plant3D p3d = Plant3D.getInstance(graph);
97                                 Instruction i = new Instruction();
98                                 i.fixed = false;
99                                 i.isOffset = false;
100                                 i.isSizeChange = false;
101                                 i.isRotate = false;
102                                 i.isReverse = false;
103                                 i.type = PointType.INLINE;
104                                 i.mod = false;
105                                 if (graph.isInheritedFrom(res, p3d.Nozzle)) {
106                                         i.fixed = true;
107                                         i.isOffset = false;
108                                         i.isSizeChange = false;
109                                         i.type = PointType.END;
110                                 } else if (graph.isInheritedFrom(res, p3d.InlineComponent)){
111                                         
112                                         i.type = PointType.INLINE;
113                                         if (graph.hasStatement(res,p3d.VariableLengthInlineComponent)) {
114                                                 i.fixed = false;
115                                         } else if (graph.hasStatement(res,p3d.FixedLengthInlineComponent)) {
116                                                 i.fixed = true;
117                                         } else if (graph.hasStatement(res,p3d.AdjustableLengthInlineComponent)) {
118                                             i.fixed = true;
119                                             i.mod = true;
120                                         } else {
121                                             throw new DatabaseException("Inline component type " + res + " does not have length configuration.");
122                                         }
123                                         
124                                         if (graph.hasStatement(res,p3d.SizeChangeComponent)) {
125                                                 i.isSizeChange = true;
126                                         }
127                                         
128                                         if (graph.hasStatement(res,p3d.OffsetComponent)) {
129                                                 i.isOffset = true;
130                                         }
131                                         if (graph.hasStatement(res,p3d.RotateComponent)) {
132                                                 i.isRotate = true;
133                                         }
134                                         
135                                         if (graph.hasStatement(res,p3d.ReverseComponent)) {
136                                                 i.isReverse = true;
137                                         }
138                                         
139                                 } else if (graph.isInheritedFrom(res, p3d.TurnComponent)) {
140                                         i.type = PointType.TURN;
141                                         if (graph.hasStatement(res,p3d.VariableAngleTurnComponent)) {
142                                                 i.fixed = false;
143                                         } else if (graph.hasStatement(res,p3d.FixedAngleTurnComponent)) {
144                                                 i.fixed = true; 
145                                         } else {
146                                             throw new DatabaseException("Turn component type " + res + " does not have angle configuration.");
147                                         }
148                                 } else if (graph.isInheritedFrom(res, p3d.EndComponent)) {
149                                         i.fixed = false;
150                                         i.type = PointType.END;
151                                 } else {
152                                         return null;
153                                 }
154                                 return i;
155                         }
156                 });
157         }
158
159 }