A First Program
As an example of a simple HOOPS Access application the following program illustrates reading the node coordinates from an SDRC Universal file. The program uses the DataSource interface to open and read the file.
Once the file is opened successfully using openFile(),
we load the finite element model data into a Model object using
loadModel(). The Model object
encapsulates all objects which define an entire finite element model, including
the mesh data. We then extract the Mesh object from the
model using getMesh(), which manages all data related to the finite element mesh, particularly
the node coordinates.
After obtaining the Mesh object, we query the total number of nodes in the
model using getEntityCount() and print the coordinates of the first and last nodes using
getCoordinates(). This demonstrates
basic mesh traversal and coordinate access functionality.
The following program is a listing of the “A First Program” in C++ language bindings.
#include "samcpp/core/core.h"
#include "samcpp/access/access.h"
#include "sam/hoops_license.h"
#include <iostream>
#include <iomanip>
/*----------------------------------------------------------------------
Read and Print Finite Element Node Coordinates
----------------------------------------------------------------------*/
int
main(int argc, char** argv)
{
char inputFile[cae::core::MAX_NAME_LENGTH] = {};
// Check input arguments
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " inputfile\n";
std::cerr << " inputfile is blank, 'bumper.unv' is assumed\n";
strcpy(inputFile, "bumper.unv");
}
else {
strcpy(inputFile, argv[1]);
}
cae::core::license::validate(HOOPS_LICENSE);
// Open file
cae::access::DataSource dataSource;
cae::core::Status status = dataSource.openFile(inputFile, nullptr);
// Check for error
if (!status) {
std::cerr << "Error: opening file " << inputFile << '\n';
exit(1);
}
// Load Model object with finite element model
cae::core::Model model;
dataSource.loadModel(&model);
// Get Mesh object created in Model
cae::core::MeshPtr mesh;
status = model.getMesh(mesh);
// Check for errors
if (!status) {
std::cerr << "Error: Unable to get connect object from model\n";
exit(1);
}
// Get number of nodes
int nodeCount = 0;
mesh->getEntityCount(cae::core::EntityType::NODE, &nodeCount);
// Print first and last node coordinates
std::cout << "Node Coordinates\n";
double coordinates[1][3] = {{0.0, 0.0, 0.0}};
std::cout << std::fixed << std::setprecision(6);
// Print first node
int nodeId = 1;
mesh->getCoordinates(1, &nodeId, coordinates);
std::cout << std::setw(10) << 1 << ' ' << std::setw(12) << coordinates[0][0] << ' ' << std::setw(12) << coordinates[0][1]
<< ' ' << std::setw(12) << coordinates[0][2] << '\n';
// Print last node
mesh->getCoordinates(1, &nodeCount, coordinates);
std::cout << std::setw(10) << nodeCount << ' ' << std::setw(12) << coordinates[0][0] << ' ' << std::setw(12)
<< coordinates[0][1] << ' ' << std::setw(12) << coordinates[0][2] << '\n';
return 0;
}
The output of this example program appears below.
Node Coordinates
1 30.826799 3.968360 -1.003940
652 -47.007900 15.984300 -2.007870
As an example of a simple HOOPS Access application the following program illustrates reading the node coordinates from an SDRC Universal file. The program uses the LMan (Library Manager) interface to open and read the file.
Once the file is opened successfully, we load the finite element model data into a Model object using the Library Manager. The Model object encapsulates all objects which define an entire finite element model, including the mesh data. We then extract the Connect object from the model, which manages all data related to the finite element mesh, particularly the node coordinates.
After obtaining the Connect object, we query the total number of nodes in the model and print the coordinates of the first and last nodes. This demonstrates basic mesh traversal and coordinate access functionality.
Finally, we properly clean up all allocated resources by deleting the model objects and closing the Library Manager interface.
The following program is a listing of the “A First Program” in C language bindings.
#include <stdio.h>
#include <stdlib.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"
/*----------------------------------------------------------------------
Read and Print Finite Element Node Coordinates
----------------------------------------------------------------------*/
int
main(int argc, char** argv)
{
char inputFile[256];
/* check input arguments */
if (argc < 2) {
fprintf(stderr, "Usage: %s inputfile\n", argv[0]);
fprintf(stderr, " inputfile is blank, 'bumper.unv' is assumed\n");
strcpy(inputFile, "bumper.unv");
}
else {
strcpy(inputFile, argv[1]);
}
vsy_LicenseValidate(HOOPS_LICENSE);
/* Open file using Library Manager */
vdm_LMan* lman = vdm_LManBegin();
vdm_LManOpenFile(lman, inputFile, nullptr);
/* check for error */
Vint ierr = vdm_LManError(lman);
if (ierr) {
fprintf(stderr, "Error: opening file %s\n", inputFile);
vdm_LManCloseFile(lman);
vdm_LManEnd(lman);
exit(1);
}
/* instance Model object for finite element model */
vis_Model* model = vis_ModelBegin();
vdm_LManLoadModel(lman, model);
/* get Connect object created in Model */
vis_Connect* connect;
vis_ModelGetObject(model, VIS_CONNECT, (Vobject**)&connect);
/* check for errors */
ierr = vis_ModelError(model);
if (ierr) {
printf("Error: Unable to get connect object from model\n");
vis_ModelDelete(model);
vis_ModelEnd(model);
vdm_LManCloseFile(lman);
vdm_LManEnd(lman);
exit(1);
}
/* get number of nodes */
Vint numnp;
vis_ConnectNumber(connect, SYS_NODE, &numnp);
/* print first and last node coordinates */
printf("Node Coordinates\n");
Vdouble coords[3];
/* print first node */
Vint nodeId = 1;
vis_ConnectCoordsdv(connect, 1, &nodeId, &coords);
printf("%10d %12f %12f %12f\n", 1, coords[0], coords[1], coords[2]);
/* print last node */
vis_ConnectCoordsdv(connect, 1, &numnp, &coords);
printf("%10d %12f %12f %12f\n", numnp, coords[0], coords[1], coords[2]);
/* clean-up */
vis_ModelDelete(model);
vis_ModelEnd(model);
vdm_LManCloseFile(lman);
vdm_LManEnd(lman);
return 0;
}
The output of this example program appears below.
Node Coordinates
1 30.826799 3.968360 -1.003940
652 -47.007900 15.984300 -2.007870