import numpy as np
from stl import mesh
def calculate_layer_height_from_stl(stl_file):
"""
Calculates the layer height from an STL file.
Args:
stl_file: Path to the STL file.
Returns:
The calculated layer height, or None if an error occurs.
"""
try:
# Load the STL file
mesh_obj = mesh.Mesh.from_file(stl_file)
# Get the vertices of the mesh
vertices = mesh_obj.vertices
# Calculate layer height
layer_height = 0.0
for vertex in vertices:
layer_height += vertex[2] # Add the z-coordinate
return layer_height
except Exception as e:
print(f"Error processing STL file: {e}")
return None
if __name__ == "__main__":
# Get the STL file path from the user
stl_file = input("Enter the path to your STL file: ")
if stl_file:
layer_height = calculate_layer_height_from_stl(stl_file)
if layer_height is not None:
print(f"Layer Height: {layer_height:.2f}")
else:
print("Please enter a valid STL file path.")
on progress
<!DOCTYPE html>
<html lang="en">
<head>
<title>Calculate Layer Height from STL</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
line-height: 1.6;
}
h1 {
text-align: center;
color: #333;
}
p {
margin-bottom: 15px;
}
ul {
list-style: disc;
margin-left: 20px;
}
li {
margin-bottom: 5px;
}
.highlight {
font-weight: bold;
color: #007BFF;
}
</style>
</head>
<body>
<h1 style="text-align:center;">Calculate Layer Height from STL</h1>
<label for="stl_file">STL File:</label>
<input type="text" id="stl_file" name="stl_file" value="path/to/your/file.stl"><br><br>
<button onclick="calculateLayerHeight()">Calculate Layer Height</button>
<p id="layer_height"></p>
<script>
function calculateLayerHeight() {
const stlFile = document.getElementById("stl_file").value;
if (stlFile) {
try {
const mesh = mesh.Mesh.from_file(stlFile);
const vertices = mesh.vertices;
const layer_height = 0.0;
for (let i = 0; i < vertices.length; i++) {
layer_height += vertices[i][2];
}
document.getElementById("layer_height").textContent = layer_height;
} catch (error) {
document.getElementById("layer_height").textContent = "Error: " + error;
}
} else {
document.getElementById("layer_height").textContent = "Please enter a valid STL file path.";
}
}
</script>
</body>
</html>
3
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>STL Layer Height Calculator</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h1 {
text-align: center;
}
</style>
</head>
<body>
<h1>Calculate Layer Height from STL</h1>
<label for="stl_file">Upload STL File:</label>
<input type="file" id="stl_file" accept=".stl"><br><br>
<button onclick="calculateLayerHeight()">Calculate Layer Height</button>
<p id="layer_height"></p>
<script>
function calculateLayerHeight() {
const fileInput = document.getElementById("stl_file");
const file = fileInput.files[0];
if (!file) {
document.getElementById("layer_height").textContent = "Please upload an STL file.";
return;
}
const reader = new FileReader();
reader.onload = function(event) {
const contents = event.target.result;
const zValues = [];
const regex = /vertex\s+([-\d.]+)\s+([-\d.]+)\s+([-\d.]+)/g;
let match;
while ((match = regex.exec(contents)) !== null) {
const z = parseFloat(match[3]);
if (!isNaN(z)) {
zValues.push(z);
}
}
if (zValues.length === 0) {
document.getElementById("layer_height").textContent = "No vertices found in STL.";
return;
}
// Estimate layer height
const minZ = Math.min(...zValues);
const maxZ = Math.max(...zValues);
const height = maxZ - minZ;
document.getElementById("layer_height").textContent =
`Estimated object height: ${height.toFixed(2)} mm`;
};
reader.readAsText(file);
}
</script>
</body>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>STL Viewer & Height Calculator</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
overflow: hidden;
}
#ui {
position: absolute;
z-index: 10;
background: rgba(255, 255, 255, 0.9);
padding: 10px;
border-bottom-right-radius: 8px;
}
#heightDisplay {
margin-top: 10px;
font-weight: bold;
}
canvas {
display: block;
}
</style>
</head>
<body>
<div id="ui">
<input type="file" id="fileInput" accept=".stl">
<div id="heightDisplay">Upload an STL file to view height.</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/three@0.150.1/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.150.1/examples/js/loaders/STLLoader.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.150.1/examples/js/controls/OrbitControls.js"></script>
<script>
let scene, camera, renderer, controls;
init();
function init() {
scene = new THREE.Scene();
scene.background = new THREE.Color(0xf0f0f0);
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 0, 100);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
controls = new THREE.OrbitControls(camera, renderer.domElement);
const light1 = new THREE.DirectionalLight(0xffffff, 1);
light1.position.set(1, 1, 1);
scene.add(light1);
const light2 = new THREE.AmbientLight(0x888888);
scene.add(light2);
animate();
}
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
document.getElementById('fileInput').addEventListener('change', function (event) {
const file = event.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = function (e) {
const contents = e.target.result;
const loader = new THREE.STLLoader();
const geometry = loader.parse(contents);
// Clear previous model
scene.clear();
scene.add(new THREE.AmbientLight(0x888888));
const light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(1, 1, 1);
scene.add(light);
// Create mesh
const material = new THREE.MeshPhongMaterial({ color: 0x007bff, specular: 0x111111, shininess: 200 });
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
// Center and scale view
geometry.computeBoundingBox();
const bbox = geometry.boundingBox;
const size = new THREE.Vector3();
bbox.getSize(size);
const center = new THREE.Vector3();
bbox.getCenter(center);
mesh.position.sub(center); // center the object
controls.reset();
camera.position.set(0, 0, size.length() * 1.5);
controls.update();
// Display object height (Z axis)
const height = (bbox.max.z - bbox.min.z).toFixed(2);
document.getElementById('heightDisplay').textContent = `Object Height: ${height} mm`;
};
reader.readAsArrayBuffer(file);
});
</script>
</body>
</html>