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
-
TTool
Class- 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 DelphiTMenuItem
that appears under Tools.
- Records all settings for a single external tool:
-
ToolList
(TStrings
)- Holds all
TTool
objects 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 createToolList
and populate it fromswmm5tools.ini
.CloseToolList
: Called at shutdown to save the final tool list back toswmm5tools.ini
and 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
I
is -1, a newTTool
is created and appended. Otherwise, the existing tool at indexI
is updated. S1
is the Tool’s menu text,S2
the exe path,S3
the directory,S4
the command-line parameters, andFlag1
,Flag2
are the two Booleans.
- If
DeleteTool(I)
: Removes the tool at indexI
from both theToolList
and 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.inp
file if$INPFILE
is 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
UpdateSWMM
is true, checks whether the external tool changed the.inp
or.out
files, 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
$PROJDIR
withUglobals.ProjectDir
,$SWMMDIR
with the SWMM program’s directory.
- Replaces
GetParams
- Replaces
$INPFILE
with a newly generated temporary SWMM input file. - Replaces
$RPTFILE
,$OUTFILE
,$RIFFILE
similarly.
- Replaces
This ensures an external tool can seamlessly reference the current project’s data.
5. Integration Flow
- Startup:
OpenToolList
loads 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
DisableSWMM
is set, the main form is hidden. - If
UpdateSWMM
is set, the.inp
or.out
files are re-read so that changes show up in SWMM’s UI.
- Shutdown:
CloseToolList
saves 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
ExeName
asC:\Tools\PlotterX.exe
,DirName
as$PROJDIR
, andParams
as-i $INPFILE -o results.png
. - They set
DisableSWMM = false
andUpdateSWMM = false
.
- They add a new Tool named “PlotterX,” specify
- At run-time, SWMM writes the current project to a temp
.inp
file. ThenPlotterX
is called with the appropriate path arguments. - The user’s external program can produce a figure from that
.inp
data. - 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.
No comments:
Post a Comment