using FileIO
using GeometryBasics
using MeshIO
Load the STL file
input_file = “C:\Users\OMG\Desktop\3D model\square.stl”
mesh = FileIO.load(input_file)
Define a slicing plane (e.g., Z = 0.5)
slice_plane_z = 0.5
Function to slice the mesh
function slice_mesh(mesh::Mesh{3, Float32}, plane_z::Float64)
vertices = GeometryBasics.coordinates(mesh) # Extract vertex positions
faces = GeometryBasics.faces(mesh) # Extract face data
# Separate vertices based on Z-coordinate
above_vertices = Vector{Point{3, Float32}}()
below_vertices = Vector{Point{3, Float32}}()
above_indices = Dict{Int, Int}()
below_indices = Dict{Int, Int}()
for (i, vertex) in enumerate(vertices)
if vertex[3] > plane_z
push!(above_vertices, vertex)
above_indices[i] = length(above_vertices)
else
push!(below_vertices, vertex)
below_indices[i] = length(below_vertices)
end
end
# Remap faces
above_faces = [
GeometryBasics.NgonFace(Int[above_indices[idx] for idx in GeometryBasics.vertices(face) if haskey(above_indices, idx)])
for face in faces if all(i -> haskey(above_indices, i), GeometryBasics.vertices(face))
]
below_faces = [
GeometryBasics.NgonFace(Int[below_indices[idx] for idx in GeometryBasics.vertices(face) if haskey(below_indices, idx)])
for face in faces if all(i -> haskey(below_indices, i), GeometryBasics.vertices(face))
]
# Create new meshes
above_mesh = GeometryBasics.Mesh(Point.(above_vertices), above_faces)
below_mesh = GeometryBasics.Mesh(Point.(below_vertices), below_faces)
return above_mesh, below_mesh
end
Perform slicing
above_half, below_half = slice_mesh(mesh, slice_plane_z)
Save the results
output_dir = “C:\Users\OMG\Desktop\3D model\”
FileIO.save(output_dir * “above_half.stl”, above_half)
FileIO.save(output_dir * “below_half.stl”, below_half)
println("Slicing completed! The STL files are saved in: ", output_dir)
ERROR: LoadError: UndefVarError: vertices
not defined
Stacktrace:
[1] (::var"#4#14"{Dict{Int64, Int64}})(face::NgonFace{3, OffsetInteger{-1, UInt32}})
@ Main .\none:0
[2] iterate
@ .\iterators.jl:518 [inlined]
[3] iterate
@ .\generator.jl:44 [inlined]
[4] grow_to!(dest::Vector{Any}, itr::Base.Generator{Base.Iterators.Filter{var"#4#14"{Dict{Int64, Int64}}, Vector{NgonFace{3, OffsetInteger{-1, UInt32}}}}, var"#1#11"{Dict{Int64, Int64}}})
@ Base .\array.jl:907
[5] collect
@ .\array.jl:831 [inlined]
[6] slice_mesh(mesh::Mesh{3, Float32, TriangleP{3, Float32, PointMeta{3, Float32, Point{3, Float32}, (:normals,), Tuple{Vec{3, Float32}}}}, FaceView{TriangleP{3, Float32, PointMeta{3, Float32, Point{3, Float32}, (:normals,), Tuple{Vec{3, Float32}}}}, PointMeta{3, Float32, Point{3, Float32}, (:normals,), Tuple{Vec{3, Float32}}}, NgonFace{3, OffsetInteger{-1, UInt32}}, StructArrays.StructVector{PointMeta{3, Float32, Point{3, Float32}, (:normals,), Tuple{Vec{3, Float32}}}, @NamedTuple{position::Vector{Point{3, Float32}}, normals::Vector{Vec{3, Float32}}}, Int64}, Vector{NgonFace{3, OffsetInteger{-1, UInt32}}}}}, plane_z::Float64)
@ Main C:\Users\OMG\Desktop\3d model\1.jl:35
[7] top-level scope
@ C:\Users\OMG\Desktop\3d model\1.jl:53
in expression starting at C:\Users\OMG\Desktop\3d model\1.jl:53