This paper presents an automatic pipe-routing algorithm for pipelines with branches in an electromechanical product. The algorithm uses chaos theory to establish the chaos grid pre-processing model (CGPM) that can efficiently pick up solution space and reduce the search range. A novel particle swarm optimization (PSO) algorithm is developed to seek for an approximate optimal trajectory in the solution space. Experiments show that the modified PSO algorithm is effective and efficient. The examples demonstrate that the automatic pipe-routing algorithm is effective and practical for pipelines with branches.