Optislang#
- class ansys.optislang.core.optislang.Optislang(host: str | None = None, port: int | None = None, executable: str | Path | None = None, project_path: str | Path | None = None, batch: bool = True, service: bool = False, port_range: Tuple[int, int] | None = None, no_run: bool | None = None, no_save: bool = False, force: bool = True, reset: bool = False, auto_relocate: bool = False, listener_id: str | None = None, multi_listener: Iterable[Tuple[str, int, str | None]] | None = None, ini_timeout: int | float = 60, name: str | None = None, password: str | None = None, loglevel: str | None = None, shutdown_on_finished: bool = True, env_vars: Mapping[str, str] | None = None, import_project_properties_file: str | Path | None = None, export_project_properties_file: str | Path | None = None, import_placeholders_file: str | Path | None = None, export_placeholders_file: str | Path | None = None, output_file: str | Path | None = None, dump_project_state: str | Path | None = None, opx_project_definition_file: str | Path | None = None, additional_args: Iterable[str] | None = None)#
Connects to the optiSLang app and provides an API to control it.
For remote connection, it is assumed that optiSLang is already running on a remote (or local) host as a server. In this case, the host and port must be specified. Parameters related to the execution of the new optiSLang server are ignored.
To run optiSLang locally, both the
host
andport
parameters must beNone
, which are their defaults. Other parameters can be optionally specified.- Parameters:
- host
Optional
[str
],optional
IPv4/v6 address or domain name on which optiSLang is running as a server. The default is
None
.- port
Optional
[int
],optional
Port on which optiSLang is running as a server. The default is
None
.- executable
Optional
[Union
[str
,pathlib.Path
]],optional
Path to the optiSLang executable file to execute on a the local host. The default is
None
. This parameter is ignored whenhost
andport
parameters are specified.- project_path
Optional
[Union
[str
,pathlib.Path
]],optional
Path to the optiSLang project file that a new local optiSLang server is to use. The default is
None
. This parameter is ignored whenhost
andport
parameters are specified. Here is how this parameter is used:If the project file exists, it is opened.
If the project file does not exist, a project is created in the specified path.
If the path is
None
, a new project is created in the temporary directory.
- batchbool,
optional
Determines whether to start optiSLang server in batch mode. Defaults to
True
...note:: Cannot be used in combination with service mode.
- service: bool, optional
Determines whether to start optiSLang server in service mode. If
True
,batch
argument is set toFalse
. Defaults toFalse
...note:: Cannot be used in combination with batch mode.
- port_range
Tuple
[int
,int
],optional
Defines the port range for optiSLang server. Defaults to
None
.- no_run
Optional
[bool],optional
Determines whether not to run the specified project when started in batch mode. Defaults to
None
.Note
Only supported in batch mode.
- no_savebool,
optional
Whether to save the specified project after all other actions are completed. The default is
False
. This parameter is ignored whenhost
andport
parameters are specified.Note
Only supported in batch mode.
- forcebool,
optional
Determines whether to force opening/processing specified project when started in batch mode even if issues occur. Defaults to
True
.Note
Only supported in batch mode.
- resetbool,
optional
Determines whether to reset specified project after load. Defaults to
False
.Note
Only supported in batch mode.
- auto_relocatebool,
optional
Determines whether to automatically relocate missing file paths. Defaults to
False
.Note
Only supported in batch mode.
- listener_id
Optional
[str
],optional
Specific unique ID for the TCP listener. Defaults to
None
.- multi_listener
Iterable
[Tuple
[str
,int
,Optional
[str
]]],optional
Multiple remote listeners (plain TCP/IP based) to be registered at optiSLang server. Each listener is a combination of host, port and (optionally) listener ID. Defaults to
None
.- ini_timeout
float
,optional
Time in seconds to connect to the optiSLang server. The default is
20
.- name
Optional
[str
],optional
ID of the optiSLang instance. Defaults to
None
.- password
Optional
[str
],optional
Server password. The default is
None
. This parameter is used when communication with the server requires that the request contain a password. Defaults toNone
.- loglevel
Optional
[str
],optional
Logging level. The options are:
CRITICAL
: Log errors that are fatal for the app.ERROR
: Log errors that are fatal for some operation, but not for the app.WARNING
: Log some oddities or potential problems.INFO
: Log some useful information that the program works as expected.DEBUG
: Log all information for use in debugging.
Defaults to
None
.- shutdown_on_finished: bool, optional
Whether to shut down when execution is finished and no listeners are registered. The default is
True
. This parameter is ignored whenhost
andport
parameters are specified.Note
Only supported in batch mode.
- env_vars
Optional
[Mapping
[str
,str
]],optional
Additional environmental variables (key and value) for the optiSLang server process. Defaults to
None
.- import_project_properties_file
Optional
[Union
[str
,pathlib.Path
]],optional
Optional path to a project properties file to import. Defaults to
None
.- export_project_properties_file
Optional
[Union
[str
,pathlib.Path
]],optional
Optional path to a project properties file to export. Defaults to
None
.Note
Only supported in batch mode.
- import_placeholders_file
Optional
[Union
[str
,pathlib.Path
]],optional
Optional path to a placeholders file to import. Defaults to
None
.- export_placeholders_file
Optional
[Union
[str
,pathlib.Path
]],optional
Optional path to a placeholders file to export. Defaults to
None
.Note
Only supported in batch mode.
- output_file
Optional
[Union
[str
,pathlib.Path
]],optional
Optional path to an output file for writing project run results to. Defaults to
None
.Note
Only supported in batch mode.
- dump_project_state
Optional
[Union
[str
,pathlib.Path
]],optional
Optional path to a project state dump file to export. If a relative path is provided, it is considered to be relative to the project working directory. Defaults to
None
.Note
Only supported in batch mode.
- opx_project_definition_file
Optional
[Union
[str
,pathlib.Path
]],optional
Optional path to an OPX project definition file. Defaults to
None
.Note
Only supported in batch mode.
- additional_args
Optional
[Iterable
[str
]],optional
Additional command line arguments used for execution of the optiSLang server process. Defaults to
None
.
- host
- Raises:
RuntimeError
Raised when the connection to the optiSLang server cannot be established before the specified timeout.
Examples
Start and connect to the local optiSLang server, get the version of optiSLang in use, print the version, and shut down the server.
>>> from ansys.optislang.core import Optislang >>> osl = Optislang() >>> osl_version = osl.osl_version_string >>> print(osl_version) >>> osl.dispose()
Methods
Close all local threads and unregister listeners.
Get the currently used instance of the OslServer.
Get the optiSLang version in use as a tuple.
Get the optiSLang version in use as a string.
Get the timeout value for executing commands.
Get the path to the optiSLang project's working directory.
Create and open a new project.
Optislang.open
(file_path[, force, restore, ...])Open a project.
Reset the project.
Optislang.run_python_file
(file_path[, args])Read a Python script from a file, load it in a project context, and run it.
Optislang.run_python_script
(script[, args])Load a Python script in a project context and run it.
Save changes to the project data and settings.
Optislang.save_as
(file_path[, force, ...])Save and open the project at a new location.
Optislang.save_copy
(file_path)Save a copy of the project to a specified location..
Optislang.set_timeout
([timeout])Set the timeout value for the executing commands.
Optislang.shutdown
([force])Shut down the optiSLang server.
Optislang.start
([wait_for_started, ...])Start project execution.
Optislang.stop
([wait_for_finished])Stop project execution.
Attributes
Instance of the
Application
class.Whether a project is loaded.
Instance logger.
Unique ID of the optiSLang instance.
Get the currently used instance of the OslServer.
Version of used optiSLang.
Version of used optiSLang.
Instance of the
Project
class.Get the timeout value for executing commands.