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.InlineComponent;
18 import org.simantics.plant3d.scenegraph.Nozzle;
19 import org.simantics.plant3d.scenegraph.P3DRootNode;
20 import org.simantics.plant3d.scenegraph.PipelineComponent;
21 import org.simantics.plant3d.scenegraph.TurnComponent;
23 public class ComponentUtils {
26 private static Map<String,Class<? extends PipelineComponent>> clazzes = new HashMap<String, Class<? extends PipelineComponent>>();
27 private static Map<String,GeometryProvider> providers = new HashMap<String,GeometryProvider>();
29 public static void preloadCache() {
30 Simantics.getSession().asyncRequest(new ReadRequest() {
33 public void run(ReadGraph graph) throws DatabaseException {
34 List<String> types = new ArrayList<String>();
35 types.add(Plant3D.URIs.Builtin_Straight);
36 types.add(Plant3D.URIs.Builtin_Elbow);
37 types.add(Plant3D.URIs.Builtin_ConcentricReducer);
38 types.add(Plant3D.URIs.Builtin_BranchSplitComponent);
39 types.add(Plant3D.URIs.Builtin_EccentricReducer);
41 for (String typeURI : types) {
48 private static GeometryProvider getProvider(ReadGraph graph, Resource type) throws DatabaseException {
50 Layer0 l0 = Layer0.getInstance(graph);
51 Plant3D p3d = Plant3D.getInstance(graph);
52 Resource geom = graph.getPossibleObject(type,p3d.hasGeometry);
54 for (Resource a : graph.getObjects(type, l0.Asserts)) {
55 if (p3d.hasGeometry.equals(graph.getPossibleObject(a, l0.HasPredicate))) {
56 geom = graph.getPossibleObject(a, l0.HasObject);
62 GeometryProvider provider = graph.adapt(geom, GeometryProvider.class);
68 private static Class<? extends PipelineComponent> getClazz(ReadGraph graph, Resource type) throws DatabaseException {
69 Plant3D p3d = Plant3D.getInstance(graph);
70 if (graph.isInheritedFrom(type, p3d.InlineComponent))
71 return InlineComponent.class;
72 if (graph.isInheritedFrom(type, p3d.TurnComponent))
73 return TurnComponent.class;
74 if (graph.isInheritedFrom(type, p3d.EndComponent))
75 return EndComponent.class;
76 if (graph.isInheritedFrom(type, p3d.Nozzle))
81 private static void load(ReadGraph graph, String typeURI) throws DatabaseException {
82 Plant3D p3d = Plant3D.getInstance(graph);
83 Resource type = graph.getResource(typeURI);
85 GeometryProvider provider = getProvider(graph, type);
86 if (provider != null || graph.hasStatement(type,p3d.NonVisibleComponent)) {
87 providers.put(typeURI, provider);
88 ComponentUtils.clazzes.put(typeURI,getClazz(graph, type));
91 throw new DatabaseException("Cannot find component for " + typeURI);
94 private static void load(final String typeURI) throws DatabaseException {
95 Simantics.getSession().syncRequest(new ReadRequest() {
98 public void run(ReadGraph graph) throws DatabaseException {
104 public static PipelineComponent createComponent(P3DRootNode root, String typeURI) throws Exception {
105 Class<? extends PipelineComponent> type = clazzes.get(typeURI);
106 GeometryProvider provider = providers.get(typeURI);
107 if (type == null || provider == null) {
109 type = clazzes.get(typeURI);
110 provider = providers.get(typeURI);
112 //PipelineComponent component = type.newInstance();
113 PipelineComponent component = null;
114 if (type == InlineComponent.class) {
115 component = root.createInline();
116 } else if (type == TurnComponent.class) {
117 component = root.createTurn();
118 } else if (type == EndComponent.class) {
119 component = root.createTurn();
120 } else if (type == Nozzle.class) {
121 component = root.createNozzle();
123 component.setType(typeURI);
124 component.setGeometry(provider);
128 public static InlineComponent createStraight(P3DRootNode root) throws Exception{
129 InlineComponent component = root.createInline();
130 component.setType(Plant3D.URIs.Builtin_Straight);
131 component.setGeometry(providers.get(Plant3D.URIs.Builtin_Straight));
135 public static TurnComponent createTurn(P3DRootNode root) throws Exception {
136 TurnComponent elbow = root.createTurn();
137 elbow.setType(Plant3D.URIs.Builtin_Elbow);
138 elbow.setGeometry(providers.get(Plant3D.URIs.Builtin_Elbow));
142 public static InlineComponent createReducer(P3DRootNode root) throws Exception {
143 InlineComponent component = root.createInline();
144 component.setType(Plant3D.URIs.Builtin_ConcentricReducer);
145 component.setGeometry(providers.get(Plant3D.URIs.Builtin_ConcentricReducer));
149 public static InlineComponent createBranchSplit(P3DRootNode root) throws Exception {
150 InlineComponent component = root.createInline();
151 component.setType(Plant3D.URIs.Builtin_BranchSplitComponent);