Using pypizza shapes

This example illustrates how Python code can access data types written in the Xc++ language. See also the documentation of the cxPython library.

Prerequisites

This example assumes you have installed the pyceda module and installed the pypizza module.

Models defined in Xc++

The pypizza module defines some simple shape data types in the Xc++ language:


// Shapes.h

@import "Pizza.h"
@import "DateTime.h"

namespace shapes
{
    $model+ TPoint
    {
        float32 X;
        float32 Y;
    };

    $model+ TPolygon
    {
        xvector<TPoint> V;
    };

    $model+ TCircle
    {
        float32 Radius;
    };

    $model+ TRectangle
    {
        float32 Width;
        float32 Height;
    };

    $variant+ TShape
    {
        default TCircle(200);
        TCircle Circle;
        TRectangle Rectangle;
        TPolygon Polygon;
    };
} // namespace shapes

Note that TShape is a $variant

Example accessing the models from Python

Using your favorite text editor, write a file named shapes-example.py as follows:


# shapes-example.py
# This is python

import pyceda
import pypizza

shapes = pyceda.cns.shapes

p = shapes.TPolygon( [ shapes.TPoint(X=1,Y=2), shapes.TPoint(X=7,Y=-2) ] )
p.V.append( shapes.TPoint(X=5,Y=1) )
p.V[0].X = 1000
p.V.append( p.V[1] )
print 'p = ' + `p`

c = shapes.TCircle( Radius = 100 )
print 'c = ' + `c`

r = shapes.TRectangle( Width=200, Height=300 )
print 'r = ' + `r`

s = shapes.TShape()
print 's = ' + `s`

s.polygon = p
print 's = ' + `s`

In a bash terminal run shapes-example.py as follows:


# bash
python shapes-example.py

This produces the following output

    p = TPolygon([TPoint(1000,2),TPoint(7,-2),TPoint(5,1),TPoint(7,-2)])
    c = TCircle(100)
    r = TRectangle(200,300)
    s = circle(TCircle(200))
    s = polygon(TPolygon([TPoint(1000,2),TPoint(7,-2),TPoint(5,1),TPoint(7,-2)]))