]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/PipelineComponent.java
Use inside diameter for eccentric reducer offset calculation
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / scenegraph / PipelineComponent.java
index b052bae435d2a25d8061e29881717b007656f237..6afa937d60f18c8cdbd38234ecfae6d3fbce360c 100644 (file)
@@ -4,6 +4,7 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Objects;
 
 import javax.vecmath.Quat4d;
 import javax.vecmath.Tuple3d;
@@ -129,6 +130,12 @@ public abstract class PipelineComponent extends GeometryNode {
         }
         setParameterMap(parameters);
     }
+    
+    public void setParameter(String name, Object value) {
+        Map<String, Object> parameters = new HashMap<>(getParameterMap());
+        parameters.put(name, value);
+        setParameterMap(parameters);
+    }
        
        public abstract void setType(String typeURI) throws Exception;
        
@@ -139,8 +146,10 @@ public abstract class PipelineComponent extends GeometryNode {
        
        @RelatedSetObj(Plant3D.URIs.HasNext)
        public void setNext(PipelineComponent comp) {
-               if (next == comp)
+               if (next == comp) {
+                   syncNext();
                    return;
+               }
                if (this.next != null)
                    this.next._removeRef(this);
         _setNext(comp);
@@ -164,8 +173,10 @@ public abstract class PipelineComponent extends GeometryNode {
        
        @RelatedSetObj(Plant3D.URIs.HasPrevious)
        public void setPrevious(PipelineComponent comp) {
-               if (previous == comp)
+               if (previous == comp) {
+                   syncPrevious();
                        return;
+               }
                if (this.previous != null)
                        this.previous._removeRef(this);
                _setPrevious(comp);
@@ -190,8 +201,10 @@ public abstract class PipelineComponent extends GeometryNode {
        
        @RelatedSetObj(Plant3D.URIs.HasBranch0)
        public void setBranch0(PipelineComponent comp) {
-               if (branch0 == comp)
+               if (branch0 == comp) {
+                   syncBranch0();
                        return;
+               }
                if (this.branch0 != null)
                        this.branch0._removeRef(this);
                this.branch0 = comp;
@@ -299,7 +312,7 @@ public abstract class PipelineComponent extends GeometryNode {
        }
        
        boolean syncnext = false;
-       private void syncNext() {
+       protected void syncNext() {
                if (syncnext)
                        return;
                syncnext = _syncNext();
@@ -338,7 +351,7 @@ public abstract class PipelineComponent extends GeometryNode {
        }
        
        boolean syncprev = false;
-       private void syncPrevious() {
+       protected void syncPrevious() {
                if (syncprev)
                        return;
                syncprev = _syncPrevious();
@@ -375,7 +388,7 @@ public abstract class PipelineComponent extends GeometryNode {
        }
        
        boolean syncbr0 = false;
-       private void syncBranch0() {
+       protected void syncBranch0() {
                if (syncbr0)
                        return;
                syncbr0 = _syncBranch0();
@@ -447,10 +460,44 @@ public abstract class PipelineComponent extends GeometryNode {
                PipeControlPoint pcp = getControlPoint();
                // Second check is needed, when remove process is initiated from control point.
                if (pcp != null && pcp.getPipelineComponent() != null) {
+                       if (pcp.isSizeChange()) {
+                               mergeWithAlternative();
+                       }
+                       
                        pcp.remove();
                }
+               
+               setPipeRun(null);
                super.remove();
        }
+
+       private void mergeWithAlternative() {
+               PipeRun run = getPipeRun();
+               PipeRun alternative = getAlternativePipeRun();
+               if (alternative != null) {
+                       // Move components from alternative pipe run to main run
+                       PipelineComponent p = getNext();
+                       while (p != null && p.getPipeRun() == alternative) {
+                               if (p.getParent() == alternative) {
+                                       p.deattach(); // For components
+                                       run.addChild(p);
+                               }
+                               else {
+                                       p.setPipeRun(run); // For nozzles
+                               }
+
+                               p.updateParameters();
+                               PipingRules.requestUpdate(p.getControlPoint());
+                               
+                               p = p.getNext();
+                       }
+                       
+                       setAlternativePipeRun(run);
+                       
+                       if (alternative.getChild().isEmpty())
+                               alternative.remove();
+               }
+       }
        
        public void removeAndSplit() {
            PipeControlPoint pcp = getControlPoint();
@@ -458,6 +505,7 @@ public abstract class PipelineComponent extends GeometryNode {
         if (pcp != null && pcp.getPipelineComponent() != null) {
             pcp.removeAndSplit();
         }
+        setPipeRun(null);
         super.remove();
        }
 
@@ -568,8 +616,8 @@ public abstract class PipelineComponent extends GeometryNode {
                        }
                        
                        // Calculate center of mass for the frustum
-                       double r1 = getPipeRun().getPipeDiameter();
-                       double r2 = getAlternativePipeRun().getPipeDiameter();
+                       double r1 = getPipeRun().getInsideDiameter();
+                       double r2 = getAlternativePipeRun().getInsideDiameter();
                        
                        Vector3d p1 = new Vector3d(), p2 = new Vector3d();
                        pcp.getInlineControlPointEnds(p1, p2);
@@ -598,7 +646,7 @@ public abstract class PipelineComponent extends GeometryNode {
                        
                        double r = ((TurnComponent)this).getTurnRadius();
                        double a = pcp.getTurnAngle();
-                       double pipeRadius = pcp.getPipeRun().getPipeDiameter() / 2;
+                       double pipeRadius = pcp.getPipeRun().getInsideDiameter() / 2;
                        
                        // Unit vector in inlet flow direction
                        Vector3d inletDir = pcp.getPathLegDirection(Direction.PREVIOUS);
@@ -638,7 +686,7 @@ public abstract class PipelineComponent extends GeometryNode {
                if (pcp == null)
                        throw new IllegalStateException("No centroid defined");
                
-               double pipeRadius = getPipeRun().getPipeDiameter() / 2;
+               double pipeRadius = getPipeRun().getInsideDiameter() / 2;
                
                switch (pcp.getType()) {
                case INLINE:
@@ -650,7 +698,7 @@ public abstract class PipelineComponent extends GeometryNode {
                        
                        // Calculate center of mass for the frustum
                        double r1 = pipeRadius;
-                       double r2 = getAlternativePipeRun().getPipeDiameter() / 2;
+                       double r2 = getAlternativePipeRun().getInsideDiameter() / 2;
                        return pcp.getLength() * Math.PI * (r1*r1 + r1*r2 + r2*r2) / 4;
                case TURN: {
                        double r = ((TurnComponent)this).getTurnRadius();
@@ -661,4 +709,29 @@ public abstract class PipelineComponent extends GeometryNode {
                        throw new IllegalStateException("No centroid defined");
                }
        }
+       
+       
+       private String error;
+       
+       /**
+        * Returns possible pipe modelling error, or null;
+        * @return
+        */
+       @GetPropertyValue(name="Error", value="error", tabId = "Default")
+       public String getError() {
+           return error;
+       }
+       
+       /**
+        * Sets pipe modelling error. 
+        * 
+        * Error is usually set by PipingRules.
+        * @param error
+        */
+       public void setError(String error) {
+           if (Objects.equals(this.error,  error))
+               return;
+           this.error = error;
+           firePropertyChanged("error");
+       }
 }