1 package org.simantics.plant3d.utils;
3 import java.util.ArrayList;
4 import java.util.HashMap;
8 import org.simantics.Simantics;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.common.request.ReadRequest;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.g3d.scenegraph.GeometryProvider;
14 import org.simantics.layer0.Layer0;
15 import org.simantics.plant3d.ontology.Plant3D;
16 import org.simantics.plant3d.scenegraph.EndComponent;
17 import org.simantics.plant3d.scenegraph.Equipment;
18 import org.simantics.plant3d.scenegraph.InlineComponent;
19 import org.simantics.plant3d.scenegraph.Nozzle;
20 import org.simantics.plant3d.scenegraph.P3DRootNode;
21 import org.simantics.plant3d.scenegraph.PipeRun;
22 import org.simantics.plant3d.scenegraph.PipelineComponent;
23 import org.simantics.plant3d.scenegraph.TurnComponent;
25 public class ComponentUtils {
28 private static Map<String,Class<? extends PipelineComponent>> clazzes = new HashMap<String, Class<? extends PipelineComponent>>();
29 private static Map<String,GeometryProvider> providers = new HashMap<String,GeometryProvider>();
31 public static void preloadCache() {
32 Simantics.getSession().asyncRequest(new ReadRequest() {
35 public void run(ReadGraph graph) throws DatabaseException {
36 List<String> types = new ArrayList<String>();
37 types.add(Plant3D.URIs.Builtin_Straight);
38 types.add(Plant3D.URIs.Builtin_Elbow);
39 types.add(Plant3D.URIs.Builtin_ConcentricReducer);
40 types.add(Plant3D.URIs.Builtin_BranchSplitComponent);
41 types.add(Plant3D.URIs.Builtin_EccentricReducer);
43 for (String typeURI : types) {
50 private static GeometryProvider getProvider(ReadGraph graph, Resource type) throws DatabaseException {
52 Layer0 l0 = Layer0.getInstance(graph);
53 Plant3D p3d = Plant3D.getInstance(graph);
54 Resource geom = graph.getPossibleObject(type,p3d.hasGeometry);
56 for (Resource a : graph.getObjects(type, l0.Asserts)) {
57 if (p3d.hasGeometry.equals(graph.getPossibleObject(a, l0.HasPredicate))) {
58 geom = graph.getPossibleObject(a, l0.HasObject);
64 GeometryProvider provider = graph.adapt(geom, GeometryProvider.class);
70 private static Class<? extends PipelineComponent> getClazz(ReadGraph graph, Resource type) throws DatabaseException {
71 Plant3D p3d = Plant3D.getInstance(graph);
72 if (graph.isInheritedFrom(type, p3d.InlineComponent))
73 return InlineComponent.class;
74 if (graph.isInheritedFrom(type, p3d.TurnComponent))
75 return TurnComponent.class;
76 if (graph.isInheritedFrom(type, p3d.EndComponent))
77 return EndComponent.class;
78 if (graph.isInheritedFrom(type, p3d.Nozzle))
83 private static void load(ReadGraph graph, String typeURI) throws DatabaseException {
84 Plant3D p3d = Plant3D.getInstance(graph);
85 Resource type = graph.getResource(typeURI);
87 GeometryProvider provider = getProvider(graph, type);
88 if (provider != null || graph.hasStatement(type,p3d.NonVisibleComponent)) {
89 providers.put(typeURI, provider);
90 if (graph.isInheritedFrom(type, p3d.PipelineComponent))
91 clazzes.put(typeURI,getClazz(graph, type));
94 throw new DatabaseException("Cannot find component for " + typeURI);
97 private static void load(final String typeURI) throws DatabaseException {
98 Simantics.getSession().syncRequest(new ReadRequest() {
101 public void run(ReadGraph graph) throws DatabaseException {
108 * Creates a component
110 * Does not set the name or add the component to a piperun.
116 public static PipelineComponent createComponent(P3DRootNode root, String typeURI) throws Exception {
117 Class<? extends PipelineComponent> type = clazzes.get(typeURI);
118 GeometryProvider provider = providers.get(typeURI);
119 if (type == null || provider == null) {
121 type = clazzes.get(typeURI);
122 provider = providers.get(typeURI);
124 //PipelineComponent component = type.newInstance();
125 PipelineComponent component = null;
126 if (type == InlineComponent.class) {
127 component = root.createInline();
128 } else if (type == TurnComponent.class) {
129 component = root.createTurn();
130 } else if (type == EndComponent.class) {
131 component = root.createTurn();
132 } else if (type == Nozzle.class) {
133 component = root.createNozzle();
135 component.setType(typeURI);
136 component.setGeometry(provider);
141 * Creates a equipment
143 * Does not set the name
151 public static Equipment createEquipment(P3DRootNode root, String typeURI) throws Exception {
152 GeometryProvider provider = providers.get(typeURI);
153 if (provider == null) {
155 provider = providers.get(typeURI);
157 Equipment equipment = root.createEquipment();
158 equipment.setType(typeURI);
159 equipment.setGeometry(provider);
160 root.addChild(equipment);
164 public static InlineComponent createStraight(P3DRootNode root) throws Exception{
165 InlineComponent component = root.createInline();
166 component.setType(Plant3D.URIs.Builtin_Straight);
167 component.setGeometry(providers.get(Plant3D.URIs.Builtin_Straight));
171 public static TurnComponent createTurn(P3DRootNode root) throws Exception {
172 TurnComponent elbow = root.createTurn();
173 elbow.setType(Plant3D.URIs.Builtin_Elbow);
174 elbow.setGeometry(providers.get(Plant3D.URIs.Builtin_Elbow));
178 public static InlineComponent createReducer(P3DRootNode root) throws Exception {
179 InlineComponent component = root.createInline();
180 component.setType(Plant3D.URIs.Builtin_ConcentricReducer);
181 component.setGeometry(providers.get(Plant3D.URIs.Builtin_ConcentricReducer));
185 public static InlineComponent createBranchSplit(P3DRootNode root) throws Exception {
186 InlineComponent component = root.createInline();
187 component.setType(Plant3D.URIs.Builtin_BranchSplitComponent);
191 public static Equipment createEquipment(P3DRootNode root, Item equipmentType) throws Exception {
192 Equipment equipment = root.createEquipment();
193 equipment.setType(equipmentType.getUri());
194 String n = root.getUniqueName(equipmentType.getName());
195 equipment.setName(n);
196 root.addChild(equipment);
202 public static Nozzle createDefaultNozzle(P3DRootNode root, Equipment equipment) throws Exception {
203 return createNozzle(root, equipment, new Item(Plant3D.URIs.Builtin_Nozzle, "Nozzle"));
206 public static Nozzle createNozzle(P3DRootNode root, Equipment equipment, Item nozzleType) throws Exception {
207 Nozzle nozzle = root.createNozzle();
208 nozzle.setType(nozzleType.getUri());
209 String n = root.getUniqueName(nozzleType.getName());
211 PipeRun pipeRun = new PipeRun();
212 n = root.getUniqueName("PipeRun");
214 nozzle.setPipeRun(pipeRun);
216 equipment.addChild(nozzle);
217 root.addChild(pipeRun);
218 // root.getNodeMap().commit("Add nozzle " + n);