1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/**
 * MOAB, a Mesh-Oriented datABase, is a software component for creating,
 * storing and accessing finite element mesh data.
 *
 * Copyright 2004 Sandia Corporation.  Under the terms of Contract
 * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
 * retains certain rights in this software.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 */

/**
 * \class WriteSTL
 * \brief ASCII and Binary Stereo Lithography File writers.
 * \author Jason Kraftcheck
 */

#include "WriteSTL.hpp"
#include "moab/CN.hpp"
#include "moab/Interface.hpp"
#include "moab/Range.hpp"
#include "moab/WriteUtilIface.hpp"
#include "moab/FileOptions.hpp"
#include "SysUtil.hpp"

#include <cstdio>
#include <sys/types.h>
#include <sys/stat.h>
#include <cerrno>
#include <cmath>
#include <fcntl.h>
#include <climits>

namespace moab
{

#if defined( _MSC_VER ) || defined( __MINGW32__ ) /* windows */
#include <io.h>
#ifndef __MINGW32__
typedef unsigned __int32 uint32_t;
#endif
#else /* posix */
#include <unistd.h>
#define _S_IREAD  ( S_IRUSR | S_IRGRP | S_IROTH )
#define _S_IWRITE ( S_IWUSR | S_IWGRP | S_IWOTH )
#endif

const int DEFAULT_PRECISION = 6;

WriterIface* WriteSTL::factory( Interface* iface )
{
    return new WriteSTL( iface );
}

WriteSTL::WriteSTL( Interface* impl ) : mbImpl( impl )
{
    impl->query_interface( mWriteIface );
}

WriteSTL::~WriteSTL()
{
    mbImpl->release_interface( mWriteIface );
}

ErrorCode WriteSTL::write_file( const char* file_name,
                                const bool overwrite,
                                const FileOptions& opts,
                                const EntityHandle* ent_handles,
                                const int num_sets,
                                const std::vector< std::string >& qa_list,
                                const Tag* tag_list,
                                int num_tags,
                                int /* export_dimension */ )
{
    char header[81];
    Range triangles;
    ErrorCode rval;

    if( tag_list && num_tags )
    {
        MB_SET_ERR( MB_TYPE_OUT_OF_RANGE, "STL file does not support tag data" );
    }

    rval = make_header( header, qa_list );
    if( MB_SUCCESS != rval ) return rval;

    rval = get_triangles( ent_handles, num_sets, triangles );
    if( MB_SUCCESS != rval ) return rval;

    if( triangles.empty() )
    {
        MB_SET_ERR( MB_ENTITY_NOT_FOUND, "No triangles to write" );
    }

    bool is_ascii = false, is_binary = false;
    if( MB_SUCCESS == opts.get_null_option( "ASCII" ) ) is_ascii = true;
    if( MB_SUCCESS == opts.get_null_option( "BINARY" ) ) is_binary = true;
    if( is_ascii && is_binary )
    {
        MB_SET_ERR( MB_FAILURE, "Conflicting options: BINARY ASCII" );
    }

    bool big_endian = false, little_endian = false;
    if( MB_SUCCESS == opts.get_null_option( "BIG_ENDIAN" ) ) big_endian = true;
    if( MB_SUCCESS == opts.get_null_option( "LITTLE_ENDIAN" ) ) little_endian = true;
    if( big_endian && little_endian )
    {
        MB_SET_ERR( MB_FAILURE, "Conflicting options: BIG_ENDIAN LITTLE_ENDIAN" );
    }
    ByteOrder byte_order = big_endian ? STL_BIG_ENDIAN : little_endian ? STL_LITTLE_ENDIAN : STL_UNKNOWN_BYTE_ORDER;

    FILE* file = open_file( file_name, overwrite, is_binary );
    if( !file ) return MB_FILE_DOES_NOT_EXIST;

    if( is_binary )
        rval = binary_write_triangles( file, header, byte_order, triangles );
    else
    {
        // Get precision for node coordinates
        int precision;
        if( MB_SUCCESS != opts.get_int_option( "PRECISION", precision ) ) precision = DEFAULT_PRECISION;

        rval = ascii_write_triangles( file, header, triangles, precision );
    }

    fclose( file );
    return rval;
}

FILE* WriteSTL::open_file( const char* name, bool overwrite, bool binary )
{
    // Open file with write access, and create it if it doesn't exist.
    int flags = O_WRONLY | O_CREAT;
    // Select behavior if the named file already exists. If
    // overwrite is true, truncate the file. If it is false,
    // make the call to open() fail.
    if( overwrite )
        flags |= O_TRUNC;
    else
        flags |= O_EXCL;
        // If platform defines a "binary" bit in the file access
        // flags (i.e. we're building on windows), then set it
        // if we're writing a binary file.
#ifdef O_BINARY
    if( binary ) flags |= O_BINARY;<--- Skipping configuration 'O_BINARY' since the value of 'O_BINARY' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
#endif

    // Give everyone read and write, but not execute, permission.
    // These are not the final permissions for the file. Permissions
    // are removed according to the user's umask. All we want to
    // say here is that the executable bits should not be set because
    // this isn't an executable file. Everything else is a user
    // preference and should be left up to the umask.
    int creat_mode = _S_IREAD | _S_IWRITE;

    // Open the file.
    int fd = open( name, flags, creat_mode );
    if( fd < 0 )
    {
        MB_SET_ERR_RET_VAL( name << ": " << strerror( errno ), NULL );
    }
    FILE* result = fdopen( fd, binary ? "wb" : "w" );
    if( !result ) close( fd );

    return result;
}

ErrorCode WriteSTL::make_header( char header[81], const std::vector< std::string >& qa_list )
{
    memset( header, 0, 81 );

    std::string result;
    for( std::vector< std::string >::const_iterator i = qa_list.begin(); i != qa_list.end(); ++i )
    {
        result += " ";
        result += *i;
    }

    size_t len = result.size();
    if( len > 80 ) len = 80;
    memcpy( header, result.c_str(), len );

    return MB_SUCCESS;
}

ErrorCode WriteSTL::get_triangles( const EntityHandle* set_array, int set_array_length, Range& triangles )
{
    if( !set_array || 0 == set_array_length ) return mbImpl->get_entities_by_type( 0, MBTRI, triangles );

    const EntityHandle* iter = set_array;
    const EntityHandle* end  = iter + set_array_length;
    for( ; iter != end; ++iter )
    {
        Range r;
        ErrorCode rval = mbImpl->get_entities_by_type( *iter, MBTRI, r, true );
        if( MB_SUCCESS != rval ) return rval;
        triangles.merge( r );
    }

    return MB_SUCCESS;
}

ErrorCode WriteSTL::get_triangle_data( const double coords[9], float v1[3], float v2[3], float v3[3], float n[3] )
{
    CartVect cv1, cv2, cv3, cn;
    ErrorCode rval = get_triangle_data( coords, cv1, cv2, cv3, cn );
    if( MB_SUCCESS != rval ) return rval;

    cv1.get( v1 );
    cv2.get( v2 );
    cv3.get( v3 );
    cn.get( n );

    return MB_SUCCESS;
}

ErrorCode WriteSTL::get_triangle_data( const double coords[9], CartVect& v1, CartVect& v2, CartVect& v3, CartVect& n )
{
    v1 = coords;
    v2 = coords + 3;
    v3 = coords + 6;

    n = ( v2 - v1 ) * ( v3 - v1 );

    n.normalize();

    return MB_SUCCESS;
}

ErrorCode WriteSTL::ascii_write_triangles( FILE* file, const char header[81], const Range& triangles, int prec )
{
    const char solid_name[] = "MOAB";

    char myheader[81] = "solid ";
    strcat( myheader, solid_name );
    strncat( myheader, header, 80 );

    if( EOF == fputs( myheader, file ) || EOF == fputs( "\n", file ) ) return MB_FILE_WRITE_ERROR;

    ErrorCode rval;
    double coords[9];
    CartVect v1, v2, v3, n;
    for( Range::const_iterator iter = triangles.begin(); iter != triangles.end(); ++iter )
    {
        const EntityHandle* conn;
        int num_vtx;

        rval = mbImpl->get_connectivity( *iter, conn, num_vtx );
        if( MB_SUCCESS != rval ) return rval;
        if( num_vtx != 3 ) return MB_FAILURE;

        rval = mbImpl->get_coords( conn, 3, coords );
        if( MB_SUCCESS != rval ) return rval;

        rval = get_triangle_data( coords, v1, v2, v3, n );
        if( MB_SUCCESS != rval ) return rval;

        fprintf( file, "facet normal %e %e %e\n", n[0], n[1], n[2] );
        fprintf( file, "outer loop\n" );
        fprintf( file, "vertex %.*e %.*e %.*e\n", prec, (float)v1[0], prec, (float)v1[1], prec, (float)v1[2] );
        fprintf( file, "vertex %.*e %.*e %.*e\n", prec, (float)v2[0], prec, (float)v2[1], prec, (float)v2[2] );
        fprintf( file, "vertex %.*e %.*e %.*e\n", prec, (float)v3[0], prec, (float)v3[1], prec, (float)v3[2] );
        fprintf( file, "endloop\n" );
        fprintf( file, "endfacet\n" );
    }

    fprintf( file, "endsolid %s\n", solid_name );
    return MB_SUCCESS;
}

struct BinTri
{
    float normal[3];
    float vertex1[3];
    float vertex2[3];
    float vertex3[3];
    char pad[2];
};

ErrorCode WriteSTL::binary_write_triangles( FILE* file,
                                            const char header[81],
                                            ByteOrder byte_order,
                                            const Range& triangles )
{
    ErrorCode rval;
    if( 1 != fwrite( header, 80, 1, file ) ) return MB_FILE_WRITE_ERROR;

    // Default to little endian if byte_order == UNKNOWN_BYTE_ORDER
    const bool want_big_endian = ( byte_order == STL_BIG_ENDIAN );
    const bool am_big_endian   = !SysUtil::little_endian();
    const bool swap_bytes      = ( want_big_endian == am_big_endian );

    if( triangles.size() > INT_MAX )  // Can't write that many triangles
        return MB_FAILURE;

    uint32_t count = (uint32_t)triangles.size();
    if( swap_bytes ) SysUtil::byteswap( &count, 1 );
    if( 1 != fwrite( &count, 4, 1, file ) ) return MB_FILE_WRITE_ERROR;

    double coords[9];
    BinTri tri;
    tri.pad[0] = tri.pad[1] = '\0';
    for( Range::const_iterator iter = triangles.begin(); iter != triangles.end(); ++iter )
    {
        const EntityHandle* conn;
        int num_vtx;

        rval = mbImpl->get_connectivity( *iter, conn, num_vtx );
        if( MB_SUCCESS != rval ) return rval;
        if( num_vtx != 3 ) return MB_FAILURE;

        rval = mbImpl->get_coords( conn, 3, coords );
        if( MB_SUCCESS != rval ) return rval;

        rval = get_triangle_data( coords, tri.vertex1, tri.vertex2, tri.vertex3, tri.normal );
        if( MB_SUCCESS != rval ) return rval;

        if( swap_bytes )
        {
            SysUtil::byteswap( tri.normal, 3 );
            SysUtil::byteswap( tri.vertex1, 3 );
            SysUtil::byteswap( tri.vertex2, 3 );
            SysUtil::byteswap( tri.vertex3, 3 );
        }

        if( 1 != fwrite( &tri, 50, 1, file ) ) return MB_FILE_WRITE_ERROR;
    }

    return MB_SUCCESS;
}

}  // namespace moab