Python Example - Getting Started

From SysCAD Documentation
Jump to navigation Jump to search

Navigation: User Guide ➔ COM Automation ➔ Python Automation ➔ Example - Basic Usage

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)

Basic Usage: Opening, running, setting and getting tags

This basic example opens up the Demo Gold steady state ProBal project, looks at the amount of gold in the product, changes the feed composition tag, runs the project, then looks at the new value of gold in product.

## Example Automation Using python.
import sys
import time
try:
  import win32com.client as wc    ## PyWin32 COM Client
except:
  print ("Win32 Python extensions not found")
  print ("Install pywin32:  https://pypi.org/project/pywin32/")
  sys.exit(1)

ProgID = "SysCADSimulator93.Application"
SysCAD = wc.DispatchEx(ProgID)  ## Fire up SysCAD
build = SysCAD.VersionNumber(2) ## You can now query the build number

ScdDir = r'C:\SysCAD%d' % build
print ("Using Build", build, "Install Directory is ", ScdDir)

ScdPrj = r'\Examples\25 Gold\Demo Gold Project.spf\Project.spj'
#### If you use \ in file and directory strings use a raw string or escape \'s 
#### e.g. Prj = SysCAD.OpenProject('C:\\SysCADProjectFiles\\Sample.spf\\Project.spj')
####    Prj = SysCAD.OpenProject('C:/SysCADProjectFiles/Sample.spf/Project.spj')  ## also works

Prj = SysCAD.OpenProject(ScdDir+ScdPrj)  ## Open project 

Tags = Prj.Tags         
goldFlow = "P_007.Qo.QM.Au(s) (oz/d)"
print ("Gold in product", end=" ")
print (Tags.TagValue(goldFlow))  ## Get a tag value

Solver = Prj.Solver
ProBal = Solver.ProBal

ProBal.Start()                        ## Start ProBal
while True:                           ## Wait until solved 
   print ("Solving...")                 
   time.sleep(.5)
   if ProBal.IsStopped:
       break
print ("Feed ppm in gold ore", Tags.TagValue("FEED_ppm_SET.TC[1].User.Value"))   ## Print another tag
print ("Changing value to 45 ppm")
Tags.SetTagValue("FEED_ppm_SET.TC[1].User.Value", 45)  ## Change tag value
ProBal.Start()          ## and re-solve
while True:
   print ("Solving...")
   time.sleep(.5)
   if ProBal.IsStopped:
       break
print ("Gold in product", end = " ")
print (Tags.TagValue(goldFlow))   ## Print the same tag
 
SysCAD.CloseProject(False)
##
time.sleep(5)
del(Solver)
del(Tags)
del(Prj)
del(SysCAD)

The output will be something like:

Using Build 139 Install Directory is  C:\SysCAD139
Gold in product 57.6095546633
Solving...
Feed ppm in gold ore 40.0
Changing value to 45 ppm
Solving...
Gold in product 64.8079746073

See Important Notes for general information and troubleshooting on using SysCAD COM with Python.

When using COM you create a number of COM objects representing various features of SysCAD: a Project object (Prj), a Tags object, a Solver object and so on. Keeping track of these (and cleaning up afterwards) is a mess, and we can simplify this by encapsulating all the functionality in a single Python class which we will use for subsequent examples. We assume all the necessary packages are installed: if you get errors when importing a particular package such as numpy, you will need to install it to continue.

Encapsulating COM functionality in a Python class

It’s a good practice to create a Python class that encapsulates all the required functionality in one place. Once defined, the class can be reused across multiple scripts and projects.

The syscadif.py module encapsulates core SysCAD COM functionality within a Python class called SysCADCom. This class will be used in subsequent examples to simplify interaction with SysCAD.

To use this module, save the syscadif.py file either in your working directory (where your Python script is located), or in a location included in your PYTHONPATH. This ensures Python can locate and import the module when needed.

Using the SysCADCom class, the example above now becomes

## Example Automation Using python.
import syscadif  ## SysCAD COM interface class

sc = syscadif.SysCADCom()
ScdDir = r'C:\SysCAD%d' % sc.build
print ("Using Build", sc.build, "Install Directory is ", ScdDir)
ScdPrj = r'\Examples\25 Gold\Demo Gold Project.spf\Project.spj'
sc.OpenProject(ScdDir+ScdPrj)  ## Open project 
goldFlowTag = "P_007.Qo.QM.Au(s) (oz/d)"

sc.run()
print ("Feed ppm in gold ore", sc["FEED_ppm_SET.TC[1].User.Value"])   ## Print another tag
print ("Gold in product", sc[goldFlowTag])
input ("Hit any key to change a tag, model reset and rerun")
sc["$Solver.Cmd.CompleteReset"] = 1
print ("Changing value to 45 ppm")
sc["FEED_ppm_SET.TC[1].User.Value"] = 45  ## Change tag value
sc.run()          ## and re-solve
print ("Gold in product", sc[goldFlowTag])
input ("Hit any key to finish")
 
sc.CloseProject()
sc.Close()