Added TextModelParser for text files

This commit is contained in:
Thomas FORGIONE 2017-01-17 10:47:57 +01:00
parent 23a3b4535d
commit 41a13e6014
No known key found for this signature in database
GPG Key ID: 2A210FFC062E00C3
5 changed files with 20 additions and 23 deletions

View File

@ -134,23 +134,9 @@ class ModelParser:
self.current_part.add_face(face) self.current_part.add_face(face)
def parse_line(self, string): def parse_bytes(self, bytes):
"""Parses a line of a text model
This method needs to be implemented by each subclass of ModelParser.
"""
pass pass
def parse_file(self, path):
"""Sets the path of the model and parse each line
"""
self.path = path
with open(path) as f:
for line in f.readlines():
line = line.rstrip()
if line != '':
self.parse_line(line)
def draw(self): def draw(self):
"""Draws each part of the model with OpenGL """Draws each part of the model with OpenGL
""" """
@ -221,6 +207,17 @@ class ModelParser:
face.b.normal = index face.b.normal = index
face.c.normal = index face.c.normal = index
class TextModelParser(ModelParser):
def parse_file(self, path):
"""Sets the path of the model and parse each line
"""
self.path = path
with open(path) as f:
for line in f.readlines():
line = line.rstrip()
if line != '':
self.parse_line(line)
class BoundingBox: class BoundingBox:
"""Represents a bounding box of a 3D model """Represents a bounding box of a 3D model

View File

@ -1,4 +1,4 @@
from ..basemodel import ModelParser, Exporter, Vertex, TexCoord, Normal, FaceVertex, Face from ..basemodel import TextModelParser, Exporter, Vertex, TexCoord, Normal, FaceVertex, Face
from ..mesh import Material, MeshPart from ..mesh import Material, MeshPart
from functools import reduce from functools import reduce
import os.path import os.path
@ -8,7 +8,7 @@ import sys
def is_obj(filename): def is_obj(filename):
return filename[-4:] == '.obj' return filename[-4:] == '.obj'
class OBJParser(ModelParser): class OBJParser(TextModelParser):
def __init__(self, up_conversion = None): def __init__(self, up_conversion = None):
super().__init__(up_conversion) super().__init__(up_conversion)

View File

@ -1,10 +1,10 @@
from ..basemodel import ModelParser, Exporter, Vertex, TexCoord, Normal, FaceVertex, Face from ..basemodel import TextModelParser, Exporter, Vertex, TexCoord, Normal, FaceVertex, Face
from ..mesh import Material, MeshPart from ..mesh import Material, MeshPart
def is_off(filename): def is_off(filename):
return filename[-4:] == '.off' return filename[-4:] == '.off'
class OFFParser(ModelParser): class OFFParser(TextModelParser):
def __init__(self, up_conversion = None): def __init__(self, up_conversion = None):
super().__init__(up_conversion) super().__init__(up_conversion)
self.vertex_number = None self.vertex_number = None

View File

@ -1,11 +1,11 @@
import os import os
import PIL import PIL
from ..basemodel import ModelParser, Exporter, Vertex, Face, FaceVertex, TexCoord, Material from ..basemodel import ModelParser, TextModelParser, Exporter, Vertex, Face, FaceVertex, TexCoord, Material
def is_ply(filename): def is_ply(filename):
return filename[-4:] == '.ply' return filename[-4:] == '.ply'
class PLYParser(ModelParser): class PLYParser(TextModelParser):
def __init__(self, up_conversion = None): def __init__(self, up_conversion = None):
super().__init__(up_conversion) super().__init__(up_conversion)

View File

@ -1,4 +1,4 @@
from ..basemodel import ModelParser, Exporter, Vertex, FaceVertex, Face from ..basemodel import TextModelParser, Exporter, Vertex, FaceVertex, Face
from ..mesh import MeshPart from ..mesh import MeshPart
import os.path import os.path
@ -6,7 +6,7 @@ import os.path
def is_stl(filename): def is_stl(filename):
return filename[-4:] == '.stl' return filename[-4:] == '.stl'
class STLParser(ModelParser): class STLParser(TextModelParser):
def __init__(self, up_conversion = None): def __init__(self, up_conversion = None):
super().__init__(up_conversion) super().__init__(up_conversion)