Using IParsingActions — GSTPy documentation

# Using IParsingActions

## Minimal implementation

The following minimal implementations can be used as a starting point for creating your own implementation. Just add an implementation to the methods you are interested in. The listed methods are all the ones that will be called by our parser for the corresponding tpye.

Note

- You can remove any of the shown methods, not overriden once are noop (i.e. do nothing)

- You can add new methods, but be sure to not use any of the names from `IParsingActions`.

- If you get a “parsing failed!” error, you probably did something wrong with your implementation.

### Multipoint

    import GSTPy

    class MultipointCallback(GSTPy.IParsingActions):
        def set_name(self, name):
            pass

        def set_color(self, color):
            pass

        def set_transparency(self, transparency):
            pass

        def set_custom_property(self, name, value):
            pass

        def create_multipoint3(self):
            pass

        def add_point(self, point):
            pass

        def geometry_parsing_finished(self):
            pass

        def create_vertex_property(self, name, type):
            pass

        def create_cell_property(self, name, type):
            pass

        def add_property_value(self, value):
            pass

        def property_parsing_finished(self):
            pass

### Multilinestring

    import GSTPy

    class MultilinestringCallback(GSTPy.IParsingActions):
        def set_name(self, name):
            pass

        def set_color(self, color):
            pass

        def set_transparency(self, transparency):
            pass

        def set_custom_property(self, name, value):
            pass

        def create_multiline3(self):
            pass

        def add_point(self, point):
            pass

        def new_simplex(self):
            pass

        def geometry_parsing_finished(self):
            pass

        def create_vertex_property(self, name, type):
            pass

        def create_cell_property(self, name, type):
            pass

        def add_property_value(self, value):
            pass

        def property_parsing_finished(self):
            pass

### Tin

    import GSTPy

    class TinCallback(GSTPy.IParsingActions):
        def set_name(self, name):
            pass

        def set_color(self, color):
            pass

        def set_transparency(self, transparency):
            pass

        def set_custom_property(self, name, value):
            pass

        def create_triangle_net3(self):
            pass

        def add_point(self, point):
            pass

        def add_triangle(self, triangle):
            pass

        def geometry_parsing_finished(self):
            pass

        def create_vertex_property(self, name, type):
            pass

        def create_cell_property(self, name, type):
            pass

        def add_property_value(self, value):
            pass

        def property_parsing_finished(self):
            pass

### Tetrahderon Net

    import GSTPy

    class TetrahderonNetCallback(GSTPy.IParsingActions):
        def set_name(self, name):
            pass

        def set_color(self, color):
            pass

        def set_transparency(self, transparency):
            pass

        def set_custom_property(self, name, value):
            pass

        def create_tetrahedon_net3(self):
            pass

        def add_point(self, point):
            pass

        def add_tetrahedron(self, tetrahedron):
            pass

        def geometry_parsing_finished(self):
            pass

        def create_vertex_property(self, name, type):
            pass

        def create_cell_property(self, name, type):
            pass

        def add_property_value(self, value):
            pass

        def property_parsing_finished(self):
            pass

## Example implementation

As a simple example lets implement a CSV file parser callback.

We want to list all points of a geometry with its vertex properties. We are only interested in the points, so lines, triangles and so on will be ignored.

csv_points_only.py

    import GSTPy

    class CsvCallback(GSTPy.IParsingActions):
        def __init__(self):

            super(CsvCallback, self).__init__()

            self.points = []
            self.property_names = []
            self.prop_values = []

        def add_point(self, point):
            self.points.append(point)

        def create_vertex_property(self, name, type):
            self.property_names.append(name)
            # type doesn't interest us here
            # the values are returned as strings anyways

        def add_property_value(self, value):
            self.prop_values.append(value)

        def get_csv_string(self):
            # make sure our property value count is as expected
            num_properties = len(self.property_names)
            num_points = len(self.points)
            assert len(self.prop_values) == num_properties * num_points
            # CSV header line
            tokens = ["x", "y", "z"]
            tokens.extend(self.property_names)
            csv = ",".join(tokens) + "\n"
            for i in xrange(num_points):
                # add the current point
                point = self.points[i]
                tokens = [str(point.x), str(point.y), str(point.z)]
                # add the property values for the current point
                prop_offset = i * num_properties
                tokens.extend(
                    self.prop_values[prop_offset:prop_offset + num_properties])
                csv += ",".join(tokens) + "\n"
            return csv