Python Automation - Getting Started

From SysCAD Documentation
Jump to navigation Jump to search

Navigation: User Guide ➔ COM Automation ➔ Python Automation

Python Setup Python Examples
Installation &
Troubleshooting
Python
Utilities
Basic Usage & Scenarios Constrained FEM
(numpy|scipy|matplotlib)
Optimisation
(COM | scipy tools)
Model Testing
Framework
Dynamic
with GUI
Dynamic
External DLL
Programmatic
Model Generation
Importing data
to SysCAD

Python Setup

Installing Python and Python Extensions for Windows

  • 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.

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 visualization https://matplotlib.org
scipy py -m pip install scipy scipy is a library of numerical tools for optimization, equation solving, linear algebra and more https://www.scipy.org/

NOTE:

  • Start a Cmd window as Administrator.
  • To use COM automation from within python, it is necessary to have the "Python Extensions for Microsoft Windows" (pywin32) installed.
  • For other (non-COM related) python utilities, please see Python Utilities for more information.


Running Python and Troubleshooting

To test that things are working, open a python command line shell such as IDLE (e.g.: Windows Start menu, Python 3.9 - IDLE), or run python from a command window. Type the commands below

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

Python 3.7 shell

SysCAD should open just as it would if you started it normally. If not, check that the COM interface is working by opening a different application such as 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.)

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

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

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

Once this is working, you are ready to have some fun. To create a new project we need to specify a configuration file and project name. Using the distributed gold example configuration, we can say

>>> root  = 'C:\\SysCAD138\\Examples\\25 Gold\\'
>>> Prj = SysCAD.CreateProject(root+'CfgFiles\\Gold Example.cfg', root+"MyProject.spf\\")

Prj is a new COM object with additional methods, and you can use it to programatically insert units, draw connections, and pretty much anything you can do through the graphical interface. At this stage it is better to write scripts to do all this, but running from the shell does let you see the available commands and required parameters.

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 here for some python and VBA code to automate this.

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

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

Examples

  1. Python Example - Getting Started - Basic usage and example for running scenarios.
  2. Python Example - Constrained Free Energy Minimisation - For complex, numerically intensive side calculations and plotting, drive SysCAD through the COM interface and use numpy/scipy/matplotlib.
  3. Python Example - Optimisation - Using COM and scipy tools for model (SysCAD Project) optimisation.
  4. Python Example - Simple Unit Model Testing Framework - For validating model results when doing model development.
  5. Python Example - Interactive Dynamic Example - An interactive dynamic example using COM to provide Python Graphical User Interface (GUI) elements.
  6. Python Example - External DLL Example - An example of driving a Dynamic project with python, while doing calculations in an external DLL
  7. Python Example - Programmatic Model Generation - An example of using COM to insert models and graphics in a project.
  8. Python Example - Database Access - Importing data into the SysCAD database.