Download this example
Download this example as a Jupyter Notebook or as a Python script.
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#
[1]:
from pathlib import Path
import requests
from ansys.geometry.core import launch_modeler
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.
[2]:
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://raw.githubusercontent.com/ansys/pyansys-geometry/main/tests/integration/files/blockswithparameters.dsco"
# Local path to save the file to
file_path = Path.cwd() / "blockswithparameters.dsco"
# Download the file
download_file(url, file_path)
print(f"File is downloaded to {file_path}")
File is downloaded to C:\Users\Public\actions-runner\_work\pyansys-geometry\pyansys-geometry\doc\source\examples\03_modeling\blockswithparameters.dsco
Import a design with parameters#
Import the model using the open_file()
method of the modeler.
[3]:
# 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.
[4]:
my_parameters = design.parameters
print(len(my_parameters))
2
A parameter object has a name, value, and unit.
[5]:
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)
p1
0.00010872999999999982
ParameterType.DIMENSIONTYPE_AREA
p2
0.0002552758322160814
ParameterType.DIMENSIONTYPE_AREA
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 square meters.
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).
[6]:
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)
ParameterUpdateStatus.SUCCESS
0.00010872999999999982
0.00044
After a successful parameter update, the design is updated. If we request the design plot again, we see the updated design.
[7]:
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.
[8]:
parameter1 = my_parameters[0]
parameter1.dimension_value = 0.000250
response = design.set_parameter(parameter1)
print(response)
ParameterUpdateStatus.CONSTRAINED_PARAMETERS
To get the updated list, query the parameters once again.
[9]:
my_parameters = design.parameters
print(my_parameters[0].dimension_value)
print(my_parameters[1].dimension_value)
0.00024999999999997274
0.0005812699999999445
Close the modeler#
Close the modeler to free up resources and release the connection.
[10]:
modeler.close()
Download this example
Download this example as a Jupyter Notebook or as a Python script.