Commit ffe154b9 authored by Guillaume Seguin's avatar Guillaume Seguin

Add alternative autoplating method based on a packing algorithm

Packing algorithm is unlicensed for now, will import it when/if it gets a
GPL-compatible license
parent 8f2a5b3f
...@@ -150,39 +150,58 @@ class Plater(wx.Frame): ...@@ -150,39 +150,58 @@ class Plater(wx.Frame):
def autoplate(self, event = None): def autoplate(self, event = None):
print _("Autoplating") print _("Autoplating")
separation = 2 separation = 2
bedsize = self.build_dimensions[0:3] try:
cursor = [0, 0, 0] from printrun import packer
newrow = 0 p = packer.Packer()
max = [0, 0] for i in self.models:
for i in self.models: width = abs(self.models[i].dims[0] - self.models[i].dims[1])
self.models[i].offsets[2] = -1.0 * self.models[i].dims[4] height = abs(self.models[i].dims[2] - self.models[i].dims[3])
x = abs(self.models[i].dims[0] - self.models[i].dims[1]) p.add_rect(width, height, data = i)
y = abs(self.models[i].dims[2] - self.models[i].dims[3]) # FIXME: probably wrong, not taking offsets into account
centre = [x / 2, y / 2] centerx = self.build_dimensions[0] / 2
centreoffset = [self.models[i].dims[0] + centre[0], self.models[i].dims[2] + centre[1]] centery = self.build_dimensions[1] / 2
if (cursor[0] + x + separation) >= bedsize[0]: rects = p.pack(padding = separation,
cursor[0] = 0 center = packer.Vector2(centerx, centery))
cursor[1] += newrow + separation for rect in rects:
newrow = 0 i = rect.data
if (newrow == 0) or (newrow < y): self.models[i].offsets[0] = rect.position.x
newrow = y self.models[i].offsets[1] = rect.position.y
#To the person who works out why the offsets are applied differently here: except ImportError:
# Good job, it confused the hell out of me. bedsize = self.build_dimensions[0:3]
self.models[i].offsets[0] = cursor[0] + centre[0] - centreoffset[0] cursor = [0, 0, 0]
self.models[i].offsets[1] = cursor[1] + centre[1] - centreoffset[1] newrow = 0
if (max[0] == 0) or (max[0] < (cursor[0] + x)): max = [0, 0]
max[0] = cursor[0] + x for i in self.models:
if (max[1] == 0) or (max[1] < (cursor[1] + x)): self.models[i].offsets[2] = -1.0 * self.models[i].dims[4]
max[1] = cursor[1] + x x = abs(self.models[i].dims[0] - self.models[i].dims[1])
cursor[0] += x + separation y = abs(self.models[i].dims[2] - self.models[i].dims[3])
if (cursor[1] + y) >= bedsize[1]: centre = [x / 2, y / 2]
print _("Bed full, sorry sir :(") centreoffset = [self.models[i].dims[0] + centre[0],
self.Refresh() self.models[i].dims[2] + centre[1]]
return if (cursor[0] + x + separation) >= bedsize[0]:
centreoffset = [(bedsize[0] - max[0]) / 2, (bedsize[1] - max[1]) / 2] cursor[0] = 0
for i in self.models: cursor[1] += newrow + separation
self.models[i].offsets[0] += centreoffset[0] newrow = 0
self.models[i].offsets[1] += centreoffset[1] if (newrow == 0) or (newrow < y):
newrow = y
# To the person who works out why the offsets are applied
# differently here:
# Good job, it confused the hell out of me.
self.models[i].offsets[0] = cursor[0] + centre[0] - centreoffset[0]
self.models[i].offsets[1] = cursor[1] + centre[1] - centreoffset[1]
if (max[0] == 0) or (max[0] < (cursor[0] + x)):
max[0] = cursor[0] + x
if (max[1] == 0) or (max[1] < (cursor[1] + x)):
max[1] = cursor[1] + x
cursor[0] += x + separation
if (cursor[1] + y) >= bedsize[1]:
print _("Bed full, sorry sir :(")
self.Refresh()
return
centreoffset = [(bedsize[0] - max[0]) / 2, (bedsize[1] - max[1]) / 2]
for i in self.models:
self.models[i].offsets[0] += centreoffset[0]
self.models[i].offsets[1] += centreoffset[1]
self.Refresh() self.Refresh()
def clear(self, event): def clear(self, event):
......
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