-
Notifications
You must be signed in to change notification settings - Fork 268
Expand file tree
/
Copy pathtemplate_get_started.py
More file actions
270 lines (227 loc) · 10.5 KB
/
Copy pathtemplate_get_started.py
File metadata and controls
270 lines (227 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# -*- coding: utf-8 -*-
#
# Copyright (C) 2021 - 2026 Synopsys, Inc. and ANSYS, Inc. All rights reserved.
# 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 dataclasses import asdict
from dataclasses import dataclass
import os
from pathlib import Path
import tkinter
from tkinter import filedialog
from tkinter import ttk
from typing import Any
from typing import Protocol
from typing import cast
import ansys.aedt.core
from ansys.aedt.core.extensions.misc import ExtensionProjectCommon
from ansys.aedt.core.extensions.misc import get_aedt_version
from ansys.aedt.core.extensions.misc import get_arguments
from ansys.aedt.core.extensions.misc import get_port
from ansys.aedt.core.extensions.misc import get_process_id
from ansys.aedt.core.extensions.misc import is_student
from ansys.aedt.core.generic.design_types import get_pyaedt_app
from ansys.aedt.core.internal.errors import AEDTRuntimeError
PORT = get_port()
"""Port used by the extension."""
VERSION = get_aedt_version()
"""AEDT version used by the extension."""
AEDT_PROCESS_ID = get_process_id()
"""AEDT process identifier."""
IS_STUDENT = is_student()
"""Flag indicating whether the student version is used."""
EXTENSION_DEFAULT_ARGUMENTS = {"origin_x": 0, "origin_y": 0, "origin_z": 0, "radius": 1, "file_path": ""}
"""Default arguments for the extension."""
EXTENSION_TITLE = "Extension template"
"""Title displayed for the extension."""
result = None
"""Value for result."""
class TemplateAppLike(Protocol):
modeler: Any
def load_project(self, file_name: str, set_active: bool = ...) -> object: ...
@dataclass
class ExtensionData:
"""Data class containing user input.
Examples
--------
>>> from ansys.aedt.core.extensions.templates.template_get_started import ExtensionData
>>> data = ExtensionData(origin_x=1.0, origin_y=2.0, origin_z=0.0, radius=5.0)
"""
origin_x: float = 0.0
"""Value for origin x."""
origin_y: float = 0.0
"""Value for origin y."""
origin_z: float = 0.0
"""Value for origin z."""
radius: float = 1
"""Value for radius."""
file_path: str = ""
"""Path to file."""
class TemplateExtension(ExtensionProjectCommon):
"""Extension template to help get started.
Examples
--------
>>> from ansys.aedt.core.extensions.templates.template_get_started import TemplateExtension
>>> extension = TemplateExtension(withdraw=True)
"""
def __init__(self, withdraw: bool = False) -> None:
super().__init__(
EXTENSION_TITLE,
withdraw=withdraw,
add_custom_content=True,
toggle_row=6,
toggle_column=2,
)
def add_extension_content(self) -> None:
"""Add custom content to the extension UI.
Examples
--------
>>> from ansys.aedt.core.extensions.templates.template_get_started import TemplateExtension
>>> extension = TemplateExtension(withdraw=True)
>>> extension.add_extension_content()
"""
# Origin x entry
origin_x_label = ttk.Label(self.root, text="Origin X:", width=20, style="PyAEDT.TLabel")
origin_x_label.grid(row=0, column=0, padx=15, pady=10)
origin_x_entry = tkinter.Text(self.root, width=40, height=1)
origin_x_entry.grid(row=0, column=1, pady=15, padx=10)
origin_x_entry.configure(
background=self.theme.light["pane_bg"], foreground=self.theme.light["text"], font=self.theme.default_font
)
# Origin y entry
origin_y_label = ttk.Label(self.root, text="Origin Y:", width=20, style="PyAEDT.TLabel")
origin_y_label.grid(row=1, column=0, padx=15, pady=10)
origin_y_entry = tkinter.Text(self.root, width=40, height=1)
origin_y_entry.grid(row=1, column=1, pady=15, padx=10)
origin_y_entry.configure(
background=self.theme.light["pane_bg"], foreground=self.theme.light["text"], font=self.theme.default_font
)
# Origin z entry
origin_z_label = ttk.Label(self.root, text="Origin Y:", width=20, style="PyAEDT.TLabel")
origin_z_label.grid(row=2, column=0, padx=15, pady=10)
origin_z_entry = tkinter.Text(self.root, width=40, height=1)
origin_z_entry.grid(row=2, column=1, pady=15, padx=10)
origin_z_entry.configure(
background=self.theme.light["pane_bg"], foreground=self.theme.light["text"], font=self.theme.default_font
)
# Radius entry
radius_label = ttk.Label(self.root, text="Radius:", width=20, style="PyAEDT.TLabel")
radius_label.grid(row=3, column=0, padx=15, pady=10)
radius_entry = tkinter.Text(self.root, width=40, height=1)
radius_entry.grid(row=3, column=1, pady=15, padx=10)
radius_entry.configure(
background=self.theme.light["pane_bg"], foreground=self.theme.light["text"], font=self.theme.default_font
)
# Browse file entry
browse_file_label = ttk.Label(self.root, text="Browse File:", width=20, style="PyAEDT.TLabel")
browse_file_label.grid(row=4, column=0, pady=10)
browse_file_entry = tkinter.Text(self.root, width=40, height=1)
browse_file_entry.grid(row=4, column=1, pady=15, padx=10)
browse_file_entry.configure(
background=self.theme.light["pane_bg"], foreground=self.theme.light["text"], font=self.theme.default_font
)
# Project name info
project_name_label = ttk.Label(self.root, text="Project Name:", width=20, style="PyAEDT.TLabel")
project_name_label.grid(row=5, column=0, pady=10)
project_name_entry = tkinter.Text(self.root, width=40, height=1)
project_name_entry.insert(tkinter.INSERT, self.active_project_name)
project_name_entry.grid(row=5, column=1, pady=15, padx=10)
project_name_entry.configure(
background=self.theme.light["pane_bg"], foreground=self.theme.light["text"], font=self.theme.default_font
)
def callback() -> None:
global result
result = ExtensionData(
origin_x=float(origin_x_entry.get("1.0", tkinter.END).strip() or 0.0),
origin_y=float(origin_y_entry.get("1.0", tkinter.END).strip() or 0.0),
origin_z=float(origin_z_entry.get("1.0", tkinter.END).strip() or 0.0),
radius=float(radius_entry.get("1.0", tkinter.END).strip() or 1.0),
file_path=browse_file_entry.get("1.0", tkinter.END).strip(),
)
self.root.destroy()
def browse_files() -> None:
global result
filename = filedialog.askopenfilename(
initialdir="/",
title="Select an Electronics File",
filetypes=(("AEDT", ".aedt"), ("all files", "*.*")),
)
browse_file_entry.insert(tkinter.END, filename)
result = ExtensionData(file_path=browse_file_entry.get("1.0", tkinter.END).strip())
self.root.destroy()
# Create button to browse an AEDT file
browse_button = ttk.Button(
self.root, text="...", command=browse_files, width=10, style="PyAEDT.TButton", name="browse_button"
)
browse_button.grid(row=4, column=2, pady=10, padx=15)
# Create button to generate sphere
create_button = ttk.Button(
self.root, text="Create Sphere", command=callback, style="PyAEDT.TButton", name="create_button"
)
create_button.grid(row=6, column=0, padx=15, pady=10)
def main(extension_args) -> bool:
"""Return main."""
origin_x = extension_args.get("origin_x", EXTENSION_DEFAULT_ARGUMENTS["origin_x"])
origin_y = extension_args.get("origin_y", EXTENSION_DEFAULT_ARGUMENTS["origin_y"])
origin_z = extension_args.get("origin_z", EXTENSION_DEFAULT_ARGUMENTS["origin_z"])
radius = extension_args.get("radius", EXTENSION_DEFAULT_ARGUMENTS["radius"])
file_path = Path(extension_args.get("file_path", EXTENSION_DEFAULT_ARGUMENTS["file_path"]))
app = ansys.aedt.core.Desktop(
new_desktop=False,
version=VERSION,
port=PORT,
aedt_process_id=AEDT_PROCESS_ID,
student_version=IS_STUDENT,
)
active_project = app.active_project()
active_design = app.active_design()
if active_project is None:
raise AEDTRuntimeError(
"No active project found. Please open or create a project before running this extension."
)
project_name = active_project.GetName()
if active_design.GetDesignType() == "HFSS 3D Layout Design":
design_name = active_design.GetDesignName()
else:
design_name = active_design.GetName()
aedtapp = cast(TemplateAppLike, get_pyaedt_app(project_name, design_name))
if file_path.is_file():
app.logger.info("Loading project...")
aedtapp.load_project(str(file_path), set_active=True)
app.logger.info("Project loaded.")
else:
app.logger.info("Creating sphere...")
aedtapp.modeler.create_sphere([origin_x, origin_y, origin_z], radius)
app.logger.info(f"Sphere created with origin ({origin_x}, {origin_y}, {origin_z}) and radius {radius}.")
if "PYTEST_CURRENT_TEST" not in os.environ:
app.release_desktop(False, False)
return True
if __name__ == "__main__":
args = get_arguments(EXTENSION_DEFAULT_ARGUMENTS, EXTENSION_TITLE)
# Open UI
if not args["is_batch"]: # pragma: no cover
extension: ExtensionProjectCommon = TemplateExtension(withdraw=False)
tkinter.mainloop()
if result:
args.update(asdict(result))
main(args)
else:
main(args)