1 package org.simantics.plant3d.actions;
3 import java.util.ArrayList;
6 import javax.vecmath.Vector3d;
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.scenegraph.EndComponent;
14 import org.simantics.plant3d.scenegraph.InlineComponent;
15 import org.simantics.plant3d.scenegraph.Nozzle;
16 import org.simantics.plant3d.scenegraph.P3DRootNode;
17 import org.simantics.plant3d.scenegraph.PipelineComponent;
18 import org.simantics.plant3d.scenegraph.TurnComponent;
19 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
20 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.Direction;
22 public class TranslateFreeVariableLengthAction extends RoutePipeAction{
24 private InlineComponent component;
26 public TranslateFreeVariableLengthAction(InteractiveVtkComposite panel, P3DRootNode root) {
27 super(panel, root, false);
29 setImageDescriptor(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/arrow_refresh.png"));
33 public void setNode(IG3DNode node) {
34 if (node instanceof InlineComponent) {
35 component = (InlineComponent)node;
36 if (component.isVariableLength() && (component.getNext() == null || component.getPrevious() == null)) {
37 PipelineComponent pathLegStart;
38 if (component.getNext() == null) {
39 pathLegStart = component.getControlPoint().findPreviousEnd().getPipelineComponent();
41 pathLegStart = component.getControlPoint().findNextEnd().getPipelineComponent();
43 setComponent(pathLegStart);
56 protected void activate() throws Exception {
57 state = ToolState.INITIALIZING;
59 pipeRun = startComponent.getPipeRun();
60 PipeControlPoint start = startComponent.getControlPoint();
61 activateNextPrev(start);
65 protected void activateNextPrev(PipeControlPoint start) throws Exception {
66 if (!reversed && start.isDualInline())
67 start = start.getDualSub();
68 else if (reversed && start.isDualSub())
71 pipeRun = component.getPipeRun();
73 List<PipeControlPoint> cps = new ArrayList<PipeControlPoint>();
74 PipeControlPoint s = null;
75 if (component.getNext() == null) {
76 s = component.getControlPoint().findPreviousEnd(cps);
78 s = component.getControlPoint().findNextEnd(cps);
80 if (s.getPipelineComponent() != startComponent) {
81 throw new Exception("Path leg start mismatch");
86 setPreviousPosition(start.getWorldPosition());
87 currentPosition = component.getPosition();
89 if (startComponent instanceof Nozzle) {
90 direction = startComponent.getControlPoint().getDirectedControlPointDirection();
91 lock = LockType.CUSTOM;
92 } else if (startComponent instanceof PipelineComponent){
93 if (startComponent instanceof InlineComponent) {
94 direction = startComponent.getControlPoint().getPathLegDirection(reversed ? Direction.PREVIOUS : Direction.NEXT);
95 lock = LockType.CUSTOM;
96 if (((InlineComponent) startComponent).isVariableLength()) {
100 Vector3d v = new Vector3d();
102 start.getControlPointEnds(v, previousPosition);
104 start.getControlPointEnds(previousPosition,v);
106 } else if (startComponent instanceof TurnComponent) {
107 if (start.asFixedAngle()) {
108 direction = startComponent.getControlPoint().getPathLegDirection(reversed ? Direction.PREVIOUS : Direction.NEXT);
109 lock = LockType.CUSTOM;
112 lock = LockType.NONE;
114 } else if (startComponent instanceof EndComponent) {
115 throw new Exception("Not supported");
119 throw new Exception("Not supported");
121 currentPosition = new Vector3d(previousPosition);
122 state = ToolState.ROUTING;
123 if (direction != null) {
124 direction.normalize();
127 startRemovable = start.isDeletable();
128 start.setDeletable(false);
129 for (PipeControlPoint pcp : cps) {
130 added.add(pcp.getPipelineComponent());
131 pcp.setDeletable(false);
133 added.add(component);
136 translateAxisGizmo.attach(panel);
137 setPreviousPosition(previousPosition);
138 updateCurrentPoint();
142 public void deactivate() {
143 // TODO Auto-generated method stub
147 protected void addPoint() throws Exception {
148 // Translate action is not supposed to add points
153 protected void endPiping() throws Exception {
154 state = ToolState.NOT_ACTIVE;
155 panel.useDefaultAction();
158 protected void updateRouting(double x, double y) {
160 //panel.getDefaultAction().update();
168 Ray ray = vtkUtil.createMouseRay(panel.getRenderer(),x, y);
169 Vector3d o = new Vector3d(ray.pos);
170 Vector3d d = ray.dir;
173 updateCurrentPoint(o, d);