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=eb0814450768ed5de2651dfee24f81c94a0cb108;hb=02ecca5e61d2eb17de40cc058be678b414aaad00;hp=ebdb9e87adde5dd422eeedcac5dea37c5fea5d97;hpb=1bc60c2213f9b3fc7b4d935ba9afda2b767290e5;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 ebdb9e87..eb081445 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 @@ -12,9 +12,11 @@ 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.AddElement; +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; @@ -34,29 +36,40 @@ public class DNEdgeBuilder { glm = context.get(GraphSynchronizationHints.GRAPH_LAYER_MANAGER); } - public void create(WriteGraph graph, double[] start, double[] end, double padding) throws DatabaseException { + public static Resource create(WriteGraph graph, Resource diagramResource, double[] start, double[] end, double padding) throws DatabaseException { DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); // 1. Get diagram edge to construct - Resource edge = getOrCreateEdge(graph); - - // 1.1 Give running name to connection and increment the counter attached to the diagram. - AddElement.claimFreshElementName(graph, diagramResource, edge); + Resource edge = getOrCreateEdge(graph, diagramResource); // 2. Add vertices Collection vertices = graph.syncRequest(new ObjectsWithType(diagramResource, Layer0.getInstance(graph).ConsistsOf, DistrictNetworkResource.getInstance(graph).Vertex)); - Resource startVertex = getOrCreateVertex(graph, vertices, start, padding); - Resource endVertex = getOrCreateVertex(graph, vertices, end, padding); + Resource startVertex = getOrCreateVertex(graph, diagramResource, vertices, start, padding); + Resource endVertex = getOrCreateVertex(graph, diagramResource, vertices, end, padding); 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); + }); + } + + return edge; + } + public void create(WriteGraph graph, double[] start, double[] end, double padding) throws DatabaseException { + + Resource edge = create(graph, diagramResource, start, end, padding); // 7. Put the element on all the currently active layers if possible. if (glm != null) { putOnActiveLayer(graph, edge); - putOnActiveLayer(graph, startVertex); - putOnActiveLayer(graph, endVertex); } Layer0Utils.addCommentMetadata(graph, "Added edge " + edge); @@ -68,26 +81,28 @@ public class DNEdgeBuilder { glm.putElementOnVisibleLayers(diagram, graph, res); } - private Resource getOrCreateVertex(WriteGraph graph, Collection vertices, double[] coords, double padding) throws DatabaseException { + private static Resource getOrCreateVertex(WriteGraph graph, Resource diagramResource, Collection vertices, double[] coords, double padding) 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); Rectangle2D tobecreated = new Rectangle2D.Double(coords[0] - halfPadding, coords[1] - halfPadding, padding, padding); if (existing.intersects(tobecreated)) { - vertex = vertx; - break; + double dist = Math.sqrt((Math.pow(coords[0] - existingCoords[0], 2) + (Math.pow(coords[1] - existingCoords[1], 2)))); + if (dist <= maxDistance) { + vertex = vertx; + } } } if (vertex == null) { vertex = DistrictNetworkUtil.createVertex(graph, diagramResource, coords); - AddElement.claimFreshElementName(graph, diagramResource, vertex); } return vertex; } - private Resource getOrCreateEdge(WriteGraph graph) throws DatabaseException { + private static Resource getOrCreateEdge(WriteGraph graph, Resource diagramResource) throws DatabaseException { return DistrictNetworkUtil.createEdge(graph, diagramResource); }