File-to-File Translation

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

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
  6. Cleaning up resources

Overview

If no arguments are supplied the example defaults to translating cantilever.unv -> exam7.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.

C
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include "sam/base/base.h"
#include "sam/vis/visdata.h"
#include "sam/vdm/vdm.h"
#include "sam/base/license.h"
#include "sam/hoops_license.h"

int main(int argc, char** argv)
{
    Vchar inputFile[256];
    Vchar outputFile[256];

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

    vsy_LicenseValidate(HOOPS_LICENSE);

Step 2: Create Options and Open the Input File

Create library options, set the numeric convention to double precision (recommended), then create a Library Manager and open the file with those options. The manager 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.

C
/* Open file */
vdm_Options* options = vdm_OptionsBegin();
vdm_OptionsAddConvention(options, VDM_CONVENTION_DOUBLE);

vdm_LMan* libraryManager = vdm_LManBegin();
vdm_LManOpenFile(libraryManager, inputFile, options);

Step 3: Error Checking

Always validate the open operation before proceeding:

C
/* check for error */
Vint ierr = vdm_LManError(libraryManager);
if (ierr) {
    fprintf(stderr, "Error: opening Import file %s\n", inputFile);
    vdm_OptionsEnd(options);
    vdm_LManCloseFile(libraryManager);
    vdm_LManEnd(libraryManager);
    exit(1);
}

Step 4: Translate (Save) the File

Export the contents to the desired output format using vdm_LManSaveFile(). This helper loads the model and save 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.

C
/* exporting the input file data into another file */
vdm_LManSaveFile(libraryManager, outputFile, nullptr);

ierr = vdm_LManError(libraryManager);
if (ierr) {
    fprintf(stderr, "Error: Creating export file %s\n", outputFile);
    vdm_OptionsEnd(options);
    vdm_LManCloseFile(libraryManager);
    vdm_LManEnd(libraryManager);
    exit(1);
}
else {
    printf("Exported %s successfully.\n", outputFile);
}

Step 6: Cleanup

Close the file and release all resources:

C
vdm_OptionsEnd(options);
vdm_LManCloseFile(libraryManager);
vdm_LManEnd(libraryManager);
return 0;
}

Complete Source Code

The complete source (“exam7”) resides at: src/sam/vdm/exam/exam7.cpp

 1#include <stdlib.h>
 2#include <math.h>
 3#include <ctype.h>
 4#include "sam/base/base.h"
 5#include "sam/vis/visdata.h"
 6#include "sam/vdm/vdm.h"
 7#include "sam/base/license.h"
 8#include "sam/hoops_license.h"
 9
10/*-------------------------------------------------------------------------------------
11            Read Universal Result file and Write Nastran bdf input deck
12---------------------------------------------------------------------------------------*/
13int
14main(int argc, char** argv)
15{
16    Vchar inputFile[256];
17    Vchar outputFile[256];
18
19    /* check input arguments */
20    if (argc < 2) {
21        fprintf(stderr, "Usage: %s inputFile outputFile\n", argv[0]);
22        fprintf(stderr, " inputFile  is blank, 'cantilever.unv' is assumed\n");
23        fprintf(stderr, " outputFile is blank, 'exam7.bdf' is assumed\n");
24        strcpy(inputFile, "cantilever.unv");
25        strcpy(outputFile, "exam7.bdf");
26    }
27    else if (argc < 3) {
28        fprintf(stderr, "Usage: %s inputFile outputFile\n", argv[0]);
29        fprintf(stderr, " outputFile is blank, 'exam7.bdf' is assumed\n");
30        strcpy(inputFile, argv[1]);
31        strcpy(outputFile, "exam7.bdf");
32    }
33    else {
34        strcpy(inputFile, argv[1]);
35        strcpy(outputFile, argv[2]);
36    }
37
38    vsy_LicenseValidate(HOOPS_LICENSE);
39
40    /* Open file */
41    vdm_Options* options = vdm_OptionsBegin();
42    vdm_OptionsAddConvention(options, VDM_CONVENTION_DOUBLE);
43
44    vdm_LMan* libraryManager = vdm_LManBegin();
45    vdm_LManOpenFile(libraryManager, inputFile, options);
46
47    /* check for error */
48    Vint ierr = vdm_LManError(libraryManager);
49    if (ierr) {
50        fprintf(stderr, "Error: opening Import file %s\n", inputFile);
51        vdm_OptionsEnd(options);
52        vdm_LManCloseFile(libraryManager);
53        vdm_LManEnd(libraryManager);
54        exit(1);
55    }
56
57    /* exporting the input file data into another file */
58    vdm_LManSaveFile(libraryManager, outputFile, nullptr);
59
60    ierr = vdm_LManError(libraryManager);
61    if (ierr) {
62        fprintf(stderr, "Error: Creating export file %s\n", outputFile);
63        vdm_OptionsEnd(options);
64        vdm_LManCloseFile(libraryManager);
65        vdm_LManEnd(libraryManager);
66        exit(1);
67    }
68    else {
69        printf("Exported %s successfully.\n", outputFile);
70    }
71
72    vdm_OptionsEnd(options);
73    vdm_LManCloseFile(libraryManager);
74    vdm_LManEnd(libraryManager);
75
76    return 0;
77}