Commit 53fb79a6 authored by Guillaume Seguin's avatar Guillaume Seguin

More factorization and cleanup of plater

parent 4f9c2b2b
...@@ -232,6 +232,10 @@ class showstl(wx.Window): ...@@ -232,6 +232,10 @@ class showstl(wx.Window):
del dc del dc
class StlPlater(Plater): class StlPlater(Plater):
load_wildcard = _("STL files (*.stl;*.STL)|*.stl;*.STL|OpenSCAD files (*.scad)|*.scad")
save_wildcard = _("STL files (*.stl;*.STL)|*.stl;*.STL")
def __init__(self, filenames = [], size = (800, 580), callback = None, parent = None, build_dimensions = None): def __init__(self, filenames = [], size = (800, 580), callback = None, parent = None, build_dimensions = None):
super(StlPlater, self).__init__(filenames, size, callback, parent, build_dimensions) super(StlPlater, self).__init__(filenames, size, callback, parent, build_dimensions)
if glview: if glview:
...@@ -246,51 +250,18 @@ class StlPlater(Plater): ...@@ -246,51 +250,18 @@ class StlPlater(Plater):
except: except:
pass pass
name = "tempstl/" + str(int(time.time()) % 10000) + ".stl" name = "tempstl/" + str(int(time.time()) % 10000) + ".stl"
self.writefiles(name) self.export_to(name)
if cb is not None: if cb is not None:
cb(name) cb(name)
self.Destroy() self.Destroy()
def export(self, event): def load_file(self, filename):
dlg = wx.FileDialog(self, _("Pick file to save to"), self.basedir, style = wx.FD_SAVE)
dlg.SetWildcard(_("STL files (*.stl;*.STL)|*.stl;*.STL"))
if(dlg.ShowModal() == wx.ID_OK):
name = dlg.GetPath()
self.writefiles(name)
dlg.Destroy()
def writefiles(self, name):
sf = open(name.replace(".", "_") + ".scad", "w")
facets = []
for i in self.models.values():
r = i.rot
o = i.offsets
sf.write('translate([%s, %s, %s]) rotate([0, 0, %s]) import_stl("%s");\n' % (str(o[0]), str(o[1]), str(o[2]), r, os.path.split(i.filename)[1]))
if r != 0:
i = i.rotate([0, 0, r])
if o != [0, 0, 0]:
i = i.translate([o[0], o[1], o[2]])
facets += i.facets
sf.close()
stltool.emitstl(name, facets, "plater_export")
print _("wrote %s") % name
def load(self, event):
dlg = wx.FileDialog(self, _("Pick file to load"), self.basedir, style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
dlg.SetWildcard(_("STL files (*.stl;*.STL)|*.stl;*.STL|OpenSCAD files (*.scad)|*.scad"))
if dlg.ShowModal() == wx.ID_OK:
name = dlg.GetPath()
self.load_file(event, name)
dlg.Destroy()
def load_file(self, event, filename):
if filename.lower().endswith(".stl"): if filename.lower().endswith(".stl"):
self.load_stl(event, filename) self.load_stl(filename)
elif filename.lower().endswith(".scad"): elif filename.lower().endswith(".scad"):
self.load_scad(event, filename) self.load_scad(filename)
def load_scad(self, event, name): def load_scad(self, name):
lf = open(name) lf = open(name)
s = [i.replace("\n", "").replace("\r", "").replace(";", "") for i in lf if "stl" in i] s = [i.replace("\n", "").replace("\r", "").replace(";", "") for i in lf if "stl" in i]
lf.close() lf.close()
...@@ -317,7 +288,7 @@ class StlPlater(Plater): ...@@ -317,7 +288,7 @@ class StlPlater(Plater):
stl_full_path = os.path.join(stl_path[0], str(stl_file)) stl_full_path = os.path.join(stl_path[0], str(stl_file))
self.load_stl_into_model(stl_full_path, stl_file, translate_list, rotate_list[2]) self.load_stl_into_model(stl_full_path, stl_file, translate_list, rotate_list[2])
def load_stl(self, event, name): def load_stl(self, name):
if not(os.path.exists(name)): if not(os.path.exists(name)):
return return
path = os.path.split(name)[0] path = os.path.split(name)[0]
...@@ -327,22 +298,21 @@ class StlPlater(Plater): ...@@ -327,22 +298,21 @@ class StlPlater(Plater):
#Filter out the path, just show the STL filename. #Filter out the path, just show the STL filename.
self.load_stl_into_model(name, name) self.load_stl_into_model(name, name)
self.Refresh() self.Refresh()
#print time.time()-t
def load_stl_into_model(self, path, name, offset = [0, 0, 0], rotation = 0, scale = [1.0, 1.0, 1.0]): def load_stl_into_model(self, path, name, offset = [0, 0, 0], rotation = 0, scale = [1.0, 1.0, 1.0]):
newname = os.path.split(name.lower())[1] model = stltool.stl(path)
c = 1 model.offsets = offset
while newname in self.models: model.rot = rotation
newname = os.path.split(name.lower())[1] model.scale = scale
newname = newname + "(%d)" % c model.filename = name
c += 1
self.models[newname] = stltool.stl(path)
self.models[newname].offsets = offset
self.models[newname].rot = rotation
self.models[newname].scale = scale
self.models[newname].filename = name
minx, miny, minz, maxx, maxy, maxz = (10000, 10000, 10000, 0, 0, 0) minx, miny, minz, maxx, maxy, maxz = (10000, 10000, 10000, 0, 0, 0)
for i in self.models[newname].facets: minx = float("inf")
miny = float("inf")
minz = float("inf")
maxx = float("-inf")
maxy = float("-inf")
maxz = float("-inf")
for i in model.facets:
for j in i[1]: for j in i[1]:
if j[0] < minx: if j[0] < minx:
minx = j[0] minx = j[0]
...@@ -356,20 +326,30 @@ class StlPlater(Plater): ...@@ -356,20 +326,30 @@ class StlPlater(Plater):
maxy = j[1] maxy = j[1]
if j[2] > maxz: if j[2] > maxz:
maxz = j[2] maxz = j[2]
self.models[newname].dims = [minx, maxx, miny, maxy, minz, maxz] model.dims = [minx, maxx, miny, maxy, minz, maxz]
self.add_model(name, model)
#if minx < 0: #if minx < 0:
# self.models[newname].offsets[0] = -minx # model.offsets[0] = -minx
#if miny < 0: #if miny < 0:
# self.models[newname].offsets[1] = -miny # model.offsets[1] = -miny
self.s.drawmodel(self.models[newname], 2) self.s.drawmodel(model, 2)
#print time.time() - t def export_to(self, name):
self.l.Append(newname) sf = open(name.replace(".", "_") + ".scad", "w")
i = self.l.GetSelection() facets = []
if i == wx.NOT_FOUND: for i in self.models.values():
self.l.Select(0)
self.l.Select(self.l.GetCount() - 1) r = i.rot
o = i.offsets
sf.write('translate([%s, %s, %s]) rotate([0, 0, %s]) import_stl("%s");\n' % (str(o[0]), str(o[1]), str(o[2]), r, os.path.split(i.filename)[1]))
if r != 0:
i = i.rotate([0, 0, r])
if o != [0, 0, 0]:
i = i.translate([o[0], o[1], o[2]])
facets += i.facets
sf.close()
stltool.emitstl(name, facets, "plater_export")
print _("wrote %s") % name
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
from printrun.printrun_utils import install_locale, iconfile from printrun.printrun_utils import install_locale, iconfile
install_locale('plater') install_locale('plater')
import os
import wx import wx
class Plater(wx.Frame): class Plater(wx.Frame):
...@@ -80,7 +81,7 @@ class Plater(wx.Frame): ...@@ -80,7 +81,7 @@ class Plater(wx.Frame):
self.s = viewer self.s = viewer
self.mainsizer.Add(self.s, 1, wx.EXPAND) self.mainsizer.Add(self.s, 1, wx.EXPAND)
def autoplate(self, event): def autoplate(self, event = None):
print _("Autoplating") print _("Autoplating")
separation = 2 separation = 2
bedsize = self.build_dimensions[0:3] bedsize = self.build_dimensions[0:3]
...@@ -122,7 +123,7 @@ class Plater(wx.Frame): ...@@ -122,7 +123,7 @@ class Plater(wx.Frame):
result = wx.MessageBox(_('Are you sure you want to clear the grid? All unsaved changes will be lost.'), result = wx.MessageBox(_('Are you sure you want to clear the grid? All unsaved changes will be lost.'),
_('Clear the grid?'), _('Clear the grid?'),
wx.YES_NO | wx.ICON_QUESTION) wx.YES_NO | wx.ICON_QUESTION)
if (result == 2): if result == 2:
self.models = {} self.models = {}
self.l.Clear() self.l.Clear()
self.Refresh() self.Refresh()
...@@ -139,7 +140,6 @@ class Plater(wx.Frame): ...@@ -139,7 +140,6 @@ class Plater(wx.Frame):
if i != -1: if i != -1:
m = self.models[self.l.GetString(i)] m = self.models[self.l.GetString(i)]
m.offsets[2] = -1.0 * min(m.facetsminz)[0] m.offsets[2] = -1.0 * min(m.facetsminz)[0]
#print m.offsets[2]
self.Refresh() self.Refresh()
def delete(self, event): def delete(self, event):
...@@ -150,5 +150,40 @@ class Plater(wx.Frame): ...@@ -150,5 +150,40 @@ class Plater(wx.Frame):
self.l.Select(self.l.GetCount() - 1) self.l.Select(self.l.GetCount() - 1)
self.Refresh() self.Refresh()
def add_model(self, name, model):
newname = os.path.split(name.lower())[1]
c = 1
while newname in self.models:
newname = os.path.split(name.lower())[1]
newname = newname + "(%d)" % c
c += 1
self.models[newname] = model
self.l.Append(newname)
i = self.l.GetSelection()
if i == wx.NOT_FOUND:
self.l.Select(0)
self.l.Select(self.l.GetCount() - 1)
def load(self, event): def load(self, event):
dlg = wx.FileDialog(self, _("Pick file to load"), self.basedir, style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
dlg.SetWildcard(self.load_wildcard)
if dlg.ShowModal() == wx.ID_OK:
name = dlg.GetPath()
self.load_file(name)
dlg.Destroy()
def load_file(self, filename):
raise NotImplementedError
def export(self, event):
dlg = wx.FileDialog(self, _("Pick file to save to"), self.basedir, style = wx.FD_SAVE)
dlg.SetWildcard(self.save_wildcard)
if(dlg.ShowModal() == wx.ID_OK):
name = dlg.GetPath()
self.export_to(name)
dlg.Destroy()
def export_to(self, name):
raise NotImplementedError raise NotImplementedError
...@@ -126,9 +126,11 @@ class StlViewPanel(wxGLPanel): ...@@ -126,9 +126,11 @@ class StlViewPanel(wxGLPanel):
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, vec(0, 0.1, 0, 0.9)) glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, vec(0, 0.1, 0, 0.9))
if call_reshape: if call_reshape:
self.OnReshape() self.OnReshape()
if self.parent.filenames: if hasattr(self.parent, "filenames") and self.parent.filenames:
for filename in self.parent.filenames: for filename in self.parent.filenames:
self.parent.load_file(None, filename) self.parent.load_file(filename)
self.parent.autoplate()
self.parent.filenames = None
def double(self, event): def double(self, event):
p = event.GetPositionTuple() p = event.GetPositionTuple()
......
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