]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/Equipment.java
First version of fixed nozzle positions
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / scenegraph / Equipment.java
1 package org.simantics.plant3d.scenegraph;
2
3 import java.util.Collection;
4 import java.util.HashSet;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.Set;
8
9 import javax.vecmath.Quat4d;
10 import javax.vecmath.Vector3d;
11
12 import org.simantics.g3d.math.MathTools;
13 import org.simantics.g3d.property.annotations.CompoundGetPropertyValue;
14 import org.simantics.g3d.property.annotations.CompoundSetPropertyValue;
15 import org.simantics.g3d.scenegraph.GeometryProvider;
16 import org.simantics.objmap.graph.annotations.CompoundRelatedGetValue;
17 import org.simantics.objmap.graph.annotations.CompoundRelatedSetValue;
18 import org.simantics.objmap.graph.annotations.DynamicGraphType;
19 import org.simantics.objmap.graph.annotations.GetType;
20 import org.simantics.objmap.graph.annotations.RelatedElementsAdd;
21 import org.simantics.objmap.graph.annotations.RelatedElementsGet;
22 import org.simantics.objmap.graph.annotations.RelatedElementsRem;
23 import org.simantics.objmap.graph.annotations.SetType;
24 import org.simantics.plant3d.geometry.FixedNozzleProvider;
25 import org.simantics.plant3d.ontology.Plant3D;
26 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
27
28
29 @DynamicGraphType(Plant3D.URIs.Equipment)
30 public class Equipment extends P3DParentGeometryNode<Nozzle> {
31
32         private String type;
33         private FixedNozzleProvider fnp;
34         
35         @GetType(Plant3D.URIs.Equipment)
36         public String getType() {
37                 return type;
38         }
39         
40         @SetType(Plant3D.URIs.Equipment)
41         public void setType(String type) {
42                 this.type = type;
43         }
44         
45         
46         @RelatedElementsAdd(Plant3D.URIs.HasNozzle)
47         public void addChild(Nozzle node) {
48                 Set<Integer> ids = new HashSet<Integer>();
49                 for (Nozzle n : getChild()) {
50                         ids.add(n.getNozzleId());
51                 }
52                 int newId = 0;
53                 while (ids.contains(newId))
54                         newId++;
55                 addNode(Plant3D.URIs.HasNozzle,node);
56                 node.setNozzleId(newId);
57                 if (fnp != null)
58                     syncNozzles();
59         }
60         
61         @RelatedElementsGet(Plant3D.URIs.HasNozzle)
62         public Collection<Nozzle> getChild() {
63                 return getNodes(Plant3D.URIs.HasNozzle);
64         }
65         
66         @RelatedElementsRem(Plant3D.URIs.HasNozzle)
67         public void remChild(Nozzle node) {
68                 removeNode(Plant3D.URIs.HasNozzle, node);
69         }
70         
71         @Override
72         @CompoundRelatedGetValue(objRelation=Plant3D.URIs.hasParameter,objType=Plant3D.URIs.Parameter,valRelation=Plant3D.URIs.hasParameterValue)
73         @CompoundGetPropertyValue(name="Parameters",tabId="Parameters",value="parameters")
74         public Map<String, Object> getParameterMap() {
75                 return super.getParameterMap();
76         }
77         
78         @Override
79         @CompoundRelatedSetValue(Plant3D.URIs.hasParameter)
80         @CompoundSetPropertyValue(value="parameters")
81         public void setParameterMap(Map<String, Object> parameters) {
82                 super.setParameterMap(parameters);
83         }
84         
85         @Override
86         public void setGeometry(GeometryProvider provider) {
87             super.setGeometry(provider);
88             if (provider instanceof FixedNozzleProvider) {
89                 fnp = (FixedNozzleProvider)provider;
90                 syncNozzles();
91             }
92         }
93         
94         public int numberOfFixedNozzles() {
95             if (fnp == null)
96                 return 0;
97             return fnp.numberOfNozzles();
98         }
99         
100         /**
101          * Synchronizes fixed nozzles. 
102          * 
103          * Note: this method does not create nozzles, just sets their positions and names.
104          */
105         public void syncNozzles() {
106             if (fnp == null)
107                 return;
108             
109         int count = fnp.numberOfNozzles();
110         List<Nozzle> currentNozzles = getNodes();
111         for (int i = 0; i < count; i++) {
112             if (i < currentNozzles.size()) {
113                 Nozzle nozzle = currentNozzles.get(i);
114                 nozzle.setName(fnp.getNozzleName(i));
115                 fnp.updateNozzlePosition(i, nozzle);
116                 nozzle.setFixed(true);
117             }
118         }
119         }
120         
121         @Override
122         protected double[] getColor() {
123                 return new double[]{1,0,0};
124         }
125         
126         @Override
127         protected double[] getSelectedColor() {
128                 return new double[]{0.5,0,0.5};
129         }
130         
131         @Override
132         public void setPosition(Vector3d position) {
133                 if (MathTools.equals(position, getPosition()))
134                         return;
135                 super.setPosition(position);
136                 for (Nozzle n : getChild()) {
137                         n.getControlPoint()._setWorldPosition(n.getWorldPosition());
138                         n.getControlPoint()._setWorldOrientation(n.getWorldOrientation());
139                 }
140                 for (Nozzle n : getChild()) {
141                         try {
142                                 PipingRules.requestUpdate(n.getControlPoint());
143                         } catch (Exception e) {
144                                 e.printStackTrace();
145                         }
146                 }
147         }
148         
149         @Override
150         public void setOrientation(Quat4d orientation) {
151                 if (MathTools.equals(orientation, getOrientation()))
152                         return;
153                 super.setOrientation(orientation);
154                 for (Nozzle n : getChild()) {
155                         n.getControlPoint()._setWorldPosition(n.getWorldPosition());
156                         n.getControlPoint()._setWorldOrientation(n.getWorldOrientation());
157                 }
158                 for (Nozzle n : getChild()) {
159                         try {
160                                 PipingRules.requestUpdate(n.getControlPoint());
161                         } catch (Exception e) {
162                                 e.printStackTrace();
163                         }
164                 }
165         }
166
167 }