+package org.simantics.plant3d.actions;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.vecmath.Vector3d;
+
+import org.simantics.g3d.math.Ray;
+import org.simantics.g3d.scenegraph.IG3DNode;
+import org.simantics.g3d.vtk.swt.InteractiveVtkComposite;
+import org.simantics.g3d.vtk.utils.vtkUtil;
+import org.simantics.plant3d.Activator;
+import org.simantics.plant3d.actions.RoutePipeAction.LockType;
+import org.simantics.plant3d.actions.RoutePipeAction.ToolState;
+import org.simantics.plant3d.scenegraph.EndComponent;
+import org.simantics.plant3d.scenegraph.InlineComponent;
+import org.simantics.plant3d.scenegraph.Nozzle;
+import org.simantics.plant3d.scenegraph.P3DRootNode;
+import org.simantics.plant3d.scenegraph.PipelineComponent;
+import org.simantics.plant3d.scenegraph.TurnComponent;
+import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
+import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
+import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.Direction;
+import org.simantics.plant3d.utils.ComponentUtils;
+
+public class TranslateFreeVariableLengthAction extends RoutePipeAction{
+
+ private InlineComponent component;
+
+ public TranslateFreeVariableLengthAction(InteractiveVtkComposite panel, P3DRootNode root) {
+ super(panel, root, false);
+ setText("Translate");
+ setImageDescriptor(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/arrow_refresh.png"));
+ }
+
+
+ public void setNode(IG3DNode node) {
+ if (node instanceof InlineComponent) {
+ component = (InlineComponent)node;
+ if (component.isVariableLength() && (component.getNext() == null || component.getPrevious() == null)) {
+ PipelineComponent pathLegStart;
+ if (component.getNext() == null) {
+ pathLegStart = component.getControlPoint().findPreviousEnd().getPipelineComponent();
+ } else {
+ pathLegStart = component.getControlPoint().findNextEnd().getPipelineComponent();
+ }
+ setComponent(pathLegStart);
+
+ setEnabled(true);
+ } else {
+ setEnabled(false);
+ }
+ } else {
+ component = null;
+ setEnabled(false);
+ }
+ }
+
+ @Override
+ protected void activate() throws Exception {
+ state = ToolState.INITIALIZING;
+ added.clear();
+ pipeRun = startComponent.getPipeRun();
+ PipeControlPoint start = startComponent.getControlPoint();
+ activateNextPrev(start);
+ }
+
+ @Override
+ protected void activateNextPrev(PipeControlPoint start) throws Exception {
+ if (!reversed && start.isDualInline())
+ start = start.getSubPoint().get(0);
+ else if (reversed && start.isDualSub())
+ start = start.parent;
+
+ pipeRun = component.getPipeRun();
+
+ List<PipeControlPoint> cps = new ArrayList<PipeControlPoint>();
+ PipeControlPoint s = null;
+ if (component.getNext() == null) {
+ s = component.getControlPoint().findPreviousEnd(cps);
+ } else {
+ s = component.getControlPoint().findNextEnd(cps);
+ }
+ if (s.getPipelineComponent() != startComponent) {
+ throw new Exception("Path leg start mismatch");
+ }
+
+
+
+ setPreviousPosition(start.getWorldPosition());
+ currentPosition = component.getPosition();
+
+ if (startComponent instanceof Nozzle) {
+ direction = startComponent.getControlPoint().getDirectedControlPointDirection();
+ lock = LockType.CUSTOM;
+ } else if (startComponent instanceof PipelineComponent){
+ if (startComponent instanceof InlineComponent) {
+ direction = startComponent.getControlPoint().getPathLegDirection(reversed ? Direction.PREVIOUS : Direction.NEXT);
+ lock = LockType.CUSTOM;
+ if (((InlineComponent) startComponent).isVariableLength()) {
+ direction = null;
+ lock = LockType.NONE;
+ }
+ Vector3d v = new Vector3d();
+ if (!reversed) {
+ start.getControlPointEnds(v, previousPosition);
+ } else {
+ start.getControlPointEnds(previousPosition,v);
+ }
+ } else if (startComponent instanceof TurnComponent) {
+ if (start.isFixed()) {
+ direction = startComponent.getControlPoint().getPathLegDirection(reversed ? Direction.PREVIOUS : Direction.NEXT);
+ lock = LockType.CUSTOM;
+ } else {
+ direction = null;
+ lock = LockType.NONE;
+ }
+ } else if (startComponent instanceof EndComponent) {
+ throw new Exception("Not supported");
+ }
+
+ } else {
+ throw new Exception("Not supported");
+ }
+ currentPosition = new Vector3d(previousPosition);
+ state = ToolState.ROUTING;
+ if (direction != null) {
+ direction.normalize();
+
+ }
+ startRemovable = start.isDeletable();
+ start.setDeletable(false);
+ for (PipeControlPoint pcp : cps) {
+ added.add(pcp.getPipelineComponent());
+ pcp.setDeletable(false);
+ }
+ added.add(component);
+
+
+ translateAxisGizmo.attach(panel);
+ setPreviousPosition(previousPosition);
+ updateCurrentPoint();
+ }
+
+ @Override
+ public void deactivate() {
+ // TODO Auto-generated method stub
+ super.deactivate();
+ }
+
+ protected void addPoint() throws Exception {
+ // Translate action is not supposed to add points
+ return;
+ }
+
+ @Override
+ protected void endPiping() throws Exception {
+ state = ToolState.NOT_ACTIVE;
+ panel.useDefaultAction();
+ }
+
+ protected void updateRouting(double x, double y) {
+ if (useDefault) {
+ //panel.getDefaultAction().update();
+ return;
+ }
+
+ endTo = null;
+ endType = null;
+ endPort = null;
+
+ Ray ray = vtkUtil.createMouseRay(panel.getRenderer(),x, y);
+ Vector3d o = new Vector3d(ray.pos);
+ Vector3d d = ray.dir;
+
+
+ updateCurrentPoint(o, d);
+ updateRoute(o,d);
+ panel.refresh();
+ }
+
+
+}