### Display Installation Help Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/wsl.rst Use this command to display the help information for the installation script. ```bash ./INSTALL --help ``` -------------------------------- ### Display Installation Script Content Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/wsl.rst Use this command to display the content of the installation script. ```bash cat ./INSTALL ``` -------------------------------- ### Install Mechanical Product Silently Source: https://github.com/ansys/pymechanical/blob/main/docker/make_container.rst Use this command to install Mechanical with minimal files. Ensure you have write permissions or use sudo. The install directory path is required for subsequent steps. ```bash sh /path-to-mechanical-installer \ -silent -overwrite_preview -mechapdl -lsdyna \ -install_dir /path-to-install-mechanical/ ``` ```bash # example # sh /home/username/download/linx/INSTALL \ # -silent -overwrite_preview -mechapdl -lsdyna \ # -install_dir /install/ansys_inc/ ``` -------------------------------- ### Install PyMechanical from Source Source: https://github.com/ansys/pymechanical/blob/main/doc/source/contribute/user.rst Install PyMechanical from its source code to test new features before official release. This involves cloning the repository and performing a local installation. ```bash git clone https://github.com/ansys/pymechanical.git cd pymechanical pip install -e . ``` -------------------------------- ### Complete License Management Workflow Example Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/embedding/licensing.rst A comprehensive example showcasing various license management operations in PyMechanical. This includes inspecting available licenses, configuring preferences, checking out, temporarily releasing, and re-acquiring licenses, and resetting preferences to defaults. ```python from ansys.mechanical.core import App # Start in read-only mode to inspect license options app = App(readonly=True, version={mechanical_version}) print("=== Available Licenses ===") app.license_manager.show() print("\n=== Configure License Preferences ===") # Set preferred license order app.license_manager.move_to_index("Ansys Mechanical Premium", 0) app.license_manager.move_to_index("Ansys Mechanical Enterprise", 1) # Disable a license we don't want to use app.license_manager.set_license_status("Ansys Mechanical Pro", False) print("\nUpdated license order:") licenses = app.license_manager.get_all_licenses() for idx, lic in enumerate(licenses): status = app.license_manager.get_license_status(lic) print(f" {idx}: {lic} - {status}") print("\n=== Check Out License ===") # Enable the preferred license app.license_manager.enable_session_license() if not app.readonly: print("License checked out successfully") # Perform modeling operations print("Performing modeling operations...") # ... your code here ... else: print("Failed to check out license") print("\n=== Release License Temporarily ===") app.license_manager.disable_session_license() print(f"Read-only mode: {app.readonly}") # Perform operations that don't need a license print("Performing read-only operations...") print("\n=== Re-acquire License ===") app.license_manager.enable_session_license() print(f"Read-only mode: {app.readonly}") print("\n=== Reset to Defaults ===") app.license_manager.reset_preference() print("License preferences reset to default") ``` -------------------------------- ### Pre-commit Hook Execution Example Source: https://github.com/ansys/pymechanical/blob/main/doc/source/contribute/developer.rst Example output demonstrating the execution of pre-commit hooks during a Git commit. This shows which checks are performed and their status. ```bash $ pre-commit install $ git commit -am "added my cool feature" check pre-commit.ci config...............................................Passed black....................................................................Passed blacken-docs.............................................................Passed isort....................................................................Passed flake8...................................................................Passed codespell................................................................Passed Add License Headers......................................................Passed Ansys Technical Review...................................................Passed pydocstyle...............................................................Passed check for merge conflicts................................................Passed debug statements (python)................................................Passed check yaml...............................................................Passed trim trailing whitespace.................................................Passed check for added large files..............................................Passed Validate GitHub Workflows................................................Passed ``` -------------------------------- ### Install and Run Pre-commit Hooks Source: https://github.com/ansys/pymechanical/blob/main/doc/source/contribute/developer.rst Install pre-commit to enforce PEP8 coding style guidelines. Running pre-commit hooks locally ensures code quality before committing and pushing changes. ```bash pip install pre-commit pre-commit run --all-files ``` -------------------------------- ### Verify PyMechanical Installation Source: https://github.com/ansys/pymechanical/blob/main/doc/source/contribute/developer.rst Verify the installation by printing the installed PyMechanical version. ```python from ansys.mechanical.core import __version__ print(f"PyMechanical version is {__version__}") ``` -------------------------------- ### Install Documentation Dependencies Source: https://github.com/ansys/pymechanical/blob/main/doc/source/contribute/documentarian.rst Install the necessary dependencies for building the documentation using pip. This command installs PyMechanical in editable mode with the 'doc' extra. ```bash pip install -e .[doc] ``` -------------------------------- ### Install PyMechanical Source: https://github.com/ansys/pymechanical/blob/main/README.rst Install the PyMechanical package from PyPI. Use the '[graphics]' extra for graphics support. ```bash pip install ansys-mechanical-core pip install ansys-mechanical-core[graphics] ``` -------------------------------- ### Silent Installation with Single License Server Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/wsl.rst Perform a silent installation specifying a single license server. Ensure the LI port, FLEXlm port, and hostname are correctly provided. ```bash ./INSTALL -silent -install_dir /ansys_inc/ -mechapdl -licserverinfo 2325:1055:winhostIP ``` -------------------------------- ### Silent Installation with Three License Servers Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/wsl.rst Perform a silent installation specifying three license servers. The format requires LI port, FLEXlm port, and a comma-separated list of hostnames. ```bash ./INSTALL -silent -install_dir /ansys_inc/ -mechapdl -licserverinfo 2325:1055:abc,def,xyz ``` -------------------------------- ### Install Test Dependencies Source: https://github.com/ansys/pymechanical/blob/main/doc/source/contribute/developer.rst Install test dependencies for PyMechanical, including editable mode installation with the 'tests' extra. ```bash pip install -e .[tests] ``` -------------------------------- ### Configure WSL hosts file for Docker Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/wsl.rst This example shows the typical content of the WSL /etc/hosts file, highlighting the entry for host.docker.internal which is crucial for Docker communication. ```bash # This file is automatically generated by WSL. # To stop automatic generation of this file, add the following lines to the # ``/etc/wsl.conf`` file: # # [network] # generateHosts = false # 127.0.0.1 localhost 127.0.1.1 AAPDDqVK5WqNLve.win.ansys.com AAPDDqVK5WqNLve 192.168.0.12 host.docker.internal 192.168.0.12 gateway.docker.internal 127.0.0.1 kubernetes.docker.internal # The following lines are desirable for IPv6 capable hosts. ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters ``` -------------------------------- ### Install Required Libraries for CentOS7 Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/wsl.rst Before installing Mechanical on CentOS 7 within WSL, install these essential libraries using yum. ```bash sudo yum install openssl openssh-clients mesa-libGL mesa-libGLU motif libgfortran ``` -------------------------------- ### Offline Installation of PyMechanical Package Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/installation.rst Install PyMechanical on a system without internet access using a downloaded wheelhouse archive. This example is for Linux with Python 3.10. ```bash unzip ansys-mechanical-core-v0.12.dev0-wheelhouse-Linux-3.10 wheelhouse pip install ansys-mechanical-core -f wheelhouse --no-index --upgrade --ignore-installed ``` -------------------------------- ### Get All Available Licenses Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/embedding/licensing.rst Retrieve a list of all licenses available in preference order. Useful for understanding the current license hierarchy. ```python # Get all licenses in preference order licenses = license_mgr.get_all_licenses() print(licenses) # Output: ['Ansys Mechanical Enterprise', 'Ansys Mechanical Premium', 'Ansys Mechanical Pro'] ``` -------------------------------- ### Install PyMechanical Package Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/installation.rst Install the latest PyMechanical package from PyPi using pip. This command is suitable for online installations. ```bash pip install ansys-mechanical-core ``` -------------------------------- ### Install Ansys Mechanical Silently Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/wsl.rst Perform a silent installation of Ansys Mechanical within WSL, specifying the installation directory and product flag. ```bash sudo ./INSTALL -silent -install_dir /usr/ansys_inc/ -mechapdl ``` -------------------------------- ### Launch Mechanical via CLI (Insecure) Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/remote_session/grpc_security.rst Example of launching Mechanical using the command-line interface with an insecure transport mode. ```bash ``` -------------------------------- ### Run Python with Custom Mechanical Installation Path Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/cli/mechanical-env.rst Execute the Python interpreter using a custom Ansys Mechanical installation path. ```bash mechanical-env -p /usr/install/ansys_inc/v251 python ``` -------------------------------- ### Create Symbolic Link for Ansys Installation Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/wsl.rst Create a symbolic link to associate the default Ansys installation directory with your actual installation path. This is often required for PyMechanical to locate the Mechanical executable. ```bash sudo ln -s /usr/ansys_inc /ansys_inc ``` -------------------------------- ### Launch Mechanical Instance Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/remote_session/overview.rst Launches an instance of the Mechanical application in the background and establishes a connection for sending commands. This is the basic way to start a remote session. ```python import os from ansys.mechanical.core import launch_mechanical mechanical = launch_mechanical() ``` -------------------------------- ### Start Mechanical in Server Mode from Command Line Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/running_mechanical.rst Initiate Mechanical as a server process from the command line, specifying a port for gRPC communication. This allows for manual connection from Python. ```console $ ansys-mechanical --port 10000 ``` -------------------------------- ### Launch PyMechanical Automatically Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/running_mechanical.rst Use the `launch_mechanical` function to start a Mechanical instance and connect to it automatically. This is the simplest way to begin. ```pycon >>> from ansys.mechanical.core import launch_mechanical >>> mechanical = launch_mechanical() >>> mechanical Ansys Mechanical [Ansys Mechanical Enterprise] Product Version:{mechanical_version} Software build date: {build_date} ``` -------------------------------- ### Launch Mechanical from Command Line for Debugging Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/troubleshooting.rst Start Mechanical from the command line with a specified port for debugging purposes. This helps diagnose launch failures. ```console ansys-mechanical -g --port 10000 ``` -------------------------------- ### Initialize Embedded Mechanical with Environment (Linux) Source: https://github.com/ansys/pymechanical/blob/main/doc/source/faq.rst Start an embedded Ansys Mechanical instance on Linux after invoking the mechanical-env script. This ensures the environment is correctly set up. ```shell $ mechanical-env python >>> import ansys.mechanical.core as mech >>> app=mech.App(version={mechanical_version}) ``` ```shell $ mechanical-env python test.py ``` -------------------------------- ### Install PyMechanical in Editable Mode Source: https://github.com/ansys/pymechanical/blob/main/doc/source/contribute/developer.rst Install PyMechanical in editable mode to see code changes reflected without reinstallation. ```text python -m pip install --editable . ``` -------------------------------- ### Install xvfb in CentOS7 Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/wsl.rst Installs the X virtual framebuffer package (xvfb) on CentOS 7 systems, often required for replicating CI/CD behavior. ```bash yum install xorg-x11-server-Xvfb ``` -------------------------------- ### Start PyMechanical with Default License Behavior Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/embedding/licensing.rst Launch Ansys Mechanical without explicitly specifying a license or read-only mode. The application will check out the first enabled license according to the preference order. ```python from ansys.mechanical.core import App # Start with default license behavior app = App(version={mechanical_version}) # Check which license was checked out print(app.license_manager.get_all_licenses()) ``` -------------------------------- ### Build Mechanical Docker Image Source: https://github.com/ansys/pymechanical/blob/main/docker/make_container.rst This script sets environment variables, copies Docker configuration files, and builds the Docker image. Modify the export paths to match your local setup. Use sudo for the docker build command if necessary. ```bash # Create env vars for the Dockerfile export ANS_MAJOR_VERSION=26 export ANS_MINOR_VERSION=1 export ANS_VERSION=${ANS_MAJOR_VERSION}${ANS_MINOR_VERSION} export TAG=mechanical:${ANS_MAJOR_VERSION}.${ANS_MINOR_VERSION} # example: if Mechanical v261 is installed under usr/install/ansys_inc/v261 # use export MECHANICAL_INSTALL_LOCATION=/usr/install/ export MECHANICAL_INSTALL_LOCATION=/path_to_mechanical_installation/ # example: if pymechanical is cloned under /some_location/pymechanical # use /some_location for path-to-pymechanical export PYMECHANICAL_LOCATION=/path-to-pymechanical # Create working directory cd ${MECHANICAL_INSTALL_LOCATION} # Copy the Docker files cp ${PYMECHANICAL_LOCATION}/pymechanical/docker/${ANS_VERSION}/Dockerfile . cp ${PYMECHANICAL_LOCATION}/pymechanical/docker/${ANS_VERSION}/.dockerignore . # Build Docker image sudo docker build -t $TAG --build-arg VERSION=$ANS_VERSION . ``` -------------------------------- ### Verify Mechanical Installation Path Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/installation.rst Use the `find_mechanical` function from `ansys.tools.common.path` to automatically locate the Ansys Mechanical installation. You can also specify a version. ```pycon >>> from ansys.tools.common.path import find_mechanical >>> find_mechanical() ('C:/Program Files/ANSYS Inc/v{mechanical_version}/aisol/bin/winx64/AnsysWBU.exe', 26.1) # Windows ('/usr/ansys_inc/v{mechanical_version}/aisol/.workbench', 26.1) # Linux >>> find_mechanical(version={mechanical_version}) # for a specific version ``` -------------------------------- ### Example Usage of PEP 8 Aliases Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/embedding/pep8.rst Demonstrates the practical application of PEP 8 aliases for various Mechanical API calls, including analysis creation, named selection, mesh generation, camera fitting, and property access. ```python # Example usage with PEP 8 aliases analysis = Model.add_static_structural_analysis() named_selection = Model.add_named_selection() Model.mesh.generate_mesh() Graphics.camera.set_fit() print(Model.name) ``` -------------------------------- ### Choose Specific License using Read-Only Workaround Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/embedding/licensing.rst Use a workaround by starting the application in read-only mode and then manually setting the license preference and order. This is an alternative method for ensuring a specific license is checked out. ```python # Method 2: Using read-only workaround app = App(readonly=True, version={mechanical_version}) # Start without license # Modify license order app.license_manager.move_to_index("Ansys Mechanical Premium", 0) ``` -------------------------------- ### Start PyMechanical in Read-Only Mode Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/embedding/licensing.rst Initiate an Ansys Mechanical session without checking out a license. This is useful for viewing or exploring projects when modifications are not required. ```python from ansys.mechanical.core import App # Start in read-only mode app = App(readonly=True, version={mechanical_version}) # Verify read-only status print(f"Read-only mode: {app.readonly}") ``` -------------------------------- ### Prepare Environment for PyMechanical Embedding Mode on Linux Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/running_mechanical.rst On Linux, set up the necessary environment variables before starting Python by executing the `mechanical-env` script. This ensures proper initialization for embedding mode. ```shell $ mechanical-env python ``` -------------------------------- ### Install Pre-commit as a Git Hook Source: https://github.com/ansys/pymechanical/blob/main/doc/source/contribute/developer.rst Install pre-commit as a Git hook to automatically check code style on each commit. This prevents code that fails style checks from being committed. ```bash pre-commit install ``` -------------------------------- ### Check Mechanical License Status (Shell) Source: https://github.com/ansys/pymechanical/blob/main/doc/source/faq.rst Display the license information for Ansys Mechanical. This example shows the expected output format for both licensed and unlicensed scenarios. ```shell Ansys Mechanical [Ansys Mechanical Enterprise] Product Version:{mechanical_version} Software build date: {build_date} ``` ```shell Ansys Mechanical [] Product Version:{mechanical_version} Software build date: {build_date} ``` -------------------------------- ### Launch PyMechanical with Specific Executable Path Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/running_mechanical.rst Specify the exact path to the Mechanical executable if you need to use a particular version or installation. This ensures you connect to the intended Mechanical instance. ```python exec_file_path = "C:/Program Files/ANSYS Inc/v{mechanical_version}/aisol/bin/win64/AnsysWBU.exe" mechanical = launch_mechanical(exec_file=exec_file_path) ``` -------------------------------- ### Launch Mechanical Session Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/remote_session/server-launcher.rst Launches a Mechanical session. PyMechanical attempts to auto-detect the installation path on the first run. ```python from ansys.mechanical.core import launch_mechanical mec hanical = launch_mechanical() ``` -------------------------------- ### Start PyMechanical with a Specific License Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/embedding/licensing.rst Specify which Ansys Mechanical license to check out upon application startup using license keywords. This feature is available from Ansys Mechanical 2026 R1 onwards. ```python from ansys.mechanical.core import App # Start with Mechanical Enterprise Solver license app = App(start_license="meba", version={mechanical_version}) # Start with Mechanical Premium license app = App(start_license="mech_2", version={mechanical_version}) # Start with Mechanical Pro license app = App(start_license="mech_1", version={mechanical_version}) # Start with Mechanical Enterprise license app = App(start_license="ansys", version={mechanical_version}) ``` -------------------------------- ### Mechanical Container Status Output Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/docker.rst Example console output indicating the status of the gRPC server when the Mechanical container starts in insecure mode. ```console Info: Starting the grpc server at port : 10000 (Insecure:0.0.0.0:certs) Info: Started the grpc server at port : 10000 (Insecure:0.0.0.0:certs) ``` -------------------------------- ### Choose Specific License using start_license Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/embedding/licensing.rst Force a specific license checkout upon application startup using the 'start_license' parameter. This is the recommended method for ensuring a particular license is used. ```python # Method 1: Using start_license parameter (recommended) app = App(start_license="meba", version={mechanical_version}) # Forces Mechanical Premium ``` -------------------------------- ### Build Documentation (Windows) Source: https://github.com/ansys/pymechanical/blob/main/doc/source/contribute/documentarian.rst Clean previous builds and generate HTML documentation using Sphinx on Windows. Open the generated index.html file in your browser. ```text doc\make clean doc\make html start .\doc\_build\html\index.html ``` -------------------------------- ### Launch Specific Mechanical Version Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/remote_session/overview.rst Launches a specific version of Mechanical by providing the executable file path. This is useful when multiple product versions are installed on the system. The `batch=False` and `cleanup_on_exit=False` arguments control GUI behavior and resource cleanup. ```python exec_file_path = "C:/Program Files/ANSYS Inc/v{mechanical_version}/aisol/bin/winx64/AnsysWBU.exe" mechanical = launch_mechanical( exec_file=exec_file_path, batch=False, cleanup_on_exit=False ) ``` -------------------------------- ### Build Documentation (Linux/macOS) Source: https://github.com/ansys/pymechanical/blob/main/doc/source/contribute/documentarian.rst Clean previous builds and generate HTML documentation using Sphinx on Linux or macOS. Open the generated index.html file in your browser. ```bash make -C doc clean make -C doc html your_browser_name doc/html/index.html ``` -------------------------------- ### Activate Virtual Environment (macOS/Linux/UNIX) Source: https://github.com/ansys/pymechanical/blob/main/doc/source/contribute/developer.rst Activate the created virtual environment using the source command. ```text source .venv/bin/activate ``` -------------------------------- ### Navigate to Project Directory Source: https://github.com/ansys/pymechanical/blob/main/doc/source/contribute/developer.rst Navigate to the root directory of the PyMechanical project after cloning. ```text cd pymechanical ``` -------------------------------- ### Initialize License Manager Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/embedding/licensing.rst Access the LicenseManager through the App instance. This is the first step to manage licenses. ```python from ansys.mechanical.core import App app = App(version={mechanical_version}) license_mgr = app.license_manager ``` -------------------------------- ### Accessing Helpers Class Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/embedding/helpers.rst Demonstrates how to access the Helpers class from an App instance. The App instance needs to be initialized with globals=globals(). ```python from ansys.mechanical.core import App app = App(globals=globals()) helpers = app.helpers ``` -------------------------------- ### Manually Set Mechanical Installation Path Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/installation.rst If Ansys Mechanical is installed in a non-default location, use `save_mechanical_path` to set the path manually and `get_mechanical_path` to retrieve it. ```pycon >>> from ansys.tools.common.path import save_mechanical_path, get_mechanical_path >>> save_mechanical_path("/home/username/ansys_inc/v{mechanical_version}/aisol/.workbench") >>> print(get_mechanical_path()) /home/username/ansys_inc/v{mechanical_version}/aisol/.workbench ``` -------------------------------- ### Instantiate App with Globals Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/embedding/globals.rst Create an instance of the App class and merge Mechanical's global API entry points into your Python global variables. ```python from ansys.mechanical.core import App # The following line creates an instance of the app, extracts the global API entry points, # and merges them into your Python global variables. app = App(globals=globals()) ``` -------------------------------- ### Basic mechanical-env Usage Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/cli/mechanical-env.rst Shows the general syntax for the mechanical-env command, including optional version and path arguments, and the command to execute. ```bash mechanical-env [-r ] [-p ] [COMMAND] ``` -------------------------------- ### Specify Mechanical Executable Path on Windows Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/remote_session/server-launcher.rst Provides the path to the Mechanical executable on a Windows system when auto-detection fails or for non-standard installations. ```console Enter location of Mechanical executable: C:/Program Files/ANSYS Inc/v{mechanical_version}/aisol/bin/winx64/AnsysWBU.exe ``` -------------------------------- ### Specify Mechanical Executable Path on Linux Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/remote_session/server-launcher.rst Provides the path to the Mechanical executable on a Linux system when auto-detection fails or for non-standard installations. ```console Enter location of Mechanical executable: /usr/ansys_inc/v{mechanical_version}/aisol/.workbench ``` -------------------------------- ### Enable a License Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/embedding/licensing.rst Set the status of a specific license to enabled. This allows the license to be checked out. ```python # Enable a license license_mgr.set_license_status("Ansys Mechanical Premium", True) ``` -------------------------------- ### Enable Multiple Session Licenses with Priority Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/embedding/licensing.rst Attempt to check out multiple licenses in a specified priority order for the current session. This is useful for complex licensing scenarios. ```python # Try to check out licenses in the specified order app.license_manager.enable_session_license([ "Ansys Mechanical Enterprise", "Ansys Mechanical Premium" ]) print(app.readonly) # Output: False ``` -------------------------------- ### Change Default Mechanical Path Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/remote_session/server-launcher.rst Updates the default path for the Mechanical installation. Use this if you need to change the default version or if the path was initially set incorrectly. ```python from ansys.mechanical import core as pymechanical new_path = "C:/Program Files/ANSYS Inc/v{mechanical_version}/aisol/bin/winx64/AnsysWBU.exe" pymechanical.change_default_mechanical_path(new_path) ``` -------------------------------- ### Launch Mechanical with WNUA (Windows) Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/remote_session/grpc_security.rst Launches a new Ansys Mechanical instance using the WNUA transport mode on Windows. Ensure the port is available. ```bash ansys-mechanical --port 10000 --transport-mode wnua ``` -------------------------------- ### Run PyMechanical Docker image (initial run) Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/wsl.rst This command runs a PyMechanical Docker image, setting the license file environment variable and mapping ports. Ensure port 10000 is open in your firewall. ```pwsh docker run -e ANSYSLMD_LICENSE_FILE=1055@host.docker.internal --restart always --name mechanical -p 10000:10000 ghcr.io/ansys/pymechanical/mechanical > log.txt ``` -------------------------------- ### Configure Embedded Mechanical Instance Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/embedding/configuration.rst Use AddinConfiguration to set up Mechanical's configuration name and control ACT Addin loading. Pass the created configuration object to the App constructor. ```python from ansys.mechanical.core import App from ansys.mechanical.core.embedding import AddinConfiguration config = AddinConfiguration("Mechanical", no_act_addins=True) app = App(config=config) ``` -------------------------------- ### Manually Set Mechanical Executable Location on Windows Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/troubleshooting.rst Specify the exact path to the Mechanical executable if PyMechanical cannot locate it automatically. This is useful for custom installations or specific versioning. ```python from ansys.mechanical.core import launch_mechanical exec_loc = "C:/Program Files/ANSYS Inc/v{mechanical_version}/aisol/bin/winx64/AnsysWBU.exe" mechanical = launch_mechanical(exec_file=exec_loc) ``` -------------------------------- ### Manually Set Mechanical Executable Location on Linux Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/troubleshooting.rst Specify the exact path to the Mechanical executable on Linux if PyMechanical cannot locate it automatically. This is useful for custom installations or specific versioning. ```python from ansys.mechanical.core import launch_mechanical exec_loc = "/usr/ansys_inc/v{mechanical_version}/aisol/.workbench" mechanical = launch_mechanical(exec_file=exec_loc) ``` -------------------------------- ### Connect to an Existing Mechanical Instance Source: https://github.com/ansys/pymechanical/blob/main/README.rst Connect to a Mechanical instance that is already running, typically used when Mechanical is launched as a server. ```python from ansys.mechanical.core import connect_to_mechanical mechanical = connect_to_mechanical(port=10000) result = mechanical.run_python_script("Model.Name") print(result) ``` -------------------------------- ### Activate Virtual Environment (Windows CMD) Source: https://github.com/ansys/pymechanical/blob/main/doc/source/contribute/developer.rst Activate the created virtual environment using the Activate.bat script in the Scripts directory. ```text .venv\Scripts\activate.bat ``` -------------------------------- ### Handle Invalid Command Error Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/remote_session/overview.rst Demonstrates how PyMechanical catches errors immediately when an invalid command is sent to the Mechanical session. The example shows a `grpc.RpcError` being raised for a syntax error in the script. ```pycon >>> mechanical.run_python_script("2****3") grpc.RpcError: "unexpected token '**'" ``` -------------------------------- ### Execute Script using workbench (Linux) Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/embedding/overview.rst Launch a Mechanical script using the workbench executable on Linux, specifying the script file with the -script argument. ```console /usr/ansys_inc/v{mechanical_version}/aisol/.workbench -DSApplet -AppModeMech -script file.py ``` -------------------------------- ### Set PyMechanical connection environment variables Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/wsl.rst Configure PyMechanical to connect to a specific IP address and port by setting environment variables before launching the instance. This method is useful for automated setups. ```bash export PYMECHANICAL_START_INSTANCE=False export pymechanical_port=10001 export pymechanical_ip=127.0.0.1 ``` -------------------------------- ### Enter Mechanical Container for Embedding Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/docker.rst This bash command allows you to enter the Mechanical Docker container interactively using bash. This is useful for installing Python packages and setting up an embedded PyMechanical application. ```bash docker run -it -e ANSYSLMD_LICENSE_FILE=$LICENSE_SERVER --entrypoint=/bin/bash mechanical:{mechanical_version} # Once inside the container, you can install Python and packages as needed, then create an embedded app. ``` -------------------------------- ### Create Virtual Environment Source: https://github.com/ansys/pymechanical/blob/main/doc/source/contribute/developer.rst Create a new virtual environment named '.venv' to isolate your Python environment. ```text python -m venv .venv ``` -------------------------------- ### Enable Default Session License Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/embedding/licensing.rst Enable the default license for the current session, which is the first enabled license in the preference order. This exits read-only mode if it was active. ```python # Enable default license (first enabled in preference order) app.license_manager.enable_session_license() print(app.readonly) # Output: False ``` -------------------------------- ### Create a Mechanical Server Pool Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/remote_session/pool.rst Instantiate a LocalMechanicalPool with a specified number of Mechanical instances and a version. This is useful for setting up multiple Mechanical servers for parallel processing. ```pycon >>> from ansys.mechanical.core import LocalMechanicalPool >>> pool = LocalMechanicalPool(10, version="{mechanical_version}") 'Mechanical Pool with 10 active instances' ``` -------------------------------- ### Launch Mechanical with mTLS (Linux) Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/remote_session/grpc_security.rst Launches a new Ansys Mechanical instance using mTLS transport mode on Linux. Requires specifying a directory for certificates. ```bash ansys-mechanical --port 10000 --transport-mode mtls --certs-dir /path/to/certs ``` -------------------------------- ### Launch PyMechanical Embedding Mode on Windows Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/running_mechanical.rst Start Mechanical in embedding mode on Windows using the `App` class. This mode runs Mechanical directly within your Python process for reduced overhead. ```pycon >>> from ansys.mechanical.core import App >>> app = App() >>> app Ansys Mechanical [Ansys Mechanical Enterprise] Product Version:{mechanical_version} Software build date: {build_date} ``` -------------------------------- ### Integer Increment Operation Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/scripting/threading.rst A simple C code example illustrating an integer increment operation. This serves as a basis for explaining potential race conditions when multiple threads access shared data concurrently. ```c i++ ``` -------------------------------- ### Configure Logging to Standard Output Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/embedding/logging.rst Configure logging to output all warning messages and above to standard output. This can be done before or after creating the embedded application. ```python import logging import ansys.mechanical.core as mech from ansys.mechanical.core.embedding.logger import Configuration, Logger Configuration.configure(level=logging.WARNING, to_stdout=True) _ = mech.App() ``` -------------------------------- ### Connect to a Running Mechanical Instance Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/running_mechanical.rst Establish a connection to a Mechanical server instance using the `Mechanical` class. You can connect to a local instance using default parameters or specify the IP address and port for remote instances. ```python from ansys.mechanical.core import Mechanical # Local — default IP (127.0.0.1) and port (10000) mechanical = Mechanical() # Remote — by IP address mechanical = Mechanical("192.168.0.1", port=10000) # Remote — by hostname mechanical = Mechanical("myremotemachine", port=10000) ``` -------------------------------- ### Initialize PyMechanical with specified IP and port Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/wsl.rst Instantiate the Mechanical class directly, providing the IP address and port to connect to a running PyMechanical instance. ```python from ansys.mechanical.core import Mechanical mechanical = Mechanical(ip="127.0.0.1", port=10001) ``` -------------------------------- ### Set PyMechanical Environment Variables (Linux/macOS) Source: https://github.com/ansys/pymechanical/blob/main/doc/source/contribute/developer.rst Set environment variables to control PyMechanical's connection behavior. These variables inform PyMechanical whether to start a new instance of Mechanical or connect to an existing one, and specify the IP address and port for the connection. ```bash export PYMECHANICAL_START_INSTANCE=False export PYMECHANICAL_PORT= (default 10000) export PYMECHANICAL_IP= (default 127.0.0.1) ``` -------------------------------- ### Set PyMechanical Environment Variables (Windows) Source: https://github.com/ansys/pymechanical/blob/main/doc/source/contribute/developer.rst Set environment variables on Windows to control PyMechanical's connection behavior. These variables inform PyMechanical whether to start a new instance of Mechanical or connect to an existing one, and specify the IP address and port for the connection. ```bat SET PYMECHANICAL_START_INSTANCE=False SET PYMECHANICAL_PORT= (default 10000) SET PYMECHANICAL_IP= (default 127.0.0.1) ``` -------------------------------- ### Launch PyMechanical with specified IP and port Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/wsl.rst Connect to a running PyMechanical instance using the launch_mechanical function, specifying the IP address and port. Set start_instance to False to connect to an existing instance. ```python from ansys.mechanical.core import launch_mechanical mechanical = launch_mechanical(ip="127.0.0.1", port=10001, start_instance=False) ``` -------------------------------- ### Complete Geometry Visualization Workflow Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/embedding/helpers.rst A comprehensive workflow demonstrating the import of geometry and materials, followed by exporting and displaying an image of the geometry. This covers a typical visualization pipeline. ```python from ansys.mechanical.core import App app = App(globals=globals()) # Step 1: Import geometry app.helpers.import_geometry("bracket.x_t") # Step 2: Import materials app.helpers.import_materials("materials.xml") # Step 3: Export image from ansys.mechanical.core.embedding.enum_importer import GraphicsResolutionType app.helpers.export_image( obj=app.Model.Geometry, file_path="bracket_iso.png", width=1920, height=1080, resolution=GraphicsResolutionType.EnhancedResolution, ) # Step 4: Display it app.helpers.display_image("bracket_iso.png") ``` -------------------------------- ### Reset License Preferences Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/embedding/licensing.rst Revert all license status and order settings back to their default values. This action is persisted to user preferences. ```python # Reset to default license preferences license_mgr.reset_preference() # Verify preferences were reset licenses = license_mgr.get_all_licenses() print(licenses) ``` -------------------------------- ### Activate Virtual Environment (Windows PowerShell) Source: https://github.com/ansys/pymechanical/blob/main/doc/source/contribute/developer.rst Activate the created virtual environment using the Activate.ps1 script in the Scripts directory. ```text .venv\Scripts\Activate.ps1 ``` -------------------------------- ### Run a Set of Input Files with run_batch Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/remote_session/pool.rst Execute a batch of input files using the 'run_batch' method of the LocalMechanicalPool. This method processes a list of files and returns their outputs. ```pycon >>> from ansys.mechanical.core import examples >>> files = [f"test{index}.py" for index in range(1, 21)] >>> outputs = pool.run_batch(files) >>> len(outputs) 20 ``` -------------------------------- ### Change License Mid-Session Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/embedding/licensing.rst Demonstrates how to change the active license during an ongoing session. This involves disabling the current license, reordering preferences, and enabling a new one. ```python from ansys.mechanical.core import App app = App(version={mechanical_version}) # Check current license status print("Currently using:") app.license_manager.show() # Switch to read-only (releases current license) app.license_manager.disable_session_license() print(f"\nRead-only mode: {app.readonly}") # Change license preference order app.license_manager.move_to_index("Ansys Mechanical Pro", 0) # Check out the new preferred license app.license_manager.enable_session_license() print("\nAfter license change:") app.license_manager.show() print(f"Read-only mode: {app.readonly}") ``` -------------------------------- ### Set ANSYSLMD_LICENSE_FILE environment variable in WSL Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/wsl.rst Add these lines to your WSL ~/.bashrc file to create an environment variable that points to your Windows license server. This ensures PyMechanical can find the license. ```bash winhostIP=$(grep -m 1 host.docker.internal /etc/hosts | awk '{print $1}') export ANSYSLMD_LICENSE_FILE=1055@$winhostIP ``` -------------------------------- ### Launch Mechanical in Insecure Mode Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/remote_session/grpc_security.rst Launches a new Ansys Mechanical instance using the insecure transport mode. Use with caution as data is not encrypted. ```bash ansys-mechanical --port 10000 --transport-mode insecure ``` -------------------------------- ### Import Geometry Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/embedding/helpers.rst Use the import_geometry helper to load a geometry file into the Mechanical model. ```python app.helpers.import_geometry("path/to/geometry.x_t") ``` -------------------------------- ### Execute Script using AnsysWBU.exe (Windows) Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/embedding/overview.rst Launch a Mechanical script using the AnsysWBU.exe executable on Windows, specifying the script file with the -script argument. ```console "C:/Program Files/ANSYS Inc/v{mechanical_version}/aisol/bin/winx64/AnsysWBU.exe -DSApplet -AppModeMech -script file.py" ``` -------------------------------- ### Check License Manager Availability by Version Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/embedding/licensing.rst Demonstrates how to check if the LicenseManager is available for a specific Mechanical version. LicenseManager requires Mechanical 2025 R2 (version 252) or later. ```python from ansys.mechanical.core import App app = App(version=242) # Older version try: license_mgr = app.license_manager except Exception as e: print(e) # Output: "LicenseManager is only available for version 252 and later." ``` -------------------------------- ### Connect to Mechanical Server using connect_to_mechanical Source: https://github.com/ansys/pymechanical/blob/main/doc/source/getting_started/running_mechanical.rst Use the `connect_to_mechanical` function as an alternative method to connect to a running Mechanical server instance, specifying the host and port. ```python from ansys.mechanical.core import connect_to_mechanical mechanical = connect_to_mechanical("192.168.0.1", port=10000) ``` -------------------------------- ### Create Embedded App with Global Scripting Entry Points Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/embedding/overview.rst Embed Mechanical and gain direct access to global scripting entry points like DataModel by passing globals() to the App constructor. ```python from ansys.mechanical.core import App app = App(globals=globals()) ns = DataModel.Project.Model.AddNamedSelection() ns.Name = "Jarvis" ``` -------------------------------- ### Create an Embedded Mechanical App Instance Source: https://github.com/ansys/pymechanical/blob/main/doc/source/user_guide/embedding/overview.rst Instantiate the App class to embed Mechanical in your Python process. This provides direct access to the Mechanical data model. ```python from ansys.mechanical.core import App app = App() ns = app.DataModel.Project.Model.AddNamedSelection() ```