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.g3d.scenegraph.GeometryProvider; import org.simantics.layer0.Layer0; import org.simantics.plant3d.ontology.Plant3D; import org.simantics.plant3d.scenegraph.EndComponent; import org.simantics.plant3d.scenegraph.Equipment; import org.simantics.plant3d.scenegraph.InlineComponent; import org.simantics.plant3d.scenegraph.Nozzle; import org.simantics.plant3d.scenegraph.P3DRootNode; import org.simantics.plant3d.scenegraph.PipeRun; import org.simantics.plant3d.scenegraph.PipelineComponent; import org.simantics.plant3d.scenegraph.TurnComponent; public class ComponentUtils { private static Map> clazzes = new HashMap>(); private static Map providers = new HashMap(); public static void preloadCache() { Simantics.getSession().asyncRequest(new ReadRequest() { @Override public void run(ReadGraph graph) throws DatabaseException { List types = new ArrayList(); 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 GeometryProvider 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) { GeometryProvider provider = graph.adapt(geom, GeometryProvider.class); return provider; } return null; } private static Class 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); GeometryProvider provider = getProvider(graph, type); if (provider != null || graph.hasStatement(type,p3d.NonVisibleComponent)) { providers.put(typeURI, provider); if (graph.isInheritedFrom(type, p3d.PipelineComponent)) 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); } }); } /** * Creates a component * * Does not set the name or add the component to a piperun. * @param root * @param typeURI * @return * @throws Exception */ public static PipelineComponent createComponent(P3DRootNode root, String typeURI) throws Exception { Class type = clazzes.get(typeURI); GeometryProvider 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; } /** * Creates a equipment * * Does not set the name * * @param root * @param typeURI * @return * @throws Exception */ public static Equipment createEquipment(P3DRootNode root, String typeURI) throws Exception { GeometryProvider provider = providers.get(typeURI); if (provider == null) { load(typeURI); provider = providers.get(typeURI); } Equipment equipment = root.createEquipment(); equipment.setType(typeURI); equipment.setGeometry(provider); root.addChild(equipment); return equipment; } 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; } public static Equipment createEquipment(P3DRootNode root, Item equipmentType) throws Exception { Equipment equipment = root.createEquipment(); equipment.setType(equipmentType.getUri()); String n = root.getUniqueName(equipmentType.getName()); equipment.setName(n); root.addChild(equipment); return equipment; } public static Nozzle createDefaultNozzle(P3DRootNode root, Equipment equipment) throws Exception { return createNozzle(root, equipment, new Item(Plant3D.URIs.Builtin_Nozzle, "Nozzle")); } public static Nozzle createNozzle(P3DRootNode root, Equipment equipment, Item nozzleType) throws Exception { Nozzle nozzle = root.createNozzle(); nozzle.setType(nozzleType.getUri()); String n = root.getUniqueName(nozzleType.getName()); nozzle.setName(n); PipeRun pipeRun = new PipeRun(); n = root.getUniqueName("PipeRun"); pipeRun.setName(n); nozzle.setPipeRun(pipeRun); equipment.addChild(nozzle); root.addChild(pipeRun); // root.getNodeMap().commit("Add nozzle " + n); return nozzle; } }