Below is a high-level summary of the Utools.pas unit, which manages user-registered external tools from SWMM’s Tools menu.
1. Purpose & Overview
Utools.pas provides the functionality to register, store, and launch external executables (tools) integrated into SWMM’s Tools menu. By “tools,” we mean any external program or script that might interact with the current SWMM project or its temporary input and output files. The user can specify command-line arguments (macros) that reference various SWMM file locations, allowing seamless handoff of project or results data to an external program.
Core Responsibilities:
- Maintain a list (
ToolList) of external tools. - Load/save each tool’s settings (exe path, working directory, parameters, etc.) from/to a dedicated INI file (
swmm5tools.ini). - Populate the Tools menu in the SWMM GUI with each registered tool as a menu item.
- Run a selected tool, possibly updating SWMM’s input/output if so configured.
2. Key Data Structures
-
TToolClass- Records all settings for a single external tool:
ExeName: The path/name of the tool’s executable.DirName: The working directory (often a macro).Params: Command-line parameters (can also contain macros).DisableSWMM: If true, the main SWMM UI is hidden while the tool runs.UpdateSWMM: If true, SWMM’s project or results are refreshed afterward.MnuItem: A pointer to the DelphiTMenuItemthat appears under Tools.
- Records all settings for a single external tool:
-
ToolList(TStrings)- Holds all
TToolobjects and their corresponding display names. - Provides indexing and naming references.
- Holds all
-
Macros
- A set of string tokens (like
$PROJDIR,$INPFILE, etc.) replaced at runtime with actual file or directory paths. - The recognized macros are:
$PROJDIR (project directory) $SWMMDIR (SWMM’s program directory) $INPFILE (current project’s temporary input file) $RPTFILE (temporary text-based status report file) $OUTFILE (temporary output file) $RIFFILE (runoff interface file) - Tools can use these macros in their command-line
Params.
- A set of string tokens (like
3. Primary Procedures and Functions
Below is the typical lifecycle of the Tools collection:
-
Initialization / Finalization
OpenToolList: Called during SWMM startup to createToolListand populate it fromswmm5tools.ini.CloseToolList: Called at shutdown to save the final tool list back toswmm5tools.iniand free resources.
-
INI File Reading & Writing
ReadToolsIniFile: Opensswmm5tools.ini, reads each “section” as one tool.WriteToolsIniFile: Writes each tool’s properties back to the INI file.
-
Tool CRUD Operations
UpdateTool(I, S1, S2, S3, S4, Flag1, Flag2)- If
Iis -1, a newTToolis created and appended. Otherwise, the existing tool at indexIis updated. S1is the Tool’s menu text,S2the exe path,S3the directory,S4the command-line parameters, andFlag1,Flag2are the two Booleans.
- If
DeleteTool(I): Removes the tool at indexIfrom both theToolListand the Tools menu.ExchangeTools(I1, I2): Swaps the order of two tools inToolList, ensuring the Tools menu re-orders accordingly.
-
Utility Functions
GetTmpInpFileName: Creates and exports the current SWMM project to a new temporary.inpfile if$INPFILEis used.RunTool(S: String):- Locates the named tool in
ToolList. - Prepares the working directory (
DirName) by substituting macros. - Prepares command-line parameters by substituting macros in
Params. - Optionally hides SWMM, then executes the external program.
- If
UpdateSWMMis true, checks whether the external tool changed the.inpor.outfiles, and updates SWMM’s data or results accordingly.
- Locates the named tool in
4. Macro Substitution Mechanism
When RunTool is invoked, it constructs the final command-line and directory via:
GetDir- Replaces
$PROJDIRwithUglobals.ProjectDir,$SWMMDIRwith the SWMM program’s directory.
- Replaces
GetParams- Replaces
$INPFILEwith a newly generated temporary SWMM input file. - Replaces
$RPTFILE,$OUTFILE,$RIFFILEsimilarly.
- Replaces
This ensures an external tool can seamlessly reference the current project’s data.
5. Integration Flow
- Startup:
OpenToolListloads all tools fromswmm5tools.ini, creating a Tools menu item for each. - Edit Tools: The user can open a UI to add, remove, or reorder tools.
- Invoke Tool: The user clicks a Tools menu item:
RunTool(...)is called.- Temporary input/outputs are created if macros are used.
- The external exe is launched with the constructed command-line.
- If
DisableSWMMis set, the main form is hidden. - If
UpdateSWMMis set, the.inpor.outfiles are re-read so that changes show up in SWMM’s UI.
- Shutdown:
CloseToolListsaves current tools toswmm5tools.ini.
6. Example Usage Scenario
- The user wants a post-processor like “PlotterX” accessible from SWMM:
- They add a new Tool named “PlotterX,” specify
ExeNameasC:\Tools\PlotterX.exe,DirNameas$PROJDIR, andParamsas-i $INPFILE -o results.png. - They set
DisableSWMM = falseandUpdateSWMM = false.
- They add a new Tool named “PlotterX,” specify
- At run-time, SWMM writes the current project to a temp
.inpfile. ThenPlotterXis called with the appropriate path arguments. - The user’s external program can produce a figure from that
.inpdata. - Because
UpdateSWMM = false, SWMM does not automatically re-import results.
7. Conclusion
Utools.pas is a convenience unit enabling SWMM to store and launch external applications from the Tools menu. By supporting macros and optional SWMM synchronization, it offers a robust mechanism for custom post-processing, run automation, or model transformations. This integration extends SWMM’s functionality without requiring changes to its internal code.