- Parse the G-code or STL file to obtain the geometry of the 3D printed part.
- Convert the geometry into a voxel representation, where each voxel represents a volume element of the part.
- Apply any additional transformations or adjustments to represent the stair steps and voids characteristic of 3D printing.
How to generate voxel model using Gcode tool path in MATLAB ?
19 views (last 30 days)
Show older comments
I am working on a project where i need to simulate the 3D Printed part which has stair steps and voids inherently when fabircated. To get the accurate simulation results i need to include the effects in the model to be simulated. I thought voxel model would meet my need. But i dont know how to generate it in MATLAB. Can anyone help me with sources to be followed to get my job done?
The voxel model is to be generated in MATLAB using Gcode tool path information(or STL ). The model that is generated must be look like the fabricated one.
0 Comments
Answers (2)
Hari
on 27 Dec 2023
Hi TEKEE SURYA SAI,
I understand that you are looking to simulate a 3D printed part with inherent stair steps and voids for accurate results, and you want to use a voxel model would be suitable for this purpose.
To generate a voxel model in MATLAB from G-code or STL files, you would typically:
For G-code, MATLAB does not have built-in functions for directly parsing and converting to a voxel model, so you might need to write custom code or use third-party tools to interpret the G-code and create the voxel model.
For STL files, you can use MATLAB's built-in functions to read the file and create a patch object. Then, you can use the "voxelize" function from the File Exchange to convert the patch object to a voxel model.
For reading STL files in MATLAB, refer to the documentation of "stlread":
For voxelization of geometries, refer to the documentation of "voxelize":
Hope this helps!
1 Comment
DGM
on 14 Jul 2025
Edited: DGM
on 7 Oct 2025 at 18:58
Since R2018b, MATLAB has built-in STL tools. You don't need to download anything.
For legacy versions needing third-party tools, I'd strongly recommend these tools over #22409. This explains why.
If you use #22409 in a modern installation, you will shadow the inbuilt tools and likely cause problems for yourself unless you rename it. TL;DR: It's not fully functional, it will cause conflicts, and it's not even needed anyway.
Regarding FEX #27390, see also:
Maneet Kaur Bagga
on 28 Dec 2023
Hi TEKEE SURYA SAI,
As per my understanding to generate a voxel model from an STL file in MATLAB please refer to the following steps below:
- Use "stlread" to import the STL file, which will give you the vertices and faces of the mesh. Define the size of the voxel grid based on the desired resolution and the size of the printed part.
- Discretize the STL geometry into the voxel grid using a ray intersection method where you check if each voxel's center point is inside the geometry defined by the STL.
- Using the"geometryFromMesh" function create a geometry representation that can be used with MATLAB's PDE Toolbox.
- Create a PDE model and import the geometry. Use the voxel data to define the material properties, boundary conditions that reflects the voxelized structure.
- Generate a mesh for the geometry, apply boundary conditons, material properties, and solve the PDE. During simulation, the voxel grid will be used to modify the properties of the mesh elements or nodes to account for the stair-stepping and voids.
Please refer to the following code snippet below for better understanding of the steps abovs:
% Using stlread to import STL mesh data
[vertices, faces] = stlread('your_model.stl');
% Conceptual voxelization function
voxelGrid = voxelizationFunction(vertices, faces, resolution);
% Initialize PDE model
model = createpde();
% Import the geometry (this will not create a voxel-based geometry but is necessary for meshing and simulation setup)
geometry = importGeometry(model, 'your_model.stl');
% Generate mesh
generateMesh(model);
% Apply boundary conditions, material properties, etc.
% ...
% Solve PDE
result = solvepde(model);
% Post-process results
pdeplot3D(model, 'ColorMapData', result.NodalSolution);
% Additional analysis and visualization code...
For better understanding of generating model using "stlread" please refer to the MATLAB File exchange link below:
Hope this helps!
1 Comment
DGM
on 14 Jul 2025
Edited: DGM
on 8 Oct 2025 at 0:02
The generated code references FEX #29906 or FEX #36771, but the link is to FEX #6678, which is a completely different and incompatible decoder from six years prior.
Neither one of these files should be recommended except for specific, uncommon needs. MATLAB has had a built-in STL decoder stlread() since R2018b, so unless you're running a legacy copy, you don't need anything else.
Dumping AI generated answers with wrong links and code based on third-party, incomplete decoders with the same name as MATLAB's built-in decoder only ends up confusing people.
I'll also point out that this answer doesn't even attempt to answer the question of how to voxelize the model. It simply presupposes that an imaginary voxelizationFunction() exists with some ficticious syntax, then it proceeds to do nothing with it.
I have a feeling that the question wasn't actually even about regular voxelization, but rather, trying to do FEA with consideration for anisotropic properties.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!