2018-03-31 00:37:11 +00:00
|
|
|
import math
|
|
|
|
from shapely import geometry as shgeo
|
2018-05-02 01:21:07 +00:00
|
|
|
from ..i18n import _
|
|
|
|
from ..utils import cache
|
2018-03-31 00:37:11 +00:00
|
|
|
from ..stitches import auto_fill
|
2018-05-02 01:21:07 +00:00
|
|
|
from .element import param, Patch
|
|
|
|
from .fill import Fill
|
2018-03-31 00:37:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AutoFill(Fill):
|
|
|
|
element_name = _("Auto-Fill")
|
|
|
|
|
|
|
|
@property
|
|
|
|
@param('auto_fill', _('Automatically routed fill stitching'), type='toggle', default=True)
|
|
|
|
def auto_fill(self):
|
|
|
|
return self.get_boolean_param('auto_fill', True)
|
|
|
|
|
|
|
|
@property
|
|
|
|
@cache
|
|
|
|
def outline(self):
|
|
|
|
return self.shape.boundary[0]
|
|
|
|
|
|
|
|
@property
|
|
|
|
@cache
|
|
|
|
def outline_length(self):
|
|
|
|
return self.outline.length
|
|
|
|
|
|
|
|
@property
|
|
|
|
def flip(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
@param('running_stitch_length_mm', _('Running stitch length (traversal between sections)'), unit='mm', type='float', default=1.5)
|
|
|
|
def running_stitch_length(self):
|
|
|
|
return max(self.get_float_param("running_stitch_length_mm", 1.5), 0.01)
|
|
|
|
|
|
|
|
@property
|
|
|
|
@param('fill_underlay', _('Underlay'), type='toggle', group=_('AutoFill Underlay'), default=False)
|
|
|
|
def fill_underlay(self):
|
|
|
|
return self.get_boolean_param("fill_underlay", default=False)
|
|
|
|
|
|
|
|
@property
|
|
|
|
@param('fill_underlay_angle', _('Fill angle (default: fill angle + 90 deg)'), unit='deg', group=_('AutoFill Underlay'), type='float')
|
|
|
|
@cache
|
|
|
|
def fill_underlay_angle(self):
|
|
|
|
underlay_angle = self.get_float_param("fill_underlay_angle")
|
|
|
|
|
|
|
|
if underlay_angle:
|
|
|
|
return math.radians(underlay_angle)
|
|
|
|
else:
|
|
|
|
return self.angle + math.pi / 2.0
|
|
|
|
|
|
|
|
@property
|
|
|
|
@param('fill_underlay_row_spacing_mm', _('Row spacing (default: 3x fill row spacing)'), unit='mm', group=_('AutoFill Underlay'), type='float')
|
|
|
|
@cache
|
|
|
|
def fill_underlay_row_spacing(self):
|
|
|
|
return self.get_float_param("fill_underlay_row_spacing_mm") or self.row_spacing * 3
|
|
|
|
|
|
|
|
@property
|
|
|
|
@param('fill_underlay_max_stitch_length_mm', _('Max stitch length'), unit='mm', group=_('AutoFill Underlay'), type='float')
|
|
|
|
@cache
|
|
|
|
def fill_underlay_max_stitch_length(self):
|
|
|
|
return self.get_float_param("fill_underlay_max_stitch_length_mm") or self.max_stitch_length
|
|
|
|
|
|
|
|
@property
|
2018-06-02 00:34:27 +00:00
|
|
|
@param('fill_underlay_inset_mm',
|
|
|
|
_('Inset'),
|
|
|
|
tooltip='Shrink the shape before doing underlay, to prevent underlay from showing around the outside of the fill.',
|
|
|
|
unit='mm',
|
|
|
|
group=_('AutoFill Underlay'),
|
|
|
|
type='float',
|
|
|
|
default=0)
|
2018-03-31 00:37:11 +00:00
|
|
|
def fill_underlay_inset(self):
|
|
|
|
return self.get_float_param('fill_underlay_inset_mm', 0)
|
|
|
|
|
|
|
|
@property
|
2018-06-02 00:34:27 +00:00
|
|
|
@param('expand_mm',
|
|
|
|
_('Expand'),
|
|
|
|
tooltip='Expand the shape before fill stitching, to compensate for gaps between shapes.',
|
|
|
|
unit='mm',
|
|
|
|
type='float',
|
|
|
|
default=0)
|
|
|
|
def expand(self):
|
|
|
|
return self.get_float_param('expand_mm', 0)
|
|
|
|
|
|
|
|
def shrink_or_grow_shape(self, amount):
|
|
|
|
if amount:
|
|
|
|
shape = self.shape.buffer(amount)
|
2018-03-31 00:37:11 +00:00
|
|
|
if not isinstance(shape, shgeo.MultiPolygon):
|
|
|
|
shape = shgeo.MultiPolygon([shape])
|
|
|
|
return shape
|
|
|
|
else:
|
|
|
|
return self.shape
|
|
|
|
|
2018-06-02 00:34:27 +00:00
|
|
|
@property
|
|
|
|
def underlay_shape(self):
|
|
|
|
return self.shrink_or_grow_shape(-self.fill_underlay_inset)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def fill_shape(self):
|
|
|
|
return self.shrink_or_grow_shape(self.expand)
|
|
|
|
|
2018-06-23 02:29:23 +00:00
|
|
|
def get_starting_point(self, last_patch):
|
|
|
|
# If there is a "fill_start" Command, then use that; otherwise pick
|
|
|
|
# the point closest to the end of the last patch.
|
|
|
|
|
|
|
|
if self.get_command('fill_start'):
|
|
|
|
return self.get_command('fill_start').target_point
|
|
|
|
elif last_patch:
|
|
|
|
return last_patch.stitches[-1]
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2018-03-31 00:37:11 +00:00
|
|
|
def to_patches(self, last_patch):
|
|
|
|
stitches = []
|
|
|
|
|
2018-06-23 02:29:23 +00:00
|
|
|
starting_point = self.get_starting_point(last_patch)
|
2018-03-31 00:37:11 +00:00
|
|
|
|
|
|
|
if self.fill_underlay:
|
|
|
|
stitches.extend(auto_fill(self.underlay_shape,
|
|
|
|
self.fill_underlay_angle,
|
|
|
|
self.fill_underlay_row_spacing,
|
|
|
|
self.fill_underlay_row_spacing,
|
|
|
|
self.fill_underlay_max_stitch_length,
|
|
|
|
self.running_stitch_length,
|
|
|
|
self.staggers,
|
|
|
|
starting_point))
|
|
|
|
starting_point = stitches[-1]
|
|
|
|
|
2018-06-02 00:34:27 +00:00
|
|
|
stitches.extend(auto_fill(self.fill_shape,
|
2018-03-31 00:37:11 +00:00
|
|
|
self.angle,
|
|
|
|
self.row_spacing,
|
|
|
|
self.end_row_spacing,
|
|
|
|
self.max_stitch_length,
|
|
|
|
self.running_stitch_length,
|
|
|
|
self.staggers,
|
|
|
|
starting_point))
|
|
|
|
|
|
|
|
return [Patch(stitches=stitches, color=self.color)]
|