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.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;
23 public class TranslateFreeVariableLengthAction extends RoutePipeAction{
25 private InlineComponent component;
27 public TranslateFreeVariableLengthAction(InteractiveVtkComposite panel, P3DRootNode root, ToolComposite toolComposite) {
28 super(panel, root, toolComposite, false);
30 setImageDescriptor(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/arrow_refresh.png"));
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();
42 pathLegStart = component.getControlPoint().findNextEnd().getPipelineComponent();
44 setComponent(pathLegStart);
57 protected void activate() throws Exception {
58 state = ToolState.INITIALIZING;
60 pipeRun = startComponent.getPipeRun();
61 PipeControlPoint start = startComponent.getControlPoint();
62 activateNextPrev(start);
66 protected void activateNextPrev(PipeControlPoint start) throws Exception {
67 if (!reversed && start.isDualInline())
68 start = start.getDualSub();
69 else if (reversed && start.isDualSub())
72 pipeRun = component.getPipeRun();
74 List<PipeControlPoint> cps = new ArrayList<PipeControlPoint>();
75 PipeControlPoint s = null;
76 if (component.getNext() == null) {
77 s = component.getControlPoint().findPreviousEnd(cps);
79 s = component.getControlPoint().findNextEnd(cps);
81 if (s.getPipelineComponent() != startComponent) {
82 throw new Exception("Path leg start mismatch");
87 setPreviousPosition(start.getWorldPosition());
88 currentPosition = component.getPosition();
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()) {
101 Vector3d v = new Vector3d();
103 start.getControlPointEnds(v, previousPosition);
105 start.getControlPointEnds(previousPosition,v);
107 } else if (startComponent instanceof TurnComponent) {
108 if (start.asFixedAngle()) {
109 direction = startComponent.getControlPoint().getPathLegDirection(reversed ? Direction.PREVIOUS : Direction.NEXT);
110 lock = LockType.CUSTOM;
113 lock = LockType.NONE;
115 } else if (startComponent instanceof EndComponent) {
116 throw new Exception("Not supported");
120 throw new Exception("Not supported");
122 currentPosition = new Vector3d(previousPosition);
123 state = ToolState.ROUTING;
124 if (direction != null) {
125 direction.normalize();
128 startRemovable = start.isDeletable();
129 start.setDeletable(false);
130 for (PipeControlPoint pcp : cps) {
131 added.add(pcp.getPipelineComponent());
132 pcp.setDeletable(false);
134 added.add(component);
137 translateAxisGizmo.attach(panel);
138 setPreviousPosition(previousPosition);
139 updateCurrentPoint();
143 public void deactivate() {
144 // TODO Auto-generated method stub
148 protected void addPoint() throws Exception {
149 // Translate action is not supposed to add points
154 protected void endPiping() throws Exception {
155 state = ToolState.NOT_ACTIVE;
156 panel.useDefaultAction();
159 protected void updateRouting(double x, double y) {
161 //panel.getDefaultAction().update();
169 Ray ray = vtkUtil.createMouseRay(panel.getRenderer(),x, y);
170 Vector3d o = new Vector3d(ray.pos);
171 Vector3d d = ray.dir;
174 updateCurrentPoint(o, d);