]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/InlineComponent.java
Handle fixed turn components when pipe run is reversed.
[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 org.simantics.g3d.math.MathTools;
7 import org.simantics.g3d.property.annotations.GetPropertyValue;
8 import org.simantics.g3d.property.annotations.SetPropertyValue;
9 import org.simantics.g3d.scenegraph.base.ParentNode;
10 import org.simantics.objmap.graph.annotations.DynamicGraphType;
11 import org.simantics.objmap.graph.annotations.GetType;
12 import org.simantics.objmap.graph.annotations.RelatedGetValue;
13 import org.simantics.objmap.graph.annotations.RelatedSetValue;
14 import org.simantics.objmap.graph.annotations.SetType;
15 import org.simantics.plant3d.ontology.Plant3D;
16 import org.simantics.plant3d.scenegraph.controlpoint.ControlPointFactory;
17 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
18 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
19
20 @DynamicGraphType(Plant3D.URIs.InlineComponent)
21 public class InlineComponent extends PipelineComponent {
22
23         private String type;
24         private PipeControlPoint controlPoint;
25         private boolean componentCalculatedOffset = false;
26         
27         @GetType(Plant3D.URIs.InlineComponent)
28         public String getType() {
29                 return type;
30         }
31         
32         @SetType(Plant3D.URIs.InlineComponent)
33         public void setType(String type) throws Exception{
34                 this.type = type;
35                 controlPoint = ControlPointFactory.create(this);
36                 
37         }
38         
39         @Override
40         public PipeControlPoint getControlPoint() {
41                 return controlPoint;
42         }
43         
44         @Override
45         public void setParent(ParentNode<?> parent, String name) {
46                 super.setParent(parent, name);
47                 setPipeRun((PipeRun)parent);
48         }
49         
50         public boolean isVariableLength() {
51                 return !controlPoint.isFixed();
52         }
53         
54         public boolean isSizeChange() {
55             return controlPoint.isSizeChange();
56         }
57         
58         @RelatedGetValue(Plant3D.URIs.HasRotationAngle)
59         @GetPropertyValue(name="Rotation Angle", value=Plant3D.URIs.HasRotationAngle, tabId = "Default")
60         public Double getRotationAngle() {
61                 if (!controlPoint.isRotate())
62                         return null;
63                 Double d = controlPoint.getRotationAngle();
64                 if (d == null)
65                         return 0.0;
66                 return MathTools.radToDeg(d);
67         }
68         @RelatedSetValue(Plant3D.URIs.HasRotationAngle)
69         @SetPropertyValue(value=Plant3D.URIs.HasRotationAngle)
70         public void setRotationAngle(Double angle) {
71                 if (!controlPoint.isRotate())
72                         return;
73                 
74                 if (angle == null || Double.isInfinite(angle) || Double.isNaN(angle)) {
75                         return;
76                 }
77                 angle = MathTools.degToRad(angle);
78                 if (controlPoint.getRotationAngle() != null && Math.abs(controlPoint.getRotationAngle()-angle) < MathTools.NEAR_ZERO)
79                         return;
80                 controlPoint.setRotationAngle(angle);
81                 try {
82                         PipingRules.requestUpdate(getControlPoint());
83                 } catch (Exception e) {
84                         // TODO Auto-generated catch block
85                         e.printStackTrace();
86                 }       
87         }
88         
89         @RelatedGetValue(Plant3D.URIs.IsReversed)
90         @GetPropertyValue(name="Reverse", value=Plant3D.URIs.IsReversed, tabId = "Default")
91         public Boolean isReversed() {
92                 if (!controlPoint.isReverse())
93                         return null;
94                 Boolean d = controlPoint._getReversed();
95                 return d;
96         }
97         @RelatedSetValue(Plant3D.URIs.IsReversed)
98         @SetPropertyValue(value=Plant3D.URIs.IsReversed)
99         public void setReversed(Boolean reverse) {
100                 if (!controlPoint.isReverse())
101                         return;
102                 
103                 if (reverse == null) {
104                         return;
105                 }
106                 controlPoint.setReversed(reverse);
107                 try {
108                         PipingRules.requestUpdate(getControlPoint());
109                 } catch (Exception e) {
110                         // TODO Auto-generated catch block
111                         e.printStackTrace();
112                 }       
113         }
114         
115         @Override
116         public void updateParameters() {
117                 super.updateParameters();
118                 if (!isVariableLength()) {
119                         Map<String,Object> calculated = getCalculatedParameters();
120                         if (calculated.containsKey("length")) {
121                                 controlPoint.setLength((Double)calculated.get("length"));
122                         }
123                         if (calculated.containsKey("offset")) {
124                                 controlPoint.setOffset((Double)calculated.get("offset"));
125                                 componentCalculatedOffset = true;
126                         } else {
127                                 componentCalculatedOffset = false;
128                         }
129                 }
130         }
131         
132         @Override
133         public void setPipeRun(PipeRun pipeRun) {
134                 // TODO Auto-generated method stub
135                 super.setPipeRun(pipeRun);
136                 if (getPipeRun() != null && getAlternativePipeRun() != null) {
137                         updateOffset();
138                 }
139         }
140         
141         @Override
142         public void setAlternativePipeRun(PipeRun pipeRun) {
143                 // TODO Auto-generated method stub
144                 super.setAlternativePipeRun(pipeRun);
145                 if (getPipeRun() != null && getAlternativePipeRun() != null) {
146                         updateOffset();
147                 }
148         }
149         
150         private void updateOffset() {
151                 if (!componentCalculatedOffset && getControlPoint().isOffset()) {
152                         getControlPoint().setOffset(getPipeRun().getPipeDiameter()*0.5 - getAlternativePipeRun().getPipeDiameter()*0.5);
153                 }
154         }
155
156         @Override
157         public Map<String, Object> updateParameterMap() {
158                 Map<String,Object> map = new HashMap<String, Object>();
159                 if (controlPoint != null) {
160                         if (!Double.isNaN(controlPoint.getLength()))
161                                 map.put("length", controlPoint.getLength());
162                         if (controlPoint.isDualInline()) {
163                                 PipeControlPoint sub = controlPoint.getSubPoint().get(0);
164                                 PipeRun pipeRun2 = sub.getPipeRun();
165                                 if (pipeRun2 != null) {
166                                         map.put("radius2", pipeRun2.getPipeDiameter() * 0.5);
167                                 }
168                                 if (controlPoint.isOffset() && !componentCalculatedOffset) {
169                                         map.put("offset", controlPoint.getOffset());
170                                 }
171                         }
172                 }
173                         
174                 PipeRun pipeRun = getPipeRun();
175                 if (pipeRun != null) {
176                         map.put("radius", pipeRun.getPipeDiameter() * 0.5);
177                 }
178                 return map;
179         }
180         
181         
182 }