]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.plant3d/src/org/simantics/plant3d/utils/ComponentUtils.java
Fix direction calculations in addComponent()
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / utils / ComponentUtils.java
index a1ac7f82834a5a36551e93bebd42b7aa8e9fa45c..913fceb9178276045828d04d9546000e39b62dd2 100644 (file)
@@ -378,9 +378,6 @@ public class ComponentUtils {
        public static PipelineComponent addComponent(P3DRootNode root, PipelineComponent component,  InsertInstruction inst) throws Exception {
                
                PipelineComponent newComponent = ComponentUtils.createComponent(root, inst.typeUri);
-               if (inst.name != null)
-                       newComponent.setName(inst.name);
-               
                PipeControlPoint newPcp = newComponent.getControlPoint();
                
                PipeControlPoint toPcp = component.getControlPoint();
@@ -408,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: 
@@ -437,6 +436,7 @@ public class ComponentUtils {
                                break;
                        case PREVIOUS:
                                pos = new Vector3d(start);
+                               dir.negate();
                                break;
                        case SPLIT:
                                pos = new Vector3d(toPcp.getWorldPosition());
@@ -444,11 +444,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));
@@ -471,8 +469,12 @@ public class ComponentUtils {
                        }
                }
                
-               String name = component.getPipeRun().getUniqueName(typeName);
-               newComponent.setName(name);
+               if (inst.name != null) {
+                       newComponent.setName(inst.name);
+               } else {
+                       String name = component.getPipeRun().getUniqueName(typeName);
+                       newComponent.setName(name);
+               }
 
                pipeRun.addChild(newComponent);
                if (newPcp.isSizeChange())
@@ -489,14 +491,13 @@ public class ComponentUtils {
                } else if (newComponent instanceof TurnComponent) {
                    TurnComponent turnComponent = (TurnComponent)newComponent;
                    if  (turnComponent.isVariableAngle()) {
-                               newPcp.setTurnAngle(inst.angle);
+                               newPcp.setTurnAngle(Math.toRadians(inst.angle));
                                newComponent.setParameter("turnAngle", inst.angle);
                    }
                    if (inst.rotationAngle != null)
                 ((TurnComponent) newComponent).setRotationAngle(inst.rotationAngle);
                }
                
-               
                newComponent.updateParameters();
                
                Vector3d v = new Vector3d(dir);
@@ -515,12 +516,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;