1 package org.simantics.plant3d.scenegraph.controlpoint;
3 import java.util.HashMap;
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;
18 public class ControlPointFactory {
20 private static Map<String,Instruction> cache = new HashMap<String, Instruction>();
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));
29 for (Item item : items) {
30 Instruction inst = createInstruction(item.getUri());
31 cache.put(item.getUri(), inst);
35 public static PipeControlPoint create(PipelineComponent component) throws Exception {
36 Instruction inst = cache.get(component.getType());
38 inst = createInstruction(component.getType());
39 cache.put(component.getType(), inst);
45 PipeControlPoint pcp = new PipeControlPoint(component);
46 pcp.setType(inst.type);
47 pcp.setFixed(inst.fixed);
49 pcp.setRotate(inst.isRotate);
50 pcp.setReverse(inst.isReverse);
56 if (inst.isOffset || inst.isSizeChange) {
57 PipeControlPoint sub = new PipeControlPoint(component);
58 pcp.children.add(sub);
60 sub.setType(inst.type);
61 sub.setFixed(inst.fixed);
64 sub.setDeletable(false);
67 if (inst.isSizeChange)
68 pcp.setSizeChange(true);
80 private static class Instruction {
91 private static Instruction createInstruction(final String type) throws Exception {
92 return Simantics.getSession().syncRequest(new Read<Instruction>() {
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();
100 i.isSizeChange = false;
103 i.type = PointType.INLINE;
105 if (graph.isInheritedFrom(res, p3d.Nozzle)) {
108 i.isSizeChange = false;
109 i.type = PointType.END;
110 } else if (graph.isInheritedFrom(res, p3d.InlineComponent)){
112 i.type = PointType.INLINE;
113 if (graph.hasStatement(res,p3d.VariableLengthInlineComponent)) {
115 } else if (graph.hasStatement(res,p3d.FixedLengthInlineComponent)) {
117 } else if (graph.hasStatement(res,p3d.AdjustableLengthInlineComponent)) {
121 throw new DatabaseException("Inline component type " + res + " does not have length configuration.");
124 if (graph.hasStatement(res,p3d.SizeChangeComponent)) {
125 i.isSizeChange = true;
128 if (graph.hasStatement(res,p3d.OffsetComponent)) {
131 if (graph.hasStatement(res,p3d.RotateComponent)) {
135 if (graph.hasStatement(res,p3d.ReverseComponent)) {
139 } else if (graph.isInheritedFrom(res, p3d.TurnComponent)) {
140 i.type = PointType.TURN;
141 if (graph.hasStatement(res,p3d.VariableAngleTurnComponent)) {
143 } else if (graph.hasStatement(res,p3d.FixedAngleTurnComponent)) {
146 throw new DatabaseException("Turn component type " + res + " does not have angle configuration.");
148 } else if (graph.isInheritedFrom(res, p3d.EndComponent)) {
150 i.type = PointType.END;