Commit 5ae43e3b authored by sumpfralle's avatar sumpfralle

implemented a chained caller managed by the core

adapted the model drawing to this chained caller


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@1158 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent ac783bdc
...@@ -132,50 +132,6 @@ class BaseModel(TransformableContainer): ...@@ -132,50 +132,6 @@ class BaseModel(TransformableContainer):
result += 1 result += 1
return result return result
def to_OpenGL(self, visible_filter=None, show_directions=False):
if not GL_enabled:
return
# the index should contain all relevant context details
if not visible_filter is None:
for item in self.next():
# ignore invisble things like the normal of a ContourModel
if hasattr(item, "to_OpenGL"):
do_paint, color = visible_filter(item)
if do_paint:
item.to_OpenGL(color, show_directions=show_directions)
else:
if isinstance(self, Model):
get_coords = lambda p: (p.x, p.y, p.z)
def calc_normal(main, normals):
suitable = Vector(0, 0, 0)
for normal, weight in normals:
dot = main.dot(normal)
if dot > 0:
suitable = suitable.add(normal.mul(weight * dot))
return suitable.normalized()
vertices = {}
for t in self.triangles():
for p in (t.p1, t.p2, t.p3):
coords = get_coords(p)
if not coords in vertices:
vertices[coords] = []
vertices[coords].append((t.normal.normalized(), t.get_area()))
GL.glBegin(GL.GL_TRIANGLES)
for t in self.triangles():
# The triangle's points are in clockwise order, but GL expects
# counter-clockwise sorting.
for p in (t.p1, t.p3, t.p2):
coords = get_coords(p)
normal = calc_normal(t.normal.normalized(), vertices[coords])
GL.glNormal3f(normal.x, normal.y, normal.z)
GL.glVertex3f(p.x, p.y, p.z)
GL.glEnd()
else:
for item in self.next():
# ignore invisble things like the normal of a ContourModel
if hasattr(item, "to_OpenGL"):
item.to_OpenGL(show_directions=show_directions)
def is_export_supported(self): def is_export_supported(self):
return not self._export_function is None return not self._export_function is None
......
...@@ -168,6 +168,7 @@ WIDGET_NAME_INDEX, WIDGET_OBJ_INDEX, WIDGET_WEIGHT_INDEX, WIDGET_ARGS_INDEX = \ ...@@ -168,6 +168,7 @@ WIDGET_NAME_INDEX, WIDGET_OBJ_INDEX, WIDGET_WEIGHT_INDEX, WIDGET_ARGS_INDEX = \
range(4) range(4)
HANDLER_FUNC_INDEX, HANDLER_ARG_INDEX = range(2) HANDLER_FUNC_INDEX, HANDLER_ARG_INDEX = range(2)
EVENT_HANDLER_INDEX, EVENT_BLOCKER_INDEX = range(2) EVENT_HANDLER_INDEX, EVENT_BLOCKER_INDEX = range(2)
CHAIN_FUNC_INDEX, CHAIN_WEIGHT_INDEX = range(2)
class EventCore(pycam.Gui.Settings.Settings): class EventCore(pycam.Gui.Settings.Settings):
...@@ -176,6 +177,7 @@ class EventCore(pycam.Gui.Settings.Settings): ...@@ -176,6 +177,7 @@ class EventCore(pycam.Gui.Settings.Settings):
super(EventCore, self).__init__() super(EventCore, self).__init__()
self.event_handlers = {} self.event_handlers = {}
self.ui_sections = {} self.ui_sections = {}
self.chains = {}
def register_event(self, event, func, *args): def register_event(self, event, func, *args):
if not event in self.event_handlers: if not event in self.event_handlers:
...@@ -291,8 +293,32 @@ class EventCore(pycam.Gui.Settings.Settings): ...@@ -291,8 +293,32 @@ class EventCore(pycam.Gui.Settings.Settings):
ui_section[UI_WIDGET_INDEX].pop(index) ui_section[UI_WIDGET_INDEX].pop(index)
self._rebuild_ui_section(section) self._rebuild_ui_section(section)
else: else:
log.debug("Trying to unregister unknown ui section: %s" % \ log.debug("Trying to unregister unknown ui section: %s" % section)
str(section))
def register_chain(self, name, func, weight):
if not name in self.chains:
self.chains[name] = []
self.chains[name].append((func, weight))
self.chains[name].sort(key=lambda item: item[CHAIN_WEIGHT_INDEX])
def unregister_chain(self, name, func):
if name in self.chains:
for index, data in self.chains[name]:
if data[CHAIN_FUNC_INDEX] == func:
self.chains[name].pop(index)
break
else:
log.debug("Trying to unregister unknown function from " + \
"%s: %s" % (name, func))
else:
log.debug("Trying to unregister from unknown chain: %s" % name)
def call_chain(self, name, *args, **kwargs):
if name in self.chains:
for data in self.chains[name]:
data[CHAIN_FUNC_INDEX](*args, **kwargs)
else:
log.debug("Called an unknown chain: %s" % name)
class ProjectGui(object): class ProjectGui(object):
......
...@@ -21,6 +21,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>. ...@@ -21,6 +21,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
""" """
import pycam.Plugins import pycam.Plugins
import pycam.Geometry.Point
GTK_COLOR_MAX = 65535.0 GTK_COLOR_MAX = 65535.0
...@@ -81,7 +82,7 @@ class OpenGLViewModel(pycam.Plugins.PluginBase): ...@@ -81,7 +82,7 @@ class OpenGLViewModel(pycam.Plugins.PluginBase):
do_caching = False do_caching = False
# next: compile an OpenGL display list # next: compile an OpenGL display list
if not do_caching or (not key in self._cache): if not do_caching or (not key in self._cache):
model.to_OpenGL(show_directions=self.core.get("show_directions")) self.core.call_chain("draw_models", [model])
if do_caching: if do_caching:
if not key in self._cache: if not key in self._cache:
GL.glEndList() GL.glEndList()
...@@ -91,3 +92,82 @@ class OpenGLViewModel(pycam.Plugins.PluginBase): ...@@ -91,3 +92,82 @@ class OpenGLViewModel(pycam.Plugins.PluginBase):
# render a previously compiled display list # render a previously compiled display list
GL.glCallList(self._cache[key]) GL.glCallList(self._cache[key])
class OpenGLViewModelTriangle(pycam.Plugins.PluginBase):
DEPENDS = ["OpenGLViewModel"]
def setup(self):
import OpenGL.GL
self._GL = OpenGL.GL
self.core.register_chain("draw_models", self.draw_triangle_model, 10)
return True
def teardown(self):
self.core.unregister_chain("draw_models", self.draw_triangle_model)
def draw_triangle_model(self, models):
if not models:
return
GL = self._GL
removal_list = []
for index in range(len(models)):
model = models[index]
if not hasattr(model, "triangles"):
continue
get_coords = lambda p: (p.x, p.y, p.z)
def calc_normal(main, normals):
suitable = pycam.Geometry.Point.Vector(0, 0, 0)
for normal, weight in normals:
dot = main.dot(normal)
if dot > 0:
suitable = suitable.add(normal.mul(weight * dot))
return suitable.normalized()
vertices = {}
for t in model.triangles():
for p in (t.p1, t.p2, t.p3):
coords = get_coords(p)
if not coords in vertices:
vertices[coords] = []
vertices[coords].append((t.normal.normalized(), t.get_area()))
GL.glBegin(GL.GL_TRIANGLES)
for t in model.triangles():
# The triangle's points are in clockwise order, but GL expects
# counter-clockwise sorting.
for p in (t.p1, t.p3, t.p2):
coords = get_coords(p)
normal = calc_normal(t.normal.normalized(), vertices[coords])
GL.glNormal3f(normal.x, normal.y, normal.z)
GL.glVertex3f(p.x, p.y, p.z)
GL.glEnd()
removal_list.append(index)
# remove all models that we processed
removal_list.reverse()
for index in removal_list:
models.pop(index)
class OpenGLViewModelGeneric(pycam.Plugins.PluginBase):
DEPENDS = ["OpenGLViewModel"]
def setup(self):
self.core.register_chain("draw_models", self.draw_generic_model, 100)
return True
def teardown(self):
self.core.unregister_chain("draw_models", self.draw_generic_model)
def draw_generic_model(self, models):
removal_list = []
for index in range(len(models)):
model = models[index]
for item in model.next():
# ignore invisble things like the normal of a ContourModel
if hasattr(item, "to_OpenGL"):
item.to_OpenGL(show_directions=self.core.get("show_directions"))
removal_list.append(index)
removal_list.reverse()
for index in removal_list:
removal_list.pop(index)
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