]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.plant3d/src/org/simantics/plant3d/utils/P3DUtil.java
Equipment/Component library customization
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / utils / P3DUtil.java
index 71f844d866329fe2dac458d7e9c468c6eb28ce33..65b93cffd2688102500fb30a0b67dce44949c87f 100644 (file)
@@ -1,9 +1,13 @@
 package org.simantics.plant3d.utils;
 
+import java.util.ArrayDeque;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
+import java.util.Deque;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 import org.simantics.Simantics;
 import org.simantics.db.ReadGraph;
@@ -44,12 +48,12 @@ public class P3DUtil {
                });
        }
        
-       public static List<Item> getNozzles() throws DatabaseException {
+       public static List<Item> getNozzles(String libUri) throws DatabaseException {
                return Simantics.getSession().syncRequest(new Read<List<Item>>() {
                        @Override
                        public List<Item> perform(ReadGraph graph) throws DatabaseException {
                                Plant3D p3d = Plant3D.getInstance(graph);
-                               ItemQuery query = new ItemQuery(p3d.Nozzle, Plant3D.URIs.Builtin);
+                               ItemQuery query = new ItemQuery(p3d.Nozzle, libUri);
                                return graph.syncRequest(query);
                        }
                });
@@ -67,7 +71,8 @@ public class P3DUtil {
                public List<Item> perform(ReadGraph graph) throws DatabaseException {
                        Resource project = Simantics.getProject().get();
                        Resource builtins = graph.getResource(libUri);
-                       List<Item> actions = getItems(graph, project,type);
+                       List<Item> actions = new ArrayList<>();
+//                     actions.addAll(getItems(graph, project,type));
                        actions.addAll(getItems(graph, builtins,type));
                        return actions;
                }
@@ -88,34 +93,34 @@ public class P3DUtil {
                }
        }
        
-       public static List<Item> getEnds() throws DatabaseException {
+       public static List<Item> getEnds(String libUri) throws DatabaseException {
                return Simantics.getSession().syncRequest(new Read<List<Item>>() {
                        @Override
                        public List<Item> perform(ReadGraph graph) throws DatabaseException {
                                Plant3D p3d = Plant3D.getInstance(graph);
-                               ItemQuery query = new ItemQuery(p3d.EndComponent, Plant3D.URIs.Builtin);
+                               ItemQuery query = new ItemQuery(p3d.EndComponent, libUri);
                                return graph.syncRequest(query);
                        }
                });
        }
        
-       public static List<Item> getTurns() throws DatabaseException {
+       public static List<Item> getTurns(String libUri) throws DatabaseException {
                return Simantics.getSession().syncRequest(new Read<List<Item>>() {
                        @Override
                        public List<Item> perform(ReadGraph graph) throws DatabaseException {
                                Plant3D p3d = Plant3D.getInstance(graph);
-                               ItemQuery query = new ItemQuery(p3d.TurnComponent, Plant3D.URIs.Builtin);
+                               ItemQuery query = new ItemQuery(p3d.TurnComponent, libUri);
                                return graph.syncRequest(query);
                        }
                });
        }
        
-       public static List<Item> getInlines() throws DatabaseException {
+       public static List<Item> getInlines(String libUri) throws DatabaseException {
                return Simantics.getSession().syncRequest(new Read<List<Item>>() {
                        @Override
                        public List<Item> perform(ReadGraph graph) throws DatabaseException {
                                Plant3D p3d = Plant3D.getInstance(graph);
-                               ItemQuery query = new ItemQuery(p3d.InlineComponent, Plant3D.URIs.Builtin);
+                               ItemQuery query = new ItemQuery(p3d.InlineComponent, libUri);
                                return graph.syncRequest(query);
                        }
                });
@@ -134,12 +139,21 @@ public class P3DUtil {
                Plant3D p3d = Plant3D.getInstance(graph);
                Layer0 l0 = Layer0.getInstance(graph);
                List<Item> result = new ArrayList<Item>();
-               for (Resource r : graph.getObjects(lib, l0.ConsistsOf)) {
+               Set<Resource> processed = new HashSet<>();
+               Deque<Resource> stack = new ArrayDeque<Resource>();
+               stack.addAll(graph.getObjects(lib, l0.ConsistsOf));
+               stack.addAll(graph.getObjects(lib, p3d.ComponentLibrary_contains));
+               while (!stack.isEmpty()) {
+                   Resource r = stack.pop();
+                   if (processed.contains(r))
+                       continue;
+                   processed.add(r);
                        if (graph.isInstanceOf(r, type) ) {
                                Resource geom = graph.getPossibleObject(r,p3d.hasGeometry);
                                if (geom != null || graph.hasStatement(r,p3d.NonVisibleComponent)) {
                                        
                                        result.add(createItem(graph, r));
+                                       continue;
                                }
                        } 
                        if (graph.isInheritedFrom(r, type)) {
@@ -152,8 +166,13 @@ public class P3DUtil {
                                }
                                if (asserts) {          
                                        result.add(createItem(graph, r));
+                                       continue;
                                }
                        }
+                       if (graph.isInstanceOf(r, p3d.ComponentLibrary)) {
+                           stack.addAll(graph.getObjects(r, l0.ConsistsOf));
+                       stack.addAll(graph.getObjects(r, p3d.ComponentLibrary_contains));
+                       }
                }
                Collections.sort(result, new Comparator<Item>() {
                        @Override