Skip to content

Commit

Permalink
Example
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuelopez-ansys committed Jan 29, 2024
1 parent 25e620e commit 46f905b
Show file tree
Hide file tree
Showing 28 changed files with 463 additions and 342 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
exclude: |
(?x)(
src/ansys/aedt/toolkits/common/ui/ui_templates/.* |
src/ansys/aedt/toolkits/common/ui/.* |
tests/input_data/.* |
examples/input_files/.*
)
Expand Down
7 changes: 2 additions & 5 deletions examples/backend/rest_api_aedt_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
def get_materials():
logger.info("[GET] /Rest API dummy call.")

exit_code, msg = toolkit_api.get_thread_status()
if exit_code <= 0:
return jsonify(msg), 200
else:
return jsonify(msg), 500
status = toolkit_api.get_thread_status()
return jsonify(status.value), 200


if __name__ == "__main__":
Expand Down
16 changes: 2 additions & 14 deletions examples/toolkit/pyaedt_toolkit/backend/api.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
import json
import os

from models import properties
import numpy as np

from ansys.aedt.toolkits.common.backend.properties import properties

# Load toolkit properties
with open(os.path.join(os.path.dirname(__file__), "properties.json")) as fh:
_properties = json.load(fh)
properties._unfreeze()
for key, value in _properties.items():
setattr(properties, key, value)
properties._freeze()

from ansys.aedt.toolkits.common.backend.api import AEDTCommon
from ansys.aedt.toolkits.common.backend.logger_handler import logger

Expand Down Expand Up @@ -40,7 +28,7 @@ class ToolkitBackend(AEDTCommon):

def __init__(self):
"""Initialize the ``Toolkit`` class."""
Backend.__init__(self)
AEDTCommon.__init__(self, properties)
self.multiplier = 1.0

def create_geometry(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"toolkit_name" : "example",
"multiplier" : 1.0,
"geometry" : "Box",
"log_file" : "example_backend.log"
"log_file" : "example_backend.log",
"debug" : false
}
38 changes: 38 additions & 0 deletions examples/toolkit/pyaedt_toolkit/backend/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import json
import os

from pydantic import BaseModel

from ansys.aedt.toolkits.common.backend.models import CommonProperties
from ansys.aedt.toolkits.common.backend.models import common_properties


class BackendProperties(BaseModel):
"""Store toolkit properties."""

multiplier: float = 1.0
geometry: str = "Box"


class Properties(BackendProperties, CommonProperties, validate_assignment=True):
"""Store all properties."""


backend_properties = {}
if os.path.expanduser(os.path.join(os.path.dirname(__file__), "backend_properties.json")):
with open(os.path.join(os.path.dirname(__file__), "backend_properties.json")) as file_handler:
backend_properties = json.load(file_handler)

toolkit_property = {}
if backend_properties:
for backend_key in backend_properties:
if hasattr(common_properties, backend_key):
setattr(common_properties, backend_key, backend_properties[backend_key])
else:
toolkit_property[backend_key] = backend_properties[backend_key]

new_common_properties = {}
for common_key in common_properties:
new_common_properties[common_key[0]] = common_key[1]

properties = Properties(**toolkit_property, **new_common_properties)
10 changes: 5 additions & 5 deletions examples/toolkit/pyaedt_toolkit/run_toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@

import psutil
import requests
import ui
from ui.models import properties

import backend
import ui
from ansys.aedt.toolkits.common.ui.properties import general_settings

# Define global variables or constants
BACKEND_FILE = os.path.join(backend.__path__[0], "rest_api.py")
FRONTEND_FILE = os.path.join(ui.__path__[0], "run.py")
IS_LINUX = os.name == "posix"
URL = general_settings.backend_url
PORT = general_settings.backend_port
URL = properties.backend_url
PORT = properties.backend_port
PYTHON_PATH = sys.executable
KILL_BACKEND = True
BACKEND_COMMAND = [PYTHON_PATH, BACKEND_FILE]
Expand Down Expand Up @@ -125,7 +125,7 @@ def process_desktop_properties():
"aedt_version": desktop_version,
"use_grpc": grpc,
}
requests.put(URL_CALL + "/set_properties", json=properties)
requests.put(URL_CALL + "/properties", json=properties)
# requests.post(URL_CALL + "/launch_aedt")


Expand Down
23 changes: 11 additions & 12 deletions examples/toolkit/pyaedt_toolkit/ui/actions.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
from pyaedt.generic.general_methods import generate_unique_project_name
import requests

from ansys.aedt.toolkits.common.ui.actions_generic import FrontendGeneric
from ansys.aedt.toolkits.common.ui.logger_handler import logger

from pyaedt.generic.general_methods import generate_unique_project_name


class Frontend(FrontendGeneric):
def __init__(self):
FrontendGeneric.__init__(self)

def create_geometry_toolkit(self, project_selected=None, design_selected=None, geometry="Box", multiplier=1):
# Set active project and design
self.get_properties()
be_properties = self.get_properties()
if project_selected and design_selected:
if project_selected == "No Project":
project_selected = generate_unique_project_name()
project_selected = self.get_project_name(project_selected)

for project in self.be_properties.project_list:
for project in be_properties["project_list"]:
if self.get_project_name(project) == project_selected:
self.be_properties.active_project = project
if project_selected in list(self.be_properties.design_list.keys()):
designs = self.be_properties.design_list[project_selected]
be_properties["active_project"] = project
if project_selected in list(be_properties["design_list"].keys()):
designs = be_properties["design_lis"][project_selected]
for design in designs:
if design_selected == design:
self.be_properties.active_design = design
be_properties["active_design"] = design
break
break
else:
project_selected = generate_unique_project_name()
project_selected = self.get_project_name(project_selected)

# Multiplier and geometry
self.be_properties.multiplier = float(multiplier)
self.be_properties.geometry = geometry
self.be_properties.active_project = project_selected
self.set_properties()
be_properties["multiplier"] = float(multiplier)
be_properties["geometry"] = geometry
be_properties["active_project"] = project_selected
self.set_properties(be_properties)

response = requests.post(self.url + "/create_geometry")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
"toolkit_name" : "example",
"backend_url" : "127.0.0.1",
"backend_port" : 5001,
"debug" : true,
"debug" : false,
"app_name": "Example Wizard",
"main_title": "Amazing Toolkit Wizard",
"welcome_message": "Welcome to the future",
"version" : "v0.0.1",
"copyright" : "By: slopez",
"year" : 2023,
"theme" : "ansys.json",
"high_resolution": true,
"high_resolution": false,
"add_left_menus" : [
{
"btn_icon": "icon_home.svg",
Expand All @@ -23,16 +23,8 @@
{
"btn_icon": "icon_signal.svg",
"btn_id": "geometry_menu",
"btn_text": "Modeler",
"btn_tooltip": "Modeler",
"show_top": true,
"is_active": false
},
{
"btn_icon": "icon_signal.svg",
"btn_id": "geometry_menu2",
"btn_text": "Modeler",
"btn_tooltip": "Modeler",
"btn_text": "Primitives Builder",
"btn_tooltip": "Builder",
"show_top": true,
"is_active": false
},
Expand All @@ -44,5 +36,6 @@
"show_top": false,
"is_active": false
}
]
],
"primitives_created" : []
}
39 changes: 39 additions & 0 deletions examples/toolkit/pyaedt_toolkit/ui/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import json
import os
from typing import List

from pydantic import BaseModel
from pydantic import Field

from ansys.aedt.toolkits.common.ui.models import UIProperties
from ansys.aedt.toolkits.common.ui.models import general_settings


class FrontendProperties(BaseModel):
"""Store toolkit properties."""

primitives_created: List[str] = Field(default_factory=list)


class Properties(FrontendProperties, UIProperties, validate_assignment=True):
"""Store all properties."""


backend_properties = {}
if os.path.exists(os.path.join(os.path.dirname(__file__), "frontend_properties.json")):
with open(os.path.join(os.path.dirname(__file__), "frontend_properties.json")) as file_handler:
backend_properties = json.load(file_handler)

toolkit_property = {}
if backend_properties:
for backend_key in backend_properties:
if hasattr(general_settings, backend_key):
setattr(general_settings, backend_key, backend_properties[backend_key])
else:
toolkit_property[backend_key] = backend_properties[backend_key]

new_common_properties = {}
for common_key in general_settings:
new_common_properties[common_key[0]] = common_key[1]

properties = Properties(**toolkit_property, **new_common_properties)
49 changes: 20 additions & 29 deletions examples/toolkit/pyaedt_toolkit/ui/run.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,52 @@
import os
import sys
import json

from PySide6.QtWidgets import QApplication

# Toolkit frontend API
from actions import Frontend

# Default properties
from ansys.aedt.toolkits.common.ui.properties import general_settings
from models import properties

# Load toolkit properties
with open(os.path.join(os.path.dirname(__file__), "properties.json")) as fh:
_properties = json.load(fh)
for key, value in _properties.items():
if hasattr(general_settings, key):
setattr(general_settings, key, value)
# Toolkit windows
from windows.create_geometry.geometry_menu import GeometryMenu

# Import general common frontend modules
from ansys.aedt.toolkits.common.ui.logger_handler import logger
from ansys.aedt.toolkits.common.ui.main_window.main_window_layout import MainWindowLayout
from ansys.aedt.toolkits.common.ui.common_windows.home_menu import HomeMenu
from ansys.aedt.toolkits.common.ui.common_windows.main_window import MainWindow
from ansys.aedt.toolkits.common.ui.common_windows.settings_column import SettingsMenu
from ansys.aedt.toolkits.common.ui.common_windows.home_menu import HomeMenu

# Toolkit frontend API
from actions import Frontend

# Toolkit windows
from windows.create_geometry.geometry_menu import GeometryMenu
# Import general common frontend modules
from ansys.aedt.toolkits.common.ui.logger_handler import logger
from ansys.aedt.toolkits.common.ui.main_window.main_window_layout import MainWindowLayout

# Backend URL and port
url = general_settings.backend_url
port = general_settings.backend_port
url = properties.backend_url
port = properties.backend_port

os.environ["QT_API"] = "pyside6"
os.environ["QT_FONT_DPI"] = "96"

if general_settings.high_resolution:
if properties.high_resolution:
os.environ["QT_SCALE_FACTOR"] = "2"


class ApplicationWindow(Frontend):
def __init__(self):
self.thread = None
self.general_settings = general_settings
self.properties = properties

Frontend.__init__(self)

self.url = f"http://{url}:{port}"

# General Settings

# Create user interface object
self.ui = MainWindowLayout(self)
self.ui.setup_ui()

# Setup main
dummy = r"C:\AnsysDev\repos\pyaedt-toolkits-common\src\ansys\aedt\toolkits\common\ui\utils\images\icons\dummy.svg"
self.main_window = MainWindow(self)
self.main_window.setup_gui(main_window_logo=dummy)
self.main_window.setup_gui()

# Settings menu
self.settings_menu = SettingsMenu(self)
Expand All @@ -74,7 +65,7 @@ def __init__(self):
self.settings_menu.aedt_session.addItem("Backend OFF")
else:
# Get default properties
self.get_properties()
be_properties = self.get_properties()
# Get AEDT installed versions
installed_versions = self.installed_versions()
self.settings_menu.signal_flag = False
Expand All @@ -85,8 +76,8 @@ def __init__(self):
else:
self.settings_menu.aedt_version.addItem("AEDT not installed")
self.settings_menu.signal_flag = True
if hasattr(self.be_properties, "aedt_version") and self.be_properties.aedt_version in installed_versions:
self.settings_menu.aedt_version.setCurrentText(self.be_properties.aedt_version)
if be_properties.get("aedt_version") in installed_versions:
self.settings_menu.aedt_version.setCurrentText(be_properties.get("aedt_version"))

# Toolkit specific wizard starts here

Expand All @@ -103,7 +94,7 @@ def __init__(self):

def geometry_menu_clicked(self):
selected_menu = self.main_window.get_selected_menu()
if selected_menu.objectName() == 'geometry_menu':
if selected_menu.objectName() == "geometry_menu":
self.ui.set_page(self.geometry_menu.geometry_menu_widget)


Expand Down
Loading

0 comments on commit 46f905b

Please sign in to comment.