user1754487
user1754487

Reputation: 1

Access python data from C(++)

I write some python functions to read/write simple spatial data like points, polygons, triangulated surfaces into/from a simple data structure in Python. I'm aware there are some possibilities to access those data structure from C or C++ like in this post: Passing Python list to C++ vector using Boost.python

However, I'm not an experienced programmer using templates and I would like to start first with an easy way - if it exist. So if I have a list of point objects (see simple class definition), how would I access this list from a C-Program reading the respective variables.

class point3d():

def __init__(self, objectName, version, id=[], xyz=[], prop=[], unit=[], val=[]):
    self.id = id
    self.xyz = xyz
    self.prop = prop
    self.unit = unit
    self.val = val
    dim = len(self.xyz)
    self.objectName = objectName
    self.version = version
    if dim == 1:
        self.x = self.xyz[0]
    elif dim == 2:
        self.x = self.xyz[0]
        self.y = self.xyz[1]
    elif dim == 3:
        self.x = self.xyz[0]
        self.y = self.xyz[1]
        self.z = self.xyz[2]

def __del__(self):
    pass

def getObjectName(self):
    return self.objectName

def getVersion(self):
    return self.version

def getXYZ(self):
    return self.xyz

def getDim(self):
    return self.dim

def getProp(self):
    return self.prop

def getUnit(self):
    return self.unit

def getVal(self):
    return self.val

def getId(self):
    return self.id

Upvotes: 0

Views: 102

Answers (1)

Roland Smith
Roland Smith

Reputation: 43523

If you want to call a C/C++ function from your python program, ctypes is the way to go.

Upvotes: 1

Related Questions