File-to-File Translation

This tutorial demonstrates how to translate (read + export) model data from one supported format to another using the DataSource interface. A typical use case is converting a Universal (.unv) model information into a Nastran Bulk Data (.bdf) file.

Quick Navigation:

We’ll walk through:

  1. Parsing command line arguments (input / output)
  2. Creating options (set double-precision convention)
  3. Opening the input file
  4. Checking for errors
  5. Exporting (saving) the model

Overview

If no arguments are supplied the example defaults to translating cantilever.unv -> tutorial_file_translation.bdf. With one argument it assumes the second. With two arguments both are used.

Step 1: Includes and Argument Handling

We begin by including the required headers and collecting the input / output file names.

#include "samcpp/core/core.h"
#include "samcpp/access/access.h"
#include "sam/hoops_license.h"
#include <iostream>

/*-------------------------------------------------------------------------------------
            Translate one file format to another file format
---------------------------------------------------------------------------------------*/
int
main(int argc, char** argv)
{
    char inputFile[cae::core::MAX_NAME_LENGTH] = {};
    char outputFile[cae::core::MAX_NAME_LENGTH] = {};

    /* check input arguments */
    if (argc < 2) {
        std::cerr << "Usage: " << argv[0] << " inputFile outputFile\n";
        std::cerr << " inputFile  is blank, 'cantilever.unv' is assumed\n";
        std::cerr << " outputFile is blank, 'tutorial_file_translation.bdf' is assumed\n";
        strcpy(inputFile, "cantilever.unv");
        strcpy(outputFile, "exam7.bdf");
    }
    else if (argc < 3) {
        std::cerr << "Usage: " << argv[0] << " inputFile outputFile\n";
        std::cerr << " outputFile is blank, 'tutorial_file_translation.bdf' is assumed\n";
        strcpy(inputFile, argv[1]);
        strcpy(outputFile, "tutorial_file_translation.bdf");
    }
    else {
        strcpy(inputFile, argv[1]);
        strcpy(outputFile, argv[2]);
    }

    cae::core::license::validate(HOOPS_LICENSE);

The program parses command-line arguments to determine input and output file names, with sensible defaults. The license is validated using the validate() function.

Step 2: Create Options and Open the Input File

Create options, set the numeric convention to double precision (recommended), then open the file with those options. The DataSource auto-detects the file type. Opening a file does not load the model into memory; it prepares the file for subsequent operations for the given format.

// Open input file and enable double precision convention
cae::access::Options options;
options.enableConvention(cae::access::Convention::DOUBLE);

cae::access::DataSource dataSource;
cae::core::Status status = dataSource.openFile(inputFile, &options);

The Options object is configured to use double precision for numeric data using enableConvention(), which is recommended for accuracy. The DataSource automatically detects the file format when using openFile().

Step 3: Error Checking

Always validate the open operation before proceeding:

/* check for error */
if (!status) {
    std::cerr << "Error: opening Import file " << inputFile << '\n';
    exit(1);
}

Proper error checking ensures that the file was opened successfully before attempting to save it.

Step 4: Translate (Save) the File

Export the contents to the desired output format using saveFile(). This method loads the model and saves it to the specified output file. If the input file contains results, then it will also save the results in the output file based on the format.

// Export to output file
status = dataSource.saveFile(outputFile, nullptr);
if (!status) {
    std::cerr << "Error: Creating export file " << outputFile << '\n';
    exit(1);
}

std::cout << "Exported " << outputFile << " successfully.\n";

return 0;

The saveFile() method handles the translation, automatically detecting the output format from the file extension.

Complete Source Code

The complete source code for this example can be found at: src/sam/vdm/exam/tutorial_file_translation.cpp

You can also view the full source here:

 1#include "samcpp/core/core.h"
 2#include "samcpp/access/access.h"
 3#include "sam/hoops_license.h"
 4#include <iostream>
 5
 6/*-------------------------------------------------------------------------------------
 7            Translate one file format to another file format
 8---------------------------------------------------------------------------------------*/
 9int
10main(int argc, char** argv)
11{
12    char inputFile[cae::core::MAX_NAME_LENGTH] = {};
13    char outputFile[cae::core::MAX_NAME_LENGTH] = {};
14
15    /* check input arguments */
16    if (argc < 2) {
17        std::cerr << "Usage: " << argv[0] << " inputFile outputFile\n";
18        std::cerr << " inputFile  is blank, 'cantilever.unv' is assumed\n";
19        std::cerr << " outputFile is blank, 'tutorial_file_translation.bdf' is assumed\n";
20        strcpy(inputFile, "cantilever.unv");
21        strcpy(outputFile, "tutorial_file_translation.bdf");
22    }
23    else if (argc < 3) {
24        std::cerr << "Usage: " << argv[0] << " inputFile outputFile\n";
25        std::cerr << " outputFile is blank, 'tutorial_file_translation.bdf' is assumed\n";
26        strcpy(inputFile, argv[1]);
27        strcpy(outputFile, "tutorial_file_translation.bdf");
28    }
29    else {
30        strcpy(inputFile, argv[1]);
31        strcpy(outputFile, argv[2]);
32    }
33
34    cae::core::license::validate(HOOPS_LICENSE);
35
36    // Open input file and enable double precision convention
37    cae::access::Options options;
38    options.enableConvention(cae::access::Options::Convention::DOUBLE);
39
40    cae::access::DataSource dataSource;
41    cae::core::Status status = dataSource.openFile(inputFile, &options);
42    /* check for error */
43    if (!status) {
44        std::cerr << "Error: opening Import file " << inputFile << '\n';
45        exit(1);
46    }
47
48    // Export to output file
49    status = dataSource.saveFile(outputFile, nullptr);
50    if (!status) {
51        std::cerr << "Error: Creating export file " << outputFile << '\n';
52        exit(1);
53    }
54
55    std::cout << "Exported " << outputFile << " successfully.\n";
56
57    return 0;
58}