]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/InlineComponent.java
PipeCOntrolPoint setNext/setPrev/remove improved
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / scenegraph / InlineComponent.java
1 package org.simantics.plant3d.scenegraph;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import javax.vecmath.Point3d;
7 import javax.vecmath.Vector3d;
8
9 import org.simantics.g3d.math.MathTools;
10 import org.simantics.g3d.property.annotations.GetPropertyValue;
11 import org.simantics.g3d.property.annotations.SetPropertyValue;
12 import org.simantics.g3d.scenegraph.base.ParentNode;
13 import org.simantics.objmap.graph.annotations.DynamicGraphType;
14 import org.simantics.objmap.graph.annotations.GetType;
15 import org.simantics.objmap.graph.annotations.RelatedGetValue;
16 import org.simantics.objmap.graph.annotations.RelatedSetValue;
17 import org.simantics.objmap.graph.annotations.SetType;
18 import org.simantics.plant3d.ontology.Plant3D;
19 import org.simantics.plant3d.scenegraph.controlpoint.ControlPointFactory;
20 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
21 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
22
23 @DynamicGraphType(Plant3D.URIs.InlineComponent)
24 public class InlineComponent extends PipelineComponent {
25
26         private String type;
27         private PipeControlPoint controlPoint;
28         private boolean componentCalculatedOffset = false;
29         
30         @GetType(Plant3D.URIs.InlineComponent)
31         public String getType() {
32                 return type;
33         }
34         
35         @SetType(Plant3D.URIs.InlineComponent)
36         public void setType(String type) throws Exception{
37                 this.type = type;
38                 controlPoint = ControlPointFactory.create(this);
39                 
40         }
41         
42         @Override
43         public PipeControlPoint getControlPoint() {
44                 return controlPoint;
45         }
46         
47         @Override
48         public void setParent(ParentNode<?> parent, String name) {
49                 super.setParent(parent, name);
50                 setPipeRun((PipeRun)parent);
51         }
52         
53         public boolean isVariableLength() {
54                 return !controlPoint.isFixed();
55         }
56         
57         public boolean isSizeChange() {
58                 return controlPoint.isSizeChange();
59         }
60         
61         @RelatedGetValue(Plant3D.URIs.HasRotationAngle)
62         @GetPropertyValue(name="Rotation Angle", value=Plant3D.URIs.HasRotationAngle, tabId = "Default")
63         public Double getRotationAngle() {
64                 if (!controlPoint.isRotate())
65                         return null;
66                 Double d = controlPoint.getRotationAngle();
67                 if (d == null)
68                         return 0.0;
69                 return MathTools.radToDeg(d);
70         }
71         
72         @RelatedSetValue(Plant3D.URIs.HasRotationAngle)
73         @SetPropertyValue(value=Plant3D.URIs.HasRotationAngle)
74         public void setRotationAngle(Double angle) {
75                 if (!controlPoint.isRotate())
76                         return;
77                 
78                 if (angle == null || Double.isInfinite(angle) || Double.isNaN(angle)) {
79                         return;
80                 }
81                 angle = MathTools.degToRad(angle);
82                 if (controlPoint.getRotationAngle() != null && Math.abs(controlPoint.getRotationAngle()-angle) < MathTools.NEAR_ZERO)
83                         return;
84                 controlPoint.setRotationAngle(angle);
85                 try {
86                         PipingRules.requestUpdate(getControlPoint());
87                 } catch (Exception e) {
88                         // TODO Auto-generated catch block
89                         e.printStackTrace();
90                 }       
91         }
92         
93         @RelatedGetValue(Plant3D.URIs.IsReversed)
94         @GetPropertyValue(name="Reverse", value=Plant3D.URIs.IsReversed, tabId = "Default")
95         public Boolean isReversed() {
96                 if (!controlPoint.isReverse())
97                         return null;
98                 Boolean d = controlPoint._getReversed();
99                 return d;
100         }
101         @RelatedSetValue(Plant3D.URIs.IsReversed)
102         @SetPropertyValue(value=Plant3D.URIs.IsReversed)
103         public void setReversed(Boolean reverse) {
104                 if (!controlPoint.isReverse())
105                         return;
106                 
107                 if (reverse == null) {
108                         return;
109                 }
110                 controlPoint.setReversed(reverse);
111                 try {
112                         PipingRules.requestUpdate(getControlPoint());
113                 } catch (Exception e) {
114                         // TODO Auto-generated catch block
115                         e.printStackTrace();
116                 }       
117         }
118         
119         @Override
120         public void updateParameters() {
121                 super.updateParameters();
122                 if (!isVariableLength()) {
123                         Map<String,Object> calculated = getCalculatedParameters();
124                         
125                         if (calculated.containsKey("offset")) {
126                                 controlPoint.setOffset((Double)calculated.get("offset"));
127                                 componentCalculatedOffset = true;
128                         } else {
129                                 componentCalculatedOffset = false;
130                         }
131                         
132                         Map<String,Object> total = getTotalParameters();
133                         
134                         if (total.containsKey("length")) {
135                 controlPoint.setLength((Double)total.get("length"));
136             }
137                         
138                         PipingRules.requestUpdate(getControlPoint());
139                 }
140         }
141         
142         @Override
143         public void setPipeRun(PipeRun pipeRun) {
144                 // TODO Auto-generated method stub
145                 super.setPipeRun(pipeRun);
146                 if (getPipeRun() != null && getAlternativePipeRun() != null) {
147                         updateOffset();
148                 }
149         }
150         
151         @Override
152         public void setAlternativePipeRun(PipeRun pipeRun) {
153                 // TODO Auto-generated method stub
154                 super.setAlternativePipeRun(pipeRun);
155                 if (getPipeRun() != null && getAlternativePipeRun() != null) {
156                         updateOffset();
157                 }
158         }
159         
160         private void updateOffset() {
161                 if (!componentCalculatedOffset && getControlPoint().isOffset()) {
162                         getControlPoint().setOffset(getPipeRun().getPipeDiameter()*0.5 - getAlternativePipeRun().getPipeDiameter()*0.5);
163                 }
164         }
165
166         @Override
167         public Map<String, Object> updateParameterMap() {
168                 Map<String,Object> map = new HashMap<String, Object>();
169                 if (controlPoint != null) {
170                         if (!Double.isNaN(controlPoint.getLength()) && controlPoint.isVariableLength())
171                                 map.put("length", controlPoint.getLength());
172                         if (controlPoint.isDualInline()) {
173                                 PipeControlPoint sub = controlPoint.getDualSub();
174                                 PipeRun pipeRun2 = sub.getPipeRun();
175                                 if (pipeRun2 != null) {
176                                         map.put("radius2", pipeRun2.getPipeDiameter() * 0.5);
177                                 }
178                                 if (controlPoint.isOffset() && !componentCalculatedOffset) {
179                                         map.put("offset", controlPoint.getOffset());
180                                 }
181                         }
182                 }
183                         
184                 PipeRun pipeRun = getPipeRun();
185                 if (pipeRun != null) {
186                         map.put("radius", pipeRun.getPipeDiameter() * 0.5);
187                 }
188                 return map;
189         }
190         
191         @SetPropertyValue("flowlength")
192         public void setFlowLength(double l) {
193                 // Not allowed, if not at the end of a run
194                 if (getNext() != null)
195                         throw new IllegalStateException("Cannot edit length of a connected component");
196                 
197                 double length = getFlowLength();
198                 Point3d p1 = new Point3d(), p2 = new Point3d();
199                 controlPoint.getControlPointEnds(p1, p2);
200                 Vector3d dir = new Vector3d();
201                 dir.sub(p2, p1);
202                 dir.normalize();
203                 dir.scale((l - length)/2);
204                 Vector3d pos = new Vector3d(getPosition());
205                 pos.add(dir);
206                 setPosition(pos);
207         }
208 }