X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.plant3d%2Fsrc%2Forg%2Fsimantics%2Fplant3d%2Futils%2FP3DUtil.java;h=42a3b9a0f5277c3ee5aff8d8e09d4790c840cd0f;hb=d515401dd50a89bb6f0f5c8d75e045a96db39f51;hp=b34f4f0739e817230b784009349433e09c5646f0;hpb=a460e609147d064dd3da464bcf1626845e0f93b4;p=simantics%2F3d.git diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/utils/P3DUtil.java b/org.simantics.plant3d/src/org/simantics/plant3d/utils/P3DUtil.java index b34f4f07..42a3b9a0 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/utils/P3DUtil.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/utils/P3DUtil.java @@ -1,28 +1,53 @@ package org.simantics.plant3d.utils; +import java.util.ArrayDeque; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.Deque; +import java.util.HashSet; import java.util.List; +import java.util.Set; import org.simantics.Simantics; import org.simantics.db.ReadGraph; +import org.simantics.db.RequestProcessor; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.db.request.Read; +import org.simantics.g3d.scenegraph.base.INode; import org.simantics.layer0.Layer0; import org.simantics.plant3d.ontology.Plant3D; +import org.simantics.plant3d.scenegraph.Equipment; +import org.simantics.plant3d.scenegraph.P3DRootNode; +import org.simantics.plant3d.scenegraph.PipeRun; +import org.simantics.plant3d.scenegraph.PipelineComponent; +import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint; +import org.simantics.plant3d.scenegraph.controlpoint.PipingRules; +import org.simantics.plant3d.scl.SCLUtil; import org.simantics.plant3d.utils.Item.Type; -import org.simantics.ui.SimanticsUI; public class P3DUtil { - public static List getEquipments() throws DatabaseException { - return Simantics.getSession().syncRequest(new Read>() { + return getEquipments(SCLUtil.getRequestProcessor()); + } + + public static List getEquipments(RequestProcessor session) throws DatabaseException { + return getEquipments(session, Plant3D.URIs.Builtin); + } + + public static List getEquipments(final String libUri) throws DatabaseException { + return getEquipments(SCLUtil.getRequestProcessor(), libUri); + } + + public static List getEquipments(RequestProcessor session, final String libUri) throws DatabaseException { + return session.syncRequest(new Read>() { @Override public List perform(ReadGraph graph) throws DatabaseException { Plant3D p3d = Plant3D.getInstance(graph); Resource project = Simantics.getProject().get(); - Resource builtins = graph.getResource(Plant3D.URIs.Builtin); + Resource builtins = graph.getResource(libUri); List actions = getItems(graph, project,p3d.Equipment); actions.addAll(getItems(graph, builtins,p3d.Equipment)); return actions; @@ -32,76 +57,128 @@ public class P3DUtil { }); } - public static List getNozzles() throws DatabaseException { - return Simantics.getSession().syncRequest(new Read>() { + public static List getNozzles(String libUri) throws DatabaseException { + return getNozzles(SCLUtil.getRequestProcessor(), libUri); + } + + public static List getNozzles(RequestProcessor session, String libUri) throws DatabaseException { + return session.syncRequest(new Read>() { @Override public List perform(ReadGraph graph) throws DatabaseException { Plant3D p3d = Plant3D.getInstance(graph); - ItemQuery query = new ItemQuery(p3d.Nozzle); + ItemQuery query = new ItemQuery(p3d.Nozzle, libUri); return graph.syncRequest(query); } }); } - private static class ItemQuery implements Read> { + public static class ItemQuery implements Read> { private Resource type; - public ItemQuery(Resource type) { + private String libUri; + public ItemQuery(Resource type, String libUri) { this.type = type; + this.libUri = libUri; } @Override public List perform(ReadGraph graph) throws DatabaseException { - Resource project = Simantics.getProject().get(); - Resource builtins = graph.getResource(Plant3D.URIs.Builtin); - List actions = getItems(graph, project,type); +// Resource project = Simantics.getProject().get(); + Resource builtins = graph.getResource(libUri); + List actions = new ArrayList<>(); +// actions.addAll(getItems(graph, project,type)); actions.addAll(getItems(graph, builtins,type)); return actions; } + + @Override + public boolean equals(Object obj) { + if (obj.getClass() != this.getClass()) + return false; + ItemQuery other = (ItemQuery)obj; + if (!type.equals(other.type)) + return false; + return libUri.equals(other.libUri); + } + + @Override + public int hashCode() { + return type.hashCode() + libUri.hashCode(); + } + } + + public static List getEnds(String libUri) throws DatabaseException { + return getEnds(SCLUtil.getRequestProcessor(), libUri); } - public static List getEnds() throws DatabaseException { - return Simantics.getSession().syncRequest(new Read>() { + public static List getEnds(RequestProcessor session, String libUri) throws DatabaseException { + return session.syncRequest(new Read>() { @Override public List perform(ReadGraph graph) throws DatabaseException { Plant3D p3d = Plant3D.getInstance(graph); - ItemQuery query = new ItemQuery(p3d.EndComponent); + ItemQuery query = new ItemQuery(p3d.EndComponent, libUri); return graph.syncRequest(query); } }); } + + public static List getTurns(String libUri) throws DatabaseException { + return getTurns(SCLUtil.getRequestProcessor(), libUri); + } - public static List getTurns() throws DatabaseException { - return Simantics.getSession().syncRequest(new Read>() { + public static List getTurns(RequestProcessor session, String libUri) throws DatabaseException { + return session.syncRequest(new Read>() { @Override public List perform(ReadGraph graph) throws DatabaseException { Plant3D p3d = Plant3D.getInstance(graph); - ItemQuery query = new ItemQuery(p3d.TurnComponent); + ItemQuery query = new ItemQuery(p3d.TurnComponent, libUri); return graph.syncRequest(query); } }); } + + public static List getInlines(String libUri) throws DatabaseException { + return getInlines(SCLUtil.getRequestProcessor(), libUri); + } - public static List getInlines() throws DatabaseException { - return Simantics.getSession().syncRequest(new Read>() { + public static List getInlines(RequestProcessor session, String libUri) throws DatabaseException { + return session.syncRequest(new Read>() { @Override public List perform(ReadGraph graph) throws DatabaseException { Plant3D p3d = Plant3D.getInstance(graph); - ItemQuery query = new ItemQuery(p3d.InlineComponent); + ItemQuery query = new ItemQuery(p3d.InlineComponent, libUri); return graph.syncRequest(query); } }); } + public static List filterUserComponents(List items) { + List result = new ArrayList(items.size()); + for (Item i : items) { + if (!i.isCode()) + result.add(i); + } + return result; + } + private static List getItems(ReadGraph graph, Resource lib, Resource type) throws DatabaseException{ Plant3D p3d = Plant3D.getInstance(graph); Layer0 l0 = Layer0.getInstance(graph); List result = new ArrayList(); - for (Resource r : graph.getObjects(lib, l0.ConsistsOf)) { + Set processed = new HashSet<>(); + Deque stack = new ArrayDeque(); + stack.addAll(graph.getObjects(lib, l0.ConsistsOf)); + stack.addAll(graph.getObjects(lib, p3d.ComponentLibrary_contains)); + while (!stack.isEmpty()) { + Resource r = stack.pop(); + if (processed.contains(r)) + continue; + processed.add(r); if (graph.isInstanceOf(r, type) ) { Resource geom = graph.getPossibleObject(r,p3d.hasGeometry); if (geom != null || graph.hasStatement(r,p3d.NonVisibleComponent)) { result.add(createItem(graph, r)); + continue; } } if (graph.isInheritedFrom(r, type)) { @@ -114,18 +191,30 @@ public class P3DUtil { } if (asserts) { result.add(createItem(graph, r)); + continue; } } + if (graph.isInstanceOf(r, p3d.ComponentLibrary)) { + stack.addAll(graph.getObjects(r, l0.ConsistsOf)); + stack.addAll(graph.getObjects(r, p3d.ComponentLibrary_contains)); + } } + Collections.sort(result, new Comparator() { + @Override + public int compare(Item o1, Item o2) { + return o1.getName().compareTo(o2.getName()); + } + }); return result; } - private static Item createItem(ReadGraph graph, Resource r) throws DatabaseException { + public static Item createItem(ReadGraph graph, Resource r) throws DatabaseException { Layer0 l0 = Layer0.getInstance(graph); Plant3D p3d = Plant3D.getInstance(graph); String name = graph.getRelatedValue(r, l0.HasName); String uri = graph.getURI(r); - Item item = new Item(uri, name); + String label = graph.getPossibleRelatedValue(r, l0.HasLabel); + Item item = new Item(uri, name, label); if (graph.isInstanceOf(r, p3d.Equipment)) item.setType(Type.EQUIPMENT); else if (graph.isInstanceOf(r, p3d.InlineComponent)) @@ -142,10 +231,14 @@ public class P3DUtil { if (graph.hasStatement(r, p3d.CodeComponent)) item.setCode(true); if (graph.hasStatement(r, p3d.VariableAngleTurnComponent) || - graph.hasStatement(r, p3d.VariableLengthInlineComponent)) + graph.hasStatement(r, p3d.VariableLengthInlineComponent)) item.setVariable(true); + if (graph.hasStatement(r, p3d.AdjustableLengthInlineComponent)) + item.setModifiable(true); if (graph.hasStatement(r, p3d.SizeChangeComponent)) item.setSizeChange(true); + if (graph.hasStatement(r, p3d.RotateComponent)) + item.setRotated(true); return item; } @@ -158,5 +251,49 @@ public class P3DUtil { return model; } + + public static void finalizeDBLoad(P3DRootNode rootNode) throws Exception{ + for (INode node : rootNode.getChild()) { + if (node instanceof PipeRun) { + for (PipelineComponent pc : ((PipeRun) node).getChild()) { + pc.sync(); + pc.updateParameters(); + } + } else if (node instanceof Equipment) { + for (PipelineComponent pc : ((Equipment) node).getChild()) { + pc.sync(); + pc.updateParameters(); + } + } + } + + for (INode node : rootNode.getChild()) { + if (node instanceof PipeRun) { + for (PipelineComponent pc : ((PipeRun) node).getChild()) + pc.sync2(); + } else if (node instanceof Equipment) { + for (PipelineComponent pc : ((Equipment) node).getChild()) + pc.sync2(); + } + } + for (INode node : rootNode.getChild()) { + if (node instanceof PipeRun) { + PipingRules.validate((PipeRun)node); + } + } + } + + public static void finalizeDBLoad2(P3DRootNode rootNode) throws Exception { + PipingRules.setEnabled(true); + for (INode node : rootNode.getChild()) { + if (node instanceof PipeRun) { + PipeRun run = (PipeRun) node; + for (PipeControlPoint pcp : run.getControlPoints()) + if (pcp.asPathLegEnd()) + PipingRules.requestUpdate(pcp); + } + } + PipingRules.update(); + } }