read_binary_stl_file Subroutine

private subroutine read_binary_stl_file(me, filename, istat)

Read a binary STL file.

Type Bound

stl_file

Arguments

Type IntentOptional Attributes Name
class(stl_file), intent(out) :: me
character(len=*), intent(in) :: filename

STL file name

integer, intent(out) :: istat

iostat code (=0 if no errors)


Calls

proc~~read_binary_stl_file~~CallsGraph proc~read_binary_stl_file stl_file%read_binary_stl_file proc~destroy_stl_file stl_file%destroy_stl_file proc~read_binary_stl_file->proc~destroy_stl_file

Source Code

    subroutine read_binary_stl_file(me,filename,istat)

    implicit none

    class(stl_file),intent(out) :: me
    character(len=*),intent(in) :: filename !! STL file name
    integer,intent(out)         :: istat    !! `iostat` code (=0 if no errors)

    integer :: iunit                        !! file unit number
    integer :: i                            !! counter
    integer(c_int32_t) :: n_plates          !! number of plates [32 bits]
    real(c_float),dimension(3) :: n         !! normal vector [32 bits]
    real(c_float),dimension(3) :: v1,v2,v3  !! vertex vectors [32 bits]
    integer(c_int16_t) :: z                 !! Attribute byte count [16 bits]
    character(kind=c_char,len=80) :: header !! [8 bits x 80]

    call me%destroy()

    ! open the binary file:
    open(newunit = iunit,&
         file    = filename,&
         action  = 'READ',&
         status  = 'OLD',&
         form    = 'UNFORMATTED', &
         access  = 'STREAM', &
         iostat  = istat)

    if (istat==0) then

        ! read header:
        read(iunit,iostat=istat) header, n_plates

        if (istat==0) then

            ! size arrays:
            me%n_plates = int(n_plates)
            allocate(me%plates(me%n_plates))

            ! read the data from the file:
            do i = 1, me%n_plates
                read(iunit,iostat=istat) n,v1,v2,v3,z
                if (istat/=0) exit
                ! only need to save the plates:
                me%plates(i)%v1 = real(v1, wp)
                me%plates(i)%v2 = real(v2, wp)
                me%plates(i)%v3 = real(v3, wp)
            end do

        end if

        ! close the file:
        close(iunit, iostat=istat)

    end if

    end subroutine read_binary_stl_file