1 package org.simantics.district.region;
4 import java.awt.geom.FlatteningPathIterator;
5 import java.awt.geom.Path2D;
6 import java.awt.geom.PathIterator;
7 import java.util.ArrayList;
8 import java.util.Arrays;
9 import java.util.Collection;
10 import java.util.HashSet;
11 import java.util.List;
13 import java.util.UUID;
15 import org.simantics.Simantics;
16 import org.simantics.databoard.Bindings;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.WriteGraph;
20 import org.simantics.db.common.request.UniqueRead;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.layer0.QueryIndexUtils;
23 import org.simantics.db.layer0.request.PossibleActiveModel;
24 import org.simantics.db.layer0.util.RemoverUtil;
25 import org.simantics.diagram.stubs.DiagramResource;
26 import org.simantics.district.region.ontology.DiagramRegionsResource;
27 import org.simantics.layer0.Layer0;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public final class DiagramRegions {
33 private static final Logger LOGGER = LoggerFactory.getLogger(DiagramRegions.class);
35 public static Resource createRegionForDiagram(WriteGraph graph, Resource diagram, String label, double[] coordinates) throws DatabaseException {
36 if (coordinates == null || coordinates.length < 6 || coordinates.length % 2 != 0)
37 throw new IllegalArgumentException("Provided coordinates are not valid : " + Arrays.toString(coordinates));
38 Path2D path = new Path2D.Double();
39 double startX = coordinates[0];
40 double startY = coordinates[1];
41 path.moveTo(startX, startY);
42 for (int i = 2; i < coordinates.length; i++)
43 path.lineTo(coordinates[i], coordinates[++i]);
45 path.closePath(); // might not be needed
46 DiagramRegionsResource DRR = DiagramRegionsResource.getInstance(graph);
47 Layer0 L0 = Layer0.getInstance(graph);
48 Resource region = graph.newResource();
49 graph.claim(region, L0.InstanceOf, DRR.Region);
50 graph.claim(diagram, DRR.hasRegion, region);
51 graph.claim(region, DRR.regionOf, diagram);
52 graph.claimLiteral(region, L0.HasName, UUID.randomUUID().toString(), Bindings.STRING);
53 graph.claimLiteral(region, L0.HasLabel, label, Bindings.STRING);
54 graph.claimLiteral(region, DRR.Region_area, coordinates, Bindings.DOUBLE_ARRAY);
55 if (LOGGER.isDebugEnabled())
56 LOGGER.debug("Created region for diagram {} with label {} and area {}", diagram, label, Arrays.toString(coordinates));
60 public static void removeRegion(WriteGraph graph, Resource region) throws DatabaseException {
61 RemoverUtil.remove(graph, region);
64 public static class DiagramRegion {
66 private Resource resource;
70 public DiagramRegion(Resource resource, String label, Shape coordinatesToShape) {
71 this.resource = resource;
73 this.shape = coordinatesToShape;
76 public Resource getResource() {
80 public String getLabel() {
84 public Shape getShape() {
89 public static Collection<DiagramRegion> findRegions(ReadGraph graph, Resource model) throws DatabaseException {
90 List<Resource> diagrams = QueryIndexUtils.searchByType(graph, model, DiagramResource.getInstance(graph).Diagram);
91 Set<DiagramRegion> regions = new HashSet<>();
93 Layer0 L0 = Layer0.getInstance(graph);
95 for (Resource diagram : diagrams) {
96 Collection<Resource> region = graph.getObjects(diagram, DiagramRegionsResource.getInstance(graph).hasRegion);
97 for (Resource r : region) {
98 String label = graph.getRelatedValue(r, L0.HasLabel, Bindings.STRING);
99 Shape coordinatesToShape = DiagramRegions.coordinatesToShape(graph, r);
100 regions.add(new DiagramRegion(r, label, coordinatesToShape));
106 public static class DiagramRegionsRequest extends UniqueRead<Collection<DiagramRegion>> {
109 public Collection<DiagramRegion> perform(ReadGraph graph) throws DatabaseException {
110 return findRegions(graph, graph.syncRequest(new PossibleActiveModel(Simantics.getProjectResource())));
114 public static Shape coordinatesToShape(ReadGraph graph, Resource region) throws DatabaseException {
115 DiagramRegionsResource DRR = DiagramRegionsResource.getInstance(graph);
116 double[] coordinates = graph.getRelatedValue(region, DRR.Region_area, Bindings.DOUBLE_ARRAY);
117 Path2D path = new Path2D.Double();
118 double startX = coordinates[0];
119 double startY = coordinates[1];
120 path.moveTo(startX, startY);
121 for (int i = 2; i < coordinates.length; i++)
122 path.lineTo(coordinates[i], coordinates[++i]);
124 path.closePath(); // might not be needed
128 public static double[] shapeToCoordinates(Shape shape) {
129 PathIterator it = new FlatteningPathIterator(shape.getPathIterator(null), 1);
130 double points[] = new double[6];
131 double moveX = 0, moveY = 0;
132 double thisX = 0, thisY = 0;
135 List<Double> coords = new ArrayList<>();
138 while (!it.isDone()) {
139 type = it.currentSegment(points);
141 case PathIterator.SEG_MOVETO:
148 case PathIterator.SEG_CLOSE:
153 case PathIterator.SEG_LINETO:
162 double[] finalCoords = new double[coords.size()];
163 for (int i = 0; i < coords.size(); i++) {
164 double d = coords.get(i);
170 public static Resource getDiagramForRegion(ReadGraph graph, Resource region) throws DatabaseException {
171 DiagramRegionsResource DRR = DiagramRegionsResource.getInstance(graph);
172 Resource diagram = graph.getSingleObject(region, DRR.regionOf);