]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/InlineComponent.java
Support for reversible in-line components
[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         @RelatedGetValue(Plant3D.URIs.HasRotationAngle)
55         @GetPropertyValue(name="Rotation Angle", value=Plant3D.URIs.HasRotationAngle, tabId = "Default")
56         public Double getRotationAngle() {
57                 if (!controlPoint.isRotate())
58                         return null;
59                 Double d = controlPoint.getRotationAngle();
60                 if (d == null)
61                         return 0.0;
62                 return MathTools.radToDeg(d);
63         }
64         @RelatedSetValue(Plant3D.URIs.HasRotationAngle)
65         @SetPropertyValue(value=Plant3D.URIs.HasRotationAngle)
66         public void setRotationAngle(Double angle) {
67                 if (!controlPoint.isRotate())
68                         return;
69                 
70                 if (angle == null || Double.isInfinite(angle) || Double.isNaN(angle)) {
71                         return;
72                 }
73                 angle = MathTools.degToRad(angle);
74                 if (controlPoint.getRotationAngle() != null && Math.abs(controlPoint.getRotationAngle()-angle) < MathTools.NEAR_ZERO)
75                         return;
76                 controlPoint.setRotationAngle(angle);
77                 try {
78                         PipingRules.requestUpdate(getControlPoint());
79                 } catch (Exception e) {
80                         // TODO Auto-generated catch block
81                         e.printStackTrace();
82                 }       
83         }
84         
85         @RelatedGetValue(Plant3D.URIs.IsReversed)
86         @GetPropertyValue(name="Reverse", value=Plant3D.URIs.IsReversed, tabId = "Default")
87         public Boolean isReversed() {
88                 if (!controlPoint.isReverse())
89                         return null;
90                 Boolean d = controlPoint.getReversed();
91                 if (d == null)
92                         return false;
93                 return d;
94         }
95         @RelatedSetValue(Plant3D.URIs.IsReversed)
96         @SetPropertyValue(value=Plant3D.URIs.IsReversed)
97         public void setReverse(Boolean reverse) {
98                 if (!controlPoint.isReverse())
99                         return;
100                 
101                 if (reverse == null) {
102                         return;
103                 }
104                 controlPoint.setReversed(reverse);
105                 try {
106                         PipingRules.requestUpdate(getControlPoint());
107                 } catch (Exception e) {
108                         // TODO Auto-generated catch block
109                         e.printStackTrace();
110                 }       
111         }
112         
113         @Override
114         public void updateParameters() {
115                 super.updateParameters();
116                 if (!isVariableLength()) {
117                         Map<String,Object> calculated = getCalculatedParameters();
118                         if (calculated.containsKey("length")) {
119                                 controlPoint.setLength((Double)calculated.get("length"));
120                         }
121                         if (calculated.containsKey("offset")) {
122                                 controlPoint.setOffset((Double)calculated.get("offset"));
123                                 componentCalculatedOffset = true;
124                         } else {
125                                 componentCalculatedOffset = false;
126                         }
127                 }
128         }
129         
130         @Override
131         public void setPipeRun(PipeRun pipeRun) {
132                 // TODO Auto-generated method stub
133                 super.setPipeRun(pipeRun);
134                 if (getPipeRun() != null && getAlternativePipeRun() != null) {
135                         updateOffset();
136                 }
137         }
138         
139         @Override
140         public void setAlternativePipeRun(PipeRun pipeRun) {
141                 // TODO Auto-generated method stub
142                 super.setAlternativePipeRun(pipeRun);
143                 if (getPipeRun() != null && getAlternativePipeRun() != null) {
144                         updateOffset();
145                 }
146         }
147         
148         private void updateOffset() {
149                 if (!componentCalculatedOffset && getControlPoint().isOffset()) {
150                         getControlPoint().setOffset(getPipeRun().getPipeDiameter()*0.5 - getAlternativePipeRun().getPipeDiameter()*0.5);
151                 }
152         }
153
154         @Override
155         public Map<String, Object> updateParameterMap() {
156                 Map<String,Object> map = new HashMap<String, Object>();
157                 if (controlPoint != null) {
158                         if (!Double.isNaN(controlPoint.getLength()))
159                                 map.put("length", controlPoint.getLength());
160                         if (controlPoint.isDualInline()) {
161                                 PipeControlPoint sub = controlPoint.getSubPoint().get(0);
162                                 PipeRun pipeRun2 = sub.getPipeRun();
163                                 if (pipeRun2 != null) {
164                                         map.put("radius2", pipeRun2.getPipeDiameter() * 0.5);
165                                 }
166                                 if (controlPoint.isOffset() && !componentCalculatedOffset) {
167                                         map.put("offset", controlPoint.getOffset());
168                                 }
169                         }
170                 }
171                         
172                 PipeRun pipeRun = getPipeRun();
173                 if (pipeRun != null) {
174                         map.put("radius", pipeRun.getPipeDiameter() * 0.5);
175                 }
176                 return map;
177         }
178         
179         
180 }