Skip to content

FIX: Adding close desktop function #6052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog.d/6052.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Adding close desktop function
29 changes: 29 additions & 0 deletions src/ansys/aedt/core/filtersolutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

import warnings

from ansys.aedt.core import Circuit
from ansys.aedt.core import Hfss
from ansys.aedt.core import Hfss3dLayout
from ansys.aedt.core import settings
import ansys.aedt.core.filtersolutions_core
from ansys.aedt.core.filtersolutions_core.attributes import Attributes
Expand Down Expand Up @@ -96,6 +99,32 @@ def _cleanup_resources(self):
self.geometry = None
self.radial = None

def _create_design(self, desktop_version, desktop_process_id):
"""Create a new design in AEDT.
This method is called to create an ``AEDT`` object when the design is exported to ``AEDT``.

Parameters
----------
desktop_version : str
Version of AEDT in ``xxxx.x`` format.

desktop_process_id : int
Process ID of the AEDT instance.

Returns
-------
:class:``AEDT`` design object
"""
if isinstance(FilterDesignBase._active_design, LumpedDesign):
return Circuit(version=desktop_version, aedt_process_id=desktop_process_id)
elif isinstance(FilterDesignBase._active_design, DistributedDesign):
if getattr(self, "insert_hfss_3dl_design", True):
return Hfss3dLayout(version=desktop_version, aedt_process_id=desktop_process_id)
elif getattr(self, "insert_hfss_design", True):
return Hfss(version=desktop_version, aedt_process_id=desktop_process_id)
elif getattr(self, "insert_circuit_design", True):
return Circuit(version=desktop_version, aedt_process_id=desktop_process_id)


class LumpedDesign(FilterDesignBase):
"""Provides the `FilterSolutions` application interface for lumped filter designs.
Expand Down
30 changes: 27 additions & 3 deletions src/ansys/aedt/core/filtersolutions_core/export_to_aedt.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ def _define_export_to_desktop_dll_functions(self):
self._dll.getOptimizeAfterExport.argtype = POINTER(c_bool)
self._dll.getOptimizeAfterExport.restype = c_int

self._dll.exportDesign.argtypes = [c_int, c_int, c_char_p, POINTER(c_int)]
self._dll.exportDesign.restype = c_int

self._dll.loadLibraryPartsConf.argtype = c_char_p
self._dll.loadLibraryPartsConf.restype = c_int

Expand Down Expand Up @@ -861,6 +864,13 @@ def export_design(self, export_format=None, export_creation_mode=None, export_pa
When exporting to ``AEDT``, the design can either be appended to an existing project or overwrite it.
When generating a Python script, the script is created and saved to the specified file location.

Returns the design object for an exported design when ``export_format``
is set to ``ExportFormat.DIRECT_TO_AEDT``.

The returned object type is one of ``Circuit``, ``Hfss``, or ``Hfss3dLayout``.

Returns ``None`` if ``export_format`` is set to ``ExportFormat.PYTHON_SCRIPT``.

Parameters
----------
export_format : `ExportFormat`
Expand All @@ -872,21 +882,35 @@ def export_design(self, export_format=None, export_creation_mode=None, export_pa
export_path : str
The export path for Python script.
The default is ``None``.

Returns
-------
:class: ``AEDT`` design object
"""

desktop_version = getattr(self._dll_interface, "_version")
if export_format is None:
export_format = ExportFormat.DIRECT_TO_AEDT
if export_creation_mode is None:
export_creation_mode = ExportCreationMode.OVERWRITE
if not export_path:
export_path = ""
export_path_bytes = b""
else:
directory_path = os.path.dirname(export_path)
# Check if the directory path exists, if not, create it to ensure the export path is valid
if not os.path.exists(directory_path):
os.makedirs(directory_path)
export_path_bytes = bytes(export_path, "ascii")
status = self._dll.exportDesign(export_format.value, export_creation_mode.value, export_path_bytes)
export_path_bytes = bytes(export_path, "ascii")
desktop_process_id = c_int()
status = self._dll.exportDesign(
export_format.value, export_creation_mode.value, export_path_bytes, byref(desktop_process_id)
)
self._dll_interface.raise_error(status)
if export_format == ExportFormat.DIRECT_TO_AEDT:
design = ansys.aedt.core.filtersolutions.FilterDesignBase._create_design(
self, desktop_version, desktop_process_id.value
)
return design

def load_library_parts_config(self, load_library_parts_config_string):
self._dll_interface.set_string(self._dll.loadLibraryPartsConf, load_library_parts_config_string)
Expand Down
101 changes: 101 additions & 0 deletions tests/system/solvers/test_45_FilterSolutions/test_desktop_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2021 - 2025 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from ansys.aedt.core import Circuit
from ansys.aedt.core import Hfss
from ansys.aedt.core import Hfss3dLayout
from ansys.aedt.core.generic.settings import is_linux
import pytest

from tests.system.solvers.conftest import config


@pytest.mark.skipif(is_linux, reason="FilterSolutions API is not supported on Linux.")
@pytest.mark.skipif(config["desktopVersion"] < "2025.2", reason="Skipped on versions earlier than 2025.1")
class TestClass:
def test_lumped_exported_desktop(self, lumped_design):
schem_name = lumped_design.export_to_aedt.schematic_name
schem_name_length = len(schem_name)
circuit = lumped_design.export_to_aedt.export_design()
assert isinstance(circuit, Circuit)
assert circuit.project_name.split()[0][:schem_name_length] == schem_name
assert circuit.design_name == schem_name
assert next(iter(circuit.setup_sweeps_names)) == "FS_Advanced Setup"
variables = circuit.variable_manager.variables
assert variables["C1"].value == pytest.approx(1.967e-12)
assert variables["L2"].value == pytest.approx(1.288e-8)
assert variables["C3"].value == pytest.approx(6.366e-12)

def test_distributed_circuit_exported_desktop(self, distributed_design):
schem_name = distributed_design.export_to_aedt.schematic_name
schem_name_length = len(schem_name)
distributed_design.export_to_aedt.insert_circuit_design = True
circuit = distributed_design.export_to_aedt.export_design()
assert isinstance(circuit, Circuit)
assert circuit.project_name.split()[0][:schem_name_length] == schem_name
assert circuit.design_name == schem_name
assert next(iter(circuit.setup_sweeps_names)) == "FS_Advanced Setup"
variables = circuit.variable_manager.variables
assert variables["W1"].value == pytest.approx(5.08e-3)
assert variables["W2"].value == pytest.approx(3.175e-4)
assert variables["W3"].value == pytest.approx(5.08e-3)
assert variables["S1"].value == pytest.approx(3.362e-3)
assert variables["S2"].value == pytest.approx(2.172e-2)
assert variables["S3"].value == pytest.approx(1.008e-2)

def test_distributed_hfss3dl_exported_desktop(self, distributed_design):
schem_name = distributed_design.export_to_aedt.schematic_name
schem_name_length = len(schem_name)
distributed_design.export_to_aedt.insert_hfss_3dl_design = True
hfss3dl = distributed_design.export_to_aedt.export_design()
assert isinstance(hfss3dl, Hfss3dLayout)
assert hfss3dl.project_name.split()[0][:schem_name_length] == schem_name
assert hfss3dl.design_name == schem_name
assert next(iter(hfss3dl.setup_sweeps_names)) == "FS_Advanced Setup"
variables = hfss3dl.variable_manager.variables
assert variables["Win"].value == pytest.approx(1.234e-3)
assert variables["W1"].value == pytest.approx(5.08e-3)
assert variables["W2"].value == pytest.approx(3.175e-4)
assert variables["W3"].value == pytest.approx(5.08e-3)
assert variables["S1"].value == pytest.approx(3.36225452227e-3)
assert variables["S2"].value == pytest.approx(2.17231965814e-2)
assert variables["S3"].value == pytest.approx(1.00773795179e-2)

def test_distributed_hfss_exported_desktop(self, distributed_design):
schem_name = distributed_design.export_to_aedt.schematic_name
schem_name_length = len(schem_name)
distributed_design.export_to_aedt.insert_hfss_design = True
hfss = distributed_design.export_to_aedt.export_design()
assert isinstance(hfss, Hfss)
assert hfss.project_name.split()[0][:schem_name_length] == schem_name
assert hfss.design_name == schem_name
assert next(iter(hfss.setup_sweeps_names)) == "FS_Advanced_Setup"
variables = hfss.variable_manager.variables
assert variables["Win"].value == pytest.approx(1.234e-3)
assert variables["W1"].value == pytest.approx(5.08e-3)
assert variables["W2"].value == pytest.approx(3.175e-4)
assert variables["W3"].value == pytest.approx(5.08e-3)
assert variables["S1"].value == pytest.approx(3.36225452227e-3)
assert variables["S2"].value == pytest.approx(2.17231965814e-2)
assert variables["S3"].value == pytest.approx(1.00773795179e-2)
Original file line number Diff line number Diff line change
Expand Up @@ -436,16 +436,20 @@ def test_export_design(self, lumped_design):
assert info.value.args[0] == "Python export path is not specified"
else:
assert info.value.args[0] == "Python export path is not specified."
export_path = resource_path("test_exported_design.py")
design_export_path = resource_path("test_exported_design.py")
lumped_design.export_to_aedt.export_design(
export_format=ExportFormat.PYTHON_SCRIPT,
export_creation_mode=ExportCreationMode.OVERWRITE,
export_path=export_path,
export_path=design_export_path,
)
assert os.path.exists(export_path)
directory_path = os.path.dirname(export_path)
assert os.path.exists(design_export_path)
directory_path = os.path.dirname(design_export_path)
assert os.path.exists(directory_path)
os.remove(export_path)
try:
with open(design_export_path, "a"):
os.remove(design_export_path)
except (OSError, PermissionError):
return

def test_load_library_parts_config(self, lumped_design):
lumped_design.export_to_aedt.load_library_parts_config(resource_path("library_parts.cfg"))
Expand Down