Getting Started STK COM integration using Python

Information

 
QuestionHow do I use Python to drive STK?
Answer

AGI recommends using IPython with an IDE such as Spyder or Jupyter Notebook, which can be downloaded for free with the Anaconda Python installer: https://www.continuum.io/downloads

Note:  You will need an STK Integration license to integrate Python with STK.
After installation completes, launch the Anaconda navigator, then Launch Jupyter notebook (spyder is also a great IDE, more similar to MATLAB).  Type in the following code to connect to STK:

from win32api import GetSystemMetrics
import comtypes
from comtypes.client import CreateObject

# Get reference to running STK instance
uiApplication=CreateObject("STK11.Application")
uiApplication.Visible=True

# Get our IAgStkObjectRoot interface
root=uiApplication.Personality2

#Note: When 'root=uiApplication.Personality2' is executed, the comtypes library automatically creates a gen folder that contains STKUtil and STK Objects. After running this at least once on your computer, the following two lines should be moved before the 'uiApplication=CreateObject("STK11.Application")' line for improved performance.  

from comtypes.gen import STKUtil
from comtypes.gen import STKObjects

Alternatively, you can use standard python with the win32com library, which won't have intellisense.  To install the win32com library type "pip install win32com" from a command prompt.

import win32com.client
# Get reference to running STK instance
uiApplication = win32com.client.Dispatch('STK11.Application')
uiApplication.Visible = True

# Get our IAgStkObjectRoot interface
root = uiApplication.Personality2

You can now create your scenario and automate STK using the Python code snippets:

http://help.agi.com/stkdevkit/source/stkObjects/ObjModPythonCodeSamples.htm


For a more comprehensive overview of STK Integration with Python, please visit the following github link:

https://github.com/agifsnyder/STK-Integration/blob/master/STK%20and%20Python%20with%20comtypes.ipynb

 
Resource File 1 
Resource File 2