]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.plant3d/src/org/simantics/plant3d/utils/P3DUtil.java
Add units to ComponentSelectionDialog
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / utils / P3DUtil.java
index 71f844d866329fe2dac458d7e9c468c6eb28ce33..42a3b9a0f5277c3ee5aff8d8e09d4790c840cd0f 100644 (file)
@@ -1,12 +1,17 @@
 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;
+import org.simantics.db.RequestProcessor;
 import org.simantics.db.Resource;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.exception.DatabaseException;
@@ -20,16 +25,24 @@ import org.simantics.plant3d.scenegraph.PipeRun;
 import org.simantics.plant3d.scenegraph.PipelineComponent;
 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
+import org.simantics.plant3d.scl.SCLUtil;
 import org.simantics.plant3d.utils.Item.Type;
 
 public class P3DUtil {
-       
        public static List<Item> getEquipments() throws DatabaseException {
-               return getEquipments(Plant3D.URIs.Builtin);
+               return getEquipments(SCLUtil.getRequestProcessor());
        }
        
+       public static List<Item> getEquipments(RequestProcessor session) throws DatabaseException {
+               return getEquipments(session, Plant3D.URIs.Builtin);
+       }
+
        public static List<Item> getEquipments(final String libUri) throws DatabaseException {
-               return Simantics.getSession().syncRequest(new Read<List<Item>>() {
+               return getEquipments(SCLUtil.getRequestProcessor(), libUri);
+       }
+       
+       public static List<Item> getEquipments(RequestProcessor session, final String libUri) throws DatabaseException {
+               return session.syncRequest(new Read<List<Item>>() {
                        @Override
                        public List<Item> perform(ReadGraph graph) throws DatabaseException {
                                Plant3D p3d = Plant3D.getInstance(graph);
@@ -44,12 +57,16 @@ public class P3DUtil {
                });
        }
        
-       public static List<Item> getNozzles() throws DatabaseException {
-               return Simantics.getSession().syncRequest(new Read<List<Item>>() {
+       public static List<Item> getNozzles(String libUri) throws DatabaseException {
+               return getNozzles(SCLUtil.getRequestProcessor(), libUri);
+       }
+       
+       public static List<Item> getNozzles(RequestProcessor session, String libUri) throws DatabaseException {
+               return session.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);
                        }
                });
@@ -65,9 +82,10 @@ public class P3DUtil {
                
                @Override
                public List<Item> perform(ReadGraph graph) throws DatabaseException {
-                       Resource project = Simantics.getProject().get();
+//                     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;
                }
@@ -87,35 +105,47 @@ public class P3DUtil {
                        return type.hashCode() + libUri.hashCode();
                }
        }
+
+       public static List<Item> getEnds(String libUri) throws DatabaseException {
+               return getEnds(SCLUtil.getRequestProcessor(), libUri);
+       }
        
-       public static List<Item> getEnds() throws DatabaseException {
-               return Simantics.getSession().syncRequest(new Read<List<Item>>() {
+       public static List<Item> getEnds(RequestProcessor session, String libUri) throws DatabaseException {
+               return session.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(String libUri) throws DatabaseException {
+               return getTurns(SCLUtil.getRequestProcessor(), libUri);
+       }
        
-       public static List<Item> getTurns() throws DatabaseException {
-               return Simantics.getSession().syncRequest(new Read<List<Item>>() {
+       public static List<Item> getTurns(RequestProcessor session, String libUri) throws DatabaseException {
+               return session.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(String libUri) throws DatabaseException {
+               return getInlines(SCLUtil.getRequestProcessor(), libUri);
+       }
        
-       public static List<Item> getInlines() throws DatabaseException {
-               return Simantics.getSession().syncRequest(new Read<List<Item>>() {
+       public static List<Item> getInlines(RequestProcessor session, String libUri) throws DatabaseException {
+               return session.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 +164,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 +191,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
@@ -169,7 +213,8 @@ public class P3DUtil {
                Plant3D p3d = Plant3D.getInstance(graph);
                String name = graph.getRelatedValue(r, l0.HasName);
                String uri = graph.getURI(r);
-               Item item = new Item(uri, name);
+               String label = graph.getPossibleRelatedValue(r, l0.HasLabel);
+               Item item = new Item(uri, name, label);
                if (graph.isInstanceOf(r, p3d.Equipment))
                        item.setType(Type.EQUIPMENT);
                else if (graph.isInstanceOf(r, p3d.InlineComponent))
@@ -188,8 +233,12 @@ public class P3DUtil {
                if (graph.hasStatement(r, p3d.VariableAngleTurnComponent) ||
                        graph.hasStatement(r, p3d.VariableLengthInlineComponent))
                        item.setVariable(true);
+               if (graph.hasStatement(r, p3d.AdjustableLengthInlineComponent))
+                   item.setModifiable(true);
                if (graph.hasStatement(r, p3d.SizeChangeComponent))
                        item.setSizeChange(true);
+               if (graph.hasStatement(r, p3d.RotateComponent))
+            item.setRotated(true);
                return item;
        }
        
@@ -206,11 +255,15 @@ public class P3DUtil {
        public static void finalizeDBLoad(P3DRootNode rootNode) throws Exception{
                for (INode node : rootNode.getChild()) {
                        if (node instanceof PipeRun) {
-                               for (PipelineComponent pc : ((PipeRun) node).getChild())
+                               for (PipelineComponent pc : ((PipeRun) node).getChild()) {
                                        pc.sync();
+                                       pc.updateParameters();
+                               }
                        } else if (node instanceof Equipment) {
-                               for (PipelineComponent pc : ((Equipment) node).getChild())
+                               for (PipelineComponent pc : ((Equipment) node).getChild()) {
                                        pc.sync();
+                                       pc.updateParameters();
+                               }
                        }
                }
                
@@ -230,16 +283,17 @@ public class P3DUtil {
                }
        }
        
-   public static void finalizeDBLoad2(P3DRootNode rootNode) throws Exception{
-       PipingRules.setEnabled(true);
+    public static void finalizeDBLoad2(P3DRootNode rootNode) throws Exception {
+        PipingRules.setEnabled(true);
         for (INode node : rootNode.getChild()) {
             if (node instanceof PipeRun) {
-                PipeRun run = (PipeRun)node;
+                PipeRun run = (PipeRun) node;
                 for (PipeControlPoint pcp : run.getControlPoints())
-                    PipingRules.positionUpdate(pcp);
-                    //PipingRules.requestUpdate(pcp);
+                    if (pcp.asPathLegEnd())
+                        PipingRules.requestUpdate(pcp);
             }
         }
-   }
+        PipingRules.update();
+    }
 
 }