The article discusses an effective procedure for optimizing the shape of the winding and ferromagnetic shield of a solenoid magnet, which is part of the focusing system for a charged particle beam. The main purpose of the study is the formation of a uniform magnetic field in the aperture of a solenoid. The peculiarity of this optimization procedure is its division into several stages. On the first of them, the best distribution of the current density in the winding is determined, which provides the maximum possible extent of the area of uniformity of the magnetic field. At the second stage, a practically realizable structure of the magnet winding is formed, which simulates the optimal current density distribution in the first approximation. At the last stage, amendments are made, allowing the use of the magnet in the mode of both stationary and rapidly changing current in the winding. The external ferromagnetic shield of the magnet selected ensures good quality of the magnetic field in the aperture and negligible power losses at the same time.