Python Automation Examples
Setting up and Troubleshooting
To use COM automation from within python, it is useful to have some Microsoft Windows Extensions installed. This is a handy way to get started, but it has some features that make working with COM objects a bit easier.
- For Python 3.7: If you have not installed "Python extensions for Microsoft Windows" (pypiwin32), in a Cmd window as Administrator install as follows "python -m pip install pypiwin32". If pip is not installed, do this first "python -m pip install -U pip".
- For Python 2.7: You need to install the Win32All extensions .
- If just starting: Ideally install the Anaconda distribution. . This includes python, along with a large number of useful packages for numerical modelling, graphics and data analysis, including those used in these examples (numpy, scipy, matplotlib and the Windows extensions)
Some other (non COM related) python utilities are here.
To test that things are working, open the shell and type the commands below
>>> import win32com.client as wc >>> SysCAD = wc.DispatchEx("SysCADSimulator93.Application")
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. You can see a list of these by typing
in the shell as above. You can get completion by typing further characters, so typing v highlights the VersionNumber method. This 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)
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.
- 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.
- The examples are using Python3, but will also work with Python2, with minor changes to the print statements:
## Print statements for Python27: print "Gold in product", print Tags.TagValue(goldFlow) ## Get a tag value
- Python Example - Getting Started - Basic usage and example for running scenarios.
- Constrained Free Energy Minimisation - For complex, numerically intensive side calculations and plotting, drive SysCAD through the COM interface and use numpy/scipy/matplotlib.
- Optimisation - Using COM and scipy tools for model (SysCAD Project) Optimisation.
- Simple Unit Model Testing Framework - For validating model results when doing model development.
- Interactive Dynamic Example - An interactive dynamic example using COM to provide Python Graphical User Interface (GUI) elements.
- Programmatic Model Generation - An example of using COM to insert models and graphics in a project.