Basic usage#

An instance of the Optislang includes a property application, which encapsulates an instance of the Application class, equipped with functionalities to retrieve information about the employed server and perform manipulations on the project.

from ansys.optislang.core import Optislang

osl = Optislang()
application = osl.application

Within the context of the Application class instance, there is further property project, which holds an instance of the Project class (if any project is loaded). This instance groups functionality to get project information and execute operations on the active project.

project = osl.application.project

To start a project, you use the start() method. This code starts the project for the Simple calculator example:

from ansys.optislang.core import examples

path_to_file = examples.get_files("simple_calculator")[0]
osl.application.project.run_python_file(file_path=path_to_file)
osl.application.project.start()

To save the project, use the save(), save_as(), or save_copy() method. This code uses the save_as() method:

from pathlib import Path

project_path = Path().cwd() / "test_project.opf"
osl.application.save_as(project_path)

If an optiSLang instance is created with the default parameters, the optiSLang project is located in a temporary directory. Therefore, to preserve the project permanently, you should use either the save_as() or save_copy() method.

To create a project or open an existing one, you use the new() or open() method. This code creates a project:

new_project = Path().cwd() / "new_project.opf"
osl.application.new()
osl.application.save_as(new_project)

To obtain some general information about a project, run this code:

print(osl)

Or, you can run specific requests like shown in this code:

print(f"Version: {osl.application.get_version_string()}")
print(f"Working directory: {osl.application.project.get_working_dir()}")

When you no longer need to use the Optislang instance, close the connection with the optiSLang server by running this code:

osl.dispose()