# Alert! Error while slicing

**URL:** <https://discourse.julialang.org/t/alert-error-while-slicing/123439>\
**Category:** Probabilistic Programming\
**Tags:** error\
**Created:** [December 4, 2024, 7:27am UTC](https://discourse.julialang.org/t/alert-error-while-slicing/123439 "2024-12-04T07:27:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![govindanupam](https://avatars.discourse-cdn.com/v4/letter/g/ac8455/32.png) [@govindanupam](https://discourse.julialang.org/u/govindanupam)\
**Post date:** [December 4, 2024, 7:27am UTC](https://discourse.julialang.org/t/alert-error-while-slicing/123439/1 "2024-12-04T07:27:24Z")

</div>

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

 ![square-stl](https://global.discourse-cdn.com/julialang/original/3X/c/e/ce69a9f16db9f0759e7c31c10a817581142dde45.jpeg)

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [December 4, 2024, 8:03am UTC](https://discourse.julialang.org/t/alert-error-while-slicing/123439/2 "2024-12-04T08:03:44Z")

</div>

Your code is not runnable, please put the time in to provide examples people can actually run - [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757). You should also aim to provide minimum examples that reproduce your error.

You can use triple backticks (`) to quote your code.

---

<div class="post-metadata">

**Author:** ![govindanupam](https://avatars.discourse-cdn.com/v4/letter/g/ac8455/32.png) [@govindanupam](https://discourse.julialang.org/u/govindanupam)\
**Post date:** [December 4, 2024, 9:36am UTC](https://discourse.julialang.org/t/alert-error-while-slicing/123439/3 "2024-12-04T09:36:33Z")

</div>

```julia

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)

```

```julia
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

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/9/395d00daf6d40ecb29666c284d5bf512886d81d1.png)

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [December 5, 2024, 4:31am UTC](https://discourse.julialang.org/t/alert-error-while-slicing/123439/5 "2024-12-05T04:31:59Z")

</div>

> [@govindanupam](#):
>
> `GeometryBasics.vertices`

Just a hunch this function doesn’t exist, try pasting this code in a new line following `using GeometryBasics`.

---

<div class="post-metadata">

**Author:** ![govindanupam](https://avatars.discourse-cdn.com/v4/letter/g/ac8455/32.png) [@govindanupam](https://discourse.julialang.org/u/govindanupam)\
**Post date:** [December 5, 2024, 5:56am UTC](https://discourse.julialang.org/t/alert-error-while-slicing/123439/6 "2024-12-05T05:56:00Z")

</div>

Thanks for suggestion team,

i tried to modify and execute below code:

```julia
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}() # Maps old indices to new indices
    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

    # Process faces
    above_faces = [
        GeometryBasics.NgonFace(Int[above_indices[idx] for idx in GeometryBasics.get(face, :vertices) if haskey(above_indices, idx)])
        for face in faces if all(i -> haskey(above_indices, i), GeometryBasics.get(face, :vertices))
    ]

    below_faces = [
        GeometryBasics.NgonFace(Int[below_indices[idx] for idx in GeometryBasics.get(face, :vertices) if haskey(below_indices, idx)])
        for face in faces if all(i -> haskey(below_indices, i), GeometryBasics.get(face, :vertices))
    ]

    # Create new meshes
    above_mesh = GeometryBasics.Mesh(above_vertices, above_faces)
    below_mesh = GeometryBasics.Mesh(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)

```

i get output as attached screen shot:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/6/76abf84846ba42c57179a0ea7caa6ce7a60c1a90.png)

Please suggest

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [December 5, 2024, 8:57am UTC](https://discourse.julialang.org/t/alert-error-while-slicing/123439/7 "2024-12-05T08:57:29Z")

</div>

As mentioned in [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757), you should provide code samples that people can actually run if you want effective help. A large example with code people can’t run is highly unlikely to get people willing to put the time into help.

The error message says you aren’t using `get` correctly. See `?get`.
