]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/InlineComponent.java
1fd83da110d3f36bf8739db214d325444d8e74e9
[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                 if (d == null)
96                         return false;
97                 return d;
98         }
99         @RelatedSetValue(Plant3D.URIs.IsReversed)
100         @SetPropertyValue(value=Plant3D.URIs.IsReversed)
101         public void setReversed(Boolean reverse) {
102                 if (!controlPoint.isReverse())
103                         return;
104                 
105                 if (reverse == null) {
106                         return;
107                 }
108                 controlPoint.setReversed(reverse);
109                 try {
110                         PipingRules.requestUpdate(getControlPoint());
111                 } catch (Exception e) {
112                         // TODO Auto-generated catch block
113                         e.printStackTrace();
114                 }       
115         }
116         
117         @Override
118         public void updateParameters() {
119                 super.updateParameters();
120                 if (!isVariableLength()) {
121                         Map<String,Object> calculated = getCalculatedParameters();
122                         if (calculated.containsKey("length")) {
123                                 controlPoint.setLength((Double)calculated.get("length"));
124                         }
125                         if (calculated.containsKey("offset")) {
126                                 controlPoint.setOffset((Double)calculated.get("offset"));
127                                 componentCalculatedOffset = true;
128                         } else {
129                                 componentCalculatedOffset = false;
130                         }
131                 }
132         }
133         
134         @Override
135         public void setPipeRun(PipeRun pipeRun) {
136                 // TODO Auto-generated method stub
137                 super.setPipeRun(pipeRun);
138                 if (getPipeRun() != null && getAlternativePipeRun() != null) {
139                         updateOffset();
140                 }
141         }
142         
143         @Override
144         public void setAlternativePipeRun(PipeRun pipeRun) {
145                 // TODO Auto-generated method stub
146                 super.setAlternativePipeRun(pipeRun);
147                 if (getPipeRun() != null && getAlternativePipeRun() != null) {
148                         updateOffset();
149                 }
150         }
151         
152         private void updateOffset() {
153                 if (!componentCalculatedOffset && getControlPoint().isOffset()) {
154                         getControlPoint().setOffset(getPipeRun().getPipeDiameter()*0.5 - getAlternativePipeRun().getPipeDiameter()*0.5);
155                 }
156         }
157
158         @Override
159         public Map<String, Object> updateParameterMap() {
160                 Map<String,Object> map = new HashMap<String, Object>();
161                 if (controlPoint != null) {
162                         if (!Double.isNaN(controlPoint.getLength()))
163                                 map.put("length", controlPoint.getLength());
164                         if (controlPoint.isDualInline()) {
165                                 PipeControlPoint sub = controlPoint.getSubPoint().get(0);
166                                 PipeRun pipeRun2 = sub.getPipeRun();
167                                 if (pipeRun2 != null) {
168                                         map.put("radius2", pipeRun2.getPipeDiameter() * 0.5);
169                                 }
170                                 if (controlPoint.isOffset() && !componentCalculatedOffset) {
171                                         map.put("offset", controlPoint.getOffset());
172                                 }
173                         }
174                 }
175                         
176                 PipeRun pipeRun = getPipeRun();
177                 if (pipeRun != null) {
178                         map.put("radius", pipeRun.getPipeDiameter() * 0.5);
179                 }
180                 return map;
181         }
182         
183         
184 }