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