commit
03b42581b0
17
plater.py
17
plater.py
|
@ -1,9 +1,13 @@
|
|||
#!/usr/bin/env python
|
||||
import wx,time,random,threading,os,math
|
||||
import stltool
|
||||
|
||||
import wx
|
||||
import time
|
||||
import random
|
||||
import threading
|
||||
import os
|
||||
import math
|
||||
import sys
|
||||
|
||||
import stltool
|
||||
|
||||
glview = False
|
||||
if "-nogl" not in sys.argv:
|
||||
|
@ -17,6 +21,7 @@ if "-nogl" not in sys.argv:
|
|||
def evalme(s):
|
||||
return eval(s[s.find("(") + 1:s.find(")")])
|
||||
|
||||
|
||||
class stlwrap:
|
||||
def __init__(self, obj, name=None):
|
||||
self.obj = obj
|
||||
|
@ -63,7 +68,6 @@ class showstl(wx.Window):
|
|||
dc.SelectObject(wx.NullBitmap)
|
||||
m.bitmap.SetMask(wx.Mask(m.bitmap, wx.Colour(0, 0, 0, 255)))
|
||||
|
||||
|
||||
def move_shape(self, delta):
|
||||
"""moves shape (selected in l, which is list ListBox of shapes)
|
||||
by an offset specified in tuple delta.
|
||||
|
@ -213,6 +217,7 @@ class showstl(wx.Window):
|
|||
#print time.time()-t
|
||||
#s.export()
|
||||
|
||||
|
||||
class stlwin(wx.Frame):
|
||||
def __init__(self, size=(800, 580), callback=None, parent=None):
|
||||
wx.Frame.__init__(self, parent, title="Plate building tool", size=size)
|
||||
|
@ -295,7 +300,6 @@ class stlwin(wx.Frame):
|
|||
self.models[i].offsets[1] += centreoffset[1]
|
||||
self.Refresh()
|
||||
|
||||
|
||||
def clear(self, event):
|
||||
result = wx.MessageBox('Are you sure you want to clear the grid? All unsaved changes will be lost.', 'Clear the grid?',
|
||||
wx.YES_NO | wx.ICON_QUESTION)
|
||||
|
@ -328,7 +332,6 @@ class stlwin(wx.Frame):
|
|||
self.Refresh()
|
||||
|
||||
def done(self, event, cb):
|
||||
import os,time
|
||||
try:
|
||||
os.mkdir("tempstl")
|
||||
except:
|
||||
|
@ -339,7 +342,6 @@ class stlwin(wx.Frame):
|
|||
cb(name)
|
||||
self.Destroy()
|
||||
|
||||
|
||||
def export(self, event):
|
||||
dlg = wx.FileDialog(self, "Pick file to save to", self.basedir, style=wx.FD_SAVE)
|
||||
dlg.SetWildcard("STL files (;*.stl;)")
|
||||
|
@ -462,4 +464,3 @@ if __name__ == '__main__':
|
|||
main = stlwin()
|
||||
main.Show()
|
||||
app.MainLoop()
|
||||
|
||||
|
|
82
stlview.py
82
stlview.py
|
@ -1,6 +1,8 @@
|
|||
#!/usr/bin/python
|
||||
import os
|
||||
import wx,math,stltool
|
||||
import math
|
||||
import stltool
|
||||
import wx
|
||||
from wx import glcanvas
|
||||
import time
|
||||
import threading
|
||||
|
@ -8,11 +10,10 @@ import threading
|
|||
import pyglet
|
||||
pyglet.options['shadow_window'] = False
|
||||
pyglet.options['debug_gl'] = False
|
||||
from pyglet import gl
|
||||
from pyglet.gl import *
|
||||
|
||||
class GLPanel(wx.Panel):
|
||||
|
||||
class GLPanel(wx.Panel):
|
||||
'''A simple class for using OpenGL with wxPython.'''
|
||||
|
||||
def __init__(self, parent, id, pos=wx.DefaultPosition,
|
||||
|
@ -134,8 +135,6 @@ class GLPanel(wx.Panel):
|
|||
#create objects to draw
|
||||
#self.create_objects()
|
||||
|
||||
|
||||
|
||||
def OnReshape(self, width, height):
|
||||
'''Reshape the OpenGL viewport based on the dimensions of the window.'''
|
||||
|
||||
|
@ -156,7 +155,6 @@ class GLPanel(wx.Panel):
|
|||
glGetDoublev(GL_MODELVIEW_MATRIX, self.mvmat)
|
||||
#glMatrixMode(GL_PROJECTION)
|
||||
|
||||
|
||||
# Wrap text to the width of the window
|
||||
if self.GLinitialized:
|
||||
self.pygletcontext.set_current()
|
||||
|
@ -188,6 +186,7 @@ class GLPanel(wx.Panel):
|
|||
'''called in the middle of ondraw after the buffer has been cleared'''
|
||||
pass
|
||||
|
||||
|
||||
class stlview(object):
|
||||
def __init__(self, facets, batch):
|
||||
# Create the vertex and normal arrays.
|
||||
|
@ -212,6 +211,7 @@ class stlview(object):
|
|||
def delete(self):
|
||||
self.vertex_list.delete()
|
||||
|
||||
|
||||
def vdiff(v, o):
|
||||
return [x[0] - x[1] for x in zip(v, o)]
|
||||
|
||||
|
@ -254,7 +254,14 @@ class gcview(object):
|
|||
|
||||
spoints, epoints, S, E = self.genline(i, h, w)
|
||||
|
||||
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)]
|
||||
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)]
|
||||
normalstoadd = [map(vdiff, v, [S, E, S, E, S, E]) for v in verticestoadd]
|
||||
v1 = []
|
||||
map(v1.extend, verticestoadd)
|
||||
|
@ -290,7 +297,6 @@ class gcview(object):
|
|||
('v3f/static', layertemp[lasth][0]),
|
||||
('n3f/static', layertemp[lasth][1])))
|
||||
|
||||
|
||||
def genline(self, i, h, w):
|
||||
S = i[0][:3]
|
||||
E = i[1][:3]
|
||||
|
@ -318,10 +324,12 @@ class gcview(object):
|
|||
if alen > 0:
|
||||
axis = map(lambda m: m / alen, axis)
|
||||
angle = math.acos(v[2] / vlen)
|
||||
|
||||
def vrot(v, axis, angle):
|
||||
kxv = stltool.cross(axis, v)
|
||||
kdv = sum(map(lambda x, y: x * y, axis, v))
|
||||
return map(lambda x, y, z: x * math.cos(angle) + y * math.sin(angle) + z * kdv * (1.0 - math.cos(angle)), v, kxv, axis)
|
||||
|
||||
points = map(lambda x: vrot(x, axis, angle), points)
|
||||
points = map(lambda x: [x[0], x[1], htw * x[2]], points)
|
||||
|
||||
|
@ -355,7 +363,6 @@ class gcview(object):
|
|||
self.prev = cur
|
||||
return r
|
||||
|
||||
|
||||
def delete(self):
|
||||
for i in self.vlists:
|
||||
i.delete()
|
||||
|
@ -379,15 +386,19 @@ def trackball(p1x, p1y, p2x, p2y, r):
|
|||
d = map(lambda x, y: x - y, p1, p2)
|
||||
t = math.sqrt(sum(map(lambda x: x * x, d))) / (2.0 * TRACKBALLSIZE)
|
||||
|
||||
if (t > 1.0): t = 1.0
|
||||
if (t < -1.0): t = -1.0
|
||||
if (t > 1.0):
|
||||
t = 1.0
|
||||
if (t < -1.0):
|
||||
t = -1.0
|
||||
phi = 2.0 * math.asin(t)
|
||||
|
||||
return axis_to_quat(a, phi)
|
||||
|
||||
|
||||
def vec(*args):
|
||||
return (GLfloat * len(args))(*args)
|
||||
|
||||
|
||||
def axis_to_quat(a, phi):
|
||||
#print a, phi
|
||||
lena = math.sqrt(sum(map(lambda x: x * x, a)))
|
||||
|
@ -396,29 +407,31 @@ def axis_to_quat(a,phi):
|
|||
q.append(math.cos(phi / 2.0))
|
||||
return q
|
||||
|
||||
|
||||
def build_rotmatrix(q):
|
||||
m = (GLdouble * 16)()
|
||||
m[0] = 1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2])
|
||||
m[1] = 2.0 * (q[0] * q[1] - q[2] * q[3]);
|
||||
m[2] = 2.0 * (q[2] * q[0] + q[1] * q[3]);
|
||||
m[3] = 0.0;
|
||||
m[1] = 2.0 * (q[0] * q[1] - q[2] * q[3])
|
||||
m[2] = 2.0 * (q[2] * q[0] + q[1] * q[3])
|
||||
m[3] = 0.0
|
||||
|
||||
m[4] = 2.0 * (q[0] * q[1] + q[2] * q[3]);
|
||||
m[5]= 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0]);
|
||||
m[6] = 2.0 * (q[1] * q[2] - q[0] * q[3]);
|
||||
m[7] = 0.0;
|
||||
m[4] = 2.0 * (q[0] * q[1] + q[2] * q[3])
|
||||
m[5] = 1.0 - 2.0 * (q[2] * q[2] + q[0] * q[0])
|
||||
m[6] = 2.0 * (q[1] * q[2] - q[0] * q[3])
|
||||
m[7] = 0.0
|
||||
|
||||
m[8] = 2.0 * (q[2] * q[0] - q[1] * q[3]);
|
||||
m[9] = 2.0 * (q[1] * q[2] + q[0] * q[3]);
|
||||
m[10] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]);
|
||||
m[11] = 0.0;
|
||||
m[8] = 2.0 * (q[2] * q[0] - q[1] * q[3])
|
||||
m[9] = 2.0 * (q[1] * q[2] + q[0] * q[3])
|
||||
m[10] = 1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0])
|
||||
m[11] = 0.0
|
||||
|
||||
m[12] = 0.0;
|
||||
m[13] = 0.0;
|
||||
m[14] = 0.0;
|
||||
m[15] = 1.0;
|
||||
m[12] = 0.0
|
||||
m[13] = 0.0
|
||||
m[14] = 0.0
|
||||
m[15] = 1.0
|
||||
return m
|
||||
|
||||
|
||||
def project_to_sphere(r, x, y):
|
||||
d = math.sqrt(x * x + y * y)
|
||||
if (d < r * 0.70710678118654752440):
|
||||
|
@ -427,6 +440,7 @@ def project_to_sphere(r, x, y):
|
|||
t = r / 1.41421356237309504880
|
||||
return t * t / d
|
||||
|
||||
|
||||
def mulquat(q1, rq):
|
||||
return [q1[3] * rq[0] + q1[0] * rq[3] + q1[1] * rq[2] - q1[2] * rq[1],
|
||||
q1[3] * rq[1] + q1[1] * rq[3] + q1[2] * rq[0] - q1[0] * rq[2],
|
||||
|
@ -436,7 +450,7 @@ def mulquat(q1,rq):
|
|||
|
||||
class TestGlPanel(GLPanel):
|
||||
|
||||
def __init__(self, parent, size,id=wx.ID_ANY,):
|
||||
def __init__(self, parent, size, id=wx.ID_ANY):
|
||||
super(TestGlPanel, self).__init__(parent, id, wx.DefaultPosition, size, 0)
|
||||
self.batches = []
|
||||
self.rot = 0
|
||||
|
@ -462,7 +476,6 @@ class TestGlPanel(GLPanel):
|
|||
print v
|
||||
self.add_file("../prusa/metric-prusa/x-end-idler.stl", v)
|
||||
|
||||
|
||||
def forceresize(self):
|
||||
self.SetClientSize((self.GetClientSize()[0], self.GetClientSize()[1] + 1))
|
||||
self.SetClientSize((self.GetClientSize()[0], self.GetClientSize()[1] - 1))
|
||||
|
@ -540,7 +553,6 @@ class TestGlPanel(GLPanel):
|
|||
if self.initpos is not None:
|
||||
self.initpos = None
|
||||
|
||||
|
||||
elif event.Dragging() and event.RightIsDown() and event.ShiftDown():
|
||||
if self.initpos is None:
|
||||
self.initpos = event.GetPositionTuple()
|
||||
|
@ -689,8 +701,6 @@ class TestGlPanel(GLPanel):
|
|||
#threading.Thread(target = self.anim, args = (m, )).start()
|
||||
wx.CallAfter(self.Refresh)
|
||||
|
||||
|
||||
|
||||
def update_object_resize(self):
|
||||
'''called when the window recieves only if opengl is initialized'''
|
||||
pass
|
||||
|
@ -788,21 +798,24 @@ class TestGlPanel(GLPanel):
|
|||
glPopMatrix()
|
||||
glPopMatrix()
|
||||
#print "drawn batch"
|
||||
|
||||
|
||||
class GCFrame(wx.Frame):
|
||||
'''A simple class for using OpenGL with wxPython.'''
|
||||
|
||||
def __init__(self, parent, ID, title, pos=wx.DefaultPosition,
|
||||
size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE):
|
||||
super(GCFrame, self).__init__(parent, ID, title, pos, (size[0] + 150, size[1]), style)
|
||||
|
||||
class d:
|
||||
def GetSelection(self):
|
||||
return -1
|
||||
return wx.NOT_FOUND
|
||||
self.p = self
|
||||
m = d()
|
||||
m.offsets = [0, 0, 0]
|
||||
m.rot = 0
|
||||
m.curlayer = 0.0
|
||||
m.scale=[1.,1.,1.]
|
||||
m.scale = [1.0, 1.0, 1.0]
|
||||
m.batch = pyglet.graphics.Batch()
|
||||
m.gc = gcview([], batch=m.batch)
|
||||
self.models = {"": m}
|
||||
|
@ -818,7 +831,6 @@ class GCFrame(wx.Frame):
|
|||
self.models[""].gc.delete()
|
||||
self.models[""].gc = gcview([], batch=self.models[""].batch)
|
||||
|
||||
|
||||
def Show(self, arg=True):
|
||||
wx.Frame.Show(self, arg)
|
||||
self.SetClientSize((self.GetClientSize()[0], self.GetClientSize()[1] + 1))
|
||||
|
@ -828,8 +840,6 @@ class GCFrame(wx.Frame):
|
|||
#threading.Thread(target = self.update).start()
|
||||
#self.initialized = 0
|
||||
|
||||
|
||||
|
||||
def setlayerindex(self, z):
|
||||
m = self.models[""]
|
||||
mlk = sorted(m.gc.layers.keys())
|
||||
|
|
Loading…
Reference in New Issue