X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.network.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Fui%2FDNEdgeBuilder.java;h=0df72c517f533f79e89dbee085b64819ec76cb9f;hb=refs%2Fchanges%2F52%2F2752%2F2;hp=c2238d8e56eb0e082ce7ce4482cb402a7faba869;hpb=a206ff20631f652d4d40c6da20284ee2c053037f;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNEdgeBuilder.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNEdgeBuilder.java index c2238d8e..0df72c51 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNEdgeBuilder.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/DNEdgeBuilder.java @@ -1,22 +1,24 @@ package org.simantics.district.network.ui; import java.awt.geom.Rectangle2D; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; import org.simantics.databoard.Bindings; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.exception.BindingException; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.exception.ManyObjectsForFunctionalRelationException; +import org.simantics.db.exception.ServiceException; import org.simantics.db.layer0.util.Layer0Utils; import org.simantics.diagram.stubs.DiagramResource; import org.simantics.diagram.synchronization.IModifiableSynchronizationContext; import org.simantics.diagram.synchronization.SynchronizationHints; -import org.simantics.diagram.synchronization.graph.DiagramGraphUtil; import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints; -import org.simantics.diagram.synchronization.graph.layer.GraphLayer; import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager; -import org.simantics.diagram.synchronization.graph.layer.IGraphLayerUtil; import org.simantics.district.network.DistrictNetworkUtil; import org.simantics.district.network.ontology.DistrictNetworkResource; import org.simantics.g2d.diagram.IDiagram; @@ -37,10 +39,16 @@ public class DNEdgeBuilder { } public static Resource create(WriteGraph graph, Resource diagramResource, double[] start, double[] end, double padding) throws DatabaseException { - return create(graph, diagramResource, null, start, end, padding); + Collection vertices = graph.syncRequest(new ObjectsWithType(diagramResource, Layer0.getInstance(graph).ConsistsOf, DistrictNetworkResource.getInstance(graph).Vertex)); + List vv = new ArrayList<>(vertices.size()); + for (Resource vertex : vertices) { + double[] existingCoords = graph.getRelatedValue2(vertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY); + vv.add(new ResourceVertex(vertex, existingCoords)); + } + return create(graph, vv, diagramResource, null, start, end, padding, false); } - public static Resource create(WriteGraph graph, Resource diagramResource, Resource mapping, double[] start, double[] end, double padding) throws DatabaseException { + public static Resource create(WriteGraph graph, Collection vertices, Resource diagramResource, Resource mapping, double[] start, double[] end, double padding, boolean writeElevationToEdgeFromPoints) throws DatabaseException { DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); @@ -48,26 +56,38 @@ public class DNEdgeBuilder { Resource edge = getOrCreateEdge(graph, diagramResource, mapping); // 2. Add vertices - Collection vertices = graph.syncRequest(new ObjectsWithType(diagramResource, Layer0.getInstance(graph).ConsistsOf, DistrictNetworkResource.getInstance(graph).Vertex)); - Resource startVertex = getOrCreateVertex(graph, diagramResource, vertices, start, padding); - Resource endVertex = getOrCreateVertex(graph, diagramResource, vertices, end, padding); + Resource startVertex = getOrCreateVertex(graph, diagramResource, vertices, start, padding, null); + Resource endVertex = getOrCreateVertex(graph, diagramResource, vertices, end, padding, startVertex); + if (writeElevationToEdgeFromPoints) { + graph.claimLiteral(edge, DN.Edge_HasElevation, calculateElevationFromVertices(graph, startVertex, endVertex), Bindings.DOUBLE); + } graph.claim(edge, DN.HasStartVertex, startVertex); graph.claim(edge, DN.HasEndVertex, endVertex); // We need to put GraphLayer to newLayers so... - for (Resource layer : graph.getObjects(diagramResource, DiagramResource.getInstance(graph).HasLayer)) { - IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class); - - GraphLayer gl = layerUtil.loadLayer(graph, layer); - gl.forEachTag(tag -> { - DiagramGraphUtil.tag(graph, startVertex, tag, true); - DiagramGraphUtil.tag(graph, endVertex, tag, true); - }); - } - +// for (Resource layer : graph.getObjects(diagramResource, DiagramResource.getInstance(graph).HasLayer)) { +// IGraphLayerUtil layerUtil = graph.adapt(graph.getSingleObject(layer, Layer0.getInstance(graph).InstanceOf), IGraphLayerUtil.class); +// +// GraphLayer gl = layerUtil.loadLayer(graph, layer); +// gl.forEachTag(tag -> { +// DiagramGraphUtil.tag(graph, startVertex, tag, true); +// DiagramGraphUtil.tag(graph, endVertex, tag, true); +// }); +// } +// return edge; } + private static double calculateElevationFromVertices(WriteGraph graph, Resource startVertex, Resource endVertex) throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException { + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + Double startElevation = graph.getPossibleRelatedValue(startVertex, DN.Vertex_HasElevation, Bindings.DOUBLE); + Double endElevation = graph.getPossibleRelatedValue(endVertex, DN.Vertex_HasElevation, Bindings.DOUBLE); + if (startElevation != null && endElevation != null) { + return (startElevation + endElevation) / 2; + } + return 0; + } + public void create(WriteGraph graph, double[] start, double[] end, double padding) throws DatabaseException { Resource edge = create(graph, diagramResource, start, end, padding); @@ -85,23 +105,24 @@ public class DNEdgeBuilder { glm.putElementOnVisibleLayers(diagram, graph, res); } - private static Resource getOrCreateVertex(WriteGraph graph, Resource diagramResource, Collection vertices, double[] coords, double padding) throws DatabaseException { + private static Resource getOrCreateVertex(WriteGraph graph, Resource diagramResource, Collection vertices, double[] coords, double padding, Resource startVertex) throws DatabaseException { Resource vertex = null; double halfPadding = padding / 2; double maxDistance = Double.MAX_VALUE; - for (Resource vertx : vertices) { - double[] existingCoords = graph.getRelatedValue2(vertx, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY); - Rectangle2D existing = new Rectangle2D.Double(existingCoords[0] - halfPadding, existingCoords[1] - halfPadding, padding, padding); + for (ResourceVertex vertx : vertices) { + Rectangle2D existing = new Rectangle2D.Double(vertx.coords[0] - halfPadding, vertx.coords[1] - halfPadding, padding, padding); Rectangle2D tobecreated = new Rectangle2D.Double(coords[0] - halfPadding, coords[1] - halfPadding, padding, padding); if (existing.intersects(tobecreated)) { - double dist = Math.sqrt((Math.pow(coords[0] - existingCoords[0], 2) + (Math.pow(coords[1] - existingCoords[1], 2)))); - if (dist <= maxDistance) { - vertex = vertx; + double dist = Math.sqrt((Math.pow(coords[0] - vertx.coords[0], 2) + (Math.pow(coords[1] - vertx.coords[1], 2)))); + if (dist <= maxDistance && !vertx.vertex.equals(startVertex)) { + vertex = vertx.vertex; + maxDistance = dist; } } } if (vertex == null) { vertex = DistrictNetworkUtil.createVertex(graph, diagramResource, coords); + vertices.add(new ResourceVertex(vertex, coords)); } return vertex; } @@ -110,4 +131,14 @@ public class DNEdgeBuilder { return DistrictNetworkUtil.createEdge(graph, diagramResource, mapping); } + public static class ResourceVertex { + + final Resource vertex; + final double[] coords; + + public ResourceVertex(Resource vertex, double[] coords) { + this.vertex = vertex; + this.coords = coords; + } + } }