COM Integration/Calling Python code

< COM Integration
Revision as of 21:00, 1 August 2018 by Lchrisman (talk | contribs) (Python side, started writing.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Requires Analytica Enterprise or higher

You can use the COM Integration functions in Analytica to call code written in the Python programming language. This page illustrates how.

Calling Python code

In this example, we implement an object in Python that can be instantiated and used from your Analytica model. The object instantiates as a COM object (Component Object Model), and is available using the standard COM Integration features of Analytica Enterprise.

The example used here calls a Python function found in the Python scipy.spatial library to compute a Delaunay tessellation, also know as a Delaunay triangulation, of a set of points in 2-D.

After you implement your Python object, but before you instantiate if from Analytica, you'll need to register the object with Windows, which enables COMCreateObject to find it.

Prerequisites

You'll need to install Python for Windows (this example was developed using Python 3.6), and ensure you have the following Python modules installed in your Python environment:

  • win32com
  • pythoncom
  • comtypes
  • winreg
  • win32api
  • numpy
  • scipy
  • os
  • sys
  • time

How to install these is beyond the scope of this article (and beyond the scope of Lumina tech support), but is something the Python user community should be able to help you with.

The Python class

The Python code that implements the COM object is as follows:

import numpy as np
from scipy.spatial import Delaunay
import Analytica_Python

 class DelaunayCOM:
    _reg_clsid_ = "{B524651C-71B2-4521-9E9D-8CC470E51B24}"  # Do not use this CSLID! Generate your own!
    _reg_desc_ = "COM component that computes a Delaunay tesselation"   
    _reg_progid_ = "Lumina.DelaunayCOM"    
    _reg_class_spec_ = "DelaunayCOM.DelaunayCOM"
    _public_methods_ = ['Tessellation','Pause']
    _public_attrs_ = ['softspace', 'noCalls']
    _readonly_attrs_ = ['noCalls']
     
    def __init__(self):
        self.softspace = 1
        self.noCalls = 0
     
    def Pause(self):
        Analytica_Python.gBreakPump = True
            
    def Tessellation(self,pts):
        tri = Delaunay(np.array(pts))
        return tri.simplices.tolist()
     
 Analytica_Python.AddCOMClass(DelaunayCOM)
     
 if __name__ == "__main__":
    Analytica_Python.TopLevelServer()

The Tessellation method is the method that is actually called. The Pause method is optional, but is useful when debugging. The Analytica_Python module contains generic functions that assist with registering the class (or classes if you have more than one) and serving it at runtime. That module shouldn't require customization when you create your own classes.

Special COM members

The class contains several members with information about the COM object.

_reg_clsid_ is a unique class ID, and is required for any object that will be instantiated by Analytica. (It is not required for objects that are returned from methods that are called). When you create your own class, you need to generate your own unique CLSLID -- do not reuse the one shown above, which should only ever be used with the Lumina.DelaunayCOM object. You can do this from the Online GUID Generator.

_reg_progid_ is the name that will be used from COMCreateObject. _reg_class_spec_ is the name of the Python module that contains this class, plus a dot, plus the name of the class itself. Since this code is saved in a file named "DelaunayCOM.py, the part before the dot is DelaunayCOM.

_public_methods_ lists the method that are public -- that can be called from Analytica.

Analytica side

Comments


You are not allowed to post comments.