X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.plant3d%2Fsrc%2Forg%2Fsimantics%2Fplant3d%2Futils%2FComponentUtils.java;fp=org.simantics.plant3d%2Fsrc%2Forg%2Fsimantics%2Fplant3d%2Futils%2FComponentUtils.java;h=af64f180b5d1fde1fb14a70cd02f76ccce91f8b0;hb=a460e609147d064dd3da464bcf1626845e0f93b4;hp=0000000000000000000000000000000000000000;hpb=3e2205a22ddee674f7936da114f5d520bd83e0ff;p=simantics%2F3d.git diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/utils/ComponentUtils.java b/org.simantics.plant3d/src/org/simantics/plant3d/utils/ComponentUtils.java new file mode 100644 index 00000000..af64f180 --- /dev/null +++ b/org.simantics.plant3d/src/org/simantics/plant3d/utils/ComponentUtils.java @@ -0,0 +1,154 @@ +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> 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 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 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 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; + } +}