Python Automation - Getting Started

From SysCAD Documentation
(Redirected from Python Automation Examples)
Jump to navigation Jump to search

Navigation: User Guide ➔ COM Automation ➔ Python Automation

Python Setup Python Examples Python Script Optimisation Visulisation Python GUI Tags and Data
Installation &
Troubleshooting
List of Examples Simple Script
(pywin32)
SysCAD COM
Python Class
Automated
Model Testing
Constrained FEM
(numpy|matplotlib)
Optimisation
(numpy|scipy)
Adding Plots
(numpy|matplotlib)
Dynamic
with GUI
Accessing Data
(sqlite3|pandas)

Setting up Python

  • If just starting: Install a Python distribution, we recommend getting the latest version from python.org and take note of the installation directory. The default installation will install the python launcher which makes life easier, since you do not need to specify the location of the Python executable in the system PATH.
  • Other installations, such as Anaconda, or via Visual Studio, may bury the installation deep inside the file hierarchy; if you plan on using these, figure out where the install is located, since you need to access this folder on occasions, in particular the Scripts folder where the Python package manager lives.
    Pyinstall.png
    You can add this to the system PATH, or run pip directly from the Scripts folder using Cmd Window. Using the Python launcher there is no need to specify PATH. The examples of package installation below assume the Python launcher is available. If not, you can run pip directly from the Scripts folder.
  • Please see https://packaging.python.org/en/latest/tutorials/installing-packages for detailed instructions.

Useful Python packages

The following table lists some useful Python packages we can use:

Python Packages Cmd Window Command Brief Description Link to Help page
PIP Python -m pip install -U pip PIP is a package installer for Python. Once it is installed, it can be used to install the other Python packages (listed below) The default installation will include pip, so this step will not generally be necessary Python tutorial - installing packages
pywin32 py -m pip install pywin32 Python extensions for Microsoft Windows, this enables the use of COM automation from within python. https://pypi.org/project/pywin32
numpy py -m pip install numpy numpy is the base package for efficient handling of large data arrays https://www.numpy.org
matplotlib py -m pip install matplotlib matplotlib is for plotting and data visualisation https://matplotlib.org
scipy py -m pip install scipy scipy is a library of numerical tools for optimisation, equation solving, linear algebra and more https://www.scipy.org/

NOTES:

  • Run Command Prompt as Administrator.
  • To use COM automation features in Python, you must install the Python Extensions for Microsoft Windows (pywin32). This package enables Python to interact with Windows COM objects.
  • To run all the Python examples provided within the SysCAD help documentation, make sure to install all the third-party packages mentioned in the above table.

Running Python

Verifying Your Python Installation

To confirm that your Python environment is set up correctly, follow these steps:

  1. Open a Python Shell - you can use a Python command-line interface such as IDLE.
    • On Windows, go to the Start menu and select Python 3.13 - IDLE (or the version you have installed).
    • Alternatively, open a Command Prompt and type Python to launch the interactive shell.
  2. Run a Test Command
    • Once the shell is open, type the following commands to verify that Python is functioning properly:
>>> print("Hello, Python!")

If the message Hello, Python! appears, your Python installation is working correctly.

Launching SysCAD from Python

To interface with SysCAD using Python, use the win32com.client module:

>>> import win32com.client as wc
>>> SysCAD = wc.DispatchEx("SysCADSimulator93.Application")

Python 3.7 shell

This should launch SysCAD as if started manually. If it does not, refer to the Troubleshooting section.

Interacting with SysCAD

The VersionNumber method can be used to get the version and build numbers:

>>> [SysCAD.VersionNumber(i) for i in range(4)]
[9, 3, 139, 37044]

This tells us we are working with SysCAD93, the 139 release and build 37044, which you can see at the top of the main SysCAD window.

Once this is working, you can perform actions such as:

  • Open a project
  • Set a value to the SysCAD project
  • Solve the project
  • Get values from the SysCAD project
  • Close a project

For example, the following commands will open a SysCAD project:

>>> root  = 'C:\\SysCAD139\\Examples\\25 Gold\\'
>>> Prj = SysCAD.OpenProject(root+"Demo Gold Project.spf\\Project.spj")

Using Python, you can perform the above tasks either manually within SysCAD or through a Python script. One of the key advantages of Python scripting is its ability to interact with the SysCAD GUI in real time, allowing for more dynamic testing and enhanced user interaction. The Prj COM object introduces additional methods that enable you to programmatically carry out a wide range of tasks within SysCAD.

It is important to clean up any Python COM objects you create, otherwise you can be left with "zombie" SysCAD processes. These should be deleted. If things don't seem to be working right, check for zombie processes using Process Explorer. See Testing if SysCAD is open in VBA (COM Automation) for some Python and VBA code to automate this.

>>> del(Prj)
>>> del(SysCAD)

Troubleshooting

If SysCAD did not start normally, then check that the COM interface is working by opening a different application such as MS Excel

>>> excel = wc.DispatchEx('Excel.Application')
>>> excel.Visible = True

(If you don't type the second line, Excel will just be minimized with the icon on the taskbar)

  • If you can open Excel, but not SysCAD, check that SysCAD is registered as a COM object by running the RegAll_Verbose command script, see the main page Troubleshooting COM.

Once you have created the SysCAD object, you have access to further methods. (The pywin32 installation includes a COM browser shell that provides auto-completion on com objects.)

Important Notes

Notes for using SysCAD COM with Python:

  • There are issues with cleaning up COM objects after use. If these are not deleted (use the del() operation in reverse order to how you create the object) then you can end up with a "zombie" SysCAD process which is still around after SysCAD is closed, and can cause problems when you reopen SysCAD with the COM interface.
  • Timing can also be an issue due to messaging in SysCAD and asynchronous nature of some commands. If you have problems use sleep() from the time module to allow SysCAD to "catch up" before doing the next COM operation.
  • If SysCAD fails to start in time, then COM may fail, and you will get a warning. On a slow machine, you can increase the Timeout from the General Options dialog

Scdcom05.png

  • The approach in most examples (Open a new SysCAD, open the project, run and change tags, close the project, delete all the COM objects, and finally delete the SysCAD COM object to close SysCAD), is generally safe and will clean up everything.
  • If you attach to a running copy of SysCAD with an open project and do not close the project, you can end up with the "zombie" SysCAD process. These can be deleted using Process Explorer or Task Manager.
  • For exploring COM features you might like to use the PythonWin IDE/GUI shell which will show you available COM methods at the command line.

Scdcom04.png

Python Tips and Tricks