How to name a pipe fitting or a pipe in order of water flow?

Hi Guys.

I want to know how I can create a script for name a pipe fitting or a pipe.
I tried to extract and test some kind of package, but no one can be ordered in the relation of water flow.
Someone know about that?

Example:
Capturar

Script :