]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/PipingRules.java
Handle fixed turn components when pipe run is reversed.
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / scenegraph / controlpoint / PipingRules.java
index b096266f7af72901d2718812b9dd77d3570d3689..fd50bf7b4b1c26ed9e8757855f547d818464ec4d 100644 (file)
@@ -527,15 +527,15 @@ public class PipingRules {
                        pathLegPoints.add(u.end);
 
                        // TODO : values can be cached in the loop
-                       for (int i = 1; i < pathLegPoints.size(); i++) {
+                       for (int i = 0; i < pathLegPoints.size(); i++) {
                                PipeControlPoint icp = pathLegPoints.get(i);
 
-                               PipeControlPoint prev = pathLegPoints.get(i - 1);
-                               
+                               PipeControlPoint prev = i > 0 ? pathLegPoints.get(i - 1) : null;
+                               PipeControlPoint next = i < pathLegPoints.size() - 1 ? pathLegPoints.get(i + 1) : null;
 
                                if (icp.isVariableLength()) {
-                                       if (i != pathLegPoints.size() - 1) {
-                                               PipeControlPoint next = pathLegPoints.get(i + 1);
+                                       if (prev != null && next != null) {
+                                               
                                                recalcline = recalcline | updateVariableLength(icp,  prev, next);
 
                                        } else {
@@ -544,11 +544,11 @@ public class PipingRules {
                                                // the problem is that we want to keep unconnected end
                                                // of the component in the same
                                                // place, but center of the component must be moved.
-                                               updateVariableLengthEnd(icp, prev);
+                                               updateVariableLengthEnd(icp, prev != null ? prev : next);
                                        }
 
 
-                               } else if (!prev.isVariableLength()) {
+                               } else if (prev != null && !prev.isVariableLength()) {
                                        // If this and previous control point are not variable
                                        // length pcps, we'll have to check if there is no empty
                                        // space between them.
@@ -587,18 +587,18 @@ public class PipingRules {
                        ep = new Vector3d(u.endPoint);
                        ep.sub(u.offset);
                        
-                       for (int i = 1; i < pathLegPoints.size(); i++) {
+                       for (int i = 0; i < pathLegPoints.size(); i++) {
                                PipeControlPoint icp = pathLegPoints.get(i);
 
-                               PipeControlPoint prev = pathLegPoints.get(i - 1);
-                               if (prev.isDualInline())
+                               PipeControlPoint prev = i > 0 ? pathLegPoints.get(i - 1) : null;
+                PipeControlPoint next = i < pathLegPoints.size() - 1 ? pathLegPoints.get(i + 1) : null;
+                
+                               if (prev != null && prev.isDualInline())
                                        prev = prev.getSubPoint().get(0);
                                
 
                                if (icp.isVariableLength()) {
-                                       if (i != pathLegPoints.size() - 1) {
-                                               PipeControlPoint next;
-                                               next = pathLegPoints.get(i + 1);
+                                       if (prev != null && next != null) {
                                                recalcline = recalcline | updateVariableLength(icp,  prev, next);
 
                                        } else {
@@ -607,9 +607,9 @@ public class PipingRules {
                                                // the problem is that we want to keep unconnected end
                                                // of the component in the same
                                                // place, but center of the component must be moved.
-                                               updateVariableLengthEnd(icp, prev);
+                                               updateVariableLengthEnd(icp, prev != null ? prev : next);
                                        }
-                               } else if (!prev.isVariableLength()) {
+                               } else if (prev != null && !prev.isVariableLength()) {
                                        // If this and previous control point are not variable
                                        // length pcps, we'll have to check if there is no empty
                                        // space between them.
@@ -828,6 +828,13 @@ public class PipingRules {
                }
 
                Vector3d directedDirection = direction(dcp, dcpStart ? Direction.NEXT : Direction.PREVIOUS);
+               if (directedDirection == null) {
+                   updateTurnControlPointTurn(dcp, dcp.getPrevious(), dcp.getNext());
+                   directedDirection = direction(dcp, dcpStart ? Direction.NEXT : Direction.PREVIOUS);
+                   if (directedDirection == null) {
+                       return;
+                   }
+               }
                Point3d directedEndPoint = new Point3d(u.endPoint);
                if (u.hasOffsets)
                        directedEndPoint.add(u.offset);
@@ -1540,12 +1547,25 @@ public class PipingRules {
                
                if (!tcp.isFixed()) {
                    if (next == null || prev == null)
-                   return Math.PI; // FIXME : argh
+                   return tcp.getTurnAngle();
                Vector3d middlePoint = tcp.getWorldPosition();
                Vector3d nextPoint = next.getWorldPosition();
                Vector3d prevPoint = prev.getWorldPosition();
                return updateTurnControlPointTurn(tcp, middlePoint, prevPoint, nextPoint);
                } else {
+                   // Verify that fixed turn is properly defined.
+                   // TODO : when reversed flag is changed, rotation angle should be recalculated, otherwise the orientation will change.
+                   // TODO : should reverse flag toggle to be done already when we are removing defining side?
+                   if (prev != null && next != null) {
+                       // Nothing to do
+                   } else if (prev == null) {
+                       if (!tcp._getReversed())
+                           tcp.setReversed(true);
+                   } else if (next == null) {
+                       if (tcp._getReversed())
+                           tcp.setReversed(false);
+                   }
+                   
                    Vector3d dir;
                    if (!tcp._getReversed()) {
                    if (prev == null)
@@ -1563,7 +1583,7 @@ public class PipingRules {
                 Vector3d middlePoint = tcp.getWorldPosition();
                 Vector3d nextPoint = next.getWorldPosition();
                 dir = new Vector3d();
-                dir.sub(nextPoint,middlePoint);
+                dir.sub(middlePoint, nextPoint);
                    }
                    dir.normalize();
             
@@ -1695,6 +1715,12 @@ public class PipingRules {
                                }
                                
                        }
+                       if (current.isTurn() && current.isFixed()) {
+                           current.setReversed(!current._getReversed());
+                       }
+                       if (current.isInline() && current.isReverse()) {
+                           current.setReversed(!current._getReversed());
+                       }   
                }
        }