EPANET class for C#

C_Sharp_wordmark.svgThe previous C# class I posted about a year ago had some issues and bugs. Vyacheslav Shevelyov, who reported a few of these issues, took upon himself to write a new C# class for the EPANET Toolkit. From the class readme file:

This archive contains C# source code to interact with unmanaged EPANET 2 hydraulics library and example files to demonstrate/test core functionality. Sources written for epanet2.dll codeversion 20012. There are no CLI classes, that wrap EPANET object, just plain function imports and constants.

Only UnsafeNativeMethods.cs, Enumerations.cs and optionally EpanetException.cs should be used to call epanet2.dll from C# code:

  • UnsafeNativeMethods.cs contains UnsafeNativeMethods class with EPANET function declarations as well as some important constants.
  • Enumerations.cs file contains EPANET constant end defines, grouped in several enums.
  • EpanetException.cs is mapper of error codes, returned by Epanet to CLI exceptions. It is not required to call EPANET functions, but may be useful due to all EPANET functions return error codes to indicate errors.

Other files are examples, to demonstrate code functionality and make some
testing.

The code is distributed under the unlicense license.

Here are a few code snippets just to show the function calls and Enums:

[code language=”csharp” collapse=”false”]
UnsafeNativeMethods.ENgetcount(CountType.Node, out nodeCount);
UnsafeNativeMethods.ENgetnodevalue(i, NodeValue.Head, out value);
UnsafeNativeMethods.ENgetlinkvalue(i, LinkValue.Flow, out value);
[/code]

Download Epanet-c-sharp (zip).

Tagged with: , ,
6 comments on “EPANET class for C#
  1. Q.zhang says:

    I am a student in Harbin Institute of Technology,China.

  2. Lucas Vasconcelos says:

    Thanks, again.
    I went through some truly awful workarounds to use the previous classes that were roaming around without the proper marshalling. That before I just gave up! 🙂

  3. Ezgi Koker says:

    Hi,
    I am trying to combine EPANET with an optimization program GAMS. I am new to subject and searching for the best language to use for EPANET toolkit. For water distribution network analysis which one is better, C, C++, Pascal, Visual Basic, Matlab? Which one would you suggest?

  4. Neermal says:

    Hello,
    I tried the http://epanet.de/download/epanet_toolkit_example_devcpp_c.zip.
    But it seems that the inp file is not accessible since I get the following error:
    ENopen(Net1.inp,Net1.txt,Net1.bin) returned 200
    ENsolveH() returned 102
    ENsaveH() returned 104
    ENreport() returned 106

    Can someone figure this out?

    thanks.

    Neermal

  5. Kami says:

    Dear Elad;
    I can run the EPANET Class for C# for a network in visual studio 2013(windows application) and get node and link data. Now I wanna to run the EPANET in MVC.project (ASP.Net). I make a class library (dll) from C# EPANET class and reference to my MVC.project. But when I run it I receive the zero value for value the same network which I run at windows application. What’s the problem? Please guide me.
    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.