Change Scale of a Hydraulic Model using WaterObjects.NET

Change Scale of a Hydraulic Model using WaterObjects.NET

Change Scale main window


Introduction
Different units have caused different problems and in hydraulic modeling, we sometime inadvertently build the model in a wrong unit and later realize the model is at the wrong location because of the units. It would have been nice to see a feature in Stand-Alone to change the scale, however it's not there. In order to change the scale, one has to open up the model in other platform and need to know the process of changing the scale. Well, with this tool it's lot more easier to change the scale.

This tool, Change Scale helps to scale up or down with ease and once done, one can drag the entire network to the right location (which may not be needed) after opening up in stand-alone product.

The programming details covered herein are applicable to several of Bentley’s hydraulics and hydrology (H&H) applications including WaterGEMS, WaterCAD, HAMMER, SewerGEMS, SewerCAD, SewerGEMS Sanitary, CivilStorm, StormCAD, PondPack etc. In fact, the exact same lines of code can be used to create the tool that can work with most H&H applications. The tool will work with WaterGEMS SS3 and Storm Sewer STSWProducts SS3 such as SewerGEMS, SewerCAD, CivilStorm, StormCAD.
How to Run the Tool
If you simply obtain the executable (.exe) file then make sure to place/copy this file in the right directory. Since the tool is built for WaterGEMS and STSW, the .exe needs to be placed in the %ProgramFiles%\Bentley\WaterGEMS\ directory or in corresponding STSW installed directory. Once the exe file is in the right location, run it and a window as shown in the figure above will show up.

In the Change Scale tool, click on Browse button and select the model database file. Now, simply enter the scaling factor. To scale up, enter a value greater than 1 and to scale down, enter a value less than 1 like 0.5. Finally, click on Run button. Once the progress bar is filled all the way, close the tool.

Note: The tool automatically creats a "BAK" file of the database just in case. So, if change made by the Change Scaleis undesirable, rename the database file to get the origianl file.
Programming Details
Bentleys WaterObjects.NET API will be primarily discussed in the sections below using C# as a programming language. It is assumed that the reader has some basic knowledge about the .NET programming.
References Used
Haestad.Domain;
Haestad.Domain.ModelingObjects;
Haestad.Framework;
Haestad.Framework.Windows.Forms;
Haestad.Support;
Namespaces Used
Haestad.Domain;
Haestad.Domain.ModelingObjects;
Haestad.Framework.Windows.Forms.Forms;
Haestad.Framework.Windows.Forms.Resources;
Haestad.Support.Support;
How to Scale a Hydraulic Database
To make our code generic so that it will work with different product, we will read the metadata from the model database and the work accoudingly. DomainDataSet.DomainDataSetType() .DomainElementTypes(false) gives the supported element type for the dataset. In general, it will return pipes for WaterCAD/GEMs model and it will return conduits for STSW models. The next steps is to find the geometry type of the element and then process them accordingly. Checking the domainElementType.DomainElementShapeType() for each elementType we either will call, UpdateNodeGeometry or UpdateLinkGeometry.

DomainElementTypeCollection elementTypes = DomainDataSet.DomainDataSetType().DomainElementTypes(false);

foreach (IDomainElementType domainElementType in elementTypes)
{
   IDomainElementManager elementManager = DomainDataSet.DomainElementManager(domainElementType.Id);
   HmIDCollection elementIDs = elementManager.ElementIDs();
   IField field = (IField)elementManager.DomainElementField(StandardFieldName.HmiGeometry, StandardAlternativeName.HmiGeometry);

   switch (domainElementType.DomainElementShapeType())
   {
      case DomainElementShapeType.DirectedNode:
      case DomainElementShapeType.Point:
         UpdateNodeGeometry(elementIDs, field, factor);
         break;
      case DomainElementShapeType.Polygon:
      case DomainElementShapeType.Polyline:
         UpdateLinkGeometry(elementIDs, field, factor);
         break;
   }
}
In UpdateNodeGeometry or UpdateLinkGeometry we basically get the starting and ending (or vertices) nodes and modify the x and y value by multiplying the existing value with the given scale factor. Then we update the existing coordinates with the scaled coordinates.

private void UpdateNodeGeometry(HmIDCollection elementIDs, IField field, double factor)
{
   for (int i = 0; i < elementIDs.Count; i++)
   {
      int elementID = elementIDs[i];

      GeometryPoint geoPoint = (GeometryPoint) field.GetValue(elementID);

      geoPoint.X = geoPoint.X*factor;
      geoPoint.Y = geoPoint.Y*factor;
      ((IEditField) field).SetValue(elementID, geoPoint);
   }
}

private void UpdateLinkGeometry(HmIDCollection elementIDs, IField field, double factor)
{
   for (int i = 0; i < elementIDs.Count; i++)
   {
      int elementID = elementIDs[i];

      GeometryPoint[] geoPoints = (GeometryPoint[]) field.GetValue(elementID);

      GeometryPoint[] updatedGeoPoints = new GeometryPoint[geoPoints.Count()];
      for (int j = 0; j < geoPoints.Length; j++)
      {
         GeometryPoint geoPoint = geoPoints[j];

         updatedGeoPoints[j].X = geoPoint.X*factor;
         updatedGeoPoints[j].Y = geoPoint.Y*factor;
      }
      ((IEditField) field).SetValue(elementID, updatedGeoPoints);

   }
}
Notes
In the actual solution (Visual Studio file), there are a lot more lines of code than the code discussed above, and may not have an exact match. There, the solution file may exhibit slightly different code lines than above. The above code is to highlight the key codes for the task.
What if the Tool does Not Work or Help is Needed?
-If you just received the exe file, make sure the file is placed in the right location
-If you are working with Visual Studio/Express, you can contact me (by email at Akshaya.Niraula@bentley.com).
History
•     2013/09/13 - Fixed the issue with referenced nodes like isolation valve.
•     2013/09/12 - Added WTRG SS4 support.
•     2013/04/30 - Initial Post