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 clazzes.put(typeURI,getClazz(graph, type));
93 throw new DatabaseException("Cannot find component for " + typeURI);
96 private static void load(final String typeURI) throws DatabaseException {
97 Simantics.getSession().syncRequest(new ReadRequest() {
100 public void run(ReadGraph graph) throws DatabaseException {
106 public static PipelineComponent createComponent(P3DRootNode root, String typeURI) throws Exception {
107 Class<? extends PipelineComponent> type = clazzes.get(typeURI);
108 GeometryProvider provider = providers.get(typeURI);
109 if (type == null || provider == null) {
111 type = clazzes.get(typeURI);
112 provider = providers.get(typeURI);
114 //PipelineComponent component = type.newInstance();
115 PipelineComponent component = null;
116 if (type == InlineComponent.class) {
117 component = root.createInline();
118 } else if (type == TurnComponent.class) {
119 component = root.createTurn();
120 } else if (type == EndComponent.class) {
121 component = root.createTurn();
122 } else if (type == Nozzle.class) {
123 component = root.createNozzle();
125 component.setType(typeURI);
126 component.setGeometry(provider);
130 public static InlineComponent createStraight(P3DRootNode root) throws Exception{
131 InlineComponent component = root.createInline();
132 component.setType(Plant3D.URIs.Builtin_Straight);
133 component.setGeometry(providers.get(Plant3D.URIs.Builtin_Straight));
137 public static TurnComponent createTurn(P3DRootNode root) throws Exception {
138 TurnComponent elbow = root.createTurn();
139 elbow.setType(Plant3D.URIs.Builtin_Elbow);
140 elbow.setGeometry(providers.get(Plant3D.URIs.Builtin_Elbow));
144 public static InlineComponent createReducer(P3DRootNode root) throws Exception {
145 InlineComponent component = root.createInline();
146 component.setType(Plant3D.URIs.Builtin_ConcentricReducer);
147 component.setGeometry(providers.get(Plant3D.URIs.Builtin_ConcentricReducer));
151 public static InlineComponent createBranchSplit(P3DRootNode root) throws Exception {
152 InlineComponent component = root.createInline();
153 component.setType(Plant3D.URIs.Builtin_BranchSplitComponent);
157 public static Equipment createEquipment(P3DRootNode root, Item equipmentType) throws Exception {
158 Equipment equipment = root.createEquipment();
159 equipment.setType(equipmentType.getUri());
160 String n = root.getUniqueName(equipmentType.getName());
161 equipment.setName(n);
162 root.addChild(equipment);
166 public static Nozzle createDefaultNozzle(P3DRootNode root, Equipment equipment) throws Exception {
167 return createNozzle(root, equipment, new Item(Plant3D.URIs.Builtin_Nozzle, "Nozzle"));
170 public static Nozzle createNozzle(P3DRootNode root, Equipment equipment, Item nozzleType) throws Exception {
171 Nozzle nozzle = root.createNozzle();
172 nozzle.setType(nozzleType.getUri());
173 String n = root.getUniqueName(nozzleType.getName());
175 PipeRun pipeRun = new PipeRun();
176 n = root.getUniqueName("PipeRun");
178 nozzle.setPipeRun(pipeRun);
180 equipment.addChild(nozzle);
181 root.addChild(pipeRun);
182 // root.getNodeMap().commit("Add nozzle " + n);