From 9f2732ec271abb50b2fc84ad93d07494d0c6abc8 Mon Sep 17 00:00:00 2001 From: jsimomaa Date: Wed, 12 Sep 2018 20:32:15 +0300 Subject: [PATCH] Make optional to calculate elevation for edge during CSV import gitlab #10 Change-Id: I64141c02990cf306acbec11d05833e8ed7890471 (cherry picked from commit 46e2c804156e3bd2072ec91a801954c18cd15580) --- .../district/imports/ui/CSVImportModel.java | 9 ++++++++ .../district/imports/ui/CSVImportWizard.java | 4 +++- .../imports/ui/CSVImportWizardPage.java | 21 +++++++++++++++-- .../graph/DistrictNetwork.pgraph | 3 +++ .../district/network/ui/DNEdgeBuilder.java | 23 +++++++++++++++---- 5 files changed, 52 insertions(+), 8 deletions(-) diff --git a/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportModel.java b/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportModel.java index 9d5bd715..7c660b4c 100644 --- a/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportModel.java +++ b/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportModel.java @@ -70,6 +70,7 @@ public class CSVImportModel { private double edgePadding = 0.0001; // default private int valvePositionIndx = -1; private int addressIndex; + private int lengthIndex; // Third page @@ -482,4 +483,12 @@ public class CSVImportModel { public int getAddressIndex() { return addressIndex; } + + public int getLengthIndex() { + return lengthIndex; + } + + public void setLengthIndex(int lengthIndex) { + this.lengthIndex = lengthIndex; + } } diff --git a/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java b/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java index ffed0c3a..490c9505 100644 --- a/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java +++ b/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java @@ -107,6 +107,7 @@ public class CSVImportWizard extends Wizard implements IImportWizard { int edgeFlowAreaIndex = model.getEdgeFlowAreaIndex(); int kReturnIndex = model.getkReturnIndex(); int kSupplyIndex = model.getkSupplyIndex(); + int lengthIndex = model.getLengthIndex(); int mappingColumn = model.getComponentMappingIndex(); int idColumn = model.getIdIndex(); @@ -230,7 +231,7 @@ public class CSVImportWizard extends Wizard implements IImportWizard { endCoords = new double[] { endXCoord , endYCoord }; } - Resource edge = DNEdgeBuilder.create(graph, vv, model.getParentDiagram(), model.getComponentMappings().get(mappingValue), startCoords, endCoords, padding); + Resource edge = DNEdgeBuilder.create(graph, vv, model.getParentDiagram(), model.getComponentMappings().get(mappingValue), startCoords, endCoords, padding, true); writeStringValue(graph, row, idColumn, edge, DN.HasId); writeValue(graph, row, diameterColumnIndex, edge, DN.Edge_HasDiameter); @@ -240,6 +241,7 @@ public class CSVImportWizard extends Wizard implements IImportWizard { writeValue(graph, row, kReturnIndex, edge, DN.Edge_HasKReturn); writeValue(graph, row, kSupplyIndex, edge, DN.Edge_HasKSupply); writeValue(graph, row, edgeFlowAreaIndex, edge, DN.Edge_HasFlowArea); + writeValue(graph, row, lengthIndex, edge, DN.Edge_HasLength); } } catch (MismatchedDimensionException | TransformException | DatabaseException e) { throw new DatabaseException(e); diff --git a/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizardPage.java b/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizardPage.java index e5cb3a6a..b7e46475 100644 --- a/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizardPage.java +++ b/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizardPage.java @@ -12,7 +12,6 @@ import org.eclipse.jface.dialogs.IPageChangeProvider; import org.eclipse.jface.dialogs.IPageChangedListener; import org.eclipse.jface.dialogs.PageChangedEvent; import org.eclipse.jface.layout.GridDataFactory; -import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.jface.layout.TableColumnLayout; import org.eclipse.jface.viewers.ColumnWeightData; import org.eclipse.jface.wizard.IWizardContainer; @@ -79,6 +78,7 @@ public class CSVImportWizardPage extends WizardPage { private DynamicComboFieldEditor nominalPressureLossSelector; private DynamicComboFieldEditor valvePositionSelector; private DynamicComboFieldEditor addressSelector; + private DynamicComboFieldEditor lengthSelector; // For edge import private DynamicComboFieldEditor startXCoordSelector; @@ -654,6 +654,8 @@ public class CSVImportWizardPage extends WizardPage { validatePageComplete(); } }); + + } private void createEdgeIndexMappingField(Group parent) { @@ -846,6 +848,20 @@ public class CSVImportWizardPage extends WizardPage { validatePageComplete(); } }); + lengthSelector = new DynamicComboFieldEditor("lengthValue", "lengthSelector", parent); + lengthSelector.addComboListener(new SelectionListener() { + + @Override + public void widgetSelected(SelectionEvent e) { + widgetDefaultSelected(e); + } + + @Override + public void widgetDefaultSelected(SelectionEvent e) { + model.setLengthIndex(Integer.parseInt(lengthSelector.getValue())); + validatePageComplete(); + } + }); } private void updateCombos() { @@ -886,6 +902,7 @@ public class CSVImportWizardPage extends WizardPage { kReturnSelector.updateCombo(namesAndValues); kSupplySelector.updateCombo(namesAndValues); tGroundSelector.updateCombo(namesAndValues); + lengthSelector.updateCombo(namesAndValues); } private void updateVertexCombos(String[][] namesAndValues) { @@ -910,7 +927,7 @@ public class CSVImportWizardPage extends WizardPage { velocitySelector.updateCombo(namesAndValues); flowAreaSelector.updateCombo(namesAndValues); nominalPressureLossSelector.updateCombo(namesAndValues); - addressSelector.updateCombo(namesAndValues); + addressSelector.updateCombo(namesAndValues); } diff --git a/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph b/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph index 657ffc69..e9c52cc7 100644 --- a/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph +++ b/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph @@ -80,6 +80,8 @@ DN.Vertex -- DN.Edge.HasElevation + @defProperty "Elevation" L0.Double >-- DN.Edge.HasLength @defProperty "Length" L0.Double L0.readOnly true @@ -249,4 +251,5 @@ DN.Mapping.EdgeMapping -- DN.Mapping.EdgeMapping.KReturnAttribute --> L0.String -- DN.Mapping.EdgeMapping.KSupplyAttribute --> L0.String -- DN.Mapping.EdgeMapping.TGroundAttribute --> L0.String -- DN.Mapping.EdgeMapping.ElevationAttribute --> L0.String -- DN.Mapping.Terminals 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 0270fea0..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 @@ -9,16 +9,16 @@ 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; @@ -45,10 +45,10 @@ public class DNEdgeBuilder { 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); + return create(graph, vv, diagramResource, null, start, end, padding, false); } - public static Resource create(WriteGraph graph, Collection vertices, 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); @@ -58,6 +58,9 @@ public class DNEdgeBuilder { // 2. Add vertices 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); @@ -75,6 +78,16 @@ public class DNEdgeBuilder { // 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); -- 2.47.1