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:
- Overview
- Step 1: Includes and Argument Handling
- Step 2: Create Options and Open the Input File
- Step 3: Error Checking
- Step 4: Translate (Save) the File
- Complete Source Code
We’ll walk through:
- Parsing command line arguments (input / output)
- Creating options (set double-precision convention)
- Opening the input file
- Checking for errors
- 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}
This tutorial demonstrates how to translate (read + export) model data from one
supported format to another using the LMan (Library Manager) interface. A typical use case
is converting a Universal (.unv) model information into a Nastran Bulk Data (.bdf) file.
Quick Navigation:
- Overview
- Step 1: Includes and Argument Handling
- Step 2: Create Options and Open the Input File
- Step 3: Error Checking
- Step 4: Translate (Save) the File
- Step 5: Cleanup
- Complete Source Code
We’ll walk through:
- Parsing command line arguments (input / output)
- Creating options (set double-precision convention)
- Opening the input file
- Checking for errors
- Exporting (saving) the model
- 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.
#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);
The program parses command-line arguments to determine input and output file names, with sensible defaults.
The license is validated using the vsy_LicenseValidate() function.
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.
/* Open file */
vdm_Options* options = vdm_OptionsBegin();
vdm_OptionsAddConvention(options, VDM_CONVENTION_DOUBLE);
vdm_LMan* libraryManager = vdm_LManBegin();
vdm_LManOpenFile(libraryManager, inputFile, options);
The Options object is configured to use double precision for numeric data, which is recommended for accuracy. The Library Manager automatically detects the file format.
Step 3: Error Checking
Always validate the open operation before proceeding:
/* 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);
}
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 vdm_LManSaveFile().
This helper 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.
/* 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);
}
The vdm_LManSaveFile() function handles the translation, automatically detecting the output format
from the file extension.
Step 5: Cleanup
Close the file and release all resources:
vdm_OptionsEnd(options);
vdm_LManCloseFile(libraryManager);
vdm_LManEnd(libraryManager);
return 0;
}
The Library Manager requires cleanup of the options, closing the file, and ending the manager.
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}