]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/actions/TranslateFreeVariableLengthAction.java
16a388208be3c1713f7c9a0bc813b7e11fcb08a6
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / actions / TranslateFreeVariableLengthAction.java
1 package org.simantics.plant3d.actions;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.vecmath.Vector3d;
7
8 import org.simantics.g3d.math.Ray;
9 import org.simantics.g3d.scenegraph.IG3DNode;
10 import org.simantics.g3d.vtk.swt.InteractiveVtkComposite;
11 import org.simantics.g3d.vtk.utils.vtkUtil;
12 import org.simantics.plant3d.Activator;
13 import org.simantics.plant3d.actions.RoutePipeAction.LockType;
14 import org.simantics.plant3d.actions.RoutePipeAction.ToolState;
15 import org.simantics.plant3d.scenegraph.EndComponent;
16 import org.simantics.plant3d.scenegraph.InlineComponent;
17 import org.simantics.plant3d.scenegraph.Nozzle;
18 import org.simantics.plant3d.scenegraph.P3DRootNode;
19 import org.simantics.plant3d.scenegraph.PipelineComponent;
20 import org.simantics.plant3d.scenegraph.TurnComponent;
21 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
22 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
23 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.Direction;
24 import org.simantics.plant3d.utils.ComponentUtils;
25
26 public class TranslateFreeVariableLengthAction extends RoutePipeAction{
27
28     private InlineComponent component;
29     
30     public TranslateFreeVariableLengthAction(InteractiveVtkComposite panel, P3DRootNode root) {
31         super(panel, root, false);
32         setText("Translate");
33         setImageDescriptor(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/arrow_refresh.png"));
34     }
35     
36     
37     public void setNode(IG3DNode node) {
38         if (node instanceof InlineComponent) {
39             component = (InlineComponent)node;
40             if (component.isVariableLength() && (component.getNext() == null || component.getPrevious() == null)) {
41                 PipelineComponent pathLegStart;
42                 if (component.getNext() == null) {
43                     pathLegStart = component.getControlPoint().findPreviousEnd().getPipelineComponent();
44                 } else {
45                     pathLegStart = component.getControlPoint().findNextEnd().getPipelineComponent();
46                 }
47                 setComponent(pathLegStart);
48                 
49                 setEnabled(true);
50             } else {
51                 setEnabled(false);
52             }
53         } else {
54             component = null;
55             setEnabled(false);
56         }
57     }
58     
59     @Override
60     protected void activate() throws Exception {
61         state = ToolState.INITIALIZING;
62         added.clear();
63         pipeRun = startComponent.getPipeRun();
64         PipeControlPoint start = startComponent.getControlPoint();
65         activateNextPrev(start);
66     }
67     
68     @Override
69     protected void activateNextPrev(PipeControlPoint start) throws Exception {
70         if (!reversed && start.isDualInline())
71             start = start.getSubPoint().get(0);
72         else if (reversed && start.isDualSub())
73             start = start.parent;
74         
75         pipeRun = component.getPipeRun();
76         
77         List<PipeControlPoint> cps = new ArrayList<PipeControlPoint>();
78         PipeControlPoint s = null;
79         if (component.getNext() == null) {
80             s = component.getControlPoint().findPreviousEnd(cps);
81         } else {
82             s = component.getControlPoint().findNextEnd(cps);
83         }
84         if (s.getPipelineComponent() != startComponent) {
85             throw new Exception("Path leg start mismatch");
86         }
87         
88         
89         
90         setPreviousPosition(start.getWorldPosition());
91         currentPosition = component.getPosition();
92         
93         if (startComponent instanceof Nozzle) {
94             direction = startComponent.getControlPoint().getDirectedControlPointDirection();
95             lock = LockType.CUSTOM;
96         } else if (startComponent instanceof PipelineComponent){
97             if (startComponent instanceof InlineComponent) {
98                 direction = startComponent.getControlPoint().getPathLegDirection(reversed ? Direction.PREVIOUS : Direction.NEXT);
99                 lock = LockType.CUSTOM;
100                 if (((InlineComponent) startComponent).isVariableLength()) {
101                     direction = null;
102                     lock = LockType.NONE;
103                 } 
104                 Vector3d v = new Vector3d();
105                 if (!reversed) {
106                     start.getControlPointEnds(v, previousPosition);
107                 } else {
108                     start.getControlPointEnds(previousPosition,v);
109                 }
110             } else if (startComponent instanceof TurnComponent) {
111                 if (start.asFixedAngle()) {
112                     direction = startComponent.getControlPoint().getPathLegDirection(reversed ? Direction.PREVIOUS : Direction.NEXT);
113                     lock = LockType.CUSTOM;
114                 } else {
115                     direction = null;
116                     lock = LockType.NONE;
117                 }
118             } else if (startComponent instanceof EndComponent) {
119                 throw new Exception("Not supported");
120             }
121             
122         } else {
123             throw new Exception("Not supported");
124         }
125         currentPosition = new Vector3d(previousPosition);
126         state = ToolState.ROUTING;
127         if (direction != null) {
128             direction.normalize();
129             
130         } 
131         startRemovable = start.isDeletable();
132         start.setDeletable(false);
133         for (PipeControlPoint pcp : cps) {
134             added.add(pcp.getPipelineComponent());
135             pcp.setDeletable(false);
136         }
137         added.add(component);
138         
139         
140         translateAxisGizmo.attach(panel);
141         setPreviousPosition(previousPosition);
142         updateCurrentPoint();
143     }
144     
145     @Override
146     public void deactivate() {
147         // TODO Auto-generated method stub
148         super.deactivate();
149     }
150     
151     protected void addPoint() throws Exception {
152         // Translate action is not supposed to add points
153         return;
154     }
155     
156     @Override
157     protected void endPiping() throws Exception {
158         state = ToolState.NOT_ACTIVE;
159         panel.useDefaultAction();
160     }
161     
162     protected void updateRouting(double x, double y) {
163         if (useDefault) {
164             //panel.getDefaultAction().update();
165             return;
166         }
167         
168         endTo = null;
169         endType = null;
170         endPort = null;
171
172         Ray ray = vtkUtil.createMouseRay(panel.getRenderer(),x, y);
173         Vector3d o = new Vector3d(ray.pos);
174         Vector3d d = ray.dir;
175         
176         
177         updateCurrentPoint(o, d);
178         updateRoute(o,d); 
179         panel.refresh();
180     }
181     
182
183 }