AEDT open project example#
This example shows how to use the AEDTCommon
class to launch a new AEDT session in a thread and open an existing AEDT project.
Perform required imports#
Perform the required imports.
[1]:
import sys
import os
import shutil
[2]:
from ansys.aedt.core import generate_unique_folder_name
[3]:
from ansys.aedt.toolkits.common.utils import download_file
from ansys.aedt.toolkits.common.backend.api import AEDTCommon
Initialize temporary folder and project settings#
Initialize a temporary folder to copy the input file into and specify project settings.
[4]:
URL_BASE = "https://raw.githubusercontent.com/ansys/example-data/master/toolkits/common/"
AEDT_PROJECT = "Test.aedt"
URL = os.path.join(URL_BASE, AEDT_PROJECT)
temp_folder = os.path.join(generate_unique_folder_name())
local_project = os.path.join(temp_folder, AEDT_PROJECT)
download_file(URL, local_project)
[4]:
'C:\\Users\\ansys\\AppData\\Local\\Temp\\pyaedt_prj_E53\\Test.aedt'
Initialize toolkit#
Initialize the toolkit.
[5]:
toolkit = AEDTCommon()
Initialize AEDT#
Launch a new AEDT session in a thread.
[6]:
thread_msg = toolkit.launch_thread(toolkit.launch_aedt)
DEBUG - Starting thread: Toolkit_Thread
DEBUG - Toolkit is not connected to AEDT.
DEBUG - Launching AEDT.
Wait for the toolkit thread to be idle#
Wait for the toolkit thread to be idle and ready to accept a new task.
[7]:
idle = toolkit.wait_to_be_idle()
if not idle:
print("AEDT not initialized.")
sys.exit()
DEBUG - Toolkit is busy and processing a task.
DEBUG - Toolkit is busy and processing a task.
DEBUG - Toolkit is busy and processing a task.
DEBUG - Toolkit is busy and processing a task.
DEBUG - Toolkit is busy and processing a task.
DEBUG - Toolkit is busy and processing a task.
DEBUG - Toolkit is idle and ready to accept a new task.
Open project#
Open the project.
[8]:
open_msg = toolkit.open_project(local_project)
DEBUG - Toolkit is not connected to AEDT.
DEBUG - Connecting 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.15.2.
PyAEDT INFO: Initializing new Desktop session.
PyAEDT WARNING: Argument `specified_version` is deprecated for method `__init__`; use `version` instead.
PyAEDT WARNING: Argument `new_desktop_session` is deprecated for method `__init__`; use `new_desktop` instead.
PyAEDT INFO: Log on console is enabled.
PyAEDT INFO: Log on file C:\Users\ansys\AppData\Local\Temp\pyaedt_ansys_b6e9ab50-557e-462c-a034-eaf1c67c4df2.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: Connecting to AEDT session on gRPC port 56114.
PyAEDT INFO: AEDT installation Path C:\Program Files\ANSYS Inc\v251\AnsysEM
DEBUG - Toolkit is connected to AEDT.
DEBUG - Project C:\Users\ansys\AppData\Local\Temp\pyaedt_prj_E53\Test.aedt is opened
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
File C:\actions-runner\_work\pyaedt-toolkits-common\pyaedt-toolkits-common\.venv\lib\site-packages\ansys\aedt\core\internal\grpc_plugin_dll_class.py:118, in AedtObjWrapper.__Invoke__(self, funcName, argv)
117 self.dllapi.recreate_application(True)
--> 118 ret = _retry_ntimes(
119 settings.number_of_grpc_api_retries,
120 self.dllapi.AedtAPI.InvokeAedtObjMethod,
121 self.objectID,
122 funcName,
123 argv,
124 ) # Call C function
125 if ret and isinstance(ret, (AedtObjWrapper, AedtPropServer)):
File C:\actions-runner\_work\pyaedt-toolkits-common\pyaedt-toolkits-common\.venv\lib\site-packages\ansys\aedt\core\generic\general_methods.py:492, in _retry_ntimes(n, function, *args, **kwargs)
491 if "__name__" in dir(function):
--> 492 raise AttributeError(f"Error in Executing Method {function.__name__}.")
493 else:
AttributeError: Error in Executing Method InvokeAedtObjMethod.
During handling of the above exception, another exception occurred:
GrpcApiError Traceback (most recent call last)
Cell In[8], line 1
----> 1 open_msg = toolkit.open_project(local_project)
File C:\actions-runner\_work\pyaedt-toolkits-common\pyaedt-toolkits-common\.venv\lib\site-packages\ansys\aedt\toolkits\common\backend\api.py:711, in AEDTCommon.open_project(self, project_name)
709 self.desktop.odesktop.OpenProject(project_name)
710 logger.debug("Project {} is opened".format(project_name))
--> 711 self.__save_project_info()
712 self.release_aedt(False, False)
713 return True
File C:\actions-runner\_work\pyaedt-toolkits-common\pyaedt-toolkits-common\.venv\lib\site-packages\ansys\aedt\toolkits\common\backend\api.py:899, in AEDTCommon.__save_project_info(self)
897 active_project = self.desktop.odesktop.SetActiveProject(project_list[0])
898 active_project_name = active_project.GetName()
--> 899 active_design = active_project.GetActiveDesign()
901 # Save active design info
902 if active_design:
File C:\actions-runner\_work\pyaedt-toolkits-common\pyaedt-toolkits-common\.venv\lib\site-packages\ansys\aedt\core\internal\grpc_plugin_dll_class.py:138, in AedtObjWrapper.__GetObjMethod__.<locals>.DynamicFunc(self, *args)
137 def DynamicFunc(self, *args):
--> 138 return self.__Invoke__(funcName, args)
File C:\actions-runner\_work\pyaedt-toolkits-common\pyaedt-toolkits-common\.venv\lib\site-packages\ansys\aedt\core\internal\grpc_plugin_dll_class.py:129, in AedtObjWrapper.__Invoke__(self, funcName, argv)
127 return ret
128 except Exception: # pragma: no cover
--> 129 raise GrpcApiError(f"Failed to execute gRPC AEDT command: {funcName}")
GrpcApiError: Failed to execute gRPC AEDT command: GetActiveDesign
Get toolkit properties#
Get the toolkit properties, which contain the project information.
[9]:
new_properties = toolkit.get_properties()
Connect design#
Connect or create a new design.
[10]:
toolkit.connect_design()
DEBUG - Toolkit is connected to process 4648. on Grpc 56114.
DEBUG - Toolkit is connected to AEDT.
PyAEDT WARNING: Argument `designname` is deprecated for method `__init__`; use `design` instead.
PyAEDT WARNING: Argument `projectname` is deprecated for method `__init__`; use `project` instead.
PyAEDT WARNING: Argument `specified_version` is deprecated for method `__init__`; use `version` instead.
PyAEDT WARNING: Argument `new_desktop_session` is deprecated for method `__init__`; use `new_desktop` instead.
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.15.2.
PyAEDT INFO: Returning found Desktop session with PID 4648!
PyAEDT INFO: No project is defined. Project Test exists and has been read.
PyAEDT INFO: Added design 'HFSS_FKY2GJ' of type HFSS.
PyAEDT INFO: Aedt Objects correctly read
PyAEDT INFO: Project Test Saved correctly
DEBUG - Project name: Test
INFO - Updating internal properties.
DEBUG - Updating 'project_list' with value ['C:/Users/ansys/AppData/Local/Temp/pyaedt_prj_E53/Test.aedt']
DEBUG - Updating 'active_design' with value HFSS_FKY2GJ
DEBUG - Updating 'active_project' with value C:/Users/ansys/AppData/Local/Temp/pyaedt_prj_E53/Test.aedt
DEBUG - Updating 'design_list' with value {'Test': ['HFSS_FKY2GJ']}
DEBUG - Properties were updated successfully.
INFO - Toolkit is connected to AEDT design.
[10]:
True
Create a box#
Create a box in the design.
[11]:
toolkit.logger.info("Create Box")
box = toolkit.aedtapp.modeler.create_box([10, 10, 10], [20, 20, 20])
model = box.plot(show=False)
model.show_grid = False
model.plot(os.path.join(toolkit.aedtapp.working_directory, "Image.jpg"))
INFO - Create Box
PyAEDT INFO: Modeler class has been initialized! Elapsed time: 0m 1sec
PyAEDT INFO: Materials class has been initialized! Elapsed time: 0m 0sec
PyAEDT ERROR: **************************************************************
PyAEDT ERROR: File "C:\Python\Python310\Lib\runpy.py", line 196, in _run_module_as_main
PyAEDT ERROR: return _run_code(code, main_globals, None,
PyAEDT ERROR: File "C:\Python\Python310\Lib\runpy.py", line 86, in _run_code
PyAEDT ERROR: exec(code, run_globals)
PyAEDT ERROR: File "C:\actions-runner\_work\pyaedt-toolkits-common\pyaedt-toolkits-common\.venv\lib\site-packages\ipykernel_launcher.py", line 18, in <module>
PyAEDT ERROR: app.launch_new_instance()
PyAEDT ERROR: File "C:\actions-runner\_work\pyaedt-toolkits-common\pyaedt-toolkits-common\.venv\lib\site-packages\traitlets\config\application.py", line 1075, in launch_instance
PyAEDT ERROR: app.start()
PyAEDT ERROR: File "C:\actions-runner\_work\pyaedt-toolkits-common\pyaedt-toolkits-common\.venv\lib\site-packages\ipykernel\kernelapp.py", line 739, in start
PyAEDT ERROR: self.io_loop.start()
PyAEDT ERROR: File "C:\actions-runner\_work\pyaedt-toolkits-common\pyaedt-toolkits-common\.venv\lib\site-packages\tornado\platform\asyncio.py", line 205, in start
PyAEDT ERROR: self.asyncio_loop.run_forever()
PyAEDT ERROR: File "C:\Python\Python310\Lib\asyncio\base_events.py", line 603, in run_forever
PyAEDT ERROR: self._run_once()
PyAEDT ERROR: File "C:\Python\Python310\Lib\asyncio\base_events.py", line 1909, in _run_once
PyAEDT ERROR: handle._run()
PyAEDT ERROR: File "C:\Python\Python310\Lib\asyncio\events.py", line 80, in _run
PyAEDT ERROR: self._context.run(self._callback, *self._args)
PyAEDT ERROR: File "C:\actions-runner\_work\pyaedt-toolkits-common\pyaedt-toolkits-common\.venv\lib\site-packages\ipykernel\kernelbase.py", line 545, in dispatch_queue
PyAEDT ERROR: await self.process_one()
PyAEDT ERROR: File "C:\actions-runner\_work\pyaedt-toolkits-common\pyaedt-toolkits-common\.venv\lib\site-packages\ipykernel\kernelbase.py", line 534, in process_one
PyAEDT ERROR: await dispatch(*args)
PyAEDT ERROR: File "C:\actions-runner\_work\pyaedt-toolkits-common\pyaedt-toolkits-common\.venv\lib\site-packages\ipykernel\kernelbase.py", line 437, in dispatch_shell
PyAEDT ERROR: await result
PyAEDT ERROR: File "C:\actions-runner\_work\pyaedt-toolkits-common\pyaedt-toolkits-common\.venv\lib\site-packages\ipykernel\ipkernel.py", line 362, in execute_request
PyAEDT ERROR: await super().execute_request(stream, ident, parent)
PyAEDT ERROR: File "C:\actions-runner\_work\pyaedt-toolkits-common\pyaedt-toolkits-common\.venv\lib\site-packages\ipykernel\kernelbase.py", line 778, in execute_request
PyAEDT ERROR: reply_content = await reply_content
PyAEDT ERROR: File "C:\actions-runner\_work\pyaedt-toolkits-common\pyaedt-toolkits-common\.venv\lib\site-packages\ipykernel\ipkernel.py", line 449, in do_execute
PyAEDT ERROR: res = shell.run_cell(
PyAEDT ERROR: File "C:\actions-runner\_work\pyaedt-toolkits-common\pyaedt-toolkits-common\.venv\lib\site-packages\ipykernel\zmqshell.py", line 549, in run_cell
PyAEDT ERROR: return super().run_cell(*args, **kwargs)
PyAEDT ERROR: File "C:\Users\ansys\AppData\Local\Temp\ipykernel_696\396100405.py", line 3, in <module>
PyAEDT ERROR: model = box.plot(show=False)
PyAEDT ERROR: File "C:\actions-runner\_work\pyaedt-toolkits-common\pyaedt-toolkits-common\.venv\lib\site-packages\ansys\aedt\core\internal\checks.py", line 76, in wrapper
PyAEDT ERROR: raise AEDTRuntimeError("The AEDT desktop object is not available.")
PyAEDT ERROR: The aedt desktop object is not available. on wrapper
PyAEDT ERROR: Last Electronics Desktop Message - [error] the project is not activated
PyAEDT ERROR: **************************************************************
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[11], line 4
2 box = toolkit.aedtapp.modeler.create_box([10, 10, 10], [20, 20, 20])
3 model = box.plot(show=False)
----> 4 model.show_grid = False
5 model.plot(os.path.join(toolkit.aedtapp.working_directory, "Image.jpg"))
AttributeError: 'bool' object has no attribute 'show_grid'
Save and release AEDT#
Save and release AEDT.
[12]:
toolkit.release_aedt(True, True)
PyAEDT INFO: Desktop has been released and closed.
INFO - AEDT is released.
[12]:
True
Remove temporary folder#
Remove the temporary folder.
[13]:
shutil.rmtree(temp_folder, ignore_errors=True)