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.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;
26 public class TranslateFreeVariableLengthAction extends RoutePipeAction{
28 private InlineComponent component;
30 public TranslateFreeVariableLengthAction(InteractiveVtkComposite panel, P3DRootNode root) {
31 super(panel, root, false);
33 setImageDescriptor(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/arrow_refresh.png"));
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();
45 pathLegStart = component.getControlPoint().findNextEnd().getPipelineComponent();
47 setComponent(pathLegStart);
60 protected void activate() throws Exception {
61 state = ToolState.INITIALIZING;
63 pipeRun = startComponent.getPipeRun();
64 PipeControlPoint start = startComponent.getControlPoint();
65 activateNextPrev(start);
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())
75 pipeRun = component.getPipeRun();
77 List<PipeControlPoint> cps = new ArrayList<PipeControlPoint>();
78 PipeControlPoint s = null;
79 if (component.getNext() == null) {
80 s = component.getControlPoint().findPreviousEnd(cps);
82 s = component.getControlPoint().findNextEnd(cps);
84 if (s.getPipelineComponent() != startComponent) {
85 throw new Exception("Path leg start mismatch");
90 setPreviousPosition(start.getWorldPosition());
91 currentPosition = component.getPosition();
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()) {
102 lock = LockType.NONE;
104 Vector3d v = new Vector3d();
106 start.getControlPointEnds(v, previousPosition);
108 start.getControlPointEnds(previousPosition,v);
110 } else if (startComponent instanceof TurnComponent) {
111 if (start.asFixedAngle()) {
112 direction = startComponent.getControlPoint().getPathLegDirection(reversed ? Direction.PREVIOUS : Direction.NEXT);
113 lock = LockType.CUSTOM;
116 lock = LockType.NONE;
118 } else if (startComponent instanceof EndComponent) {
119 throw new Exception("Not supported");
123 throw new Exception("Not supported");
125 currentPosition = new Vector3d(previousPosition);
126 state = ToolState.ROUTING;
127 if (direction != null) {
128 direction.normalize();
131 startRemovable = start.isDeletable();
132 start.setDeletable(false);
133 for (PipeControlPoint pcp : cps) {
134 added.add(pcp.getPipelineComponent());
135 pcp.setDeletable(false);
137 added.add(component);
140 translateAxisGizmo.attach(panel);
141 setPreviousPosition(previousPosition);
142 updateCurrentPoint();
146 public void deactivate() {
147 // TODO Auto-generated method stub
151 protected void addPoint() throws Exception {
152 // Translate action is not supposed to add points
157 protected void endPiping() throws Exception {
158 state = ToolState.NOT_ACTIVE;
159 panel.useDefaultAction();
162 protected void updateRouting(double x, double y) {
164 //panel.getDefaultAction().update();
172 Ray ray = vtkUtil.createMouseRay(panel.getRenderer(),x, y);
173 Vector3d o = new Vector3d(ray.pos);
174 Vector3d d = ray.dir;
177 updateCurrentPoint(o, d);