]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/utils/P3DUtil.java
SCL bindings to some G3D and Plant3D Java classes
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / utils / P3DUtil.java
1 package org.simantics.plant3d.utils;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.List;
7
8 import org.simantics.Simantics;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.WriteGraph;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.request.Read;
14 import org.simantics.g3d.scenegraph.base.INode;
15 import org.simantics.layer0.Layer0;
16 import org.simantics.plant3d.ontology.Plant3D;
17 import org.simantics.plant3d.scenegraph.Equipment;
18 import org.simantics.plant3d.scenegraph.P3DRootNode;
19 import org.simantics.plant3d.scenegraph.PipeRun;
20 import org.simantics.plant3d.scenegraph.PipelineComponent;
21 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
22 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
23 import org.simantics.plant3d.utils.Item.Type;
24
25 public class P3DUtil {
26         
27         public static List<Item> getEquipments() throws DatabaseException {
28                 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
29                         @Override
30                         public List<Item> perform(ReadGraph graph) throws DatabaseException {
31                                 Plant3D p3d = Plant3D.getInstance(graph);
32                                 Resource project = Simantics.getProject().get();
33                                 Resource builtins = graph.getResource(Plant3D.URIs.Builtin);
34                                 List<Item> actions = getItems(graph, project,p3d.Equipment);
35                                 actions.addAll(getItems(graph, builtins,p3d.Equipment));
36                                 return actions;
37                         }
38                         
39                         
40                 });
41         }
42         
43         public static List<Item> getNozzles() throws DatabaseException {
44                 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
45                         @Override
46                         public List<Item> perform(ReadGraph graph) throws DatabaseException {
47                                 Plant3D p3d = Plant3D.getInstance(graph);
48                                 ItemQuery query = new ItemQuery(p3d.Nozzle);
49                                 return graph.syncRequest(query);
50                         }
51                 });
52         }
53         
54         private static class ItemQuery implements Read<List<Item>> {
55                 private Resource type;
56                 public ItemQuery(Resource type) {
57                         this.type = type;
58                 }
59                 
60                 @Override
61                 public List<Item> perform(ReadGraph graph) throws DatabaseException {
62                         Resource project = Simantics.getProject().get();
63                         Resource builtins = graph.getResource(Plant3D.URIs.Builtin);
64                         List<Item> actions = getItems(graph, project,type);
65                         actions.addAll(getItems(graph, builtins,type));
66                         return actions;
67                 }
68         }
69         
70         public static List<Item> getEnds() throws DatabaseException {
71                 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
72                         @Override
73                         public List<Item> perform(ReadGraph graph) throws DatabaseException {
74                                 Plant3D p3d = Plant3D.getInstance(graph);
75                                 ItemQuery query = new ItemQuery(p3d.EndComponent);
76                                 return graph.syncRequest(query);
77                         }
78                 });
79         }
80         
81         public static List<Item> getTurns() throws DatabaseException {
82                 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
83                         @Override
84                         public List<Item> perform(ReadGraph graph) throws DatabaseException {
85                                 Plant3D p3d = Plant3D.getInstance(graph);
86                                 ItemQuery query = new ItemQuery(p3d.TurnComponent);
87                                 return graph.syncRequest(query);
88                         }
89                 });
90         }
91         
92         public static List<Item> getInlines() throws DatabaseException {
93                 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
94                         @Override
95                         public List<Item> perform(ReadGraph graph) throws DatabaseException {
96                                 Plant3D p3d = Plant3D.getInstance(graph);
97                                 ItemQuery query = new ItemQuery(p3d.InlineComponent);
98                                 return graph.syncRequest(query);
99                         }
100                 });
101         }
102         
103         public static List<Item> filterUserComponents(List<Item> items) {
104             List<Item> result = new ArrayList<Item>(items.size());
105             for (Item i : items) {
106                 if (!i.isCode())
107                     result.add(i);
108             }
109             return result;
110         }
111         
112         private static List<Item> getItems(ReadGraph graph, Resource lib, Resource type) throws DatabaseException{
113                 Plant3D p3d = Plant3D.getInstance(graph);
114                 Layer0 l0 = Layer0.getInstance(graph);
115                 List<Item> result = new ArrayList<Item>();
116                 for (Resource r : graph.getObjects(lib, l0.ConsistsOf)) {
117                         if (graph.isInstanceOf(r, type) ) {
118                                 Resource geom = graph.getPossibleObject(r,p3d.hasGeometry);
119                                 if (geom != null || graph.hasStatement(r,p3d.NonVisibleComponent)) {
120                                         
121                                         result.add(createItem(graph, r));
122                                 }
123                         } 
124                         if (graph.isInheritedFrom(r, type)) {
125                                 boolean asserts = false;
126                                 for (Resource a : graph.getObjects(r, l0.Asserts)) {
127                                         if (p3d.hasGeometry.equals(graph.getPossibleObject(a, l0.HasPredicate))) {
128                                                 asserts = true;
129                                                 break;
130                                         }
131                                 }
132                                 if (asserts) {          
133                                         result.add(createItem(graph, r));
134                                 }
135                         }
136                 }
137                 Collections.sort(result, new Comparator<Item>() {
138                     @Override
139                     public int compare(Item o1, Item o2) {
140                         return o1.getName().compareTo(o2.getName());
141                     }
142         });
143                 return result;
144         }
145         
146         public static Item createItem(ReadGraph graph, Resource r) throws DatabaseException {
147                 Layer0 l0 = Layer0.getInstance(graph);
148                 Plant3D p3d = Plant3D.getInstance(graph);
149                 String name = graph.getRelatedValue(r, l0.HasName);
150                 String uri = graph.getURI(r);
151                 Item item = new Item(uri, name);
152                 if (graph.isInstanceOf(r, p3d.Equipment))
153                         item.setType(Type.EQUIPMENT);
154                 else if (graph.isInstanceOf(r, p3d.InlineComponent))
155                         item.setType(Type.INLINE);
156                 else if (graph.isInstanceOf(r, p3d.EndComponent))
157                         item.setType(Type.END);
158                 else if (graph.isInstanceOf(r, p3d.TurnComponent))
159                         item.setType(Type.TURN);
160                 else if (graph.isInstanceOf(r, p3d.Nozzle))
161                         item.setType(Type.NOZZLE);
162                 else 
163                         throw new RuntimeException("Cannot detect type for " + r);
164                 
165                 if (graph.hasStatement(r, p3d.CodeComponent))
166                         item.setCode(true);
167                 if (graph.hasStatement(r, p3d.VariableAngleTurnComponent) ||
168                     graph.hasStatement(r, p3d.VariableLengthInlineComponent))
169                         item.setVariable(true);
170                 if (graph.hasStatement(r, p3d.SizeChangeComponent))
171                         item.setSizeChange(true);
172                 return item;
173         }
174         
175         public static Resource createModel(WriteGraph graph, String name) throws DatabaseException{
176                 Layer0 l0 = Layer0.getInstance(graph);
177                 Plant3D p3d = Plant3D.getInstance(graph);
178                 Resource model = graph.newResource();
179                 graph.claim(model, l0.InstanceOf, p3d.Plant);
180                 graph.claimLiteral(model, l0.HasName, name);
181                 
182                 return model;
183         }
184         
185         public static void finalizeDBLoad(P3DRootNode rootNode) throws Exception{
186             for (INode node : rootNode.getChild()) {
187             if (node instanceof PipeRun) {
188                 for (PipelineComponent pc : ((PipeRun) node).getChild())
189                     pc.sync();
190             } else if (node instanceof Equipment) {
191                 for (PipelineComponent pc : ((Equipment) node).getChild())
192                     pc.sync();
193             }
194         }
195         
196         for (INode node : rootNode.getChild()) {
197             if (node instanceof PipeRun) {
198                 for (PipelineComponent pc : ((PipeRun) node).getChild())
199                     pc.sync2();
200             } else if (node instanceof Equipment) {
201                 for (PipelineComponent pc : ((Equipment) node).getChild())
202                     pc.sync2();
203             }
204         }
205         for (INode node : rootNode.getChild()) {
206             if (node instanceof PipeRun) {
207                 PipingRules.validate((PipeRun)node);
208             }
209         }
210         PipingRules.setEnabled(true);
211         for (INode node : rootNode.getChild()) {
212             if (node instanceof PipeRun) {
213                 PipeRun run = (PipeRun)node;
214                 for (PipeControlPoint pcp : run.getControlPoints())
215                     PipingRules.positionUpdate(pcp);        
216             }
217         }
218         }
219
220 }