Pipes are commonly used to transport fuels, air, water, gas, hydraulic power, and other fluid-like materials in engine rooms, houses, factories, airplanes, and ships. Thus, pipe routing is essential in many industrial applications, including ship construction, machinery manufacturing, house building, laying out engine rooms, etc. To be functional, a pipe system should be economical while satisfying spatial constraints and safety regulations. Numerous routing algorithms have been published to optimize the pipe length and the number of elbows. However, relatively few methods have been designed to lay out pipes which strictly meet the spatial constraints and safety regulations. This article proposes a distance-field-based piping algorithm to remedy this problem. The proposed method converts the workspace into a 3D image and computes a distance field upon the workspace first. It then creates a feasible space out of the workspace by peeling the distance field and segmenting the 3D image. The resultant feasible space is collision-free and satisfies the spatial constraints and safety regulations. In the following step, a path-finding process, subjected to a cost function, is triggered to arrange the pipe inside the feasible space. Consequently, the cost of the pipe is optimized, and the pipe path rigidly meets the spatial constraints and safety regulations. The proposed method works effectively even if the workspace is narrow and complicated. In three experiments, the proposed method is employed to lay out pipes inside an underwater vehicle, a machinery room, and a two-story house, respectively. Not only do the resultant pipes possess minimal costs, but they also meet the spatial constraints and safety regulations, as predicted. In addition to developing the routing procedure, we also design a visualization subsystem to reveal the progression of the piping process and the variation of the workspace in the run time. Based on the displayed images, users can therefore evaluate the quality of the pipes on the fly and tune the piping parameters if necessary.