]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.plant3d/src/org/simantics/plant3d/utils/ComponentUtils.java
Fix division by zero in ComponentUtls.addComponent()
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / utils / ComponentUtils.java
index 78f4166547b8740367cc3c18eaccb5523d0a3148..ea9f492f6b96e8144215cff483399e6ee7d9af32 100644 (file)
@@ -405,6 +405,8 @@ public class ComponentUtils {
                        sizeChange = ((InlineComponent)newComponent).isSizeChange();
                }
                
+               // Calculate component position and direction vectors
+               // 'dir' is a unit vector that represents the direction from 'component' to 'newComponent'
                if (toPcp.isInline()) {
                        switch (position) {
                        case NEXT: 
@@ -425,15 +427,15 @@ public class ComponentUtils {
                        }
                        Vector3d start = new Vector3d();
                        Vector3d end = new Vector3d();
-                       dir = new Vector3d();
-                       toPcp.getInlineControlPointEnds(start, end, dir);
-                       dir.normalize();
+                       dir = toPcp.getInlineDir();
+                       toPcp.getControlPointEnds(start, end);
                        switch (position) {
                        case NEXT:
                                pos = new Vector3d(end);
                                break;
                        case PREVIOUS:
                                pos = new Vector3d(start);
+                               dir.negate();
                                break;
                        case SPLIT:
                                pos = new Vector3d(toPcp.getWorldPosition());
@@ -441,11 +443,9 @@ public class ComponentUtils {
                        default:
                                break;
                        }
-
                } else if (toPcp.isDirected()) {
-                       dir = new Vector3d(toPcp.getDirection(Direction.NEXT));
-                       if (position == PositionType.PREVIOUS)
-                               dir.negate();
+                       // 'dir' always points out of a nozzle regardless of insertion direction
+                       dir = new Vector3d(toPcp.getDirectedControlPointDirection());
                        pos = new Vector3d(toPcp.getWorldPosition());
                } else if (toPcp.isTurn() && toPcp.asFixedAngle()) {
                        dir = new Vector3d(toPcp.getDirection(position == PositionType.NEXT ? Direction.NEXT : Direction.PREVIOUS));
@@ -497,7 +497,6 @@ public class ComponentUtils {
                 ((TurnComponent) newComponent).setRotationAngle(inst.rotationAngle);
                }
                
-               
                newComponent.updateParameters();
                
                Vector3d v = new Vector3d(dir);
@@ -516,12 +515,11 @@ public class ComponentUtils {
                } else {
                        v.scale(newComponent.getControlPoint().getInlineLength());
                }
+               
                switch (position) {
                case NEXT:
-                       pos.add(v);
-                       break;
                case PREVIOUS:
-                       pos.sub(v);
+                       pos.add(v);
                        break;
                case SPLIT:
                        break;