Radiolaria-cad-550x557 For me, Stl files came into existence via solidworks. For passing solidworks files from my lab to the machining department, we had to export the solidworks model to stl file formats.

That was it. After that i had no contact with them until this year when I again got some stl models. I was using meshlab to read and view them. Then one boring day decided to see what was inside these files.

That enquiry resulted into a small project and the following program is the result of that project. Binary stl file reader in Fortran. There are many programs available to read stl files, some in matlab, some in c but there wasn’t any available in fortran.

I found one fortran stl file reader but that only worked for ASCII stl files. Thus began this project to read the binary stl files via fortran. After multiple visits to wikipedia, and endless hours of debugging, I have the complete fortran program to read binary stl file. So here’s the result. Fortran STL file reader

program stlread

! Program to Read Binary STL file.
! programmed by Sukhbinder Singh
! stores normals and the triangles in the normals and triangles arrays.
!
! 23rd May 2011

real*4 n(3),x1(3),x2(3),x3(3)
 integer*2 padding
 integer*4 ntri,iunit,irc
 character(len=80) title, filename
 double precision, allocatable :: normals(:,:),triangles(:,:)

 filename="C:\Documents and Settings\sukhbinder\Desktop\projects\stlbinaryread\sample.stl"

iunit=13

 open(unit=iunit,file=filename,status='old',form='unformatted', &
 & access='direct',recl=80)

read(iunit,rec=1)title
 close(iunit,status='keep')

open(unit=iunit,file=filename,status='old',form='unformatted', &
 & access='direct',recl=4)

 read(iunit,rec=21)ntri
 close(iunit,status='keep')

 open(unit=iunit,file=filename,status='old',form='unformatted', &
 & access='direct',recl=2)
 irc=(80+4)/2+1

 allocate(normals(3,ntri))
 allocate(triangles(3,ntri*3))

 k=1
 do i=1,ntri
 call readbin(iunit,n(1),irc)
 call readbin(iunit,n(2),irc)
 call readbin(iunit,n(3),irc)

call readbin(iunit,x1(1),irc)
 call readbin(iunit,x1(2),irc)
 call readbin(iunit,x1(2),irc)

call readbin(iunit,x2(1),irc)
 call readbin(iunit,x2(2),irc)
 call readbin(iunit,x2(3),irc)

call readbin(iunit,x3(1),irc)
 call readbin(iunit,x3(2),irc)
 call readbin(iunit,x3(3),irc)

normals(1,i)=n(1)
 normals(2,i)=n(2)
 normals(3,i)=n(3)

 triangles(1,k)=x1(1)
 triangles(2,k)=x1(2)
 triangles(3,k)=x1(3)

triangles(1,k+1)=x2(1)
 triangles(2,k+1)=x2(2)
 triangles(3,k+1)=x2(3)
 triangles(1,k+2)=x3(1)
 triangles(2,k+2)=x3(2)
 triangles(3,k+2)=x3(3)

k=k+3
 read(iunit,rec=irc)padding
 irc=irc+1
 end do

write(*,*) trim(filename),' has this title ',trim(title),' and has',ntri, ' triangles'
 end program

subroutine readbin(iunit,a,irc)
 real*4 a,b
 integer*2 ib(2)
 equivalence(b,ib)

read(iunit,rec=irc)ib(1)
 irc=irc+1
 read(iunit,rec=irc)ib(2)
 irc=irc+1

 a=b

end

13 responses to “STL files and Fortran”

  1. Hi Sukhbinder, your contribution here is amazing.I cannot get my data from a solidwork stl appear correctly however. My suspecion is that perhaps because the solidwork file was made on a 64 bits machine, and I am running your fortran on a 32 bits laptop, the error appears. What do you think? Can this be the source to my frustration?

    All the best
    Thomas

    Like

    1. Thanks Thomas. I don’t think 64 bit or 32bit should be an issue. During this weekend I will give it a check.

      Can you by any chance upload a similar model for me to test in my laptop.

      Cheers

      Like

  2. […] the post STL files and fortran, I posted a fortran binary stl file reader […]

    Like

  3. Hi Sukhbinder, your technique for reading the STL file works well except for one issue, speed. I have a file with about 7,500,000 triangular patches. It takes roughly 14 minutes to do the read. GOM Inspect from http://www.gom.com can read the same file on the same computer in less than a minute. Can you think of a way to improve the FORTRAN read speed?

    Like

    1. Thanks Don for the comment and suggestion. I know this issue but never gave it much thought as never got a big enough need. Anyways, thanks to your pointer, will try to see if this can be improved. I am sure it has lot of room to improve. Thanks again!!

      Like

  4. […] the STL and Fortran post Don Durschmidt asked if the stl binary reader fortran code can be made fast for reading big […]

    Like

  5. […] old one still wins Well Dan won’t be happy with it. I am not very happy with it. But While thinking about optimising the […]

    Like

  6. […] with VTK and wxpython, got this basic app for loading and viewing SLT files. The GUI is wxpython, vtk provides the STL file […]

    Like

  7. Hi Sukhbinder, this is a nice utility, but I think there is a small bug in the program. In the sixth call to “readbin”, it should be “x1(3)” instead of “x1(2)”

    Like

    1. Thanks Scott. I will check this. I haven’t encounter any problem reading any stl file with this but will look at it as soon as I get some time.

      Thanks

      Like

    2. Arash Asadollahi Avatar
      Arash Asadollahi

      Hi Sukhbinder, I agree with Scott and you need to replace x1(2) with x1(3). x1(2) is repeated twice and I had a problem with it as well.
      Thanks
      Arash

      Like

      1. Hi Arash for pointing this out. I missed it again. Do you have an stl which can demonstrate this? Will you be able to share it. I will check this this week and update according

        Like

Leave a comment

Trending