]> gerrit.simantics Code Review - simantics/3d.git/commitdiff
Use unique names at the model scope instead of pipe run 17/4317/1
authorReino Ruusu <reino.ruusu@semantum.fi>
Mon, 15 Jun 2020 10:29:48 +0000 (13:29 +0300)
committerReino Ruusu <reino.ruusu@semantum.fi>
Mon, 15 Jun 2020 10:30:35 +0000 (13:30 +0300)
gitlab #137

Change-Id: I25a1105fb6eb8eabb575332e95311a1fa85da0b8

org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/P3DParentNode.java
org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/P3DRootNode.java

index d7f6749e53fcb4dacbdd1dc50da03a0f47b40125..c37d4627d4107f9a29ebadb11dfa3ea90820d072 100644 (file)
@@ -180,20 +180,39 @@ public abstract class P3DParentNode<T extends IP3DNode> extends ParentNode<T> im
                return null;
        }
        
-       public String getUniqueName(String prefix) {
-               Set<String> names = new HashSet<String>();
+       /**
+        * 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<String> 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<String> names = new HashSet<String>();
+                       getComponentNames(names);
+                       int i = 1;
+                       while (true) {
+                               String genName = prefix + "_" + i;
+                               if (!names.contains(genName))
+                                       return genName;
+                               i++;
+                       }
                }
        }
 
index 7a5ce551a7c317c16df776455125398213378934..93f4ecf27d4be74d4e67ade02c20a983a884eda1 100644 (file)
@@ -141,6 +141,10 @@ public class P3DRootNode extends ParentNode<INode> 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<String> names = new HashSet<String>();
                for (INode node : getChild()) {
@@ -148,6 +152,8 @@ public class P3DRootNode extends ParentNode<INode> 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) {