This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstlviewer.py
216 lines (186 loc) · 6.29 KB
/
stlviewer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
from PySide import QtCore, QtGui, QtOpenGL
from numpy import *
import re, cmath
try:
from OpenGL.GL import *
except ImportError:
app = QtGui.QApplication(sys.argv)
QtGui.QMessageBox.critical(None, "STL Viewer",
"PyOpenGL must be installed to run this application.",
QtGui.QMessageBox.Ok | QtGui.QMessageBox.Default,
QtGui.QMessageBox.NoButton)
sys.exit(1)
class Window(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.glWidget = GLWidget()
mainLayout = QtGui.QHBoxLayout()
mainLayout.addWidget(self.glWidget)
self.setLayout(mainLayout)
self.setWindowTitle(self.tr('STL Viewer'))
class GLWidget(QtOpenGL.QGLWidget,QtGui.QWidget):
triangles = []
timerId= 0
fieldSize = 20
step = 2
delay = 500
cellsCount = 0
pos = 1.0
def __init__(self,parent=None):
QtOpenGL.QGLWidget.__init__(self,parent)
self.object = 0
self.xRot = 2440
self.yRot = 2160
self.zRot = 0
self.lastPos = QtCore.QPoint()
self.trolltechGreen = QtGui.QColor.fromCmykF(0.40, 0.0, 1.0, 0.0)
self.trolltechPurple = QtGui.QColor.fromCmykF(0.39, 0.39, 0.0, 0.0)
self.open()
def sizeRotation(self):
return self.xRot
def sizeRotation(self):
return self.yRot
def sizeRotation(self):
return self.zRot
def minimumSizeHint(self):
return QtCore.QSize(50, 50)
def sizeHint(self):
return QtCore.QSize(600, 600)
def setXRotation(self, angle):
angle = self.normalizeAngle(angle)
if angle != self.xRot:
self.xRot = angle
self.updateGL()
def setYRotation(self, angle):
angle = self.normalizeAngle(angle)
if angle != self.yRot:
self.yRot = angle
self.updateGL()
def setZRotation(self, angle):
angle = self.normalizeAngle(angle)
if angle != self.zRot:
self.zRot = angle
self.updateGL()
def initializeGL(self):
self.qglClearColor(self.trolltechPurple.darker())
self.object = self.makeObject()
glShadeModel(GL_FLAT)
glEnable(GL_DEPTH_TEST)
glEnable(GL_CULL_FACE)
def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glScalef(self.pos, self.pos, self.pos)
glTranslated(0.0, 0.0, -20.0)
glRotated(self.xRot / 16.0, 1.0, 0.0, 0.0)
glRotated(self.yRot / 16.0, 0.0, 1.0, 0.0)
glRotated(self.zRot / 16.0, 0.0, 0.0, 1.0)
glCallList(self.object)
def resizeGL(self, width, height):
side = max(width, height)
glViewport((width - side) / 2, (height - side) / 2, side, side)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(-50, +50, +50, -50, -100.0, 500.0)
glMatrixMode(GL_MODELVIEW)
def mousePressEvent(self, event):
self.lastPos = QtCore.QPoint(event.pos())
if event.buttons() & QtCore.Qt.RightButton:
pass
def wheelEvent(self, event):
if event.delta() > 0:
self.pos += 0.1
else:
self.pos -= 0.1
self.updateGL()
def mouseMoveEvent(self,event):
dx = event.x() - self.lastPos.x()
dy = event.y() - self.lastPos.y()
if event.buttons() & QtCore.Qt.LeftButton:
self.setXRotation(self.xRot + 8 * dy)
self.setYRotation(self.yRot + 8 * dx)
elif event.buttons() & QtCore.Qt.MiddleButton:
self.setXRotation(self.xRot + 8 * dy)
self.setZRotation(self.zRot + 8 * dx)
self.lastPos = QtCore.QPoint(event.pos())
def makeObject(self):
genList = glGenLists(1)
glNewList(genList, GL_COMPILE)
self.axis()
glEndList()
return genList
def timerEvent(self, event):
self.updateGL()
def axis(self):
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST)
glEnable(GL_LINE_SMOOTH)
glEnable(GL_BLEND)
glLineWidth(0.5)
glColor3f(0.0,0.0,0.0)
glBegin(GL_LINES)
for i in range(self.fieldSize+1):
glVertex3f(-100, 0, (float(i)*10)-100)
glVertex3f(100, 0, (float(i)*10)-100)
for i in range(self.fieldSize+1):
glVertex3f((float(i)*10)-100, 0, -100)
glVertex3f((float(i)*10)-100, 0, 100)
glEnd()
glDisable(GL_BLEND)
glDisable(GL_LINE_SMOOTH)
glLineWidth(1)
glColor3f(1,0,0)
glBegin(GL_LINES)
glVertex3f(0,0,0.0)
glVertex3f(0,100,0.0)
glEnd()
glColor3f(0,0,1)
glBegin(GL_LINES)
glVertex3f(0,0,0)
glVertex3f(100,0,0)
glEnd()
glColor3f(0,1,0)
glBegin(GL_LINES)
glVertex3f(0,0,0)
glVertex3f(0,0,100)
glEnd()
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, (0.8, 0.1, 0.0, 1.0))
glShadeModel(GL_FLAT)
self.qglColor(self.trolltechGreen)
glNormal3d(0, 0, 1)
glBegin(GL_TRIANGLES)
for triangel in self.triangles:
glVertex3f(float(triangel[0]), float(triangel[1]), float(triangel[2]))
glEnd()
def open(self):
fileName, filtr = QtGui.QFileDialog.getOpenFileName(self)
if fileName:
file = QtCore.QFile(fileName)
if not file.open( QtCore.QFile.ReadOnly | QtCore.QFile.Text):
return False
instr = QtCore.QTextStream(file)
start = False
width = 0
lines = []
while not instr.atEnd():
line = instr.readLine()
pattern = re.compile("vertex")
if pattern.search(line):
triangel = re.findall('(-?\d+\.\d+e\+?-?\d+)', line)
if len(triangel):
self.triangles.append(triangel)
return True
def normalizeAngle(self, angle):
while angle < 0:
angle += 360 * 16
while angle > 360 * 16:
angle -= 360 * 16
return angle
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())