X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.plant3d%2Fsrc%2Forg%2Fsimantics%2Fplant3d%2Factions%2FRoutePipeAction.java;h=c7c7234ef0f679a4b038253aa6a73b4c1be14a7c;hb=b6de12aa6ceb6d0a77264ee7a1b2e01f3138411c;hp=dac9d2c11b004c428a5103116db1fd8c88eed920;hpb=a1e1faa6915445e786f482170576b9c9d0f5d982;p=simantics%2F3d.git diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/actions/RoutePipeAction.java b/org.simantics.plant3d/src/org/simantics/plant3d/actions/RoutePipeAction.java index dac9d2c1..c7c7234e 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/actions/RoutePipeAction.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/actions/RoutePipeAction.java @@ -13,11 +13,18 @@ import javax.vecmath.Point3d; import javax.vecmath.Tuple3d; import javax.vecmath.Vector3d; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Combo; +import org.eclipse.swt.widgets.Label; import org.simantics.db.Resource; import org.simantics.g3d.math.MathTools; import org.simantics.g3d.math.Ray; import org.simantics.g3d.scenegraph.NodeMap; import org.simantics.g3d.scenegraph.base.INode; +import org.simantics.g3d.toolbar.ToolComposite; import org.simantics.g3d.tools.ConstraintDetector; import org.simantics.g3d.vtk.gizmo.TranslateAxisGizmo; import org.simantics.g3d.vtk.swt.InteractiveVtkComposite; @@ -47,12 +54,15 @@ import vtk.vtkTextActor; public class RoutePipeAction extends vtkSwtAction { enum LockType { - X, Y, Z, XY, YZ, XZ, NONE, CUSTOM + X, Y, Z, XY, XZ, YZ, NONE, CUSTOM }; LockType lock = LockType.NONE; + boolean lockForced; private double BRANCH_SNAP_DISTANCE = 0.05; private double NOZZLE_SNAP_DISTANCE = 0.05; + private static double BRANCH_DOT_PRODUCT = 0.95; // dot product value used for prevent branch creation + private static double ALIGN_DOT_PRODUCT = 0.99; // dot product for creating turn when connecting pipes private double istep = 10.0; private int decimals = 2; @@ -88,11 +98,15 @@ public class RoutePipeAction extends vtkSwtAction { private Set allowed = new HashSet(); - public RoutePipeAction(InteractiveVtkComposite panel, P3DRootNode root) { - this(panel,root, true); + protected ToolComposite toolComposite; + protected Combo axisCombo; + protected Button cameraButton; + + public RoutePipeAction(InteractiveVtkComposite panel, P3DRootNode root, ToolComposite toolComposite) { + this(panel,root, toolComposite, true); } - public RoutePipeAction(InteractiveVtkComposite panel, P3DRootNode root, boolean allowBranches) { + public RoutePipeAction(InteractiveVtkComposite panel, P3DRootNode root, ToolComposite toolComposite, boolean allowBranches) { super(panel); this.root = root; this.allowBranches = allowBranches; @@ -103,6 +117,7 @@ public class RoutePipeAction extends vtkSwtAction { terminalSelectionGizmo = new TerminalSelectionGizmo(panel); constraintPointGizmo = new ConstraintPointGizmo(panel); detector = new org.simantics.g3d.vtk.swt.ConstraintDetector(panel); + this.toolComposite = toolComposite; } public void setComponent(PipelineComponent component) { @@ -117,19 +132,71 @@ public class RoutePipeAction extends vtkSwtAction { setEnabled(allowed.size() > 0); } - public void deattach() { - deactivate(); - startComponent = null; - - deattachUI(); - super.deattach(); - panel.refresh(); - } + protected void createTools(ToolComposite toolComposite) { + Label label = new Label(toolComposite, SWT.READ_ONLY); + label.setText("Route direction:"); + axisCombo = new Combo(toolComposite, SWT.READ_ONLY); + axisCombo.add("X"); + axisCombo.add("Y"); + axisCombo.add("Z"); + axisCombo.add("XY"); + axisCombo.add("XZ"); + axisCombo.add("YZ"); + axisCombo.add("None"); + axisCombo.add("Custom"); + axisCombo.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + Combo c = (Combo)e.getSource(); + setLockType(LockType.values()[c.getSelectionIndex()],false); + panel.getComponent().setFocus(); + + } + }); + axisCombo.select(lock.ordinal()); + cameraButton = new Button(toolComposite, SWT.TOGGLE); + cameraButton.setText("Camera"); + cameraButton.setSelection(useDefault); + cameraButton.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + setUseDefault(((Button)e.getSource()).getSelection()); + panel.getComponent().setFocus(); + } + }); + Button close = new Button(toolComposite, SWT.PUSH); + close.setText("Close"); + close.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent e) { + panel.useDefaultAction(); + }; + }); + toolComposite.relayout(); + } + + public void deattach() { + deactivate(); + setDBUndo(true); + if (toolComposite != null) { + toolComposite.clear(); + axisCombo = null; + cameraButton = null; + } + + startComponent = null; + + deattachUI(); + super.deattach(); + panel.refresh(); + } public void attach() { if (startComponent == null) return; - + if (toolComposite != null) { + createTools(toolComposite); + } + setDBUndo(false); super.attach(); ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() { public void run() { @@ -176,7 +243,7 @@ public class RoutePipeAction extends vtkSwtAction { public boolean keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ESCAPE) panel.useDefaultAction(); - if (lock != LockType.CUSTOM) { + if (lock != LockType.CUSTOM || !lockForced) { if ((e.getModifiersEx() & KeyEvent.SHIFT_DOWN_MASK) > 0) { if (e.getKeyCode() == KeyEvent.VK_X) { if (lock != LockType.XY && lock != LockType.XZ) { @@ -224,20 +291,30 @@ public class RoutePipeAction extends vtkSwtAction { else setLockType(LockType.NONE, false); } + if (e.getKeyCode() == KeyEvent.VK_L && direction != null) { + if (lock != LockType.CUSTOM) + setLockType(LockType.CUSTOM, false); + else + setLockType(LockType.NONE, false); + } } } if (e.getKeyCode() == KeyEvent.VK_C) { - useDefault = !useDefault; - System.out.println("UseDefault " + useDefault); + setUseDefault(!useDefault); } - - - update(); return true; } + private void setUseDefault(boolean b) { + useDefault = b; + if (useDefault) + setInfoText("Rotating camera"); + if (cameraButton != null) + cameraButton.setSelection(useDefault); + } + private void update() { panel.refresh(); @@ -299,7 +376,7 @@ public class RoutePipeAction extends vtkSwtAction { protected void activateNextPrev(PipeControlPoint start) throws Exception{ if (!reversed && start.isDualInline()) - start = start.getSubPoint().get(0); + start = start.getDualSub(); else if (reversed && start.isDualSub()) start = start.parent; @@ -310,14 +387,17 @@ public class RoutePipeAction extends vtkSwtAction { if (startComponent instanceof Nozzle) { direction = startComponent.getControlPoint().getDirectedControlPointDirection(); lock = LockType.CUSTOM; + lockForced = true; } else if (startComponent instanceof PipelineComponent){ if (startComponent instanceof InlineComponent) { direction = startComponent.getControlPoint().getPathLegDirection(reversed ? Direction.PREVIOUS : Direction.NEXT); lock = LockType.CUSTOM; + lockForced = true; if (((InlineComponent) startComponent).isVariableLength()) { startWithTurn = true; direction = null; lock = LockType.NONE; + lockForced = false; } Vector3d v = new Vector3d(); if (!reversed) { @@ -329,9 +409,11 @@ public class RoutePipeAction extends vtkSwtAction { if (start.asFixedAngle()) { direction = startComponent.getControlPoint().getPathLegDirection(reversed ? Direction.PREVIOUS : Direction.NEXT); lock = LockType.CUSTOM; + lockForced = start.isFixedAngle(); } else { direction = null; lock = LockType.NONE; + lockForced = false; } } else if (startComponent instanceof EndComponent) { throw new Exception("Not supported"); @@ -371,6 +453,7 @@ public class RoutePipeAction extends vtkSwtAction { translateAxisGizmo.attach(panel); setPreviousPosition(previousPosition); updateCurrentPoint(); + updateWidget(); } protected void setPreviousPosition(Vector3d v) { @@ -417,6 +500,7 @@ public class RoutePipeAction extends vtkSwtAction { translateAxisGizmo.attach(panel); setPreviousPosition(previousPosition); updateCurrentPoint(); + updateWidget(); } private void activateSplit(PipeControlPoint start) throws Exception{ @@ -466,7 +550,10 @@ public class RoutePipeAction extends vtkSwtAction { } private void setLockType(LockType type, boolean force) { - if (force || lock != LockType.CUSTOM) { + if (type == LockType.CUSTOM && (direction == null || added.size() > 0)) { + // nothing to do.. + } else if (force || (lock != LockType.CUSTOM || !lockForced || added.size() > 0) ) { + lock = type; switch (lock) { @@ -494,7 +581,16 @@ public class RoutePipeAction extends vtkSwtAction { break; } + } + updateWidget(); + } + + private void updateWidget() { + if (axisCombo != null) { + axisCombo.select(lock.ordinal()); + axisCombo.setEnabled(!lockForced || lock != LockType.CUSTOM); + } } @Override @@ -541,7 +637,7 @@ public class RoutePipeAction extends vtkSwtAction { addPoint(); } } else { - throw new RuntimeException("RoutePipeAction initlialization has been failed, no added components found"); + throw new RuntimeException("RoutePipeAction initialization has failed, no added components found"); // // user was selecting position of branch // lastPoint.set(startPoint); // controlPoints.add(new Point3d(startPoint)); @@ -745,7 +841,7 @@ public class RoutePipeAction extends vtkSwtAction { updateRoute(o,d); } } else { - if (hoverObject instanceof InlineComponent && ((InlineComponent)hoverObject).isVariableLength() && (endType = endingLockToStraight(hoverObject,mu)) != null) { + if (hoverObject instanceof InlineComponent && ((InlineComponent)hoverObject).isVariableLength() && (endType = endingLockToStraight(hoverObject,mu, new Point3d(currentPosition))) != null) { endTo = (InlineComponent)hoverObject; } else if (hoverObject instanceof Nozzle && endingLockToNozzle(hoverObject)) { endTo = (Nozzle)hoverObject; @@ -823,6 +919,7 @@ public class RoutePipeAction extends vtkSwtAction { return true; } + @SuppressWarnings("unused") private Vector3d getLockDir() { switch (lock) { case CUSTOM: @@ -833,8 +930,9 @@ public class RoutePipeAction extends vtkSwtAction { return new Vector3d(0,1,0); case Z: return new Vector3d(0,0,1); + default: + return null; } - return null; } protected void updateRoute(Vector3d o, Vector3d d) { @@ -924,12 +1022,12 @@ public class RoutePipeAction extends vtkSwtAction { } private PositionType endingToStraight(INode straightNode, double mu[], Vector3d o, Vector3d d) { - if (!allowBranches) { - updateCurrentPoint(); - return null; - } +// if (!allowBranches) { +// updateCurrentPoint(); +// return null; +// } InlineComponent s = (InlineComponent)straightNode; - String info = ""; + Point3d sStart = new Point3d(); Point3d sEnd = new Point3d(); s.getEnds(sStart, sEnd); @@ -943,7 +1041,7 @@ public class RoutePipeAction extends vtkSwtAction { if (p != null) { currentPosition = new Vector3d(p); // snapping is detected, check if snapped point can create branch with straight - PositionType t = endingLockToStraight(s, mu); + PositionType t = endingLockToStraight(s, mu, currentPipePoint); if (t != null) return t; // if not, we'll have to remove highlight that was added when snapped point was detected @@ -965,92 +1063,104 @@ public class RoutePipeAction extends vtkSwtAction { // branch point must lie between straight's ends. If connection point is exactly // on straight end user may want to connect pipes to each other // TODO : take account sizes of inline components) - - boolean connectPrev = false; - boolean connectNext = false; - boolean branch = false; - if (mu[0] < 0.1) { - connectPrev = true; - } - else if (mu[0] > 0.9) { - connectNext = true; - } + return endingToStraight(mu, s, sStart, sEnd, currentPipePoint); + } + + private PositionType endingToStraight(double mu[], InlineComponent s, Point3d sStart, Point3d sEnd , Point3d currentPipePoint) { + String info = ""; + + boolean connectPrev = false; + boolean connectNext = false; + boolean branch = false; + if (mu[0] < 0.1) { + connectPrev = true; + } + else if (mu[0] > 0.9) { + connectNext = true; + } - - if (connectPrev) { - PipeControlPoint pcp = s.getControlPoint(); - if (pcp.getPrevious() != null) - connectPrev = false; - } else if (connectNext) { - PipeControlPoint pcp = s.getControlPoint(); - if (pcp.getNext() != null) - connectNext = false; - } else { - Vector3d dir = s.getControlPoint().getPathLegDirection(Direction.NEXT); - Vector3d currDir = getLast().getControlPoint().getPathLegDirection(Direction.NEXT); - dir.normalize(); - currDir.normalize(); - double dot = dir.dot(currDir); - System.out.println(dot + " " + currDir + " " + dir); - if (dot > 0.95 || dot < -0.95) { - // pipes are almost in the same direction, creating a branch is not feasible. - branch = false; - } else { - branch = true; - } - } - - - if (connectNext || connectPrev) - info += "Connect pipes :"; - else if (branch) - info += "Create a Branch :"; - - setInfoText(info + currentPosition + " " + Math.max(0.0, Math.min(mu[0], 1.0))); - if (connectNext) { - currentPosition.set(sEnd); - updateCurrentPoint(); - return PositionType.NEXT; - } else if (connectPrev){ - currentPosition.set(sStart); - updateCurrentPoint(); - return PositionType.PREVIOUS; - } else if (branch) { - return PositionType.SPLIT; - } else { - currentPosition.set(currentPipePoint); - updateCurrentPoint(); - return null; - } - + + if (connectPrev) { + PipeControlPoint pcp = s.getControlPoint(); + if (pcp.getPrevious() != null) + connectPrev = false; + } else if (connectNext) { + PipeControlPoint pcp = s.getControlPoint(); + if (pcp.getNext() != null) + connectNext = false; + } else { + Vector3d dir = s.getControlPoint().getPathLegDirection(Direction.NEXT); + Vector3d currDir = getLast().getControlPoint().getPathLegDirection(Direction.NEXT); + dir.normalize(); + currDir.normalize(); + double dot = dir.dot(currDir); + System.out.println(dot + " " + currDir + " " + dir); + if (dot > BRANCH_DOT_PRODUCT || dot < -BRANCH_DOT_PRODUCT) { + // pipes are almost in the same direction, creating a branch is not feasible. + branch = false; + } else { + branch = true; + } + } + + + if (connectNext || connectPrev) + info += "Connect pipes :"; + else if (branch) + info += "Create a Branch :"; + + setInfoText(info + currentPosition + " " + Math.max(0.0, Math.min(mu[0], 1.0))); + if (connectNext) { + currentPosition.set(sEnd); + updateCurrentPoint(); + return PositionType.NEXT; + } else if (connectPrev){ + currentPosition.set(sStart); + updateCurrentPoint(); + return PositionType.PREVIOUS; + } else if (branch && allowBranches) { + return PositionType.SPLIT; + } else { + currentPosition.set(currentPipePoint); + updateCurrentPoint(); + return null; + } } private PipeControlPoint endingToComponent(INode componentNode, Vector3d o, Vector3d d) { PipelineComponent component = (PipelineComponent)componentNode; PipeControlPoint pcp = component.getControlPoint(); + PipeControlPoint connect = null; if (component instanceof EndComponent) { if (pcp.getNext() != null || pcp.getPrevious() != null) return null; - return pcp; + connect = pcp; } else if (component instanceof TurnComponent) { if (pcp.getNext() == null || pcp.getPrevious() == null) - return pcp; - return null; + connect = pcp; + else + return null; } else if (component instanceof InlineComponent) { // TODO : scan all empty pcps of the component and select closest one. if (pcp.getNext() == null || pcp.getPrevious() == null) - return pcp; - return null; + connect = pcp; + else + return null; + } + if (connect != null) { + currentPosition.set(connect.getWorldPosition()); + updateCurrentPoint(); + setInfoText("Connect to " + component.getName()); + return connect; } - return null; } - private PositionType endingLockToStraight(INode straightNode, double mu[]) { - if (!allowBranches) { - updateCurrentPoint(); - return null; - } + private PositionType endingLockToStraight(INode straightNode, double mu[], Point3d currentPipePoint) { +// if (!allowBranches) { +// updateCurrentPoint(); +// return null; +// } InlineComponent s = (InlineComponent)straightNode; Point3d sStart = new Point3d(); Point3d sEnd = new Point3d(); @@ -1069,15 +1179,21 @@ public class RoutePipeAction extends vtkSwtAction { // startPoint of branch must be between pipe ends // TODO : take account sizes of elbows (or other components) // branch point must be between pipe ends and intersection points must be quite close to each other - if (mu[0] > 0.0 && mu[0] < 1.0 && routePoint.lengthSquared() < BRANCH_SNAP_DISTANCE) { - currentPosition.set(branchPoint); - - updateCurrentPoint(); - - setInfoText("Create a branch (l) :" + currentPosition + " " + Math.max(0.0, Math.min(mu[0], 1.0)) + " " + routePoint.lengthSquared()); - return PositionType.SPLIT; - } - return null; + + if (routePoint.lengthSquared() > BRANCH_SNAP_DISTANCE) + return null; + + return endingToStraight(mu, s, sStart, sEnd, currentPipePoint); + +// if (mu[0] > 0.0 && mu[0] < 1.0 && routePoint.lengthSquared() < BRANCH_SNAP_DISTANCE) { +// currentPosition.set(branchPoint); +// +// updateCurrentPoint(); +// +// setInfoText("Create a branch (l) :" + currentPosition + " " + Math.max(0.0, Math.min(mu[0], 1.0)) + " " + routePoint.lengthSquared()); +// return PositionType.SPLIT; +// } +// return null; } private boolean endingLockToNozzle(INode nozzleNode) { @@ -1105,40 +1221,64 @@ public class RoutePipeAction extends vtkSwtAction { return null; } + protected void addTurn() throws Exception{ + InlineComponent previous = (InlineComponent)getLast(); + PipeControlPoint previousCP = previous.getControlPoint(); + TurnComponent turn = ComponentUtils.createTurn(root); + PipeControlPoint turnCP = turn.getControlPoint(); + turn.setName(pipeRun.getUniqueName("Elbow")); + pipeRun.addChild(turn); + added.add(turn); + turnCP.setDeletable(false); // mark turnCP nonDeletable so that PipingRules won't delete it immediately. + if (!reversed) { + previousCP.setNext(turnCP); + turnCP.setPrevious(previousCP); + } else { + previousCP.setPrevious(turnCP); + turnCP.setNext(previousCP); + } + + turnCP.setWorldPosition(currentPosition); + turnCP.setTurnAngle(0.0); + turnCP.setLength(0.0); + + } + + protected void addStraight() throws Exception{ + TurnComponent turn = (TurnComponent)getLast(); + PipeControlPoint turnCP = turn.getControlPoint(); + + InlineComponent straight = ComponentUtils.createStraight(root); + + PipeControlPoint straightCP = straight.getControlPoint(); + straight.setName(pipeRun.getUniqueName("Pipe")); + + + pipeRun.addChild(straight); + + added.add(straight); + + + + if (!reversed) { + turnCP.setNext(straightCP); + straightCP.setPrevious(turnCP); + } else { + turnCP.setPrevious(straightCP); + straightCP.setNext(turnCP); + } + + turnCP.setWorldPosition(currentPosition); + turnCP.setTurnAngle(0.0); + turnCP.setLength(0.0); + straightCP.setWorldPosition(currentPosition); + straightCP.setLength(0.0); + + } + protected void addPoint() throws Exception { - InlineComponent previous = (InlineComponent)getLast(); - PipeControlPoint previousCP = previous.getControlPoint(); - TurnComponent turn = ComponentUtils.createTurn(root); - InlineComponent straight = ComponentUtils.createStraight(root); - PipeControlPoint turnCP = turn.getControlPoint(); - PipeControlPoint straightCP = straight.getControlPoint(); - straight.setName(pipeRun.getUniqueName("Pipe")); - turn.setName(pipeRun.getUniqueName("Elbow")); - pipeRun.addChild(turn); - pipeRun.addChild(straight); - added.add(turn); - added.add(straight); - - turnCP.setDeletable(false); // mark turnCP nonDeletable so that PipingRules won't delete it immediately. - - if (!reversed) { - previousCP.setNext(turnCP); - turnCP.setPrevious(previousCP); - turnCP.setNext(straightCP); - straightCP.setPrevious(turnCP); - } else { - previousCP.setPrevious(turnCP); - turnCP.setNext(previousCP); - turnCP.setPrevious(straightCP); - straightCP.setNext(turnCP); - } - - turnCP.setWorldPosition(currentPosition); - turnCP.setTurnAngle(0.0); - turnCP.setLength(0.0); - straightCP.setWorldPosition(currentPosition); - straightCP.setLength(0.0); - + addTurn(); + addStraight(); setPreviousPosition(currentPosition); updateCurrentPoint(); @@ -1206,6 +1346,18 @@ public class RoutePipeAction extends vtkSwtAction { state = ToolState.NOT_ACTIVE; if (endTo != null) { + if (endType == PositionType.NEXT || endType == PositionType.PREVIOUS && endTo instanceof InlineComponent) { + Vector3d dir = endTo.getControlPoint().getPathLegDirection(Direction.NEXT); + Vector3d currDir = getLast().getControlPoint().getPathLegDirection(Direction.NEXT); + dir.normalize(); + currDir.normalize(); + double dot = dir.dot(currDir); + System.out.println(dot + " " + currDir + " " + dir); + if (dot < ALIGN_DOT_PRODUCT && dot> -ALIGN_DOT_PRODUCT) { + addTurn(); + } + + } ComponentUtils.connect(getLast(), endTo, endType, currentPosition); } panel.useDefaultAction();