Navigation using names#
This section present how to search a declaration from a module or from a scope using its namespace. The search starts on the module or scope moving up to the declarations of scope sections, operators, module bodies and interfaces.
The QuadFlightControl
example is used. To setup the example see
QuadFlightControl example setup code.
The proper module is retrieved:
model.load_all_modules()
def get_module(name: str, model: Model) -> swan.Module:
return next(filter(lambda module: module.name.as_string == name, model.modules), None)
quad_flight_ctrl_module = get_module("QuadFlightControl", model)
Getting an operator from the module#
Operator declarations can be found from a module (ModuleBody
or ModuleInterface
).
This code shows how to get the operator from a module using the ModuleBody.get_declaration()
method:
# Get the 'MotorControl' operator from the 'QuadFlightControl' module
motor_control_op = quad_flight_ctrl_module.get_declaration("MotorControl")
Get global declarations from operator scope#
Global declarations like groups, constants, types, or other operators from the operator scope are found using the
Scope.get_declaration()
method:
# Get the 'MotorControl' scope
scope = motor_control_op.body
# Get the 'MotorsHealth' group, defined in the 'QuadTypes' module (alias 'T'),
# from the 'MotorControl' scope
mh_group = scope.get_declaration("T::MotorsHealth")
# Get the 'AttitudeType' type, defined in the 'QuadTypes' module (alias 'T'),
# from the 'MotorControl' scope
att_type = scope.get_declaration("T::AttitudeType")
# Get the 'PI' constant from the 'MotorControl' scope
pi_const = scope.get_declaration("PI")
# Get the 'RotorControl' operator from the 'MotorControl' scope
rotor_ctrl_op = scope.get_declaration("RotorControl")
With the same method can be used to access to the inputs or outputs of the operator:
# Get the 'RotateXY' operator, defined in the 'QuadUtils' module, from the 'MotorControl' scope
rotate_xy_op = scope.get_declaration("QuadUtils::RotateXY")
# Get the 'thrustControlCmd' input from the 'MotorControl' scope
thrust_ctrl_cmd_var = scope.get_declaration("thrustControlCmd")
Get local declarations from any scope#
In addition to global declarations, local declarations like variables, inputs, or outputs from any scope
can be found using the Scope.get_declaration()
method.
This method looks for a declaration of a given name in its current namespace. If the declaration is not found, it is searched in the enclosing scope.
Complete example#
This is the complete script for the name-based scope navigation section.
# Copyright (C) 2022 - 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 pathlib import Path
from ansys.scadeone.core import ScadeOne
from ansys.scadeone.core.model import Model
import ansys.scadeone.core.swan as swan
# Update according to your installation
s_one_install = Path(r"C:\Scade One")
quad_flight_project = (
s_one_install / "examples/QuadFlightControl/QuadFlightControl" / "QuadFlightControl.sproj"
)
app = ScadeOne(install_dir=s_one_install)
model = app.load_project(quad_flight_project).model
model.load_all_modules()
def get_module(name: str, model: Model) -> swan.Module:
return next(filter(lambda module: module.name.as_string == name, model.modules), None)
quad_flight_ctrl_module = get_module("QuadFlightControl", model)
# Get the 'MotorControl' operator from the 'QuadFlightControl' module
motor_control_op = quad_flight_ctrl_module.get_declaration("MotorControl")
# Get the 'MotorControl' scope
scope = motor_control_op.body
# Get the 'MotorsHealth' group, defined in the 'QuadTypes' module (alias 'T'),
# from the 'MotorControl' scope
mh_group = scope.get_declaration("T::MotorsHealth")
# Get the 'AttitudeType' type, defined in the 'QuadTypes' module (alias 'T'),
# from the 'MotorControl' scope
att_type = scope.get_declaration("T::AttitudeType")
# Get the 'PI' constant from the 'MotorControl' scope
pi_const = scope.get_declaration("PI")
# Get the 'RotorControl' operator from the 'MotorControl' scope
rotor_ctrl_op = scope.get_declaration("RotorControl")
# Get the 'RotateXY' operator, defined in the 'QuadUtils' module, from the 'MotorControl' scope
rotate_xy_op = scope.get_declaration("QuadUtils::RotateXY")
# Get the 'thrustControlCmd' input from the 'MotorControl' scope
thrust_ctrl_cmd_var = scope.get_declaration("thrustControlCmd")