Skip to content

Commit 5978893

Browse files
authored
Merge pull request #908 from Mariosmsk/add_setnodevalues_setlinkvalues
Add new functions ENsetnodevalues, ENsetlinkvalues
2 parents 8379146 + 8a3b5f0 commit 5978893

File tree

9 files changed

+230
-9
lines changed

9 files changed

+230
-9
lines changed

include/epanet2.bas

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Attribute VB_Name = "Module1"
55
'Declarations of functions in the EPANET PROGRAMMERs TOOLKIT
66
'(EPANET2.DLL)
77

8-
'Last updated on 04/23/2025
8+
'Last updated on 03/05/2026
99

1010
' These are codes used by the DLL functions
1111
Public Const EN_ELEVATION = 0 ' Node parameters
@@ -361,6 +361,7 @@ Public Const EN_TRUE = 1 ' boolean true
361361
Declare Function ENgetcoord Lib "epanet2.dll" (ByVal index As Long, x As Double, y As Double) As Long
362362
Declare Function ENsetcoord Lib "epanet2.dll" (ByVal index As Long, ByVal x As Double, ByVal y As Double) As Long
363363
Declare Function ENgetnodevalues Lib "epanet2.dll" (ByVal property as Long, values as Any) As Long
364+
Declare Function ENsetnodevalues Lib "epanet2.dll" (ByVal property As Long, values As Any, badIndex As Long) As Long
364365

365366
'Nodal Demand Functions
366367
Declare Function ENgetdemandmodel Lib "epanet2.dll" (type_ As Long, pmin As Single, preq As Single, pexp As Single) As Long
@@ -394,7 +395,8 @@ Public Const EN_TRUE = 1 ' boolean true
394395
Declare Function ENsetvertex Lib "epanet2.dll" (ByVal index As Long, ByVal vertex As Long, ByVal x As Double, ByVal y As Double) As Long
395396
Declare Function ENsetvertices Lib "epanet2.dll" (ByVal index As Long, xCoords As Any, yCoords As Any, ByVal count As Long) As Long
396397
Declare Function ENgetlinkvalues Lib "epanet2.dll" (ByVal property as Long, values as Any) As Long
397-
398+
Declare Function ENsetlinkvalues Lib "epanet2.dll" (ByVal property As Long, values As Any, badIndex As Long) As Long
399+
398400
'Pump Functions
399401
Declare Function ENgetheadcurveindex Lib "epanet2.dll" (ByVal linkIndex As Long, curveIndex As Long) As Long
400402
Declare Function ENsetheadcurveindex Lib "epanet2.dll" (ByVal linkIndex As Long, ByVal curveIndex As Long) As Long

include/epanet2.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,9 @@ public static class Epanet
483483

484484
[DllImport(EPANETDLL, EntryPoint = "ENgetnodevalues")]
485485
public static extern int ENgetnodevalues(int param, ref float values);
486+
487+
[DllImport(EPANETDLL, EntryPoint = "ENsetnodevalues")]
488+
public static extern int ENsetnodevalues(int property, float values, ref int badIndex);
486489

487490
//Nodal Demand Functions
488491
[DllImport(EPANETDLL, EntryPoint = "ENgetdemandmodel")]
@@ -571,6 +574,8 @@ public static class Epanet
571574
[DllImport(EPANETDLL, EntryPoint = "ENgetlinkvalues")]
572575
public static extern int ENgetlinkvalues(int param, ref float values);
573576

577+
[DllImport(EPANETDLL, EntryPoint = "ENsetlinkvalues")]
578+
public static extern int ENsetlinkvalues(int property, float values, ref int badIndex);
574579

575580
//Pump Functions
576581
[DllImport(EPANETDLL, EntryPoint = "ENgetheadcurveindex")]

include/epanet2.def

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,10 @@ EXPORTS
112112
ENsetlinknodes = _ENsetlinknodes@12
113113
ENsetlinktype = _ENsetlinktype@12
114114
ENsetlinkvalue = _ENsetlinkvalue@12
115+
ENsetlinkvalues = _ENsetlinkvalues@12
115116
ENsetnodeid = _ENsetnodeid@8
116-
ENsetnodevalue = _ENsetnodevalue@12
117+
ENsetnodevalue = _ENsetnodevalue@12
118+
ENsetnodevalues = _ENsetnodevalues@12
117119
ENsetoption = _ENsetoption@8
118120
ENsetpattern = _ENsetpattern@12
119121
ENsetpatternid = _ENsetpatternid@8

include/epanet2.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Authors: see AUTHORS
88
Copyright: see AUTHORS
99
License: see LICENSE
10-
Last Updated: 02/14/2025
10+
Last Updated: 03/05/2026
1111
******************************************************************************
1212
*/
1313

@@ -223,6 +223,8 @@ extern "C" {
223223

224224
int DLLEXPORT ENsetnodevalue(int index, int property, EN_API_FLOAT_TYPE value);
225225

226+
int DLLEXPORT ENsetnodevalues(int property, EN_API_FLOAT_TYPE *values, int *badIndex);
227+
226228
int DLLEXPORT ENsetjuncdata(int index, EN_API_FLOAT_TYPE elev,
227229
EN_API_FLOAT_TYPE dmnd, const char *dmndpat);
228230

@@ -302,6 +304,8 @@ extern "C" {
302304

303305
int DLLEXPORT ENsetlinkvalue(int index, int property, EN_API_FLOAT_TYPE value);
304306

307+
int DLLEXPORT ENsetlinkvalues(int property, EN_API_FLOAT_TYPE *values, int *badIndex);
308+
305309
int DLLEXPORT ENsetpipedata(int index, EN_API_FLOAT_TYPE length,
306310
EN_API_FLOAT_TYPE diam, EN_API_FLOAT_TYPE rough,
307311
EN_API_FLOAT_TYPE mloss);

include/epanet2.pas

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{ Declarations of imported procedures from the EPANET PROGRAMMERs TOOLKIT }
44
{ (EPANET2.DLL) }
55

6-
{Last updated on 04/23/2025}
6+
{Last updated on 03/05/2026}
77

88
interface
99

@@ -381,6 +381,7 @@ interface
381381
function ENgetcoord(Index: Integer; var X: Double; var Y: Double): Integer; cdecl; external EpanetLib;
382382
function ENsetcoord(Index: Integer; X: Double; Y: Double): Integer; cdecl; external EpanetLib;
383383
function ENgetnodevalues(Code: Integer; var X: array of Single): Integer; cdecl; external EpanetLib;
384+
function ENsetnodevalues(Code: Integer; var X: array of Double; var badIndex: Integer): Integer; cdecl; external EpanetLib;
384385

385386
{Demand Functions}
386387
function ENgetdemandmodel(var Model: Integer; var Pmin: Single; var Preq: Single; var Pexp: Single): Integer; cdecl; external EpanetLib;
@@ -410,6 +411,7 @@ interface
410411
function ENsetlinkvalue(Index: Integer; Code: Integer; Value: Single): Integer; cdecl; external EpanetLib;
411412
function ENsetpipedata(Index: Integer; Length: Single; Diam: Single; Rough: Single; Mloss:Single): Integer; cdecl; external EpanetLib;
412413
function ENgetlinkvalues(Code: Integer; var X: array of Single): Integer; cdecl; external EpanetLib;
414+
function ENsetlinkvalues(Code: Integer; var X: array of Double; var badIndex: Integer): Integer; cdecl; external EpanetLib;
413415

414416
function ENgetvertexcount(Index: Integer; var Count: Integer): Integer; cdecl; external EpanetLib;
415417
function ENgetvertex(Index: Integer; Vertex: Integer; var X: Double; var Y: Double): Integer; cdecl; external EpanetLib;

include/epanet2.vb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
'Declarations of functions in the EPANET PROGRAMMERs TOOLKIT
55
'(EPANET2.DLL) for use with VB.Net.
66

7-
'Last updated on 04/23/2025
7+
'Last updated on 03/05/2026
88

99
Imports System.Runtime.InteropServices
1010
Imports System.Text
@@ -350,6 +350,7 @@ Public Const EN_TRUE = 1 ' boolean true
350350
Declare Function ENgetcoord Lib "epanet2.dll" (ByVal index As Int32, x As Double, y As Double) As Int32
351351
Declare Function ENsetcoord Lib "epanet2.dll" (ByVal index As Int32, ByVal x As Double, ByVal y As Double) As Int32
352352
Declare Function ENgetnodevalues Lib "epanet2.dll" (ByVal property as Int32, values as Any) As Int32
353+
Declare Function ENsetnodevalues Lib "epanet2.dll" (ByVal property As Int32, values As Any, badIndex As Int32) As Int32
353354

354355
'Nodal Demand Functions
355356
Declare Function ENgetdemandmodel Lib "epanet2.dll" (type_ As Int32, pmin As Single, preq As Single, pexp As Single) As Int32
@@ -383,6 +384,7 @@ Public Const EN_TRUE = 1 ' boolean true
383384
Declare Function ENsetvertex Lib "epanet2.dll" (ByVal index As Int32, ByVal vertex As Int32, ByVal x As Double, ByVal y As Double) As Int32
384385
Declare Function ENsetvertices Lib "epanet2.dll" (ByVal index As Int32, xCoords As Any, yCoords As Any, ByVal count As Int32) As Int32
385386
Declare Function ENgetlinkvalues Lib "epanet2.dll" (ByVal property as Int32, values as Any) As Int32
387+
Declare Function ENsetlinkvalues Lib "epanet2.dll" (ByVal property As Int32, values As Any, badIndex As Int32) As Int32
386388

387389
'Pump Functions
388390
Declare Function ENgetheadcurveindex Lib "epanet2.dll" (ByVal linkIndex As Int32, curveIndex As Int32) As Int32

include/epanet2_2.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
Authors: see AUTHORS
1212
Copyright: see AUTHORS
1313
License: see LICENSE
14-
Last Updated: 04/25/2025
14+
Last Updated: 03/05/2026
1515
******************************************************************************
1616
*/
1717

@@ -952,7 +952,19 @@ typedef struct Project *EN_Project;
952952
Values are in units that depend on the units used for flow rate (see @ref Units).
953953
*/
954954
int DLLEXPORT EN_setnodevalue(EN_Project ph, int index, int property, double value);
955+
956+
/**
957+
@brief Sets property values for all nodes.
958+
@param ph an EPANET project handle.
959+
@param property the property to set (see @ref EN_NodeProperty).
960+
@param values an array of property values for all nodes.
961+
@param badIndex returns the index of the node whose assignment fails, or 0 if all succeed.
962+
@return an error code.
955963
964+
Values are in units that depend on the units used for flow rate (see @ref Units).
965+
*/
966+
int DLLEXPORT EN_setnodevalues(EN_Project ph, int property, double *values, int *badIndex);
967+
956968
/**
957969
@brief Sets a group of properties for a junction node.
958970
@param ph an EPANET project handle.
@@ -1322,6 +1334,18 @@ typedef struct Project *EN_Project;
13221334
Values are in units that depend on the units used for flow rate (see @ref Units).
13231335
*/
13241336
int DLLEXPORT EN_setlinkvalue(EN_Project ph, int index, int property, double value);
1337+
1338+
/**
1339+
@brief Sets property values for all links.
1340+
@param ph an EPANET project handle.
1341+
@param property the property to set (see @ref EN_LinkProperty).
1342+
@param values an array of property values for all links.
1343+
@param badIndex returns the index of the link whose assignment fails, or 0 if all succeed.
1344+
@return an error code.
1345+
1346+
Values are in units that depend on the units used for flow rate (see @ref Units).
1347+
*/
1348+
int DLLEXPORT EN_setlinkvalues(EN_Project ph, int property, double *values, int *badIndex);
13251349

13261350
/**
13271351
@brief Sets a group of properties for a pipe link.

src/epanet.c

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
Authors: see AUTHORS
88
Copyright: see AUTHORS
99
License: see LICENSE
10-
Last Updated: 02/17/2026
10+
Last Updated: 03/19/2026
1111
******************************************************************************
1212
*/
1313

@@ -2758,6 +2758,55 @@ int DLLEXPORT EN_setnodevalue(EN_Project p, int index, int property, double valu
27582758
return 0;
27592759
}
27602760

2761+
int DLLEXPORT EN_setnodevalues(EN_Project p, int property, double *values, int *badIndex)
2762+
/*----------------------------------------------------------------
2763+
** Input: property = node property code (see EN_NodeProperty)
2764+
** values = array of node property values
2765+
** Output: badIndex = index of node whose assignment fails (0 if none)
2766+
** Returns: error code
2767+
** Purpose: sets an array of node property values
2768+
**----------------------------------------------------------------
2769+
*/
2770+
{
2771+
int i, j, errcode = 0;
2772+
int n = p->network.Nnodes;
2773+
double *old = NULL;
2774+
2775+
if (badIndex) *badIndex = 0;
2776+
2777+
old = (double *)malloc(n * sizeof(double));
2778+
if (!old) return 101; /* insufficient memory available */
2779+
2780+
for (i = 1; i <= n; i++)
2781+
{
2782+
errcode = EN_getnodevalue(p, i, property, &old[i - 1]);
2783+
if (errcode != 0)
2784+
{
2785+
*badIndex = i;
2786+
j = i - 1; // Need to restore values for nodes 1 to i-1
2787+
}
2788+
else
2789+
{
2790+
errcode = EN_setnodevalue(p, i, property, values[i - 1]);
2791+
if (errcode != 0)
2792+
{
2793+
*badIndex = i;
2794+
j = i; // Need to restore values for nodes 1 to i
2795+
}
2796+
}
2797+
if (errcode != 0)
2798+
{
2799+
for (int k = 1; k <= j; k++)
2800+
{
2801+
EN_setnodevalue(p, k, property, old[k - 1]);
2802+
}
2803+
break;
2804+
}
2805+
}
2806+
free(old);
2807+
return errcode;
2808+
}
2809+
27612810
int DLLEXPORT EN_setjuncdata(EN_Project p, int index, double elev,
27622811
double dmnd, const char *dmndpat)
27632812
/*----------------------------------------------------------------
@@ -4304,6 +4353,55 @@ int DLLEXPORT EN_setlinkvalue(EN_Project p, int index, int property, double valu
43044353
return 0;
43054354
}
43064355

4356+
int DLLEXPORT EN_setlinkvalues(EN_Project p, int property, double *values, int *badIndex)
4357+
/*----------------------------------------------------------------
4358+
** Input: property = link property code (see EN_LinkProperty)
4359+
** values = array of link property values
4360+
** Output: badIndex = index of link whose assignment fails (0 if none)
4361+
** Returns: error code
4362+
** Purpose: sets property values for all links
4363+
**----------------------------------------------------------------
4364+
*/
4365+
{
4366+
int i, j, errcode = 0;
4367+
int n = p->network.Nlinks;
4368+
double *old = NULL;
4369+
4370+
if (badIndex) *badIndex = 0;
4371+
4372+
old = (double *)malloc(n * sizeof(double));
4373+
if (!old) return 101; /* insufficient memory available */
4374+
4375+
for (i = 1; i <= n; i++)
4376+
{
4377+
errcode = EN_getlinkvalue(p, i, property, &old[i - 1]);
4378+
if (errcode != 0)
4379+
{
4380+
*badIndex = i;
4381+
j = i - 1; // Need to restore values for links 1 to i-1
4382+
}
4383+
else
4384+
{
4385+
errcode = EN_setlinkvalue(p, i, property, values[i - 1]);
4386+
if (errcode != 0)
4387+
{
4388+
*badIndex = i;
4389+
j = i; // Need to restore values for links 1 to i
4390+
}
4391+
}
4392+
if (errcode != 0)
4393+
{
4394+
for (int k = 1; k <= j; k++)
4395+
{
4396+
EN_setlinkvalue(p, k, property, old[k - 1]);
4397+
}
4398+
break;
4399+
}
4400+
}
4401+
free(old);
4402+
return errcode;
4403+
}
4404+
43074405
int DLLEXPORT EN_setpipedata(EN_Project p, int index, double length,
43084406
double diam, double rough, double mloss)
43094407
/*----------------------------------------------------------------

0 commit comments

Comments
 (0)