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