--- /dev/null
+package org.simantics.plant3d.utils;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.simantics.Simantics;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.layer0.Layer0;
+import org.simantics.opencascade.SolidModelProvider;
+import org.simantics.plant3d.ontology.Plant3D;
+import org.simantics.plant3d.scenegraph.EndComponent;
+import org.simantics.plant3d.scenegraph.InlineComponent;
+import org.simantics.plant3d.scenegraph.Nozzle;
+import org.simantics.plant3d.scenegraph.P3DRootNode;
+import org.simantics.plant3d.scenegraph.PipelineComponent;
+import org.simantics.plant3d.scenegraph.TurnComponent;
+
+public class ComponentUtils {
+
+
+ private static Map<String,Class<? extends PipelineComponent>> clazzes = new HashMap<String, Class<? extends PipelineComponent>>();
+ private static Map<String,SolidModelProvider> providers = new HashMap<String,SolidModelProvider>();
+
+ public static void preloadCache() {
+ Simantics.getSession().asyncRequest(new ReadRequest() {
+
+ @Override
+ public void run(ReadGraph graph) throws DatabaseException {
+ List<String> types = new ArrayList<String>();
+ types.add(Plant3D.URIs.Builtin_Straight);
+ types.add(Plant3D.URIs.Builtin_Elbow);
+ types.add(Plant3D.URIs.Builtin_ConcentricReducer);
+ types.add(Plant3D.URIs.Builtin_BranchSplitComponent);
+// types.add(Plant3D.URIs.Builtin_EccentricReducer);
+
+ for (String typeURI : types) {
+ load(graph, typeURI);
+ }
+ }
+ });
+ }
+
+ private static SolidModelProvider getProvider(ReadGraph graph, Resource type) throws DatabaseException {
+
+ Layer0 l0 = Layer0.getInstance(graph);
+ Plant3D p3d = Plant3D.getInstance(graph);
+ Resource geom = graph.getPossibleObject(type,p3d.hasGeometry);
+ if (geom == null) {
+ for (Resource a : graph.getObjects(type, l0.Asserts)) {
+ if (p3d.hasGeometry.equals(graph.getPossibleObject(a, l0.HasPredicate))) {
+ geom = graph.getPossibleObject(a, l0.HasObject);
+ break;
+ }
+ }
+ }
+ if (geom != null) {
+ SolidModelProvider provider = graph.adapt(geom, SolidModelProvider.class);
+ return provider;
+ }
+ return null;
+ }
+
+ private static Class<? extends PipelineComponent> getClazz(ReadGraph graph, Resource type) throws DatabaseException {
+ Plant3D p3d = Plant3D.getInstance(graph);
+ if (graph.isInheritedFrom(type, p3d.InlineComponent))
+ return InlineComponent.class;
+ if (graph.isInheritedFrom(type, p3d.TurnComponent))
+ return TurnComponent.class;
+ if (graph.isInheritedFrom(type, p3d.EndComponent))
+ return EndComponent.class;
+ if (graph.isInheritedFrom(type, p3d.Nozzle))
+ return Nozzle.class;
+ return null;
+ }
+
+ private static void load(ReadGraph graph, String typeURI) throws DatabaseException {
+ Plant3D p3d = Plant3D.getInstance(graph);
+ Resource type = graph.getResource(typeURI);
+
+ SolidModelProvider provider = getProvider(graph, type);
+ if (provider != null || graph.hasStatement(type,p3d.NonVisibleComponent)) {
+ providers.put(typeURI, provider);
+ ComponentUtils.clazzes.put(typeURI,getClazz(graph, type));
+ return;
+ }
+ throw new DatabaseException("Cannot find component for " + typeURI);
+ }
+
+ private static void load(final String typeURI) throws DatabaseException {
+ Simantics.getSession().syncRequest(new ReadRequest() {
+
+ @Override
+ public void run(ReadGraph graph) throws DatabaseException {
+ load(graph,typeURI);
+ }
+ });
+ }
+
+ public static PipelineComponent createComponent(P3DRootNode root, String typeURI) throws Exception {
+ Class<? extends PipelineComponent> type = clazzes.get(typeURI);
+ SolidModelProvider provider = providers.get(typeURI);
+ if (type == null || provider == null) {
+ load(typeURI);
+ type = clazzes.get(typeURI);
+ provider = providers.get(typeURI);
+ }
+ //PipelineComponent component = type.newInstance();
+ PipelineComponent component = null;
+ if (type == InlineComponent.class) {
+ component = root.createInline();
+ } else if (type == TurnComponent.class) {
+ component = root.createTurn();
+ } else if (type == EndComponent.class) {
+ component = root.createTurn();
+ } else if (type == Nozzle.class) {
+ component = root.createNozzle();
+ }
+ component.setType(typeURI);
+ component.setGeometry(provider);
+ return component;
+ }
+
+ public static InlineComponent createStraight(P3DRootNode root) throws Exception{
+ InlineComponent component = root.createInline();
+ component.setType(Plant3D.URIs.Builtin_Straight);
+ component.setGeometry(providers.get(Plant3D.URIs.Builtin_Straight));
+ return component;
+ }
+
+ public static TurnComponent createTurn(P3DRootNode root) throws Exception {
+ TurnComponent elbow = root.createTurn();
+ elbow.setType(Plant3D.URIs.Builtin_Elbow);
+ elbow.setGeometry(providers.get(Plant3D.URIs.Builtin_Elbow));
+ return elbow;
+ }
+
+ public static InlineComponent createReducer(P3DRootNode root) throws Exception {
+ InlineComponent component = root.createInline();
+ component.setType(Plant3D.URIs.Builtin_ConcentricReducer);
+ component.setGeometry(providers.get(Plant3D.URIs.Builtin_ConcentricReducer));
+ return component;
+ }
+
+ public static InlineComponent createBranchSplit(P3DRootNode root) throws Exception {
+ InlineComponent component = root.createInline();
+ component.setType(Plant3D.URIs.Builtin_BranchSplitComponent);
+ return component;
+ }
+}