Download this example

Download this example as a Jupyter Notebook or as a Python script.


Magnet segmentation#

This example shows how to use PyAEDT to segment magnets of an electric motor. The method is valid and usable for any object you would like to segment.

Keywords: Maxwell 3D, Magnet segmentation.

Perform imports and define constants#

Perform required imports.

[1]:
import tempfile
import time

import ansys.aedt.core
from ansys.aedt.core.examples.downloads import download_file

Define constants.

[2]:
AEDT_VERSION = "2025.1"
NG_MODE = False  # Open AEDT UI when it is launched.

Create temporary directory#

Create a temporary directory where downloaded data or dumped data can be stored. If you’d like to retrieve the project data for subsequent use, the temporary folder name is given by temp_folder.name.

[3]:
temp_folder = tempfile.TemporaryDirectory(suffix=".ansys")

Download AEDT file example#

Set the local temporary folder to export the AEDT file to.

[4]:
aedt_file = download_file(
    source="object_segmentation",
    name="Motor3D_obj_segments.aedt",
    local_path=temp_folder.name,
)

Launch Maxwell 3D#

Launch Maxwell 3D, providing the version, rgw path to the project, and the graphical mode.

[5]:
m3d = ansys.aedt.core.Maxwell3d(
    project=aedt_file,
    version=AEDT_VERSION,
    new_desktop=True,
    non_graphical=NG_MODE,
)
PyAEDT INFO: Parsing C:\Users\ansys\AppData\Local\Temp\tmpuaotu72h.ansys\object_segmentation\Motor3D_obj_segments.aedt.
PyAEDT INFO: Python version 3.10.11 (tags/v3.10.11:7d4cc5a, Apr  5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)].
PyAEDT INFO: PyAEDT version 0.18.dev0.
PyAEDT INFO: Initializing new Desktop session.
PyAEDT INFO: Log on console is enabled.
PyAEDT INFO: Log on file C:\Users\ansys\AppData\Local\Temp\pyaedt_ansys_de56154d-5c04-4896-823b-7f6071e2e44b.log is enabled.
PyAEDT INFO: Log on AEDT is disabled.
PyAEDT INFO: Debug logger is disabled. PyAEDT methods will not be logged.
PyAEDT INFO: Launching PyAEDT with gRPC plugin.
PyAEDT INFO: New AEDT session is starting on gRPC port 52708.
PyAEDT INFO: File C:\Users\ansys\AppData\Local\Temp\tmpuaotu72h.ansys\object_segmentation\Motor3D_obj_segments.aedt correctly loaded. Elapsed time: 0m 0sec
PyAEDT INFO: Electronics Desktop started on gRPC port: 52708 after 6.543122053146362 seconds.
PyAEDT INFO: AEDT installation Path C:\Program Files\ANSYS Inc\v251\AnsysEM
PyAEDT INFO: Ansoft.ElectronicsDesktop.2025.1 version started with process ID 9392.
PyAEDT INFO: Project Motor3D_obj_segments has been opened.
PyAEDT INFO: Active Design set to Maxwell3DDesign1
PyAEDT INFO: Active Design set to Maxwell3DDesign1
PyAEDT INFO: Aedt Objects correctly read

Segment first magnet by specifying number of segments#

Select the first magnet to segment by specifying the number of segments. The method accepts as input the list of magnets names to segment, magnet IDs, or the magnet :class:ansys.aedt.core.modeler.cad.object3d.Object3d object. When apply_mesh_sheets is enabled, the mesh sheets are also applied in the geometry. In the following code, the name of the magnet is also given as an input.

[6]:
segments_number = 2
object_name = "PM_I1"
sheets_1 = m3d.modeler.objects_segmentation(
    object_name,
    segments=segments_number,
    apply_mesh_sheets=True,
    mesh_sheets=3,
)
PyAEDT INFO: Modeler class has been initialized! Elapsed time: 0m 0sec

Segment second magnet by specifying number of segments#

Select the second magnet to segment by specifying the number of segments. The following code gives the ID of the magnet as an input.

[7]:
segments_number = 2
object_name = "PM_I1_1"
magnet_id = [obj.id for obj in m3d.modeler.object_list if obj.name == object_name][0]
sheets_2 = m3d.modeler.objects_segmentation(
    magnet_id, segments=segments_number, apply_mesh_sheets=True
)

Segment third magnet by specifying segmentation thickness#

Select the third magnet to segment by specifying the segmentation thickness. The following code gives the magnet object type ansys.aedt.core.modeler.cad.object3d.Object3d as an input.

[8]:
segmentation_thickness = 1
object_name = "PM_O1"
magnet = [obj for obj in m3d.modeler.object_list if obj.name == object_name][0]
sheets_3 = m3d.modeler.objects_segmentation(
    magnet, segmentation_thickness=segmentation_thickness, apply_mesh_sheets=True
)

Segment fourth magnet by specifying number of segments#

Select the fourth magnet to segment by specifying the number of segments. The following code gives the name of the magnet as input and disables the mesh sheets.

[9]:
object_name = "PM_O1_1"
segments_number = 2
sheets_4 = m3d.modeler.objects_segmentation(object_name, segments=segments_number)

Release AEDT#

[10]:
m3d.save_project()
m3d.release_desktop()
# Wait 3 seconds to allow AEDT to shut down before cleaning the temporary directory.
time.sleep(3)
PyAEDT INFO: Project Motor3D_obj_segments Saved correctly
PyAEDT INFO: Desktop has been released and closed.

Clean up#

All project files are saved in the folder temp_folder.name. If you’ve run this example as a Jupyter notebook, you can retrieve those project files. The following cell removes all temporary files, including the project folder.

[11]:
temp_folder.cleanup()

Download this example

Download this example as a Jupyter Notebook or as a Python script.