写入二进制文件

时间:2015-12-07 22:47:44

标签: fortran binaryfiles stl-fileformat

我正在尝试在Fortran 90中编写STL二进制文件。该文件具有以下格式

  

HEADER:一个80字节的ASCII标题 - TITLE

     

4字节无符号长整数,NO。 OF FACETS

     

每个格式的格式:

     

法向量,3个浮点值,每个4字节;

     

Vertex 1 XYZ坐标,3个浮动值,每个4字节;

     

Vertex 2 XYZ坐标,3个浮动值,每个4字节;

     

Vertex 3 XYZ坐标,3个浮动值,每个4字节;

     

2个字节的无符号整数,应为零;

我正在尝试创建一个未格式化的文件来写入相关信息,但是在定义正确的记录长度时遇到了问题。假设我有N个方面,我使用以下命令来打开和写入信息

open(unit = 1, status = 'replace', iostat = ioerror, format = 'unformatted', access = 'direct', recl = 84 + N * 50, file = 'c:\temp\test.stl')

我可以发出第一个写语句来写出标题信息,然后写第二个写语句(在do循环中)来写出构面信息吗?

如果是这样,那么每个写语句需要的记录号是什么,因为我有标题和具有不同记录长度的构面信息。

write(1,rec=?), *header information*
do,i=1,N,1
   write(1,rec=?), *facet information*
enddo

1 个答案:

答案 0 :(得分:2)

这很有趣。我已经破解了一个小程序,它使用STREAM访问创建了一个非常简单的金字塔。它似乎有效:

program write_stl
    use ISO_FORTRAN_ENV
    implicit none
    integer, parameter :: u = 400
    character(len=*), parameter :: fname = 'pyramid.stl'
    integer :: ios
    integer(kind=4) :: num_facets
    character(len=80) :: title
    real(kind=4), dimension(3) :: top_vertex, front_vertex, left_vertex, right_vertex

    top_vertex = (/0.0, 0.0, 2.0/)
    front_vertex = (/0.0, -1.0, 0.0/)
    left_vertex = (/-1.0, 1.0, 0.0/)
    right_vertex = (/1.0, 1.0, 0.0/)

    open(unit=u, file=fname, access='stream', status='replace', &
        action='write', iostat=ios)
    call check(ios, 'open')

    title = "Testpyramid"
    write(u, iostat=ios) title
    call check(ios, 'write title')
    num_facets = 4
    write(u, iostat=ios) num_facets
    call check(ios, 'write number of facets')
    ! bottom facet
    call write_facet(u, front_vertex, left_vertex, right_vertex)
    call write_facet(u, top_vertex, right_vertex, left_vertex)
    call write_facet(u, top_vertex, left_vertex, front_vertex)
    call write_facet(u, top_vertex, front_vertex, right_vertex)

    close(u, iostat=ios)
    call check(ios, 'close')

contains

    subroutine check(ios, operation)
        implicit none
        integer, intent(in) :: ios
        character(len=*), intent(in) :: operation
        if (ios == 0) return
        write(*, '(A, I0, 2A)') "Encountered error ", ios, " while performing ", operation
        stop 1
    end subroutine check

    subroutine write_facet(u, vertex1, vertex2, vertex3)
        implicit none
        integer, intent(in) :: u
        real(kind=4), dimension(3), intent(in) :: vertex1, vertex2, vertex3
        real(kind=4), dimension(3) :: normal
        integer(kind=2), parameter :: zero = 0

        normal = calc_normal(vertex1, vertex2, vertex3)
        write(u, iostat=ios) normal
        call check(ios, 'write normal')
        write(u, iostat=ios) vertex1
        call check(ios, 'write vertex')
        write(u, iostat=ios) vertex2
        call check(ios, 'write vertex')
        write(u, iostat=ios) vertex3
        call check(ios, 'write vertex')
        write(u, iostat=ios) zero
        call check(ios, 'write zero')
    end subroutine write_facet

    function calc_normal(vec1, vec2, vec3)
        implicit none
        real(kind=4), dimension(3), intent(in) :: vec1, vec2, vec3
        real(kind=4), dimension(3) :: calc_normal
        real(kind=4), dimension(3) :: d1, d2
        d1 = vec2 - vec1
        d2 = vec3 - vec1
        calc_normal(1) = d1(2) * d2(3) - d1(3) * d2(2)
        calc_normal(2) = d1(3) * d2(1) - d1(1) * d2(3)
        calc_normal(3) = d1(1) * d2(2) - d1(2) * d2(1)
        calc_normal = calc_normal / norm(calc_normal)
    end function calc_normal

    function norm(vec)
        implicit none
        real(kind=4), dimension(3), intent(in) :: vec
        real(kind=4) :: norm

        norm = sqrt(vec(1)**2 + vec(2)**2 + vec(3)**2)
    end function norm

end program write_stl