From: Marko Luukkainen Date: Tue, 12 Nov 2019 09:31:48 +0000 (+0200) Subject: Allow PipeRun merges when diameters are the same X-Git-Tag: v1.43.0~130 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=e92fe8961f1de7bc132e64814bba4c5f92f67ba6;p=simantics%2F3d.git Allow PipeRun merges when diameters are the same gitlab #43 Change-Id: I5f85b1abc62da4eff0790f77b42999fa4274a324 --- diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/PipeRun.java b/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/PipeRun.java index 958228e8..2c334c1f 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/PipeRun.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/PipeRun.java @@ -4,7 +4,9 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.simantics.g3d.math.MathTools; import org.simantics.g3d.property.annotations.GetPropertyValue; @@ -158,6 +160,86 @@ public class PipeRun extends P3DParentNode { return true; } + public boolean canMerge(PipeRun other) { + return MathTools.equals(pipeDiameter,other.pipeDiameter); + } + + /** + * Merges contents of PipeRun r2 to this PipeRun. Note: does not connect boundary components! + * @param r2 + */ + public void merge(PipeRun r2) { + Map turnIndexMap = null; + if (!this.equalSpecs(r2)) { + if (!this.canMerge(r2)) + throw new IllegalArgumentException("PipeRuns cannot be merged"); + // Merge turn radii. + turnIndexMap = new HashMap<>(); + List mergedTurnRadius = new ArrayList<>(); + for (double t : this.getTurnRadiusArray()) { + mergedTurnRadius.add(t); + } + for (int i2 = 0; i2 < r2.getTurnRadiusArray().length; i2++) { + double t2 = r2.getTurnRadiusArray()[i2]; + boolean found = false; + for (int i = 0; i < mergedTurnRadius.size(); i++) { + if (MathTools.equals(mergedTurnRadius.get(i), t2)) { + turnIndexMap.put(i2, i); + found = true; + break; + } + } + if (!found) { + turnIndexMap.put(i2, mergedTurnRadius.size()); + mergedTurnRadius.add(t2); + } + } + for (PipeControlPoint pcp : r2.getControlPoints()) { + PipelineComponent comp = pcp.getPipelineComponent(); + if (comp instanceof TurnComponent) { + + } + } + if (mergedTurnRadius.size() > this.getTurnRadiusArray().length) { + double arr[] = new double[mergedTurnRadius.size()]; + for (int i = 0; i < mergedTurnRadius.size(); i++) { + arr[i] = mergedTurnRadius.get(i); + } + this.setTurnRadiusArray(arr); + } + } + // Move components and control points + Collection pcps = r2.getControlPoints(); + for (PipeControlPoint pcp : pcps) { + r2.deattachChild(pcp); + this.addChild(pcp); + PipelineComponent component = pcp.getPipelineComponent(); + if (component != null) { + if (!(component instanceof Nozzle)) { + component.deattach(); + this.addChild(component); + } else { + Nozzle n = (Nozzle)component; + n.setPipeRun(this); + } + } + } + // Use new turn radii indexes + if (turnIndexMap != null) { + for (PipeControlPoint pcp : pcps) { + PipelineComponent component = pcp.getPipelineComponent(); + if (component instanceof TurnComponent) { + TurnComponent tc = (TurnComponent)component; + if (tc.getTurnRadiusIndex() == null || tc.getTurnRadiusIndex() < 0) + continue; + tc.setTurnRadiusIndex(turnIndexMap.get(tc.getTurnRadiusIndex())); + } + } + } + r2.remove(); + + } + private class ComponentComparator implements Comparator { @Override public int compare(PipelineComponent o1, PipelineComponent o2) { diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/PipingRules.java b/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/PipingRules.java index de965175..3094f7d2 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/PipingRules.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/PipingRules.java @@ -1770,30 +1770,7 @@ public class PipingRules { } } - public static void merge(PipeRun run1, PipeRun r2) { - Map positions = new HashMap(); - Map orientations = new HashMap(); - for (PipeControlPoint pcp : r2.getControlPoints()) { - positions.put(pcp, pcp.getWorldPosition()); - orientations.put(pcp, pcp.getWorldOrientation()); - } - for (PipeControlPoint pcp : r2.getControlPoints()) { - r2.deattachChild(pcp); - run1.addChild(pcp); - PipelineComponent component = pcp.getPipelineComponent(); - if (component != null) { - if (!(component instanceof Nozzle)) { - component.deattach(); - run1.addChild(component); - } else { - Nozzle n = (Nozzle)component; - n.setPipeRun(run1); - } - } - } - r2.remove(); - - } + public static void validate(PipeRun pipeRun) { if (pipeRun == null) diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/utils/ComponentUtils.java b/org.simantics.plant3d/src/org/simantics/plant3d/utils/ComponentUtils.java index 69bf5931..14dce68d 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/utils/ComponentUtils.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/utils/ComponentUtils.java @@ -538,7 +538,7 @@ public class ComponentUtils { requiresReverse = true; } PipeRun other = endCP.getPipeRun(); - boolean mergeRuns = other == null ? true : pipeRun.equalSpecs(other); + boolean mergeRuns = other == null ? true : pipeRun.canMerge(other); if (requiresReverse) { // Pipe line must be traversible with next/previous relations without direction change. @@ -546,10 +546,11 @@ public class ComponentUtils { PipingRules.reverse(other); } + if (mergeRuns) { // Runs have compatible specs and must be merged if (other != null && pipeRun != other) - PipingRules.merge(pipeRun, other); + pipeRun.merge(other); else if (other == null) { if (!(endTo instanceof Nozzle)) { pipeRun.addChild(endTo);