]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/ControlPointFactory.java
Support for fillets and chamfers
[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                 switch(inst.type) {
49                 case END:
50                         
51                         break;
52                 case INLINE:
53                         if (inst.isOffset || inst.isSizeChange) {
54                                 PipeControlPoint sub = new PipeControlPoint(component);
55                                 pcp.children.add(sub);
56                                 sub.parent = pcp;
57                                 sub.setType(inst.type);
58                                 sub.setFixed(inst.fixed);
59                                 sub.setSub(true);
60                                 sub.setDeletable(false);
61 //                              pcp.setOffset(0.0);
62                                 if (inst.isOffset)
63                                         pcp.setOffset(0.0);
64                         }
65                         
66                         break;
67                 case TURN:
68                         
69                         break;
70                 }
71                 return pcp;
72         }
73         
74         
75         private static class Instruction {
76                 Type type;
77                 boolean fixed;
78                 boolean isOffset;
79                 boolean isSizeChange;
80                 
81                 
82         }
83         
84         private static Instruction createInstruction(final String type) throws Exception {
85                 return Simantics.getSession().syncRequest(new Read<Instruction>() {
86                         @Override
87                         public Instruction perform(ReadGraph graph) throws DatabaseException {
88                                 Resource res = graph.getResource(type);
89                                 Plant3D p3d = Plant3D.getInstance(graph);
90                                 Instruction i = new Instruction();
91                                 i.fixed = false;
92                                 i.isOffset = false;
93                                 i.isSizeChange = false;
94                                 i.type = Type.INLINE;
95                                 if (graph.isInheritedFrom(res, p3d.Nozzle)) {
96                                         i.fixed = true;
97                                         i.isOffset = false;
98                                         i.isSizeChange = false;
99                                         i.type = Type.END;
100                                 } else if (graph.isInheritedFrom(res, p3d.InlineComponent)){
101                                         
102                                         i.type = Type.INLINE;
103                                         if (graph.hasStatement(res,p3d.VariableLengthInlineComponent)) {
104                                                 i.fixed = false;
105                                         } else if (graph.hasStatement(res,p3d.FixedLengthInlineComponent)) {
106                                                 i.fixed = true;
107                                         }
108                                         
109                                         if (graph.hasStatement(res,p3d.SizeChangeComponent)) {
110                                                 i.isSizeChange = true;
111                                         }
112                                         
113                                         if (graph.hasStatement(res,p3d.OffsetComponent)) {
114                                                 i.isOffset = true;
115                                         }
116                                         
117                                 } else if (graph.isInheritedFrom(res, p3d.TurnComponent)) {
118                                         i.type = Type.TURN;
119                                         if (graph.hasStatement(res,p3d.VariableAngleTurnComponent)) {
120                                                 i.fixed = false;
121                                         } else if (graph.hasStatement(res,p3d.FixedAngleTurnComponent)) {
122                                                 i.fixed = true; 
123                                         }
124                                 } else if (graph.isInheritedFrom(res, p3d.EndComponent)) {
125                                         i.fixed = false;
126                                         i.type = Type.END;
127                                 } else {
128                                         return null;
129                                 }
130                                 return i;
131                         }
132                 });
133         }
134
135 }