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 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 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;
lman.OpenFile(inputFile, nullptr);
/* check for error */
Vint ierr = lman.Error();
if (ierr) {
fprintf(stderr, "Error: opening file %s\n", inputFile);
lman.CloseFile();
exit(1);
}
/* instance Model object for finite element model */
vis_Model model;
lman.LoadModel(&model);
/* get Connect object created in Model */
vis_Connect* connect;
model.GetObject(VIS_CONNECT, (Vobject**)&connect);
/* check for errors */
ierr = model.Error();
if (ierr) {
printf("Error: Unable to get connect object from model\n");
model.Delete();
lman.CloseFile();
exit(1);
}
/* get number of nodes */
Vint numnp;
connect->Number(SYS_NODE, &numnp);
/* print first and last node coordinates */
printf("Node Coordinates\n");
Vdouble coords[3];
/* print first node */
Vint nodeId = 1;
connect->Coordsdv(1, &nodeId, &coords);
printf("%10d %12f %12f %12f\n", 1, coords[0], coords[1], coords[2]);
/* print last node */
connect->Coordsdv(1, &numnp, &coords);
printf("%10d %12f %12f %12f\n", numnp, coords[0], coords[1], coords[2]);
/* clean-up */
model.Delete();
lman.CloseFile();
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