]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/InlineComponent.java
Expose offset calculation as API in PipingRules.
[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                 syncNext();
40         syncPrevious();
41         syncBranch0();
42                 
43         }
44         
45         @Override
46         public PipeControlPoint getControlPoint() {
47                 return controlPoint;
48         }
49         
50         @Override
51         public void setParent(ParentNode<?> parent, String name) {
52                 super.setParent(parent, name);
53                 setPipeRun((PipeRun)parent);
54         }
55         
56         public boolean isVariableLength() {
57                 return !controlPoint.isFixed();
58         }
59         
60         public boolean isModifialble() {
61         return controlPoint.isMod();
62     }
63         
64         public boolean isSizeChange() {
65                 return controlPoint.isSizeChange();
66         }
67         
68         @RelatedGetValue(Plant3D.URIs.HasRotationAngle)
69         @GetPropertyValue(name="Rotation Angle", value=Plant3D.URIs.HasRotationAngle, tabId = "Default")
70         public Double getRotationAngle() {
71                 if (!controlPoint.isRotate())
72                         return null;
73                 Double d = controlPoint.getRotationAngle();
74                 if (d == null)
75                         return 0.0;
76                 return MathTools.radToDeg(d);
77         }
78         
79         @RelatedSetValue(Plant3D.URIs.HasRotationAngle)
80         @SetPropertyValue(value=Plant3D.URIs.HasRotationAngle)
81         public void setRotationAngle(Double angle) {
82                 if (!controlPoint.isRotate())
83                         return;
84                 
85                 if (angle == null || Double.isInfinite(angle) || Double.isNaN(angle)) {
86                         return;
87                 }
88                 angle = MathTools.degToRad(angle);
89                 if (controlPoint.getRotationAngle() != null && Math.abs(controlPoint.getRotationAngle()-angle) < MathTools.NEAR_ZERO)
90                         return;
91                 controlPoint.setRotationAngle(angle);
92                 PipingRules.requestUpdate(getControlPoint());
93
94         }
95         
96         @RelatedGetValue(Plant3D.URIs.IsReversed)
97         @GetPropertyValue(name="Reverse", value=Plant3D.URIs.IsReversed, tabId = "Default")
98         public Boolean isReversed() {
99                 if (!controlPoint.isReverse())
100                         return null;
101                 Boolean d = controlPoint._getReversed();
102                 return d;
103         }
104         @RelatedSetValue(Plant3D.URIs.IsReversed)
105         @SetPropertyValue(value=Plant3D.URIs.IsReversed)
106         public void setReversed(Boolean reverse) {
107                 if (!controlPoint.isReverse())
108                         return;
109                 
110                 if (reverse == null) {
111                         return;
112                 }
113                 controlPoint.setReversed(reverse);
114                 PipingRules.requestUpdate(getControlPoint());
115         }
116         
117         @Override
118         public void updateParameters() {
119                 super.updateParameters();
120                 if (!isVariableLength()) {
121                         Map<String,Object> calculated = getCalculatedParameters();
122                         
123                         if (calculated.containsKey("offset")) {
124                                 controlPoint.setOffset((Double)calculated.get("offset"));
125                                 componentCalculatedOffset = true;
126                         } else {
127                                 componentCalculatedOffset = false;
128                         }
129                         
130                         Map<String,Object> total = getTotalParameters();
131                         
132                         if (total.containsKey("length")) {
133                 controlPoint.setLength((Double)total.get("length"));
134             }
135                         
136                         PipingRules.requestUpdate(getControlPoint());
137                 }
138         }
139         
140         @Override
141         public void setPipeRun(PipeRun pipeRun) {
142                 // TODO Auto-generated method stub
143                 super.setPipeRun(pipeRun);
144                 if (getPipeRun() != null && getAlternativePipeRun() != null) {
145                         updateOffset();
146                 }
147         }
148         
149         @Override
150         public void setAlternativePipeRun(PipeRun pipeRun) {
151                 // TODO Auto-generated method stub
152                 super.setAlternativePipeRun(pipeRun);
153                 if (getPipeRun() != null && getAlternativePipeRun() != null) {
154                         updateOffset();
155                 }
156         }
157         
158         private void updateOffset() {
159                 if (!componentCalculatedOffset && getControlPoint().isOffset()) {
160                         getControlPoint().setOffset(getPipeRun().getPipeDiameter()*0.5 - getAlternativePipeRun().getPipeDiameter()*0.5);
161                 }
162         }
163
164         @Override
165         public Map<String, Object> updateParameterMap() {
166                 Map<String,Object> map = new HashMap<String, Object>();
167                 if (controlPoint != null) {
168                         if (!Double.isNaN(controlPoint.getLength()) && controlPoint.isVariableLength())
169                                 map.put("length", controlPoint.getLength());
170                         if (controlPoint.isDualInline()) {
171                                 PipeControlPoint sub = controlPoint.getDualSub();
172                                 PipeRun pipeRun2 = sub.getPipeRun();
173                                 if (pipeRun2 != null) {
174                                         map.put("radius2", pipeRun2.getPipeDiameter() * 0.5);
175                                 }
176                                 if (controlPoint.isOffset() && !componentCalculatedOffset) {
177                                         map.put("offset", controlPoint.getOffset());
178                                 }
179                         }
180                 }
181                         
182                 PipeRun pipeRun = getPipeRun();
183                 if (pipeRun != null) {
184                         map.put("radius", pipeRun.getPipeDiameter() * 0.5);
185                 }
186                 return map;
187         }
188         
189         @SetPropertyValue("flowlength")
190         public void setFlowLength(double l) {
191                 // Not allowed, if not at the end of a run
192                 if (getNext() != null)
193                         throw new IllegalStateException("Cannot edit length of a connected component");
194                 
195                 double length = getFlowLength();
196                 Point3d p1 = new Point3d(), p2 = new Point3d();
197                 controlPoint.getControlPointEnds(p1, p2);
198                 Vector3d dir = new Vector3d();
199                 dir.sub(p2, p1);
200                 dir.normalize();
201                 dir.scale((l - length)/2);
202                 Vector3d pos = new Vector3d(getPosition());
203                 pos.add(dir);
204                 setPosition(pos);
205         }
206 }