]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/Equipment.java
Enable model loading using an existing transaction.
[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                 Collection<Nozzle> children = getChild();
49                 if (numberOfFixedNozzles() > 0 && children.size() >= numberOfFixedNozzles())
50                         throw new RuntimeException("Equipment has already all fixed nozzles");
51                 
52                 Set<Integer> ids = new HashSet<Integer>();
53                 for (Nozzle n : children) {
54                         ids.add(n.getNozzleId());
55                 }
56                 int newId = 0;
57                 while (ids.contains(newId))
58                         newId++;
59                 addNode(Plant3D.URIs.HasNozzle,node);
60                 node.setNozzleId(newId);
61                 if (fnp != null)
62                         syncNozzles();
63         }
64         
65         @RelatedElementsGet(Plant3D.URIs.HasNozzle)
66         public Collection<Nozzle> getChild() {
67                 return getNodes(Plant3D.URIs.HasNozzle);
68         }
69         
70         @RelatedElementsRem(Plant3D.URIs.HasNozzle)
71         public void remChild(Nozzle node) {
72                 removeNode(Plant3D.URIs.HasNozzle, node);
73         }
74         
75         @Override
76         @CompoundRelatedGetValue(objRelation=Plant3D.URIs.hasParameter,objType=Plant3D.URIs.Parameter,valRelation=Plant3D.URIs.hasParameterValue)
77         @CompoundGetPropertyValue(name="Parameters",tabId="Parameters",value="parameters")
78         public Map<String, Object> getParameterMap() {
79                 return super.getParameterMap();
80         }
81         
82         @Override
83         @CompoundRelatedSetValue(Plant3D.URIs.hasParameter)
84         @CompoundSetPropertyValue(value="parameters")
85         public void setParameterMap(Map<String, Object> parameters) {
86                 super.setParameterMap(parameters);
87                 syncNozzles();
88         }
89         
90         @Override
91         public void setGeometry(GeometryProvider provider) {
92                 super.setGeometry(provider);
93                 if (provider instanceof FixedNozzleProvider) {
94                         fnp = (FixedNozzleProvider)provider;
95                         syncNozzles();
96                 }
97         }
98         
99         public int numberOfFixedNozzles() {
100                 if (fnp == null)
101                         return 0;
102                 return fnp.numberOfNozzles();
103         }
104         
105         /**
106          * Synchronizes fixed nozzles. 
107          * 
108          * Note: this method does not create nozzles, just sets their positions and names.
109          */
110         public void syncNozzles() {
111                 if (fnp == null)
112                         return;
113                 updateParameters();
114                 int count = fnp.numberOfNozzles();
115                 List<Nozzle> currentNozzles = getNodes();
116                 for (int i = 0; i < count; i++) {
117                         if (i < currentNozzles.size()) {
118                                 Nozzle nozzle = currentNozzles.get(i);
119                                 nozzle.setName(fnp.getNozzleName(i));
120                                 fnp.updateNozzlePosition(i, nozzle);
121                                 nozzle.setFixed(true);
122                         }
123                 }
124         }
125         
126         @Override
127         protected double[] getColor() {
128                 return new double[]{1,0,0};
129         }
130         
131         @Override
132         protected double[] getSelectedColor() {
133                 return new double[]{0.5,0,0.5};
134         }
135         
136         @Override
137         public void setPosition(Vector3d position) {
138                 if (MathTools.equals(position, getPosition()))
139                         return;
140                 super.setPosition(position);
141                 for (Nozzle n : getChild()) {
142                         n.getControlPoint()._setWorldPosition(n.getWorldPosition());
143                         n.getControlPoint()._setWorldOrientation(n.getWorldOrientation());
144                 }
145                 for (Nozzle n : getChild()) {
146                         try {
147                                 PipingRules.requestUpdate(n.getControlPoint());
148                         } catch (Exception e) {
149                                 e.printStackTrace();
150                         }
151                 }
152         }
153         
154         @Override
155         public void setOrientation(Quat4d orientation) {
156                 if (MathTools.equals(orientation, getOrientation()))
157                         return;
158                 super.setOrientation(orientation);
159                 for (Nozzle n : getChild()) {
160                         n.getControlPoint()._setWorldPosition(n.getWorldPosition());
161                         n.getControlPoint()._setWorldOrientation(n.getWorldOrientation());
162                 }
163                 for (Nozzle n : getChild()) {
164                         try {
165                                 PipingRules.requestUpdate(n.getControlPoint());
166                         } catch (Exception e) {
167                                 e.printStackTrace();
168                         }
169                 }
170         }
171
172 }