{ "cells": [ { "cell_type": "markdown", "id": "b5067fcd", "metadata": {}, "source": [ "# Magnet segmentation" ] }, { "cell_type": "markdown", "id": "0ccd14bc", "metadata": {}, "source": [ "This example shows how to use PyAEDT to segment magnets of an electric motor.\n", "The method is valid and usable for any object you would like to segment.\n", "\n", "Keywords: **Maxwell 3D**, **Magnet segmentation**." ] }, { "cell_type": "markdown", "id": "499d19b2", "metadata": {}, "source": [ "## Perform imports and define constants\n", "\n", "Perform required imports." ] }, { "cell_type": "code", "execution_count": null, "id": "66f2cba1", "metadata": {}, "outputs": [], "source": [ "import tempfile\n", "import time" ] }, { "cell_type": "code", "execution_count": null, "id": "1c415aa1", "metadata": {}, "outputs": [], "source": [ "import ansys.aedt.core" ] }, { "cell_type": "markdown", "id": "f9d45b29", "metadata": {}, "source": [ "Define constants." ] }, { "cell_type": "code", "execution_count": null, "id": "e9e9717c", "metadata": {}, "outputs": [], "source": [ "AEDT_VERSION = \"2024.2\"\n", "NG_MODE = False # Open AEDT UI when it is launched." ] }, { "cell_type": "markdown", "id": "720dc075", "metadata": {}, "source": [ "## Create temporary directory\n", "\n", "Create a temporary directory where downloaded data or\n", "dumped data can be stored.\n", "If you'd like to retrieve the project data for subsequent use,\n", "the temporary folder name is given by ``temp_folder.name``." ] }, { "cell_type": "code", "execution_count": null, "id": "54560a42", "metadata": {}, "outputs": [], "source": [ "temp_folder = tempfile.TemporaryDirectory(suffix=\".ansys\")" ] }, { "cell_type": "markdown", "id": "3cd8ef97", "metadata": {}, "source": [ "## Download AEDT file example\n", "\n", "Set the local temporary folder to export the AEDT file to." ] }, { "cell_type": "code", "execution_count": null, "id": "ca255a52", "metadata": {}, "outputs": [], "source": [ "aedt_file = ansys.aedt.core.downloads.download_file(\n", " source=\"object_segmentation\",\n", " name=\"Motor3D_obj_segments.aedt\",\n", " destination=temp_folder.name,\n", ")" ] }, { "cell_type": "markdown", "id": "75ddcb1a", "metadata": {}, "source": [ "## Launch Maxwell 3D\n", "\n", "Launch Maxwell 3D, providing the version, rgw path to the project, and the graphical mode." ] }, { "cell_type": "code", "execution_count": null, "id": "cbc9aa5e", "metadata": {}, "outputs": [], "source": [ "m3d = ansys.aedt.core.Maxwell3d(\n", " project=aedt_file,\n", " version=AEDT_VERSION,\n", " new_desktop=True,\n", " non_graphical=NG_MODE,\n", ")" ] }, { "cell_type": "markdown", "id": "e7edfc8e", "metadata": {}, "source": [ "## Segment first magnet by specifying number of segments\n", "\n", "Select the first magnet to segment by specifying the number of segments.\n", "The method accepts as input the list of magnets names to segment,\n", "magnet IDs, or the magnet :class:`ansys.aedt.core.modeler.cad.object3d.Object3d` object.\n", "When ``apply_mesh_sheets`` is enabled, the mesh sheets are also\n", "applied in the geometry.\n", "In the following code, the name of the magnet is also given as an input." ] }, { "cell_type": "code", "execution_count": null, "id": "bf44947c", "metadata": {}, "outputs": [], "source": [ "segments_number = 2\n", "object_name = \"PM_I1\"\n", "sheets_1 = m3d.modeler.objects_segmentation(\n", " object_name,\n", " segments=segments_number,\n", " apply_mesh_sheets=True,\n", " mesh_sheets=3,\n", ")" ] }, { "cell_type": "markdown", "id": "e9d23c4b", "metadata": {}, "source": [ "## Segment second magnet by specifying number of segments\n", "\n", "Select the second magnet to segment by specifying the number of segments.\n", "The following code gives the ID of the magnet as an input." ] }, { "cell_type": "code", "execution_count": null, "id": "04a31615", "metadata": {}, "outputs": [], "source": [ "segments_number = 2\n", "object_name = \"PM_I1_1\"\n", "magnet_id = [obj.id for obj in m3d.modeler.object_list if obj.name == object_name][0]\n", "sheets_2 = m3d.modeler.objects_segmentation(\n", " magnet_id, segments=segments_number, apply_mesh_sheets=True\n", ")" ] }, { "cell_type": "markdown", "id": "90c3f832", "metadata": {}, "source": [ "## Segment third magnet by specifying segmentation thickness\n", "\n", "Select the third magnet to segment by specifying the segmentation thickness.\n", "The following code gives the magnet object type `ansys.aedt.core.modeler.cad.object3d.Object3d`\n", "as an input." ] }, { "cell_type": "code", "execution_count": null, "id": "2f04f452", "metadata": {}, "outputs": [], "source": [ "segmentation_thickness = 1\n", "object_name = \"PM_O1\"\n", "magnet = [obj for obj in m3d.modeler.object_list if obj.name == object_name][0]\n", "sheets_3 = m3d.modeler.objects_segmentation(\n", " magnet, segmentation_thickness=segmentation_thickness, apply_mesh_sheets=True\n", ")" ] }, { "cell_type": "markdown", "id": "9f00125f", "metadata": {}, "source": [ "## Segment fourth magnet by specifying number of segments\n", "\n", "Select the fourth magnet to segment by specifying the number of segments.\n", "The following code gives the name of the magnet as input and disables the mesh sheets." ] }, { "cell_type": "code", "execution_count": null, "id": "a118fe22", "metadata": {}, "outputs": [], "source": [ "object_name = \"PM_O1_1\"\n", "segments_number = 2\n", "sheets_4 = m3d.modeler.objects_segmentation(object_name, segments=segments_number)" ] }, { "cell_type": "markdown", "id": "a6bc1743", "metadata": {}, "source": [ "## Release AEDT" ] }, { "cell_type": "code", "execution_count": null, "id": "220b4864", "metadata": {}, "outputs": [], "source": [ "m3d.save_project()\n", "m3d.release_desktop()\n", "# Wait 3 seconds to allow AEDT to shut down before cleaning the temporary directory.\n", "time.sleep(3)" ] }, { "cell_type": "markdown", "id": "e42f5346", "metadata": {}, "source": [ "## Clean up\n", "\n", "All project files are saved in the folder ``temp_folder.name``.\n", "If you've run this example as a Jupyter notebook, you\n", "can retrieve those project files. The following cell\n", "removes all temporary files, including the project folder." ] }, { "cell_type": "code", "execution_count": null, "id": "061b8874", "metadata": {}, "outputs": [], "source": [ "temp_folder.cleanup()" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" } }, "nbformat": 4, "nbformat_minor": 5 }