- Extrude and cut
This commit is contained in:
@@ -10,12 +10,13 @@ class SnapLineWidget(QWidget):
|
||||
self.selected_line = None
|
||||
self.snapping_range = 20 # Range in pixels for snapping
|
||||
|
||||
|
||||
def mousePressEvent(self, event):
|
||||
if event.button() == Qt.LeftButton:
|
||||
if event.button() == Qt.LeftButton :
|
||||
self.points.append(event.pos())
|
||||
self.update()
|
||||
|
||||
if event.button() == Qt.RightButton:
|
||||
elif event.button() == Qt.RightButton:
|
||||
for i in range(len(self.points) - 1):
|
||||
if self.is_point_on_line(event.pos(), self.points[i], self.points[i + 1]):
|
||||
self.selected_line = i
|
||||
@@ -24,7 +25,7 @@ class SnapLineWidget(QWidget):
|
||||
self.selected_line = None
|
||||
|
||||
def mouseMoveEvent(self, event):
|
||||
if event.buttons() & Qt.LeftButton:
|
||||
if event.buttons() & Qt.RightButton:
|
||||
if self.selected_line is not None:
|
||||
self.points[self.selected_line] = event.pos()
|
||||
else:
|
||||
@@ -41,7 +42,8 @@ class SnapLineWidget(QWidget):
|
||||
distance1 = self.distance(p, p1)
|
||||
distance2 = self.distance(p, p2)
|
||||
total_distance = self.distance(p1, p2)
|
||||
return abs(distance1 + distance2 - total_distance) < 0.1
|
||||
|
||||
return abs(distance1 + distance2 - total_distance) < 1
|
||||
|
||||
def paintEvent(self, event):
|
||||
painter = QPainter(self)
|
||||
@@ -65,6 +67,11 @@ class SnapLineWidget(QWidget):
|
||||
p2 = self.points[self.selected_line + 1]
|
||||
painter.setPen(QPen(Qt.red, 2))
|
||||
painter.drawLine(p1, p2)
|
||||
painter.end()
|
||||
|
||||
def clear_sketch(self):
|
||||
self.points = []
|
||||
self.update()
|
||||
|
||||
|
||||
# Example usage
|
||||
|
||||
@@ -8,13 +8,16 @@ from stl import mesh
|
||||
class OpenGLWidget(QOpenGLWidget):
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent)
|
||||
self.scale_factor = 0.001
|
||||
self.mesh_loaded = None
|
||||
self.centroid = None
|
||||
self.stl_file = "out.stl" # Replace with your STL file path
|
||||
self.lastPos = QPoint()
|
||||
self.startPos = None
|
||||
self.endPos = None
|
||||
self.xRot = 0
|
||||
self.yRot = 0
|
||||
self.zoom = -10.0
|
||||
self.zoom = -2
|
||||
self.sketch = []
|
||||
self.gl_width = self.width() / 1000
|
||||
self.gl_height = self.height() / 1000
|
||||
@@ -22,9 +25,11 @@ class OpenGLWidget(QOpenGLWidget):
|
||||
def map_value_to_range(self, value, value_min=0, value_max=1920, range_min=-1, range_max=1):
|
||||
value = max(value_min, min(value_max, value))
|
||||
mapped_value = ((value - value_min) / (value_max - value_min)) * (range_max - range_min) + range_min
|
||||
|
||||
return mapped_value
|
||||
|
||||
def load_stl(self, filename):
|
||||
|
||||
def load_stl(self, filename: str) -> object:
|
||||
try:
|
||||
stl_mesh = mesh.Mesh.from_file(filename)
|
||||
|
||||
@@ -40,17 +45,37 @@ class OpenGLWidget(QOpenGLWidget):
|
||||
centroid_y = (min_y + max_y) / 2.0
|
||||
centroid_z = (min_z + max_z) / 2.0
|
||||
|
||||
return stl_mesh.vectors, (centroid_x, centroid_y, centroid_z)
|
||||
self.mesh_loaded = stl_mesh.vectors
|
||||
self.centroid = (centroid_x, centroid_y, centroid_z)
|
||||
|
||||
except FileNotFoundError:
|
||||
print(f"Error: File {filename} not found.")
|
||||
except Exception as e:
|
||||
print(f"Error loading {filename}: {e}")
|
||||
|
||||
return None, (0, 0, 0)
|
||||
|
||||
def load_mesh_direct(self, mesh) -> object:
|
||||
try:
|
||||
stl_mesh = mesh
|
||||
|
||||
# Extract vertices
|
||||
vertices = np.array(stl_mesh)
|
||||
|
||||
# Calculate centroid based on the average position of vertices
|
||||
centroid = np.mean(vertices, axis=0)
|
||||
|
||||
self.mesh_loaded = vertices
|
||||
self.centroid = tuple(centroid)
|
||||
print(f"Centroid: {self.centroid}")
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
def initializeGL(self):
|
||||
glClearColor(0, 0, 0, 1)
|
||||
glEnable(GL_DEPTH_TEST)
|
||||
|
||||
|
||||
def resizeGL(self, width, height):
|
||||
glViewport(0, 0, width, height)
|
||||
glMatrixMode(GL_PROJECTION)
|
||||
@@ -60,36 +85,33 @@ class OpenGLWidget(QOpenGLWidget):
|
||||
self.gl_width = self.width() / 1000
|
||||
self.gl_height = self.height() / 1000
|
||||
|
||||
gluPerspective(45.0, aspect, 1.0, 1000.0)
|
||||
gluPerspective(45.0, aspect, 1.0, 10000.0)
|
||||
glMatrixMode(GL_MODELVIEW)
|
||||
|
||||
|
||||
def paintGL(self):
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
|
||||
glLoadIdentity()
|
||||
|
||||
mesh_loaded, centroid = self.load_stl(self.stl_file)
|
||||
centroid_x, centroid_y, centroid_z = centroid
|
||||
|
||||
glTranslatef(0, 0, self.zoom)
|
||||
glRotatef(self.xRot, 1.0, 0.0, 0.0)
|
||||
glRotatef(self.yRot, 0.0, 1.0, 0.0)
|
||||
glRotatef(self.xRot, 0.0, 0.0, 0.0)
|
||||
glRotatef(self.yRot, 0.0, 0.0, 0.0)
|
||||
|
||||
glColor3f(1.0, 1.0, 1.0)
|
||||
|
||||
self.draw_area()
|
||||
|
||||
if mesh is not None:
|
||||
# Scale the object
|
||||
scale_factor = 0.001 # Example scale factor (adjust as needed)
|
||||
glScalef(scale_factor, scale_factor, scale_factor)
|
||||
if self.mesh_loaded is not None:
|
||||
# Adjust the camera
|
||||
if self.centroid:
|
||||
glScalef(self.scale_factor, self.scale_factor, self.scale_factor) # Apply scaling
|
||||
|
||||
# Translate to move the centroid of the object to the origin
|
||||
glTranslatef(-centroid_x, -centroid_y, -centroid_z)
|
||||
cx, cy, cz = self.centroid
|
||||
gluLookAt(cx, cy, cz + 100, cx, cy, cz, 0, 1, 0)
|
||||
|
||||
self.draw_mesh_direct(self.mesh_loaded)
|
||||
|
||||
|
||||
self.draw_stl(mesh_loaded)
|
||||
|
||||
def draw_stl(self, vertices):
|
||||
glEnable(GL_LIGHTING)
|
||||
glEnable(GL_LIGHT0)
|
||||
@@ -105,15 +127,24 @@ class OpenGLWidget(QOpenGLWidget):
|
||||
for vertex in triangle:
|
||||
glVertex3fv(vertex)
|
||||
glEnd()
|
||||
self.update()
|
||||
|
||||
"""# Draw outer vertices as points
|
||||
glDisable(GL_LIGHTING)
|
||||
glColor3f(1.0, 0.0, 0.0) # Set color to red
|
||||
glPointSize(5.0) # Set point size
|
||||
glBegin(GL_POINTS)
|
||||
for vertex in vertices:
|
||||
def draw_mesh_direct(self, points):
|
||||
glEnable(GL_LIGHTING)
|
||||
glEnable(GL_LIGHT0)
|
||||
glEnable(GL_DEPTH_TEST)
|
||||
glEnable(GL_COLOR_MATERIAL)
|
||||
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
|
||||
|
||||
glLightfv(GL_LIGHT0, GL_POSITION, (0, 1, 1, 0))
|
||||
glLightfv(GL_LIGHT0, GL_DIFFUSE, (0.6, 0.6, 0.6, 1.0))
|
||||
|
||||
glBegin(GL_TRIANGLES)
|
||||
for vertex in points:
|
||||
glVertex3fv(vertex)
|
||||
glEnd()"""
|
||||
glEnd()
|
||||
self.update()
|
||||
|
||||
|
||||
def draw_area(self):
|
||||
glColor3f(0.5, 0.5, 0.5) # Gray color
|
||||
|
||||
Reference in New Issue
Block a user