Skip to content

Commit 062ffcc

Browse files
committed
Add IncrementNumberAtEnd
Add sub index in OD: Auto increment sub index name if the name has a number at the end
1 parent 884f022 commit 062ffcc

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

libEDSsharp/eds.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,32 @@ public void AccessPDO(AccessPDO accessPDO)
12811281
break;
12821282
}
12831283
}
1284+
/// <summary>
1285+
/// Incremement number at end of string by given increment
1286+
/// </summary>
1287+
/// <param input>SubObject name as string to increment</param>
1288+
/// <param increment>increment for next SubObject</param>
1289+
/// <returns>incremented name or the original string if no number is found at the end</returns>
1290+
public static string IncrementNumberAtEnd(string input, int increment)
1291+
{
1292+
// Regex to match the last token if it's a number
1293+
Regex regex = new Regex(@"(\d+)$");
1294+
Match match = regex.Match(input);
1295+
1296+
if (match.Success)
1297+
{
1298+
// Extract the number and increment it
1299+
int number = int.Parse(match.Value);
1300+
number += increment;
12841301

1302+
// Replace the old number with the new number
1303+
string result = regex.Replace(input, number.ToString());
1304+
return result;
1305+
}
1306+
1307+
// Return the original string if no number is found at the end
1308+
return input;
1309+
}
12851310
/// <summary>
12861311
/// Duplicate current sub entry and add it to parent
12871312
/// </summary>
@@ -1341,8 +1366,11 @@ public ODentry AddSubEntry()
13411366

13421367
newSubObjects.Add(newSubIndex++, subOd);
13431368

1344-
if (originalOd == subOd)
1345-
newSubObjects.Add(newSubIndex++, newOd);
1369+
if (lastSubOd == subOd) {
1370+
newSubObjects.Add(newSubIndex, newOd);
1371+
// Auto increment if the parameter name has a number at the end
1372+
newSubObjects[newSubIndex].parameter_name = IncrementNumberAtEnd(newSubObjects[(ushort)(newSubIndex-1)].parameter_name, 1);
1373+
}
13461374
}
13471375
if (originalOd == null)
13481376
newSubObjects.Add(newSubIndex++, newOd);

0 commit comments

Comments
 (0)