]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/InlineComponent.java
e098de63a637303a787aca53f85db0259380f845
[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         @Override
86         public void updateParameters() {
87                 super.updateParameters();
88                 if (!isVariableLength()) {
89                         Map<String,Object> calculated = getCalculatedParameters();
90                         if (calculated.containsKey("length")) {
91                                 controlPoint.setLength((Double)calculated.get("length"));
92                         }
93                         if (calculated.containsKey("offset")) {
94                                 controlPoint.setOffset((Double)calculated.get("offset"));
95                                 componentCalculatedOffset = true;
96                         } else {
97                                 componentCalculatedOffset = false;
98                         }
99                 }
100         }
101         
102         @Override
103         public void setPipeRun(PipeRun pipeRun) {
104                 // TODO Auto-generated method stub
105                 super.setPipeRun(pipeRun);
106                 if (getPipeRun() != null && getAlternativePipeRun() != null) {
107                         updateOffset();
108                 }
109         }
110         
111         @Override
112         public void setAlternativePipeRun(PipeRun pipeRun) {
113                 // TODO Auto-generated method stub
114                 super.setAlternativePipeRun(pipeRun);
115                 if (getPipeRun() != null && getAlternativePipeRun() != null) {
116                         updateOffset();
117                 }
118         }
119         
120         private void updateOffset() {
121                 if (!componentCalculatedOffset && getControlPoint().isOffset()) {
122                         getControlPoint().setOffset(getPipeRun().getPipeDiameter()*0.5 - getAlternativePipeRun().getPipeDiameter()*0.5);
123                 }
124         }
125
126         @Override
127         public Map<String, Object> updateParameterMap() {
128                 Map<String,Object> map = new HashMap<String, Object>();
129                 if (controlPoint != null) {
130                         if (!Double.isNaN(controlPoint.getLength()))
131                                 map.put("length", controlPoint.getLength());
132                         if (controlPoint.isDualInline()) {
133                                 PipeControlPoint sub = controlPoint.getSubPoint().get(0);
134                                 PipeRun pipeRun2 = sub.getPipeRun();
135                                 if (pipeRun2 != null) {
136                                         map.put("radius2", pipeRun2.getPipeDiameter() * 0.5);
137                                 }
138                                 if (controlPoint.isOffset() && !componentCalculatedOffset) {
139                                         map.put("offset", controlPoint.getOffset());
140                                 }
141                         }
142                 }
143                         
144                 PipeRun pipeRun = getPipeRun();
145                 if (pipeRun != null) {
146                         map.put("radius", pipeRun.getPipeDiameter() * 0.5);
147                 }
148                 return map;
149         }
150         
151         
152 }