Commit 47e4bb66 authored by sumpfralle's avatar sumpfralle

turned the BaseModel into an iterator for simpler handling of complex content


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@383 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent 2290a87b
...@@ -63,14 +63,26 @@ class BaseModel(object): ...@@ -63,14 +63,26 @@ class BaseModel(object):
def __add__(self, other_model): def __add__(self, other_model):
""" combine two models """ """ combine two models """
result = self.__class__() result = self.__class__()
for item_group in self._item_groups + other_model._item_groups: for item in self.next():
for item in item_group: result.append(item)
for item in other_model.next():
result.append(item) result.append(item)
return result return result
def to_OpenGL(self): def __iter__(self):
return self
def next(self):
for item_group in self._item_groups: for item_group in self._item_groups:
for item in item_group: for item in item_group:
if isinstance(item, list):
for subitem in item:
yield subitem
else:
yield item
def to_OpenGL(self):
for item in self.next():
item.to_OpenGL() item.to_OpenGL()
def _update_limits(self, item): def _update_limits(self, item):
...@@ -98,8 +110,7 @@ class BaseModel(object): ...@@ -98,8 +110,7 @@ class BaseModel(object):
def subdivide(self, depth): def subdivide(self, depth):
model = self.__class__() model = self.__class__()
for item_group in self._item_groups: for item in self.next():
for item in item_group:
for s in item.subdivide(depth): for s in item.subdivide(depth):
model.append(s) model.append(s)
return model return model
...@@ -111,14 +122,12 @@ class BaseModel(object): ...@@ -111,14 +122,12 @@ class BaseModel(object):
self.maxx = None self.maxx = None
self.maxy = None self.maxy = None
self.maxz = None self.maxz = None
for item_group in self._item_groups: for item in self.next():
for item in item_group:
self._update_limits(item) self._update_limits(item)
def transform_by_matrix(self, matrix): def transform_by_matrix(self, matrix):
processed = [] processed = []
for item_group in self._item_groups: for item in self.next():
for item in item_group:
for point in item.get_points(): for point in item.get_points():
if not point.id in processed: if not point.id in processed:
processed.append(point.id) processed.append(point.id)
...@@ -175,8 +184,7 @@ class ContourModel(BaseModel): ...@@ -175,8 +184,7 @@ class ContourModel(BaseModel):
super(ContourModel, self).__init__() super(ContourModel, self).__init__()
self.name = "contourmodel%d" % self.id self.name = "contourmodel%d" % self.id
self._line_groups = [] self._line_groups = []
self._item_groups.append(self._lines) self._item_groups.append(self._line_groups)
_lines = property(lambda self: sum(self._line_groups, []))
def append(self, item): def append(self, item):
super(ContourModel, self).append(item) super(ContourModel, self).append(item)
...@@ -191,11 +199,12 @@ class ContourModel(BaseModel): ...@@ -191,11 +199,12 @@ class ContourModel(BaseModel):
line_group.append(item) line_group.append(item)
break break
else: else:
if len(self._line_groups) <= 2:
# add a new group with this single item # add a new group with this single item
self._line_groups.append([item]) self._line_groups.append([item])
def get_lines(self): def get_lines(self):
return self._lines return sum(self._line_groups, [])
def get_line_groups(self): def get_line_groups(self):
return self._line_groups return self._line_groups
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment