Update Labels of a Hydraulic Model using WaterObjects.NET

Update Labels of a Hydraulic Model using WaterObjects.NET

Update Labels main window


Introduction
Need to combine two models together and planning to use Submodel import/export feature? Everything will work perfectly fine as long as labels are different and if they are not, things will get overwritten where labels are same between the models. If in case there is a label like 'P-1' in both models and they need to be brought in separately, this tool, Update Labels, will help. The tool will allow to append, prepend, remove append or remove prepend a given text to ALL labels related to SubModel Import / Export . It will go to every nook and corner and update the labels.

Sometime there are situations with Scenarios, Alternatives and Calculation Options (SACO) were we do wants things to be overwritten. Considerting this in mind the tool offers options whether to overwrite the SACO.

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, Storm Sewer STSWProducts SS3 such as SewerGEMS, SewerCAD, CivilStorm, StormCAD and PondPack.
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. For eaxample, for UpdateLabels.Water, 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 Update Labels tool, click on Browse button and select the model database file. Select the Update Label as dropdown and pick the desired option. Enter the desired text and click on Run button. The button next to Run will show a log report.

Note: The tool automatically creats a "BACKUP" file of the database just in case. So, if changes made by the Update Labelsare 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;
Haestad.Support.User;
How to Update the Labels in a Hydraulic Database
To make our code generic so that it will work with different product, the API will give us the metadata about the selected database and based on that we will work accoudingly. DomainDataSet.DomainDataSetType().SupportedElementTypes() gives all the supported element type for the dataset which includes most of the stuff under Components and Tools menu pertaining to that type of database. Scenarios, Alternatives, Calculation Options, Selection Sets needs to called directly and these can be access from DomainDataSet.

Updating label is rather easy as we just need to do some string manipulation on the Object.Label

The tool updates the Labels on,
•     DomainElements
•     SupportElements
•     Scenarios
•     Alternatives
•     Calculation Options
•     Selection Sets

Here's an example that updates all the Labels of DomainElements.
foreach (IDomainElementType domainElementType in DomainDataSet.DomainDataSetType().DomainElementTypes())
{
   ModelingElementCollection modelingElementCollection = DomainDataSet.DomainElementManager(domainElementType.Id).Elements();
   string processLabel = domainElementType.Label.Replace("#", "");

   foreach (IDomainElement domainElement in modelingElementCollection)
   {
      ChangeLabel(domainElement.Label, "\t\t", (IModelingElement)domainElement);
   }
}
Here's another example that updates the label of Alternatives.
foreach (IAlternativeType alternativeType in DomainDataSet.DomainDataSetType().AlternativeTypes())
{
    string processLabel = alternativeType.Label.Replace("#", "");

    foreach (IAlternative alternative in DomainDataSet.AlternativeManager(alternativeType.Id).Elements())
    {
       ChangeLabel(alternative.Label, "\t\t", (IModelingElement)alternative);
    }
}
The ChangeLabel method basically does the string manipulation and updates the Label.
private void ChangeLabel(string label, string tabsString, IModelingElement modelingElement)
{
   string newLabel = "";
   switch (SelectedLabelUpdateMethod)
   {
       case LabelUpdateMethod.Prefix:
          newLabel = textBoxUpdateLabelAs.Text + label;
          break;

       case LabelUpdateMethod.Suffix:
          // TODO
          break;

       case LabelUpdateMethod.RemovePrefix:
          // TODO
          break;

       case LabelUpdateMethod.RemoveSuffix:
          // TODO
          break;
}
   modelingElement.Label = newLabel;
}
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/07/14 - Initial Post.