-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: parameters refurbished (#1647)
Co-authored-by: pyansys-ci-bot <[email protected]> Co-authored-by: Roberto Pastor Muela <[email protected]> Co-authored-by: Kathy Pippert <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
85dcb8b
commit 034e060
Showing
10 changed files
with
380 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
parameters refurbished |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
doc/source/examples/03_modeling/design_parameters.mystnb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
--- | ||
jupytext: | ||
text_representation: | ||
extension: .mystnb | ||
format_name: myst | ||
format_version: 0.13 | ||
jupytext_version: 1.16.4 | ||
kernelspec: | ||
display_name: Python 3 (ipykernel) | ||
language: python | ||
name: python3 | ||
--- | ||
|
||
# Modeling: Using design parameters | ||
|
||
You can read and update parameters that are part of the design. | ||
The simple design in this example has two associated parameters. | ||
|
||
+++ | ||
|
||
## Perform required imports | ||
|
||
```{code-cell} ipython3 | ||
import os | ||
import requests | ||
from ansys.geometry.core import launch_modeler | ||
from ansys.geometry.core.modeler import * | ||
from ansys.geometry.core.parameters import * | ||
``` | ||
|
||
The file for this example is in the integration tests folder and can be downloaded. | ||
|
||
+++ | ||
|
||
## Download the example file | ||
|
||
+++ | ||
|
||
Download the file for this example from the integration tests folder in the PyAnsys Geometry repository. | ||
|
||
```{code-cell} ipython3 | ||
import requests | ||
|
||
def download_file(url, filename): | ||
"""Download a file from a URL and save it to a local file.""" | ||
response = requests.get(url) | ||
response.raise_for_status() # Check if the request was successful | ||
with open(filename, 'wb') as file: | ||
file.write(response.content) | ||
|
||
# URL of the file to download | ||
url = "https://github.com/ansys/pyansys-geometry/blob/main/tests/integration/files/blockswithparameters.dsco" | ||
|
||
# Local path to save the file to | ||
file_name = "blockswithparameters.dsco" | ||
current_path = os.getcwd() | ||
file_path = os.path.join(current_path, file_name) | ||
# Download the file | ||
download_file(url, file_path) | ||
print("File is downloaded to " + file_path) | ||
``` | ||
|
||
## Import a design with parameters | ||
|
||
+++ | ||
|
||
Import the model using the ``open_file()`` method of the modeler. | ||
|
||
```{code-cell} ipython3 | ||
# Create a modeler object | ||
modeler = launch_modeler() | ||
design = modeler.open_file(file_path) | ||
design.plot() | ||
``` | ||
|
||
## Read existing parameters of the design | ||
|
||
You can get all the parameters of the design as a list of parameters. Because this example has two parameters, you see two items in the list. | ||
|
||
```{code-cell} ipython3 | ||
my_parameters = design.get_all_parameters() | ||
print(len(my_parameters)) | ||
``` | ||
|
||
A parameter object has a name, value, and unit. | ||
|
||
```{code-cell} ipython3 | ||
print(my_parameters[0].name) | ||
print(my_parameters[0].dimension_value) | ||
print(my_parameters[0].dimension_type) | ||
|
||
print(my_parameters[1].name) | ||
print(my_parameters[1].dimension_value) | ||
print(my_parameters[1].dimension_type) | ||
``` | ||
|
||
Parameter values are returned in the default unit for each dimension type. Since default length unit is meter and default area unit is meter square, the value is returned in metersquare. | ||
|
||
+++ | ||
|
||
## Edit a parameter value | ||
|
||
You can edit the parameter's name or value by simply setting these fields. | ||
Set the second parameter (p2 value to 350 mm). | ||
|
||
```{code-cell} ipython3 | ||
parameter1 = my_parameters[1] | ||
parameter1.dimension_value = 0.000440 | ||
response = design.set_parameter(parameter1) | ||
print(response) | ||
print(my_parameters[0].dimension_value) | ||
print(my_parameters[1].dimension_value) | ||
``` | ||
|
||
After a successful parameter update, the design in the backend might have been updated. Therefore, you must refresh the design on the client. | ||
|
||
```{code-cell} ipython3 | ||
design = modeler.read_existing_design() | ||
design.plot() | ||
``` | ||
|
||
The ``set_parameter()`` method returns a ``Success`` status message if the parameter is updated or a "FAILURE" status message if the update fails. If the ``p2`` parameter depends on the ``p1`` parameter, updating the ``p1`` parameter might also change the ``p2`` parameter. In such cases, the method returns ``CONSTRAINED_PARAMETERS``, which indicates other parameters were also updated. | ||
|
||
```{code-cell} ipython3 | ||
parameter1 = my_parameters[0] | ||
parameter1.dimension_value = 0.000250 | ||
response = design.set_parameter(parameter1) | ||
print(response) | ||
``` | ||
|
||
To get the updated list, query the parameters once again. | ||
|
||
```{code-cell} ipython3 | ||
my_parameters = design.get_all_parameters() | ||
print(my_parameters[0].dimension_value) | ||
print(my_parameters[1].dimension_value) | ||
``` | ||
|
||
## Close the modeler | ||
|
||
Close the modeler to free up resources and release the connection. | ||
|
||
```{code-cell} ipython3 | ||
modeler.close() | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Copyright (C) 2023 - 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. | ||
"""PyAnsys Geometry parameters subpackage.""" | ||
|
||
from ansys.geometry.core.parameters.parameter import Parameter, ParameterType |
Oops, something went wrong.