Commit 04211253 authored by Kliment Yanev's avatar Kliment Yanev

Add buggy and slow gcode rendering (hard disabled by default)

parent 344cee84
import wx,time import wx,time
class window(wx.Frame): class window(wx.Frame):
def __init__(self,f,size=(600,600),bedsize=(200,200),grid=(10,50),extrusion_width=0.5): def __init__(self,f,size=(600,600),bedsize=(200,200),grid=(10,50),extrusion_width=0.5):
wx.Frame.__init__(self,None,title="Layer view (Use shift+mousewheel to switch layers)",size=(size[0],size[1])) wx.Frame.__init__(self,None,title="Gcode view, shift to move view, mousewheel to set layer",size=(size[0],size[1]))
self.p=gviz(self,size=size,bedsize=bedsize,grid=grid,extrusion_width=extrusion_width) self.p=gviz(self,size=size,bedsize=bedsize,grid=grid,extrusion_width=extrusion_width)
s=time.time() s=time.time()
for i in f:
self.p.addgcode(i)
#print time.time()-s #print time.time()-s
self.initpos=[0,0] self.initpos=[0,0]
self.p.Bind(wx.EVT_KEY_DOWN,self.key) self.p.Bind(wx.EVT_KEY_DOWN,self.key)
...@@ -213,6 +213,11 @@ class gviz(wx.Panel): ...@@ -213,6 +213,11 @@ class gviz(wx.Panel):
dc.DrawBitmap(self.blitmap,0,0) dc.DrawBitmap(self.blitmap,0,0)
del dc del dc
def addfile(self,gcodes=[]):
self.clear()
for i in gcodes:
self.addgcode(i)
def addgcode(self,gcode="M105",hilight=0): def addgcode(self,gcode="M105",hilight=0):
gcode=gcode.split("*")[0] gcode=gcode.split("*")[0]
gcode=gcode.split(";")[0] gcode=gcode.split(";")[0]
......
...@@ -619,6 +619,11 @@ class PronterWindow(wx.Frame,pronsole.pronsole): ...@@ -619,6 +619,11 @@ class PronterWindow(wx.Frame,pronsole.pronsole):
grid=(self.settings.preview_grid_step1,self.settings.preview_grid_step2), grid=(self.settings.preview_grid_step1,self.settings.preview_grid_step2),
extrusion_width=self.settings.preview_extrusion_width) extrusion_width=self.settings.preview_extrusion_width)
self.gviz.showall=1 self.gviz.showall=1
#try:
#
# import stlview
# self.gwindow=stlview.GCFrame(None, wx.ID_ANY, 'Gcode view, shift to move view, mousewheel to set layer', size=(600,600))
#except:
self.gwindow=gviz.window([], self.gwindow=gviz.window([],
bedsize=(self.settings.bed_size_x,self.settings.bed_size_y), bedsize=(self.settings.bed_size_x,self.settings.bed_size_y),
grid=(self.settings.preview_grid_step1,self.settings.preview_grid_step2), grid=(self.settings.preview_grid_step1,self.settings.preview_grid_step2),
...@@ -659,6 +664,7 @@ class PronterWindow(wx.Frame,pronsole.pronsole): ...@@ -659,6 +664,7 @@ class PronterWindow(wx.Frame,pronsole.pronsole):
#uts.Layout() #uts.Layout()
self.cbuttons_reload() self.cbuttons_reload()
def plate(self,e): def plate(self,e):
import plater import plater
print "plate function activated" print "plate function activated"
...@@ -689,7 +695,7 @@ class PronterWindow(wx.Frame,pronsole.pronsole): ...@@ -689,7 +695,7 @@ class PronterWindow(wx.Frame,pronsole.pronsole):
def showwin(self,event): def showwin(self,event):
if(self.f is not None): if(self.f is not None):
self.gwindow.Show() self.gwindow.Show(True)
def setfeeds(self,e): def setfeeds(self,e):
self.feedrates_changed = True self.feedrates_changed = True
...@@ -1347,11 +1353,15 @@ class PronterWindow(wx.Frame,pronsole.pronsole): ...@@ -1347,11 +1353,15 @@ class PronterWindow(wx.Frame,pronsole.pronsole):
print _("the print goes from"),Ymin,_("mm to"),Ymax,_("mm in Y\nand is"),Ytot,_("mm wide\n") print _("the print goes from"),Ymin,_("mm to"),Ymax,_("mm in Y\nand is"),Ytot,_("mm wide\n")
print _("the print goes from"),Zmin,_("mm to"),Zmax,_("mm in Z\nand is"),Ztot,_("mm high\n") print _("the print goes from"),Zmin,_("mm to"),Zmax,_("mm in Z\nand is"),Ztot,_("mm high\n")
print _("Estimated duration (pessimistic): "), pronsole.estimate_duration(self.f) print _("Estimated duration (pessimistic): "), pronsole.estimate_duration(self.f)
#import time
#t0=time.time()
self.gviz.clear() self.gviz.clear()
self.gwindow.p.clear() self.gwindow.p.clear()
for i in self.f: self.gviz.addfile(self.f)
self.gviz.addgcode(i) #print "generated 2d view in %f s"%(time.time()-t0)
self.gwindow.p.addgcode(i) #t0=time.time()
self.gwindow.p.addfile(self.f)
#print "generated 3d view in %f s"%(time.time()-t0)
self.gviz.showall=1 self.gviz.showall=1
wx.CallAfter(self.gviz.Refresh) wx.CallAfter(self.gviz.Refresh)
......
...@@ -212,6 +212,10 @@ class stlview(object): ...@@ -212,6 +212,10 @@ class stlview(object):
def delete(self): def delete(self):
self.vertex_list.delete() self.vertex_list.delete()
def vdiff(v,o):
return [x[0]-x[1] for x in zip(v,o)]
class gcview(object): class gcview(object):
def __init__(self, lines, batch, w=0.5, h=0.5): def __init__(self, lines, batch, w=0.5, h=0.5):
# Create the vertex and normal arrays. # Create the vertex and normal arrays.
...@@ -219,25 +223,25 @@ class gcview(object): ...@@ -219,25 +223,25 @@ class gcview(object):
normals = [] normals = []
self.prev=[0.001,0.001,0.001,0.001] self.prev=[0.001,0.001,0.001,0.001]
self.fline=1 self.fline=1
f=open("20cube_export.gcode")
lines=list(f)
f.close()
self.vlists=[] self.vlists=[]
self.layers={} self.layers={}
t0=time.time()
lines=[self.transform(i) for i in lines] lines=[self.transform(i) for i in lines]
lines=[i for i in lines if i is not None] lines=[i for i in lines if i is not None]
print "transformed lines in %fs"%(time.time()-t0)
t0=time.time()
layertemp={} layertemp={}
lasth=None lasth=None
counter=0
if len(lines)==0:
return
for i in lines: for i in lines:
counter+=1
if i[0][2] not in layertemp: if i[0][2] not in layertemp:
layertemp[i[0][2]]=[[],[]] layertemp[i[0][2]]=[[],[]]
if lasth is not None: if lasth is not None:
self.layers[lasth]=pyglet.graphics.Batch() self.layers[lasth]=pyglet.graphics.Batch()
lt=layertemp[lasth][0] lt=layertemp[lasth][0]
#for p in [[lt[k:k+3],lt[k+3:k+6],lt[k+6:k+9]] for k in range(0, len(lt), 9)]:
# area= 0.5*math.sqrt(sum(map(lambda x:x*x,stltool.cross(vdiff(p[1],p[0]),vdiff(p[2],p[0])))))
# if area>3: print p,area
#print len(lt),lasth
indices = range(len(layertemp[lasth][0])//3)#[[3*i,3*i+1,3*i+2] for i in xrange(len(facets))] indices = range(len(layertemp[lasth][0])//3)#[[3*i,3*i+1,3*i+2] for i in xrange(len(facets))]
self.vlists.append(self.layers[lasth].add_indexed(len(layertemp[lasth][0])//3, self.vlists.append(self.layers[lasth].add_indexed(len(layertemp[lasth][0])//3,
GL_TRIANGLES, GL_TRIANGLES,
...@@ -247,45 +251,29 @@ class gcview(object): ...@@ -247,45 +251,29 @@ class gcview(object):
('n3f/static', layertemp[lasth][1]))) ('n3f/static', layertemp[lasth][1])))
lasth=i[0][2] lasth=i[0][2]
#if lasth==3.8:
# return
def vdiff(v,o):
return map(lambda x,y:x-y,v,o)
spoints,epoints,S,E=self.genline(i,h,w) spoints,epoints,S,E=self.genline(i,h,w)
#if abs(sum(vdiff(S,E)))<10:
# continue verticestoadd=[[spoints[(j+1)%8],epoints[(j)%8],spoints[j],epoints[j],spoints[(j+1)%8],epoints[(j+1)%8]] for j in xrange(8)]
# print spoints normalstoadd=[map(vdiff,v,[S,E,S,E,S,E]) for v in verticestoadd]
for j in xrange(8): v1=[]
map(v1.extend,verticestoadd)
layertemp[i[0][2]][0].extend(spoints[(j+1)%8]) v2=[]
layertemp[i[0][2]][1].extend(vdiff(spoints[(j+1)%8],S)) map(v2.extend,v1)
layertemp[i[0][2]][0].extend(epoints[(j)%8]) n1=[]
layertemp[i[0][2]][1].extend(vdiff(epoints[(j)%8],E)) map(n1.extend,normalstoadd)
layertemp[i[0][2]][0].extend(spoints[j]) n2=[]
layertemp[i[0][2]][1].extend(vdiff(spoints[j],S)) map(n2.extend,n1)
layertemp[i[0][2]][0].extend(epoints[(j)])
layertemp[i[0][2]][1].extend(vdiff(epoints[(j)],E)) layertemp[i[0][2]][0]+=v2
layertemp[i[0][2]][0].extend(spoints[(j+1)%8]) vertices+=v2
layertemp[i[0][2]][1].extend(vdiff(spoints[j],S)) layertemp[i[0][2]][1]+=n2
layertemp[i[0][2]][0].extend(epoints[(j+1)%8]) normals+=n2
layertemp[i[0][2]][1].extend(vdiff(epoints[(j+1)%8],E)) print "appended lines in %fs"%(time.time()-t0)
t0=time.time()
vertices.extend(spoints[(j+1)%8])
normals.extend(vdiff(spoints[(j+1)%8],S))
vertices.extend(epoints[(j)%8])
normals.extend(vdiff(epoints[(j)%8],E))
vertices.extend(spoints[j])
normals.extend(vdiff(spoints[j],S))
vertices.extend(epoints[(j)])
normals.extend(vdiff(epoints[(j)],E))
vertices.extend(spoints[(j+1)%8])
normals.extend(vdiff(spoints[j],S))
vertices.extend(epoints[(j+1)%8])
normals.extend(vdiff(epoints[(j+1)%8],E))
# Create a list of triangle indices. # Create a list of triangle indices.
indices = range(3*16*len(lines))#[[3*i,3*i+1,3*i+2] for i in xrange(len(facets))] indices = range(3*16*len(lines))#[[3*i,3*i+1,3*i+2] for i in xrange(len(facets))]
#print indices[:10]
self.vlists.append(batch.add_indexed(len(vertices)//3, self.vlists.append(batch.add_indexed(len(vertices)//3,
GL_TRIANGLES, GL_TRIANGLES,
None,#group, None,#group,
...@@ -338,7 +326,7 @@ class gcview(object): ...@@ -338,7 +326,7 @@ class gcview(object):
points=map(lambda x:[x[0],x[1],htw*x[2]],points) points=map(lambda x:[x[0],x[1],htw*x[2]],points)
def vadd(v,o): def vadd(v,o):
return map(lambda x,y:x+y,v,o) return map(sum,zip(v,o))
spoints=map(lambda x:vadd(S,x),points) spoints=map(lambda x:vadd(S,x),points)
epoints=map(lambda x:vadd(E,x),points) epoints=map(lambda x:vadd(E,x),points)
return spoints,epoints,S,E return spoints,epoints,S,E
...@@ -567,6 +555,10 @@ class TestGlPanel(GLPanel): ...@@ -567,6 +555,10 @@ class TestGlPanel(GLPanel):
i=self.parent.l.GetSelection() i=self.parent.l.GetSelection()
if i<0: if i<0:
try:
self.parent.setlayerindex(z)
except:
pass
return return
m=self.parent.models[self.parent.l.GetString(i)] m=self.parent.models[self.parent.l.GetString(i)]
...@@ -731,49 +723,69 @@ class TestGlPanel(GLPanel): ...@@ -731,49 +723,69 @@ class TestGlPanel(GLPanel):
glPopMatrix() glPopMatrix()
glPopMatrix() glPopMatrix()
#print "drawn batch" #print "drawn batch"
class TestFrame(wx.Frame): class GCFrame(wx.Frame):
'''A simple class for using OpenGL with wxPython.''' '''A simple class for using OpenGL with wxPython.'''
def __init__(self, parent, ID, title, pos=wx.DefaultPosition, def __init__(self, parent, ID, title, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE): size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE):
super(TestFrame, self).__init__(parent, ID, title, pos, (size[0]+150,size[1]), style) super(GCFrame, self).__init__(parent, ID, title, pos, (size[0]+150,size[1]), style)
self.mainsizer = wx.BoxSizer(wx.HORIZONTAL)
self.panel=wx.Panel(self,-1,size=(150,600),pos=(0,0))
self.panel.SetBackgroundColour((10,10,10))
self.SetBackgroundColour((10,10,10))
self.mainsizer.Add(self.panel)
#self.mainsizer.AddSpacer(10)
class d: class d:
def GetSelection(self): def GetSelection(self):
return -1 return -1
self.p=self
m=d() m=d()
m.offsets=[0,0,0] m.offsets=[0,0,0]
m.rot=0 m.rot=0
m.curlayer=7.0 m.curlayer=0.0
m.scale=[1.,1.,1.] m.scale=[1.,1.,1.]
m.batch=pyglet.graphics.Batch() m.batch=pyglet.graphics.Batch()
m.gc=gcview([], batch=m.batch) m.gc=gcview([], batch=m.batch)
self.models={"":m} self.models={"":m}
self.l=d() self.l=d()
self.modelindex=0
self.GLPanel1 = TestGlPanel(self,size) self.GLPanel1 = TestGlPanel(self,size)
self.mainsizer.Add(self.GLPanel1, 1, wx.EXPAND)
#self.GLPanel2 = TestGlPanel(self, wx.ID_ANY, (20, 20))
#self.mainsizer.Add(self.GLPanel2, 1, wx.EXPAND)
self.SetSizer(self.mainsizer)
#self.mainsizer.Fit(self)
self.Layout()
def addfile(self,gcode=[]):
self.models[""].gc.delete()
self.models[""].gc=gcview(gcode, batch=self.models[""].batch)
def clear(self):
self.models[""].gc.delete()
self.models[""].gc=gcview([], batch=self.models[""].batch)
if __name__=="__main__":
rx = ry = rz = 0
def Show(self,arg=True):
wx.Frame.Show(self,arg)
self.SetClientSize((self.GetClientSize()[0],self.GetClientSize()[1]+1))
self.SetClientSize((self.GetClientSize()[0],self.GetClientSize()[1]-1))
self.Refresh()
wx.FutureCall(500,self.GLPanel1.forceresize)
#threading.Thread(target=self.update).start()
#self.initialized=0
def setlayerindex(self,z):
m=self.models[""]
mlk=sorted(m.gc.layers.keys())
if z>0 and self.modelindex<len(mlk)-1:
self.modelindex+=1
if z<0 and self.modelindex>0:
self.modelindex-=1
m.curlayer=mlk[self.modelindex]
wx.CallAfter(self.SetTitle,"Gcode view, shift to move. Layer %d, Z=%f"%(self.modelindex,m.curlayer))
def main():
app = wx.App(redirect=False) app = wx.App(redirect=False)
frame = TestFrame(None, wx.ID_ANY, 'GL Window', size=(400,400)) frame = GCFrame(None, wx.ID_ANY, 'Gcode view, shift to move view, mousewheel to set layer', size=(400,400))
frame.addfile(list(open("carriage dump_export.gcode")))
#frame = wx.Frame(None, -1, "GL Window", size=(400,400)) #frame = wx.Frame(None, -1, "GL Window", size=(400,400))
#panel = TestGlPanel(frame) #panel = TestGlPanel(frame)
frame.Show(True) #frame.Show(True)
app.MainLoop() #app.MainLoop()
app.Destroy() app.Destroy()
if __name__=="__main__":
import cProfile
print cProfile.run("main()")
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