write_binary_stl_file Subroutine

private subroutine write_binary_stl_file(me, filename, istat, bounding_box)

Generate a binary STL file.

Notes

The file format is:

  UINT8[80]  Header
  UINT32  Number of triangles
  foreach triangle
    REAL32[3]  Normal vector
    REAL32[3]  Vertex 1
    REAL32[3]  Vertex 2
    REAL32[3]  Vertex 3
    UINT16  Attribute byte count
  end

Type Bound

stl_file

Arguments

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

STL file name

integer, intent(out) :: istat

iostat code (=0 if no errors)

real(kind=wp), intent(in), optional :: bounding_box

scale vertices so that model fits in a box of this size (if <=0, no scaling is done)


Calls

proc~~write_binary_stl_file~~CallsGraph proc~write_binary_stl_file stl_file%write_binary_stl_file proc~compute_vertex_scale stl_file%compute_vertex_scale proc~write_binary_stl_file->proc~compute_vertex_scale proc~normal normal proc~write_binary_stl_file->proc~normal proc~cross cross proc~normal->proc~cross proc~unit unit proc~normal->proc~unit

Source Code

    subroutine write_binary_stl_file(me,filename,istat,bounding_box)

    implicit none

    class(stl_file),intent(in)    :: me
    character(len=*),intent(in)   :: filename      !! STL file name
    integer,intent(out)           :: istat         !! `iostat` code (=0 if no errors)
    real(wp),intent(in),optional  :: bounding_box  !! scale vertices so that model fits in a
                                                   !! box of this size (if <=0, no scaling is done)

    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]
    real(wp) :: scale                       !! scale factor

    integer(c_int16_t),parameter :: z = 0  !! Attribute byte count [16 bits]
    character(kind=c_char,len=80),parameter :: header = repeat(' ',80) !! [8 bits x 80]

    n_plates = int(me%n_plates,kind=c_int32_t)

    scale = me%compute_vertex_scale(bounding_box)

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

    if (istat==0) then

        ! write the file:
        write(iunit,iostat=istat) header,n_plates
        if (istat==0) then
            do i = 1, me%n_plates
                n = real(normal(me%plates(i)%v1,me%plates(i)%v2,me%plates(i)%v3), c_float)
                v1 = real(me%plates(i)%v1*scale, c_float)
                v2 = real(me%plates(i)%v2*scale, c_float)
                v3 = real(me%plates(i)%v3*scale, c_float)
                write(iunit,iostat=istat) n,v1,v2,v3,z
                if (istat/=0) exit
            end do
        end if
        ! close the file:
        close(iunit)

    end if

    end subroutine write_binary_stl_file