linetypes.py

Mon, 11 Dec 2017 00:46:35 +0200

author
Santeri Piippo
date
Mon, 11 Dec 2017 00:46:35 +0200
changeset 3
1dc58f44d556
parent 1
5411a25cfca7
child 4
8eb83f200486
permissions
-rw-r--r--

Can now write dat files, added direct color handling

def ldraw_str(value):
    ' Like str() except removes unneeded ".0"-suffixes '
    rep = str(value)
    if isinstance(value, float):
        if rep.endswith('.0'):
            rep = rep[:-2]
        if rep == '-0':
            rep = '0'
    return rep

class EmptyLine:
    def __repr__(self):
        return 'linetypes.EmptyLine()'
    def textual_representation(self):
        return ''

class Comment:
    def __init__(self, text, style = 'old'):
        if style == 'old' and text.startswith('//'):
            self.text = text[2:].strip()
            self.style = 'new'
        else:
            self.text = text
            self.style = style
    def __repr__(self):
        return str.format('linetypes.Comment({text!r}, {style!r})',
            text = self.text,
            style = self.style,
        )
    def textual_representation(self):
        if self.style == 'old':
            return '0 ' + self.text
        else:
            return '0 // ' + self.text

class SubfileReference:
    def __init__(self, *, color, subfile_path, anchor, matrix):
        self.color, self.subfile_path, = color, subfile_path
        self.anchor, self.matrix = anchor, matrix
    def __repr__(self):
        return str.format('linetypes.SubfileReference(' \
            'color = {color!r}, ' \
            'subfile_path = {subfile_path!r}, ' \
            'anchor = {anchor!r}, ' \
            'matrix = {matrix!r})', **self.__dict__)
    def textual_representation(self):
        args = [
            1,
            self.color,
            *self.anchor.coordinates,
            *self.matrix.values,
            self.subfile_path,
        ]
        return ' '.join(ldraw_str(arg) for arg in args)

class BasePolygon:
    def __init__(self, *, color, geometry):
        self.color, self.geometry = color, geometry
    def __repr__(self):
        return str.format('linetypes.{typename}(' \
            'color = {color!r}, ' \
            'geometry = {geometry!r})',
            typename = type(self).__name__,
            color = self.color,
            geometry = self.geometry,
        )
    def base_textual_representation(self):
        args = [self.color]
        for vertex in self.geometry.vertices:
            args += vertex.coordinates
        return ' '.join(ldraw_str(arg) for arg in args)

class LineSegment(BasePolygon):
    def textual_representation(self):
        return '2 ' + self.base_textual_representation()

class Triangle(BasePolygon):
    def textual_representation(self):
        return '3 ' + self.base_textual_representation()

class Quadrilateral(BasePolygon):
    def textual_representation(self):
        return '4 ' + self.base_textual_representation()

class Contour(LineSegment):
    def __init__(self, *, color, geometry, control_points):
        super().__init__(color = color, geometry = geometry)
        self.control_points = control_points
        assert(len(self.control_points) == 2)
    def __repr__(self):
        return str.format('linetypes.Contour(' \
            'color = {color!r}, ' \
            'geometry = {geometry!r}, ' \
            'control_points = {control_points!r})', **self.__dict__)
    def textual_representation(self):
        result = '5 ' + self.base_textual_representation()
        for control_point in self.control_points:
            strings = (ldraw_str(value) for value in control_point.coordinates)
            result += ' '
            result += ' '.join(strings)
        return result

mercurial