I'll create a Python program using PyQt5 for the GUI and trimesh for accurate STL file processing. Here's a complete solution:
import sys
import os
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
QPushButton, QLabel, QFileDialog, QGroupBox,
QGridLayout, QMessageBox)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont
import trimesh
import numpy as np
class STLAnalyzer(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('STL File Analyzer')
self.setGeometry(100, 100, 500, 400)
# Create central widget and layout
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QVBoxLayout(central_widget)
# Title label
title_label = QLabel('STL File Surface Area & Volume Calculator')
title_font = QFont()
title_font.setPointSize(14)
title_font.setBold(True)
title_label.setFont(title_font)
title_label.setAlignment(Qt.AlignCenter)
layout.addWidget(title_label)
# File selection section
file_group = QGroupBox('File Selection')
file_layout = QVBoxLayout()
self.file_label = QLabel('No file selected')
self.file_label.setWordWrap(True)
file_layout.addWidget(self.file_label)
self.select_button = QPushButton('Select STL File')
self.select_button.clicked.connect(self.select_file)
file_layout.addWidget(self.select_button)
file_group.setLayout(file_layout)
layout.addWidget(file_group)
# Results section
results_group = QGroupBox('Analysis Results')
results_layout = QGridLayout()
# Labels for results
results_layout.addWidget(QLabel('Surface Area:'), 0, 0)
self.surface_area_label = QLabel('--')
self.surface_area_label.setAlignment(Qt.AlignRight)
results_layout.addWidget(self.surface_area_label, 0, 1)
results_layout.addWidget(QLabel('Volume:'), 1, 0)
self.volume_label = QLabel('--')
self.volume_label.setAlignment(Qt.AlignRight)
results_layout.addWidget(self.volume_label, 1, 1)
results_layout.addWidget(QLabel('Bounding Box:'), 2, 0)
self.bbox_label = QLabel('--')
self.bbox_label.setAlignment(Qt.AlignRight)
results_layout.addWidget(self.bbox_label, 2, 1)
results_layout.addWidget(QLabel('Triangle Count:'), 3, 0)
self.triangle_label = QLabel('--')
self.triangle_label.setAlignment(Qt.AlignRight)
results_layout.addWidget(self.triangle_label, 3, 1)
results_group.setLayout(results_layout)
layout.addWidget(results_group)
# Analyze button
self.analyze_button = QPushButton('Analyze STL File')
self.analyze_button.clicked.connect(self.analyze_file)
self.analyze_button.setEnabled(False)
layout.addWidget(self.analyze_button)
# Status label
self.status_label = QLabel('Ready')
self.status_label.setAlignment(Qt.AlignCenter)
layout.addWidget(self.status_label)
# Add stretch to push everything to the top
layout.addStretch()
self.selected_file = None
def select_file(self):
file_path, _ = QFileDialog.getOpenFileName(
self,
'Select STL File',
'',
'STL Files (*.stl);;All Files (*.*)'
)
if file_path:
self.selected_file = file_path
self.file_label.setText(f'Selected: {os.path.basename(file_path)}')
self.analyze_button.setEnabled(True)
self.status_label.setText('File selected. Click "Analyze" to calculate.')
# Clear previous results
self.surface_area_label.setText('--')
self.volume_label.setText('--')
self.bbox_label.setText('--')
self.triangle_label.setText('--')
def analyze_file(self):
if not self.selected_file:
return
try:
self.status_label.setText('Analyzing...')
QApplication.processEvents() # Update UI
# Load the STL file
mesh = trimesh.load_mesh(self.selected_file)
# Ensure the mesh is valid
if not mesh.is_valid:
mesh.fix_normals()
# Calculate properties with high precision
surface_area = mesh.area # in square millimeters if STL is in mm
volume = mesh.volume # in cubic millimeters if STL is in mm
# Get bounding box dimensions
bbox = mesh.bounding_box.extents
# Get triangle count
triangle_count = len(mesh.faces)
# Format results with appropriate precision
# Assuming STL units are in millimeters (standard)
surface_area_cm2 = surface_area / 100 # mm² to cm²
volume_cm3 = volume / 1000 # mm³ to cm³
# Display results with high precision
self.surface_area_label.setText(f'{surface_area_cm2:.6f} cm²')
self.volume_label.setText(f'{volume_cm3:.6f} cm³')
self.bbox_label.setText(f'{bbox[0]:.3f} × {bbox[1]:.3f} × {bbox[2]:.3f} mm')
self.triangle_label.setText(f'{triangle_count:,}')
# Also show in other units for convenience
surface_area_m2 = surface_area / 1_000_000 # mm² to m²
volume_m3 = volume / 1_000_000_000 # mm³ to m³
detailed_msg = (
f"Analysis Complete!\n\n"
f"Surface Area:\n"
f" {surface_area:.6f} mm²\n"
f" {surface_area_cm2:.6f} cm²\n"
f" {surface_area_m2:.9f} m²\n\n"
f"Volume:\n"
f" {volume:.6f} mm³\n"
f" {volume_cm3:.6f} cm³\n"
f" {volume_m3:.9f} m³\n\n"
f"Mesh is {'watertight' if mesh.is_watertight else 'not watertight'}\n"
f"Mesh is {'valid' if mesh.is_valid else 'not valid'}"
)
self.status_label.setText('Analysis complete!')
# Show detailed results in a message box
QMessageBox.information(self, 'Detailed Results', detailed_msg)
except Exception as e:
error_msg = f'Error analyzing file: {str(e)}'
self.status_label.setText('Error occurred')
QMessageBox.critical(self, 'Error', error_msg)
def main():
app = QApplication(sys.argv)
window = STLAnalyzer()
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
To use this program, you'll need to install the required dependencies:
pip install PyQt5 trimesh numpy
This program provides:
-
Clean PyQt5 GUI with:
- File selection dialog
- Clear display of results
- Detailed analysis button
- Status updates
-
High Precision Calculations:
- Uses
trimesh library for accurate geometric calculations
- Displays results to 6 decimal places for area/volume
- Shows results in multiple metric units (mm², cm², m², mm³, cm³, m³)
-
Additional Features:
- Shows bounding box dimensions
- Triangle count
- Validates if mesh is watertight and valid
- Handles mesh repair if needed
-
Metric Units:
- Assumes STL files are in millimeters (standard)
- Converts and displays in cm² and cm³ as primary units
- Shows detailed breakdown in message box
The program is designed to be user-friendly while maintaining the precision you requested. The trimesh library is particularly good for accurate STL analysis and handles various edge cases in mesh geometry.