This paper presents a framework for the simultaneous application of shape and topology optimization in electro-mechanical design problems. Whereas the design variables of a shape optimization are the geometrical parameters of the CAD description, the design variables upon which densitybased topology optimization acts represent the presence or absence of material at each point of the region where it is applied. These topology optimization design variables, which are called densities, are by essence substantial quantities. This means that they are attached to matter while, on the other hand, shape optimization implies ongoing changes of the model geometry. An appropriate combination of the two representations is therefore necessary to ensure a consistent design space as the joint shape-topology optimization process unfolds. The optimization problems dealt with in this paper are furthermore constrained to verify the governing partial differential equations (PDEs) of a physical model, possibly nonlinear and discretized by means of, e.g., the finite element method (FEM). Theoretical formulas, based on the Lie derivative, to express the sensitivity of the performance functions of the optimization problem are derived and validated to be used in gradient-based algorithms. The method is applied to the torque ripple minimization in an interior permanent magnet synchronous machine (PMSM).