]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/Equipment.java
SCL bindings to some G3D and Plant3D Java classes
[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         }
88         
89         @Override
90         public void setGeometry(GeometryProvider provider) {
91             super.setGeometry(provider);
92             if (provider instanceof FixedNozzleProvider) {
93                 fnp = (FixedNozzleProvider)provider;
94                 syncNozzles();
95             }
96         }
97         
98         public int numberOfFixedNozzles() {
99             if (fnp == null)
100                 return 0;
101             return fnp.numberOfNozzles();
102         }
103         
104         /**
105          * Synchronizes fixed nozzles. 
106          * 
107          * Note: this method does not create nozzles, just sets their positions and names.
108          */
109         public void syncNozzles() {
110             if (fnp == null)
111                 return;
112             
113         int count = fnp.numberOfNozzles();
114         List<Nozzle> currentNozzles = getNodes();
115         for (int i = 0; i < count; i++) {
116             if (i < currentNozzles.size()) {
117                 Nozzle nozzle = currentNozzles.get(i);
118                 nozzle.setName(fnp.getNozzleName(i));
119                 fnp.updateNozzlePosition(i, nozzle);
120                 nozzle.setFixed(true);
121             }
122         }
123         }
124         
125         @Override
126         protected double[] getColor() {
127                 return new double[]{1,0,0};
128         }
129         
130         @Override
131         protected double[] getSelectedColor() {
132                 return new double[]{0.5,0,0.5};
133         }
134         
135         @Override
136         public void setPosition(Vector3d position) {
137                 if (MathTools.equals(position, getPosition()))
138                         return;
139                 super.setPosition(position);
140                 for (Nozzle n : getChild()) {
141                         n.getControlPoint()._setWorldPosition(n.getWorldPosition());
142                         n.getControlPoint()._setWorldOrientation(n.getWorldOrientation());
143                 }
144                 for (Nozzle n : getChild()) {
145                         try {
146                                 PipingRules.requestUpdate(n.getControlPoint());
147                         } catch (Exception e) {
148                                 e.printStackTrace();
149                         }
150                 }
151         }
152         
153         @Override
154         public void setOrientation(Quat4d orientation) {
155                 if (MathTools.equals(orientation, getOrientation()))
156                         return;
157                 super.setOrientation(orientation);
158                 for (Nozzle n : getChild()) {
159                         n.getControlPoint()._setWorldPosition(n.getWorldPosition());
160                         n.getControlPoint()._setWorldOrientation(n.getWorldOrientation());
161                 }
162                 for (Nozzle n : getChild()) {
163                         try {
164                                 PipingRules.requestUpdate(n.getControlPoint());
165                         } catch (Exception e) {
166                                 e.printStackTrace();
167                         }
168                 }
169         }
170
171 }