0% found this document useful (0 votes)
4 views

# 3d_printer_slicer

slicer

Uploaded by

9634187
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

# 3d_printer_slicer

slicer

Uploaded by

9634187
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

# 3d_printer_slicer.

py

import sys
import numpy as np
from stl import mesh # Install with `pip install numpy-stl`
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QFileDialog, QSlider,
QLabel, QVBoxLayout, QWidget, QComboBox, QCheckBox
from PyQt5.QtCore import Qt

# Global variables
LAYER_HEIGHT = 0.2 # Default layer height
INFILL_DENSITY = 48 # Default infill density (%)
INFILL_PATTERN = "Rectilinear" # Default infill pattern

class SlicerGUI(QMainWindow):
def __init__(self):
super().__init__()

self.initUI()

def initUI(self):
self.setWindowTitle('Advanced 3D Printing Slicer')
self.setGeometry(100, 100, 800, 600)

# Create widgets
self.btn_load_model = QPushButton('Load STL', self)
self.btn_load_model.clicked.connect(self.load_stl_file)

self.slider_layer_height = QSlider(Qt.Horizontal, self)


self.slider_layer_height.setMinimum(1)
self.slider_layer_height.setMaximum(10)
self.slider_layer_height.setValue(int(LAYER_HEIGHT * 10))
self.slider_layer_height.valueChanged.connect(self.update_layer_height)
self.label_layer_height = QLabel(f'Layer Height: {LAYER_HEIGHT} mm', self)

self.slider_infill_density = QSlider(Qt.Horizontal, self)


self.slider_infill_density.setMinimum(0)
self.slider_infill_density.setMaximum(100)
self.slider_infill_density.setValue(INFILL_DENSITY)
self.slider_infill_density.valueChanged.connect(self.update_infill_density)
self.label_infill_density = QLabel(f'Infill Density: {INFILL_DENSITY} %', self)

self.combo_infill_pattern = QComboBox(self)
self.combo_infill_pattern.addItems(["Rectilinear", "Honeycomb", "Grid"])
self.combo_infill_pattern.setCurrentText(INFILL_PATTERN)
self.combo_infill_pattern.currentTextChanged.connect(self.update_infill_pattern)
self.label_infill_pattern = QLabel(f'Infill Pattern: {INFILL_PATTERN}', self)

self.chk_generate_supports = QCheckBox('Generate Supports', self)


self.chk_generate_supports.setChecked(False)

self.btn_slice = QPushButton('Slice Model', self)


self.btn_slice.clicked.connect(self.slice_model)

# Layout
layout = QVBoxLayout()
layout.addWidget(self.btn_load_model)
layout.addWidget(self.slider_layer_height)
layout.addWidget(self.label_layer_height)
layout.addWidget(self.slider_infill_density)
layout.addWidget(self.label_infill_density)
layout.addWidget(self.combo_infill_pattern)
layout.addWidget(self.label_infill_pattern)
layout.addWidget(self.chk_generate_supports)
layout.addWidget(self.btn_slice)

widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)

def load_stl_file(self):
file_dialog = QFileDialog(self)
file_dialog.setNameFilter("STL files (*.stl)")
if file_dialog.exec_():
file_path = file_dialog.selectedFiles()[0]
self.process_stl(file_path)

def process_stl(self, file_path):


# Load STL file
try:
self.mesh_data = mesh.Mesh.from_file(file_path)
print(f"Loaded STL: {file_path}")
except Exception as e:
print(f"Error loading STL file: {e}")
self.mesh_data = None

def update_layer_height(self, value):


global LAYER_HEIGHT
LAYER_HEIGHT = value / 10
self.label_layer_height.setText(f'Layer Height: {LAYER_HEIGHT:.2f} mm')

def update_infill_density(self, value):


global INFILL_DENSITY
INFILL_DENSITY = value
self.label_infill_density.setText(f'Infill Density: {INFILL_DENSITY} %')

def update_infill_pattern(self, pattern):


global INFILL_PATTERN
INFILL_PATTERN = pattern
self.label_infill_pattern.setText(f'Infill Pattern: {INFILL_PATTERN}')

def slice_model(self):
if hasattr(self, 'mesh_data'):
slices = self.slice_model_process(self.mesh_data)
# TODO: Generate G-code and further process slices

def slice_model_process(self, mesh_data):


# Perform slicing based on parameters
min_z = np.min(mesh_data.vectors[:,:,2])
max_z = np.max(mesh_data.vectors[:,:,2])

layer_heights = np.arange(min_z, max_z, LAYER_HEIGHT)

slices = []
for z in layer_heights:
slice_polygons = []
for triangle in mesh_data.vectors:
# Check if triangle intersects current layer
if (triangle[:,2].min() < z < triangle[:,2].max()):
# Interpolate to find intersection points on triangle
intersection_points = []
for i in range(3):
j = (i + 1) % 3
if (triangle[i, 2] > z) != (triangle[j, 2] > z):
t = (z - triangle[i, 2]) / (triangle[j, 2] - triangle[i, 2])
intersection_points.append(triangle[i] + t * (triangle[j] - triangle[i]))
slice_polygons.append(intersection_points)
slices.append(slice_polygons)

return slices

def main():
app = QApplication(sys.argv)
window = SlicerGUI()
window.show()
sys.exit(app.exec_())

if __name__ == '__main__':
main()

You might also like