From def2c5c9bd1598baa121105cd2610e9e823a6bc7 Mon Sep 17 00:00:00 2001 From: Reino Ruusu Date: Mon, 15 Jun 2020 13:29:48 +0300 Subject: [PATCH] Use unique names at the model scope instead of pipe run gitlab #137 Change-Id: I25a1105fb6eb8eabb575332e95311a1fa85da0b8 (cherry picked from commit c2bc02ab23627234920e89e364c2b4b2e7657249) --- .../plant3d/scenegraph/P3DParentNode.java | 35 ++++++++++++++----- .../plant3d/scenegraph/P3DRootNode.java | 6 ++++ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/P3DParentNode.java b/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/P3DParentNode.java index d7f6749e..c37d4627 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/P3DParentNode.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/P3DParentNode.java @@ -180,20 +180,39 @@ public abstract class P3DParentNode extends ParentNode im return null; } - public String getUniqueName(String prefix) { - Set names = new HashSet(); + /** + * Get a set of all of the names contained by this node and all its descendants + * @param names A set to which the names are added + */ + public void getComponentNames(Set names) { for (IP3DNode node : getNodes()) { if (!(node instanceof IP3DVisualNode)) continue; IP3DVisualNode n = (IP3DVisualNode)node; names.add(n.getName()); + if (node instanceof P3DParentNode) + ((P3DParentNode) node).getComponentNames(names); } - int i = 1; - while (true) { - String genName = prefix + "_" + i; - if (!names.contains(genName)) - return genName; - i++; + } + + /** + * Get a unique name in the Plant3D model with a given prefix followed by + * an underscore and number. + */ + public String getUniqueName(String prefix) { + ParentNode root = getRootNode(); + if (root instanceof P3DRootNode) { + return ((P3DRootNode) root).getUniqueName(prefix); + } else { + Set names = new HashSet(); + getComponentNames(names); + int i = 1; + while (true) { + String genName = prefix + "_" + i; + if (!names.contains(genName)) + return genName; + i++; + } } } diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/P3DRootNode.java b/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/P3DRootNode.java index 7a5ce551..93f4ecf2 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/P3DRootNode.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/P3DRootNode.java @@ -141,6 +141,10 @@ public class P3DRootNode extends ParentNode implements IG3DNode, NodeMapP throw new NodeException("Cannot set root node orientation"); } + /** + * Get a unique name in the Plant3D model with a given prefix followed by + * an underscore and number. + */ public String getUniqueName(String prefix) { Set names = new HashSet(); for (INode node : getChild()) { @@ -148,6 +152,8 @@ public class P3DRootNode extends ParentNode implements IG3DNode, NodeMapP continue; IP3DVisualNode n = (IP3DVisualNode)node; names.add(n.getName()); + if (node instanceof P3DParentNode) + ((P3DParentNode) node).getComponentNames(names); } int i = 1; while (true) { -- 2.47.1