File Exchange

## slice_stl_create_pa​th(triangles,slice_​height)

version 4.0.1 (92 KB) by Sunil Bhandari

### Sunil Bhandari (view profile)

slice stl files and create continuous contour along the slices

Updated 10 Aug 2018

This contains the matlab files to slice a stl file and create a continuous contour along which the machine can move for deposition for 3D printing.
The main file is slice_stl_create.m
The supporting functions are triangle_plane_intersection.m, read_binary_stl_file.m, orient_stl.m, rotate_stl.m and plot_slices.m.
The script stl_slice_and_plot.m is an example using the functions.

PAQUET Elodie

### PAQUET Elodie (view profile)

Hi Sunil, thank you for your work. I have a question. If i would like to slice the part on a z plane with a given angle ( for example 20°) and not only just on the z plane. Could you please tell me in your code, where i can add that angle?

Sunil Bhandari

### Sunil Bhandari (view profile)

Lukas,

triangle_plane_intersection calculates the points of intersection between triangles from the STL model and the slicing plane. The function checks which of the three edges does the slicing plane intersects. The function calculates the point of intersection for each of those edges.

You can run a quick web search for "line plane intersection" and read one of the many websites explaining the mathematics behind it.

Best Regards,
Sunil

Lukas Hulinsky

### Lukas Hulinsky (view profile)

Hi Sunil Hbandari, thank you for sharing your work.

I would like to ask you one question. What is the the math behind this function? triangle_plane_intersection.

Lukas

renqiang tian

### renqiang tian (view profile)

Sunil Bhandari，Thank you very much.
I have another question for you.check = 2;check = 0;check = -1，check = 1;What does that mean? can you explain it for me?

Lukas Hulinsky

dduo yan

Chaochao

Sunil Bhandari

### Sunil Bhandari (view profile)

Liu,

I have updated the file so that the first move is from point with lower x-coordinate to higher x-coordinate or from point with lower y-coordinate to higher y-coordinate. This should solve the problem you described.

Sunil Bhandari

### Sunil Bhandari (view profile)

Chintan,

It is my implementation binary search algorithm. I find the triangles that need to be considered for each slice using this step. Because of this, I don't need to consider all the triangles in the model and only the ones that are intersecting one particular plane.

Chintan Gandhi

### Chintan Gandhi (view profile)

Hi Sunil,
I am trying to understand your code and am not able to understand part of finding intersecting traingles related to determining high and low in zslices. Please can you tell me related to this...

jian liu

### jian liu (view profile)

Hi Sunil:
I noticed that after slicing, the inner loop of a layer countour is sometimes in the counterclockwise instead of the correct clockwise direction.
The attached STL is the example of a simple Ring. If I slice it with a slice_height of 0.04mm, the first 14 layers are correct, i.e., the outer loop part of movelist_all (output of slice_stl_create_path ) is counterclockwise and the inner loop part is clockwise . However, begining at 15th layer, the inner loop is also counterclockwise.

Christina F.

Sunil Bhandari

### Sunil Bhandari (view profile)

Ashish,
graph is an in-built function in Matlab since 2015b.

https://www.mathworks.com/help/matlab/ref/graph.html

Saurabh Bajaj

Ashish Mehta

### Ashish Mehta (view profile)

Hi Sunil
Can you please share me the files to mehtaashish999@gmail.com

thanooja rao

### thanooja rao (view profile)

Hello sir,
can I have coding on adaptive slicing method....where the slicing thickness is called by the algorithm...
I need code as a part of my assignment...
plz....sir Can you drop in my mail box
mail id:thanoojarao@gmail.com

Gerald Lee

### Gerald Lee (view profile)

I have known the {movelidt}. Thank you!

Gerald Lee

### Gerald Lee (view profile)

hello,Sunil
I have some difficulty in data structure of the cell {movelist} . Are there the coordinate values of "double vertexes of each intersect line" in {movelidt} ? Kind of you to give me some advice !
I am going to make ues of them to reshaping the 2D graph and generating the G-code.

Sunil Bhandari

### Sunil Bhandari (view profile)

Markus,
-1e-5 and +1e-5 are there to ensure the lowest and the highest z values in the stl model are accounted for. 1e-5 is an arbitrary small number. Any other small number should work fine.

Markus Kucera

### Markus Kucera (view profile)

Hello i have a question regarding the stl_slice_create_path function. What is the reason for the terms -1e-5 & +1e-5 at the end of the min/max function?

Sunil Bhandari

### Sunil Bhandari (view profile)

Gerald,
the 60 x 14 matrix is made by 60 triangles with 3 vertices and one normal vector. If the P1(x1,y1,z1), P2(x2,y2,z2), and P3(x3,y3,z3) are the three vertices and N(n1,n2,n3) is the normal vector for a triangle. The first row of the triangles matrix that represents this triangle will be:
x1, y1, z1, x2, y2, z2, x3, y3, z3, n1, n2, n3

If you are using ASCII stl file instead of binary stl file, you will need to read the data and arrange the data in this format.

Gerald Lee

### Gerald Lee (view profile)

I found that triangles is a matrix with 60X14 dimension. it is different with the stl file datatype that have coordinates of 3 points and a normal vector of each triangle. I want to use the stl file(with coordinates of 3 points and a normal vector of each triangle) to update the program that always shows exceeding the dimension and cannot test another stl model .Could you give me some advice?

renqiang tian

### renqiang tian (view profile)

Sunil Bhandari：I want to that iterates through an array of every Triangle object and finds the minimum and maximum slope of the entire Triangle array. The slope is equal to the angle of a Triangle's normal vector and the Z axis (either the positive or negative direction, whichever is the smaller angle). Can you realize my idea? Thank you.

renqiang tian

Sunil Bhandari

### Sunil Bhandari (view profile)

Vijay,

I updated the files so that you can orient the stl file along x, y or z axis before slicing.

Vijay Anand

### Vijay Anand (view profile)

In matlab, is it possible to rotate the stl model?
Kindly help me out. I need to generate slices in one of the three axis depending on requirement.

Sunil Bhandari

### Sunil Bhandari (view profile)

Vijay,
you could always rotate the input STL model to orient it in the X-direction or the Y-direction.

Vijay Anand

### Vijay Anand (view profile)

A Very Nice Submission.
Can the code be extended to slice in X or Y directions as well ?
It will be highly appreciated.

Noah Bellomo

Sunil Bhandari

### Sunil Bhandari (view profile)

Noah,

The "triangles" variable is an array with coordinates of points (3 points with three x y z coordinates each) and a normal vector (with x y z coordinates) to the surface formed by the triangle. This gives 12 columns for each row, where each row gives the coordinates for one triangle.

The cell array given out after the slicer program has been run is the list of coordinates through which the extruder head has to move and make depositions. Each cell in the cell array is the movement path for a given layer height.

In additive manufacturing, depositions start from the bottom and deposition is made in the direction opposite to the normal of the triangle. For subtractive manufacturing, you will have to start from the last layer and remove material in the direction of the normal of the triangle.

Noah Bellomo

### Noah Bellomo (view profile)

Could you explain more about the spread sheet of values under the "triangles" variable, after the slicer program has been run on an stl. what do the vectors mean? We are trying to take this information and run it in an array so that it can go where the slice is not. (subtractive manufacturing based on an STL)

Dongmin Han

### Dongmin Han (view profile)

Renqiang,
The code is not commented very well but it is not difficult to figure out.
p1, p2 and p3 are the vertices of the triangles. t1, t2 and t3 are just some ratios in z-direction which will be used to calculate the coordinates of the intersections. And obviously, intersect1, 2 and 3 are intersections of three edges and the slicing plane.
i1, 2, and 3 determine if each intersection is on the edges of the triangles (they could be out of the segments).
Hope this helps.

Dongmin Han

### Dongmin Han (view profile)

thank you so much for sharing the code! it worked well.

renqiang tian

### renqiang tian (view profile)

Sunil Bhandari ：
I've been studying them for a long time and I don't know how to get them.
If yes, can you send me a copy?
thank you... my mailbox:trq163yx@163.com

Sunil Bhandari

### Sunil Bhandari (view profile)

Luo,

uniquetol was first introduced in Matlab R2015a. Are you sure you have a Matlab version R2015a or higher?

Luo zeyi

### Luo zeyi (view profile)

Undefined function 'uniquetol' for input arguments of type 'double'.

Hi, I try to run the code but I received following error, would you pls help me?

Error in slice_stl_create_path (line 137)
nodes = uniquetol(nodes,tol_uniquetol,'ByRows',true);

Error in stl_slice_and_plot (line 9)
tic;[movelist, z_slices] = slice_stl_create_path(triangles, slice_height);toc;

renqiang tian

### renqiang tian (view profile)

Sunil Bhandari ：very thank you，I have resolved the issue，
but，I don t know what is meaning in the “new_line_plane_intersection”function，example：p1，p2，p1，c = ones(1,size(p1,2))*z_slices，t1 = (c-sum(P.*p1))./sum(P.*(p1-p2))，imain = i1+i2+i3 == 2;，pts_out。
I can't understand its. Could you explain it to me，please?

Sunil Bhandari

### Sunil Bhandari (view profile)

Renqiang,

looks like the stl file you are using is too big.
Each row in "triangles" is one triangle with three vertices and one normal. Each of three vertices is represented using three doubles. The normal is also represented using three doubles. That is why there are 12 columns per row.

renqiang tian

### renqiang tian (view profile)

thanks：Sunil Bhandari
Error using zeros
The requested 537529712x12 (48.1GB) array exceeds the default maximum array size

Out of memory. Type HELP MEMORY for your options.
triangles = zeros(numTriangles,12);

Error in stl_slice_and_plot (line 2)

why did it have to happen？ What does "12" mean?

Sunil Bhandari

### Sunil Bhandari (view profile)

Renqiang,
for the error listed below, the error is there because you changed 'f' to 'C' in 'rd = fread(C,inf,'uint8=>uint8')'. Also, you need to provide the path for your own file in the line 'triangles = read_binary_stl_file('C:\Users\sunil.bhandari\Downloads\skullashtray1.stl');'

Undefined function or variable 'C'.
Error in stl_slice_and_plot (line 7)
why？Can you explain it to me?

renqiang tian

### renqiang tian (view profile)

Index exceeds matrix dimensions.

numTriangles = typecast(rd(81:84),'uint32')

why？thank you

renqiang tian

### renqiang tian (view profile)

Hello, do you have any papers on this subject? If yes, can I get a connection?

renqiang tian

### renqiang tian (view profile)

Warning: Input arguments must be scalar.
In stl_slice_and_plot at 3
Error using reshape
Size arguments must be integer scalars.

sh = reshape(rd(85:end),50,numTriangles);%每个三角面片占50个字节，

Error in stl_slice_and_plot (line 3)

why？can you help me ?

hamid reza

renqiang tian

### renqiang tian (view profile)

Hello，Sunil Bhandari，triangles = zeros(numTriangles,12);
I did not have the foggiest idea what it meant.Can you explain it for me? Thanks

renqiang tian

### renqiang tian (view profile)

slice_stl_create_path

min_z = min([triangles(:,3); triangles(:,6);triangles(:,9)])-1e-5;

function [movelist_all,z_slices] = slice_stl_create_path(triangles,slice_height)

why？Can you help me?

renqiang tian

### renqiang tian (view profile)

Undefined function or variable 'C'.

Error in stl_slice_and_plot (line 7)

why？Can you explain it to me?

Sunil Bhandari

### Sunil Bhandari (view profile)

Sergio,
the problem is that if the STL file has triangles that have not been formed properly, or if there are gaps, an extra piece of code needs to run, which I commented out in the files for the sake of speed. I will uncomment that in the new upload.

The getting stuck thing was a bug in the step where I group the triangles by layer heights. I noticed that bug earlier and corrected it. But I could not find time to upload it. I will upload the corrected code.

Cheers,
Sunil

Sergio Pertuz Mendez

### Sergio Pertuz Mendez (view profile)

Also.... when putting the control slice height to 0.2 the function "slice_stl_create_path" gets stuck in an infinite loop in i=1621
Do you have any idea why this might be?

Sergio Pertuz Mendez

### Sergio Pertuz Mendez (view profile)

Hey! Very good work.
I'm having issues reading this file (https://1drv.ms/u/s!Am0SDZmYuu4dhLEOREo71yavTzbqgA).... do you have any idea what might it be?
The issue is theat the first couple of layers at 0.3 slice height doesnt get done correctly

Sergio Pertuz Mendez

Sunil Bhandari

### Sunil Bhandari (view profile)

Shalom, sorry about that. I wrote triangle_plane_intersection to replace new_line_plane_intersection. But I missed replacing it in the code. You can replace "new_line_plane_intersection" with "triangle_plane_intersection" and the code should work fine. I will also upload the corrected solution.

Thank you for pointing out the mistake.

Shalom

### Shalom (view profile)

I get a "Undefined function or variable 'new_line_plane_intersection'." every time I run the code. Can you upload this missing function?

 10 Aug 2018 4.0.1 error corrections to the gui 10 Aug 2018 4.0 gui tool added 10 Aug 2018 3.2.2.1 corrected files uploaded for version 3.2.2.0 9 Aug 2018 3.2.2.0 updated so that the sliced paths move in same direction when possible 4 May 2018 3.2.1.0 function to read ASCII stl files added 12 Mar 2018 3.2.0.0 Function to rotate stl file added. 12 Mar 2018 3.1.0.0 The stl file can now be oriented along x, y or z axis before slicing. The original configuration is assumed to be oriented along x axis. 12 Mar 2018 3.1.0.0 function to orient the stl file along x,y or z axes added 14 Jul 2017 3.0.0.0 bug with infinite loop corrected code for improperly formed triangles uncommented 15 Apr 2017 3.0.0.0 Corrected code: "new_line_plane_intersection" replaced with "triangle_plane_intersection" 24 Mar 2017 3.0.0.0 STL fileread speed increased. Large stl files can be read in seconds. 19 Mar 2017 2.0.0.0 intersecting triangles with each slicing plane now calculated using binary search. Big O for this step changed from O(nk) to O(nlogk), where n is the number of triangles and k is the number of slicing planes.
##### MATLAB Release Compatibility
Created with R2015b
Compatible with R2015b to any release
##### Platform Compatibility
Windows macOS Linux