]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/actions/TranslateFreeVariableLengthAction.java
Add units to ComponentSelectionDialog
[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().getInlineDir();
96                 if (reversed) direction.negate();
97                 lock = LockType.CUSTOM;
98                 if (((InlineComponent) startComponent).isVariableLength()) {
99                     direction = null;
100                     lock = LockType.NONE;
101                 } 
102                 Vector3d v = new Vector3d();
103                 if (!reversed) {
104                     start.getControlPointEnds(v, previousPosition);
105                 } else {
106                     start.getControlPointEnds(previousPosition,v);
107                 }
108             } else if (startComponent instanceof TurnComponent) {
109                 if (start.asFixedAngle()) {
110                     direction = startComponent.getControlPoint().getPathLegDirection(reversed ? Direction.PREVIOUS : Direction.NEXT);
111                     lock = LockType.CUSTOM;
112                 } else {
113                     direction = null;
114                     lock = LockType.NONE;
115                 }
116             } else if (startComponent instanceof EndComponent) {
117                 throw new Exception("Not supported");
118             }
119             
120         } else {
121             throw new Exception("Not supported");
122         }
123         currentPosition = new Vector3d(previousPosition);
124         state = ToolState.ROUTING;
125         if (direction != null) {
126             direction.normalize();
127             
128         } 
129         startRemovable = start.isDeletable();
130         start.setDeletable(false);
131         for (PipeControlPoint pcp : cps) {
132             added.add(pcp.getPipelineComponent());
133             pcp.setDeletable(false);
134         }
135         added.add(component);
136         
137         
138         translateAxisGizmo.attach(panel);
139         setPreviousPosition(previousPosition);
140         updateCurrentPoint();
141     }
142     
143     @Override
144     public void deactivate() {
145         // TODO Auto-generated method stub
146         super.deactivate();
147     }
148     
149     protected void addPoint() throws Exception {
150         // Translate action is not supposed to add points
151         return;
152     }
153     
154     @Override
155     protected void endPiping() throws Exception {
156         state = ToolState.NOT_ACTIVE;
157         panel.useDefaultAction();
158     }
159     
160     protected void updateRouting(double x, double y) {
161         if (useDefault) {
162             //panel.getDefaultAction().update();
163             return;
164         }
165         
166         endTo = null;
167         endType = null;
168         endPort = null;
169
170         Ray ray = vtkUtil.createMouseRay(panel.getRenderer(),x, y);
171         Vector3d o = new Vector3d(ray.pos);
172         Vector3d d = ray.dir;
173         
174         
175         updateCurrentPoint(o, d);
176         updateRoute(o,d); 
177         panel.refresh();
178     }
179     
180
181 }