Magnets from CAD#
The easiest way to create complex magnet shapes from CAD files is through Pyvista using the TriangularMesh class. Pyvista supports *.stl files, and any open CAD file format is easily transformed to stl.
Warning
CAD files might include many Triangles, especially when dealing with round sides and edges, that do not significantly contribute to the field and will slow down the Magpylib computation.
import pyvista as pv
from magpylib.magnet import TriangularMesh
# Import *.stl file with Pyvista
mesh = pv.read("logo.stl")
# Transform into Magpylib magnet
magnet = TriangularMesh.from_pyvista(
polydata=mesh,
polarization=(1, -1, 0),
check_disconnected=False,
)
magnet.show(backend="plotly")
Hint
A quick way to work with cad files, especially transforming 2D *.svg to 3D *.stl, is provided by Tinkercad.