Commit e7fab8b1 authored by sumpfralle's avatar sumpfralle

moved toolpath cropping to a separate plugin

added a basic model manager
added dependency checks for plugins


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@1104 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent 7e9cdffe
<?xml version="1.0"?>
<interface>
<!-- interface-requires gtk+ 2.12 -->
<!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="window1">
<child>
<object class="GtkVPaned" id="ModelBox">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkHBox" id="hbox1">
<property name="visible">True</property>
<child>
<object class="GtkScrolledWindow" id="scrolledwindow1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hscrollbar_policy">automatic</property>
<property name="vscrollbar_policy">automatic</property>
<property name="shadow_type">etched-out</property>
<child>
<object class="GtkIconView" id="ModelView">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="selection_mode">multiple</property>
<property name="model">ModelList</property>
<child>
<object class="GtkCellRendererText" id="cellrenderertext1"/>
<attributes>
<attribute name="text">0</attribute>
</attributes>
</child>
</object>
</child>
</object>
<packing>
<property name="padding">5</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkVButtonBox" id="vbuttonbox1">
<property name="visible">True</property>
<property name="orientation">vertical</property>
<property name="layout_style">center</property>
<child>
<object class="GtkButton" id="ModelDelete">
<property name="label">gtk-delete</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="ModelDeleteAll">
<property name="label">gtk-clear</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="ModelMoveUp">
<property name="label">gtk-go-up</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="ModelMoveDown">
<property name="label">gtk-go-down</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkLinkButton" id="ModelTransformationsHelp">
<property name="label">gtk-help</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="relief">none</property>
<property name="use_stock">True</property>
<property name="uri">http://sourceforge.net/apps/mediawiki/pycam/index.php?title=ModelTransformations</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">4</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="resize">False</property>
<property name="shrink">False</property>
</packing>
</child>
<child>
<object class="GtkNotebook" id="ModelHandlingNotebook">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="tab_pos">left</property>
</object>
<packing>
<property name="resize">True</property>
<property name="shrink">True</property>
</packing>
</child>
</object>
</child>
</object>
<object class="GtkListStore" id="ModelList">
<columns>
<!-- column-name name -->
<column type="gchararray"/>
</columns>
<data>
<row>
<col id="0" translatable="yes">Model 1</col>
</row>
</data>
</object>
</interface>
This diff is collapsed.
<?xml version="1.0"?>
<interface>
<!-- interface-requires gtk+ 2.12 -->
<!-- interface-naming-policy project-wide -->
<object class="GtkDialog" id="ToolpathCropDialog">
<property name="border_width">5</property>
<property name="title" translatable="yes">Crop toolpath</property>
<property name="type_hint">normal</property>
<child internal-child="vbox">
<object class="GtkVBox" id="dialog-vbox1">
<property name="visible">True</property>
<property name="orientation">vertical</property>
<property name="spacing">2</property>
<child>
<object class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<property name="orientation">vertical</property>
<property name="spacing">3</property>
<child>
<object class="GtkTable" id="table1">
<property name="visible">True</property>
<property name="n_rows">2</property>
<property name="n_columns">2</property>
<property name="column_spacing">3</property>
<property name="row_spacing">3</property>
<child>
<object class="GtkLabel" id="ToolpathCropMarginLabel">
<property name="visible">True</property>
<property name="xalign">0</property>
<property name="label" translatable="yes">Margin:</property>
</object>
<packing>
<property name="top_attach">1</property>
<property name="bottom_attach">2</property>
<property name="x_options">GTK_FILL</property>
<property name="y_options">GTK_FILL</property>
</packing>
</child>
<child>
<object class="GtkSpinButton" id="ToolpathCropMargin">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">&#x25CF;</property>
<property name="width_chars">5</property>
<property name="adjustment">ToolpathCropMarginValue</property>
<property name="digits">2</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="top_attach">1</property>
<property name="bottom_attach">2</property>
<property name="y_options">GTK_FILL</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="ToolpathCropZSliceLabel">
<property name="visible">True</property>
<property name="xalign">0</property>
<property name="label" translatable="yes">Slicing z-level:</property>
</object>
<packing>
<property name="y_options">GTK_FILL</property>
</packing>
</child>
<child>
<object class="GtkSpinButton" id="ToolpathCropZSlice">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">&#x25CF;</property>
<property name="width_chars">5</property>
<property name="adjustment">ToolpathCropZSliceValue</property>
<property name="digits">2</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="right_attach">2</property>
<property name="y_options">GTK_FILL</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="ToolpathCropKeepOriginal">
<property name="label" translatable="yes">Keep original toolpath</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkViewport" id="ToolpathCropInfoBox">
<property name="no_show_all">True</property>
<property name="resize_mode">queue</property>
<child>
<object class="GtkLabel" id="ToolpathCropInfo">
<property name="visible">True</property>
<property name="xalign">0</property>
<property name="label" translatable="yes">label</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="position">2</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="position">1</property>
</packing>
</child>
<child internal-child="action_area">
<object class="GtkHButtonBox" id="dialog-action_area1">
<property name="visible">True</property>
<property name="layout_style">end</property>
<child>
<object class="GtkButton" id="ToolpathCropCancel">
<property name="label">gtk-cancel</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="ToolpathCropOK">
<property name="label">gtk-ok</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="use_stock">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="pack_type">end</property>
<property name="position">0</property>
</packing>
</child>
</object>
</child>
<action-widgets>
<action-widget response="0">ToolpathCropCancel</action-widget>
<action-widget response="0">ToolpathCropOK</action-widget>
</action-widgets>
</object>
<object class="GtkAdjustment" id="ToolpathCropMarginValue">
<property name="lower">-1000</property>
<property name="upper">1000</property>
<property name="step_increment">1</property>
</object>
<object class="GtkAdjustment" id="ToolpathCropZSliceValue">
<property name="lower">-1000</property>
<property name="upper">1000</property>
<property name="step_increment">1</property>
</object>
<object class="GtkWindow" id="window1">
<child>
<object class="GtkButton" id="ToolpathCropButton">
<property name="label" translatable="yes">Crop</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
</child>
</object>
</interface>
......@@ -29,10 +29,12 @@ from pycam.Geometry.Point import Vector
class Plane(TransformableContainer):
id = 0
def __init__(self, point, normal):
def __init__(self, point, normal=None):
super(Plane, self).__init__()
self.id = Plane.id
Plane.id += 1
if normal is None:
normal = Vector(0, 0, 1)
self.p = point
self.n = normal
if not isinstance(self.n, Vector):
......
......@@ -216,10 +216,15 @@ class Camera(object):
v = self.view
# position the light according to the current bounding box
light_pos = range(3)
model = self.settings.get("model")
light_pos[0] = 2 * model.maxx - model.minx
light_pos[1] = 2 * model.maxy - model.miny
light_pos[2] = 2 * model.maxz - model.minz
values = {}
for model in self.settings.get("models"):
for key in ("minx", "miny", "minz", "maxx", "maxy", "maxz"):
if not key in values:
values[key] = []
values[key].append(getattr(model, key))
light_pos[0] = 2 * max(values["maxx"]) - min(values["minx"])
light_pos[1] = 2 * max(values["maxy"]) - min(values["miny"])
light_pos[2] = 2 * max(values["maxz"]) - min(values["minz"])
GL.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, (light_pos[0], light_pos[1],
light_pos[2], 1.0))
# position the camera
......@@ -907,7 +912,7 @@ def draw_complete_model_view(settings):
and not (settings.get("show_simulation") \
and settings.get("simulation_toolpath_moves")):
GL.glColor4f(*settings.get("color_model"))
model = settings.get("model")
for model in settings.get("models"):
"""
min_area = abs(model.maxx - model.minx) * abs(model.maxy - model.miny) / 100
# example for coloring specific triangles
......
This diff is collapsed.
......@@ -24,11 +24,8 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
import math
import pycam.Plugins
import pycam.Utils.log
log = pycam.Utils.log.get_logger()
EXTRUSION_TYPES = (("radius_up", "Radius (bulge)", "ExtrusionRadiusUpIcon"),
("radius_down", "Radius (valley)", "ExtrusionRadiusDownIcon"),
("skewed", "Chamfer", "ExtrusionChamferIcon"),
......@@ -40,6 +37,7 @@ EXTRUSION_TYPES = (("radius_up", "Radius (bulge)", "ExtrusionRadiusUpIcon"),
class ModelExtrusion(pycam.Plugins.PluginBase):
UI_FILE = "model_extrusion.ui"
DEPENDS = ["Models"]
def setup(self):
if self.gui:
......@@ -102,7 +100,7 @@ class ModelExtrusion(pycam.Plugins.PluginBase):
elif type_string == "sigmoid":
func = lambda x: height * ((math.sin(((min(x, width) / width) - 0.5) * math.pi) + 1) / 2)
else:
log.error("Unknown extrusion type selected: %s" % type_string)
self.log.error("Unknown extrusion type selected: %s" % type_string)
return
new_model = model.extrude(stepping=grid_size, func=func,
callback=self.core.get("update_progress"))
......
......@@ -27,6 +27,7 @@ import pycam.Plugins
class ModelPlaneMirror(pycam.Plugins.PluginBase):
UI_FILE = "model_plane_mirror.ui"
DEPENDS = ["Models"]
def setup(self):
if self.gui:
......
......@@ -27,6 +27,7 @@ import pycam.Plugins
class ModelPolygons(pycam.Plugins.PluginBase):
UI_FILE = "model_polygons.ui"
DEPENDS = ["Models"]
def setup(self):
if self.gui:
......
......@@ -27,6 +27,7 @@ import pycam.Plugins
class ModelPosition(pycam.Plugins.PluginBase):
UI_FILE = "model_position.ui"
DEPENDS = ["Models"]
def setup(self):
if self.gui:
......
......@@ -24,14 +24,12 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
import pycam.Plugins
from pycam.Geometry.Plane import Plane
from pycam.Geometry.Point import Point, Vector
import pycam.Utils.log
log = pycam.Utils.log.get_logger()
class ModelProjection(pycam.Plugins.PluginBase):
UI_FILE = "model_projection.ui"
DEPENDS = ["Models"]
def setup(self):
if self.gui:
......@@ -73,12 +71,12 @@ class ModelProjection(pycam.Plugins.PluginBase):
self.gui.get_object("ProjectionZLevel").get_value())):
if self.gui.get_object(objname).get_active():
plane = Plane(Point(0, 0, z_level), Vector(0, 0, 1))
log.info("Projecting 3D model at level z=%g" % plane.p.z)
self.log.info("Projecting 3D model at level z=%g" % plane.p.z)
projection = model.get_waterline_contour(plane)
if projection:
self.core.get("load_model")(projection)
else:
log.warn("The 2D projection at z=%g is empty. Aborted." % \
self.log.warn("The 2D projection at z=%g is empty. Aborted." % \
plane.p.z)
break
......@@ -28,6 +28,7 @@ import pycam.Geometry.Matrix
class ModelRotation(pycam.Plugins.PluginBase):
UI_FILE = "model_rotation.ui"
DEPENDS = ["Models"]
def setup(self):
if self.gui:
......
......@@ -27,6 +27,7 @@ import pycam.Plugins
class ModelScaling(pycam.Plugins.PluginBase):
UI_FILE = "model_scaling.ui"
DEPENDS = ["Models"]
def setup(self):
if self.gui:
......
......@@ -27,6 +27,7 @@ import pycam.Plugins
class ModelSupport(pycam.Plugins.PluginBase):
UI_FILE = "model_support.ui"
DEPENDS = ["Models"]
def setup(self):
if self.gui:
......
......@@ -27,6 +27,7 @@ import pycam.Plugins
class ModelSupportDistributed(pycam.Plugins.PluginBase):
UI_FILE = "model_support_distributed.ui"
DEPENDS = ["Models"]
def setup(self):
if self.gui:
......
......@@ -20,15 +20,16 @@ You should have received a copy of the GNU General Public License
along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
"""
# gtk is imported later (on demand)
#import gtk
import pycam.Plugins
# gtk is imported later
#import gtk
class ModelSupportGrid(pycam.Plugins.PluginBase):
UI_FILE = "model_support_grid.ui"
DEPENDS = ["Models"]
def setup(self):
if self.gui:
......
......@@ -27,6 +27,7 @@ import pycam.Plugins
class ModelSwapAxes(pycam.Plugins.PluginBase):
UI_FILE = "model_swap_axes.ui"
DEPENDS = ["Models"]
def setup(self):
if self.gui:
......
# -*- coding: utf-8 -*-
"""
$Id: __init__.py 1061 2011-04-12 13:14:12Z sumpfralle $
Copyright 2011 Lars Kruse <devel@sumpfralle.de>
This file is part of PyCAM.
PyCAM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PyCAM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
"""
# imported later (on demand)
#import gtk
import pycam.Plugins
class Models(pycam.Plugins.ListPluginBase):
UI_FILE = "models.ui"
def setup(self):
if self.gui:
import gtk
model_frame = self.gui.get_object("ModelBox")
model_frame.unparent()
self.core.register_ui("main", "Models", model_frame, -50)
model_handling_obj = self.gui.get_object("ModelHandlingNotebook")
def clear_model_handling_obj():
for index in range(model_handling_obj.get_n_pages()):
model_handling_obj.remove_page(0)
def add_model_handling_item(item, name):
model_handling_obj.append_page(item, gtk.Label(name))
self.core.register_ui_section("model_handling",
add_model_handling_item, clear_model_handling_obj)
self._modelview = self.gui.get_object("ModelView")
for action, obj_name in ((self.ACTION_UP, "ModelMoveUp"),
(self.ACTION_DOWN, "ModelMoveDown"),
(self.ACTION_DELETE, "ModelDelete"),
(self.ACTION_CLEAR, "ModelDeleteAll")):
self.register_list_action_button(action, self._modelview,
self.gui.get_object(obj_name))
treemodel = self.gui.get_object("ModelList")
def update_model():
treemodel.clear()
for index in range(len(self)):
treemodel.append((self[index], ))
# clean the model now
self.register_model_update(update_model)
#update_model()
self.core.add_item("models", lambda: self)
return True
def get_selected(self):
return self._get_selected(self._modelview, force_list=True)
def teardown(self):
if self.gui:
self.core.unregister_ui("main", self.gui.get_object("ModelBox"))
self.core.unregister_ui_section("main", "model_handling")
self.core.set("models", None)
return True
# -*- coding: utf-8 -*-
"""
$Id$
Copyright 2011 Lars Kruse <devel@sumpfralle.de>
This file is part of PyCAM.
PyCAM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PyCAM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
"""
import pycam.Plugins
from pycam.Geometry.Point import Point
from pycam.Geometry.Plane import Plane
"""
TODO:
- get the currently selected toolpath (from the table)
- update the current crop-polygons instantly (3D)
- update the ToolpathCropInfoLabel content if no polygons are found
"""
class ToolpathCrop(pycam.Plugins.PluginBase):
UI_FILE = "toolpath_crop.ui"
def setup(self):
if self.gui:
action_button = self.gui.get_object("ToolpathCropButton")
action_button.unparent()
self.core.register_ui("toolpath_crop", "Crop", action_button, -3)
self.core.register_event("model-change-after",
self._update_model_type_controls)
return True
def teardown(self):
if self.gui:
self.core.unregister_ui("toolpath__crop",
self.gui.get_object("ToolpathCropButton"))
def _update_model_type_controls(self):
model = self.core.get("model")
if not model:
return
# show or hide z-slice controls
can_slice = hasattr(model, "get_waterline_contour")
for name in "ToolpathCropZSliceLabel", "ToolpathCropZSlice":
if can_slice:
self.gui.get_object(name).show()
else:
self.gui.get_object(name).hide()
# set lower and upper limit for z-slice
z_slice_value = self.gui.get_object("ToolpathCropZSliceValue")
z_slice_value.set_lower(model.minz)
z_slice_value.set_upper(model.maxz)
def crop_toolpath(self, widget=None):
# TODO: how to get the currently selected toolpath???
toolpath = self.core.get("toolpath")[0]
model = self.core.get("model")
if not model:
return
if hasattr(model, "get_polygons"):
contour = model
elif hasattr(model, "get_waterline_contour"):
z_slice = self.gui.get_object("ToolpathCropZSlice").get_value()
plane = Plane(Point(0, 0, z_slice))
#self.update_progress_bar("Calculating the 2D projection")
contour = model.get_waterline_contour(plane)
else:
self.log.warn(("The current model (%s) does not support " \
+ "projections") % str(type(model)))
return
#self.update_progress_bar("Applying the tool diameter offset")
margin = self.gui.get_object("ToolpathCropMargin").get_value()
if margin:
contour = contour.get_offset_model(margin)
#self.update_progress_bar("Cropping the toolpath")
#toolpath.crop(contour.get_polygons(), callback=self.update_progress_bar)
if self.gui.get_object("ToolpathCropKeepOriginal").get_active():
new_tp = toolpath.get_cropped_copy(contour.get_polygons(),
callback=self.update_progress_bar)
new_tp.visible = True
old_index = self.core.get("toolpath").index(toolpath)
self.core.get("toolpath").insert(old_index + 1, new_tp)
else:
toolpath.crop(contour.get_polygons(), callback=self.update_progress_bar)
# -*- coding: utf-8 -*-
"""
$Id: __init__.py 1061 2011-04-12 13:14:12Z sumpfralle $
Copyright 2011 Lars Kruse <devel@sumpfralle.de>
This file is part of PyCAM.
PyCAM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PyCAM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
"""
import pycam.Plugins
class Toolpaths(pycam.Plugins.ListPluginBase):
def setup(self):
self.core.add_item("toolpaths", self)
return True
def teardown(self):
self.core.set("toolpaths", None)
return True
......@@ -35,12 +35,14 @@ _log = pycam.Utils.log.get_logger()
class PluginBase(object):
UI_FILE = None
DEPENDS = []
def __init__(self, core, name):
self.enabled = True
self.name = name
self.core = core
self.gui = None
self.log = _log
if self.UI_FILE:
gtk_build_file = pycam.Utils.locations.get_ui_file_location(
self.UI_FILE)
......@@ -50,12 +52,10 @@ class PluginBase(object):
self.gui.add_from_file(gtk_build_file)
except RuntimeError:
self.gui = None
if not self.setup():
raise RuntimeError("Failed to load plugin '%s'" % str(name))
def setup(self):
raise NotImplementedError("Module %s (%s) does not implement " + \
"'setup'" % (self.name, __file__))
raise NotImplementedError(("Module %s (%s) does not implement " + \
"'setup'") % (self.name, __file__))
def teardown(self):
raise NotImplementedError("Module %s (%s) does not implement " + \
......@@ -75,8 +75,10 @@ class PluginManager(object):
files = os.listdir(directory)
except OSError:
return
plugins = []
for filename in files:
if filename.endswith(".py") and (filename != "__init__.py") and \
if filename.endswith(".py") and \
(filename.lower() != "__init__.py") and \
os.path.isfile(os.path.join(directory, filename)):
mod_name = filename[0:-(len(".py"))]
try:
......@@ -86,19 +88,176 @@ class PluginManager(object):
mod = imp.load_module(full_mod_name, mod_file,
mod_filename, mod_desc)
except ImportError:
_log.debug("Skipping broken plugin %s" % os.path.join(
_log.info("Skipping broken plugin %s" % os.path.join(
directory, filename))
continue
for attr in dir(mod):
item = getattr(mod, attr)
if inspect.isclass(item) and hasattr(item, "setup"):
self._load_plugin(item, mod_filename, attr)
def _load_plugin(self, obj, filename, local_name):
name = "%s.%s" % (os.path.basename(filename)[0:-len(".py")], local_name)
if name in self.modules:
_log.debug("Cleaning up module %s" % name)
self.modules[name].teardown()
_log.debug("Initializing module %s (%s)" % (name, filename))
self.modules[name] = obj(self.core, name)
plugin_name = "%s.%s" % (os.path.basename(
mod_filename)[0:-len(".py")], attr)
plugins.append((item, mod_filename, attr))
try_again = True
while try_again:
try_again = False
postponed_plugins = []
for plugin, filename, name in plugins:
for dep in plugin.DEPENDS:
if not dep in self.modules and \
not "%s.%s" % (dep, dep) in self.modules:
# dependency not loaded, yet
postponed_plugins.append((plugin, filename, name))
break
else:
self._load_plugin(plugin, filename, name)
try_again = True
plugins = postponed_plugins
for plugin, filename, name in plugins:
# module failed to load due to missing dependencies
_log.info("Skipping plugin '%s' due to missing dependencies: %s" % \
(name, ", ".join(plugin.DEPENDS)))
def _load_plugin(self, obj, filename, plugin_name):
if plugin_name in self.modules:
_log.debug("Cleaning up module %s" % plugin_name)
self.modules[plugin_name].teardown()
_log.debug("Initializing module %s (%s)" % (plugin_name, filename))
new_plugin = obj(self.core, plugin_name)
if not new_plugin.setup():
raise RuntimeError("Failed to load plugin '%s'" % str(name))
else:
self.modules[plugin_name] = new_plugin
class ListPluginBase(PluginBase, list):
ACTION_UP, ACTION_DOWN, ACTION_DELETE, ACTION_CLEAR = range(4)
def __init__(self, *args, **kwargs):
super(ListPluginBase, self).__init__(*args, **kwargs)
self._update_model_funcs = []
def get_function(func_name):
return lambda *args, **kwargs: self._change_wrapper(func_name, *args, **kwargs)
for name in "append", "insert", "pop", "reverse", "sort":
setattr(self, name, get_function(name))
def _change_wrapper(self, func_name, *args, **kwargs):
value = getattr(super(ListPluginBase, self), func_name)(*args, **kwargs)
self._update_model()
return value
def _get_selected(self, modelview, index=False, force_list=False):
if hasattr(modelview, "get_selection"):
# a treeview selection
selection = modelview.get_selection()
selection_mode = selection.get_mode()
paths = selection.get_selected_rows()[1]
else:
# an iconview
selection_mode = modelview.get_selection_mode()
paths = modelview.get_selected_items()
if index:
get_result = lambda path: path[0]
else:
get_result = lambda path: self[path[0]]
if (selection_mode == gtk.SELECTION_MULTIPLE) or force_list:
result = []
for path in paths:
result.append(get_result(path))
else:
if not paths:
return None
else:
result = get_result(paths[0])
return result
def _update_model(self):
for update_func in self._update_model_funcs:
update_func()
def register_model_update(self, func):
self._update_model_funcs.append(func)
def unregister_model_update(self, func):
if func in self._update_model_funcs:
self._update_model_funcs.remove(func)
def _list_action(self, *args):
# the second-to-last paramater should be the model view
modelview = args[-2]
# the last parameter should be the action (ACTION_UP|DOWN|DELETE|CLEAR)
action = args[-1]
if not action in (self.ACTION_UP, self.ACTION_DOWN,
self.ACTION_DELETE, self.ACTION_CLEAR):
self.log.info("Invalid action for ListPluginBase.list_action: " + \
str(action))
return
selected_items = self._get_selected(modelview, index=True,
force_list=True)
selected_items.sort()
if action in (self.ACTION_DOWN, self.ACTION_DELETE):
selected_items.sort(reverse=True)
new_selection = []
if action == self.ACTION_CLEAR:
while len(self) > 0:
self.pop(0)
else:
for index in selected_items:
if action == self.ACTION_UP:
if index > 0:
item = self.pop(index)
self.insert(index - 1, item)
new_selection.append(index - 1)
elif action == self.ACTION_DOWN:
if index < len(self) - 1:
item = self.pop(index)
self.insert(index + 1, item)
new_selection.append(index + 1)
elif action == self.ACTION_DELETE:
self.pop(index)
new_selection.append(min(index, len(self) - 1))
else:
pass
self._update_model()
if hasattr(modelview, "get_selection"):
selection = modelview.get_selection()
else:
selection = modelview
selection.unselect_all()
for index in new_selection:
selection.select_path((index,))
def _update_list_action_button_state(self, *args):
modelview = args[-3]
action = args[-2]
button = args[-1]
paths = self._get_selected(modelview, index=True, force_list=True)
if action == self.ACTION_CLEAR:
button.set_sensitive(len(self) > 0)
elif not paths:
button.set_sensitive(False)
else:
if action == self.ACTION_UP:
button.set_sensitive(not 0 in paths)
elif action == self.ACTION_DOWN:
button.set_sensitive(not (len(self) - 1) in paths)
else:
button.set_sensitive(True)
def register_list_action_button(self, action, modelview, button):
if hasattr(modelview, "get_selection"):
# a treeview
selection = modelview.get_selection()
selection.connect("changed", self._update_list_action_button_state,
modelview, action, button)
else:
modelview.connect("selection-changed",
self._update_list_action_button_state, modelview, action,
button)
model = modelview.get_model()
for signal in ("row-changed", "row-deleted",
"row-has-child-toggled", "row-inserted", "rows-reordered"):
model.connect(signal, self._update_list_action_button_state,
modelview, action, button)
button.connect("clicked", self._list_action, modelview, action)
......@@ -233,6 +233,21 @@ class Toolpath(object):
current_position = new_pos
return result
def get_cropped_copy(self, polygons, callback=None):
# create a deep copy of the current toolpath
new_paths = []
for path in self.toolpath:
if path:
new_path = Path()
for point in path.points:
new_path.append(point)
new_paths.append(new_path)
tp = Toolpath(new_paths, "%s (cropped)" % self.name,
self.toolpath_settings)
tp.visible = self.visible
tp.crop(polygons, callback=callback)
return tp
def crop(self, polygons, callback=None):
# collect all existing toolpath lines
open_lines = []
......
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