Hello,
The python code in this post might be of use to you: Invalid Duct/Pipe Connections - #10 by T_Pover