Append data to VTFx

This example opens an existing VTFx file in append mode and adds a new state. It also adds scalar result values for this new state and updates the existing case properties to include this new state in the animation.

The initial VTFx file has three states. The VTFx created in this example will also contain a fourth state and scalar results for this state. The first three states (with their results) are identical to the initial VTFx file.

// Create the VTFx file
// -------------------------------------------------------------------------
cee::Str fileName = "ExampleAppend2.vtfx";
// Make copy original file
std::ifstream src("../../DemoFiles/SimpleExample.vtfx", std::ios::binary);
if (!src.good())
{
return EXIT_FAILURE;
}
std::ofstream dst(fileName.toStdString().c_str(), std::ios::binary);
dst << src.rdbuf();
src.close();
dst.close();
if (!file->openForAppend(fileName))
{
return EXIT_FAILURE;
}
// Get the first database from file
// Add extra state
{
// Get state info block
cee::vtfx::StateInfoBlock* stateInfoBlock = dynamic_cast<cee::vtfx::StateInfoBlock*>(block.get());
if (!stateInfoBlock->addStateInfo(4, "Fourth state", 4.0f, cee::vtfx::StateInfoBlock::TIME))
{
return EXIT_FAILURE;
}
if (!db->writeBlock(stateInfoBlock))
{
return EXIT_FAILURE;
}
}
// Add extra results for fourth state
// Time step 4 - Scalar Result 1
{
// Part 1 (the cube)
{
// Result values for step 4
{
if (!resultValuesBlockPart1Step4->setMapToBlockId(1, cee::vtfx::Block::NODES))
{
return EXIT_FAILURE;
}
const float RESULT_VALUES[] = { 0.0f, 0.0f, 1.5f, 2.0f, 2.0f, 2.0f, 3.0f, 4.0f };
std::vector<float> resultValues(RESULT_VALUES, RESULT_VALUES + sizeof(RESULT_VALUES) / sizeof(RESULT_VALUES[0]));
if (!resultValuesBlockPart1Step4->setResultValues(resultValues))
{
return EXIT_FAILURE;
}
if (!db->writeBlock(resultValuesBlockPart1Step4.get()))
{
return EXIT_FAILURE;
}
}
}
// Part 2 (the pyramid)
{
// Result values for step 4
{
if (!resultValuesBlockPart2Step4->setMapToBlockId(2, cee::vtfx::Block::NODES))
{
return EXIT_FAILURE;
}
const float RESULT_VALUES[] = { 0.0f, 0.0f, 0.5f, 0.5f, 4.0f };
std::vector<float> resultValues(RESULT_VALUES, RESULT_VALUES + sizeof(RESULT_VALUES) / sizeof(RESULT_VALUES[0]));
if (!resultValuesBlockPart2Step4->setResultValues(resultValues))
{
return EXIT_FAILURE;
}
if (!db->writeBlock(resultValuesBlockPart2Step4.get()))
{
return EXIT_FAILURE;
}
}
}
// Get result block
cee::PtrRef<cee::vtfx::Block> block = db->blockById(cee::vtfx::Block::RESULT, 1); // Result block id = 1
cee::vtfx::ResultBlock* resultBlock = dynamic_cast<cee::vtfx::ResultBlock*>(block.get());
// Add results for step 4 (cube)
if (!resultBlock->addResultValuesBlock(14, 4))
{
return EXIT_FAILURE;
}
// Add results for step 4 (pyramid)
if (!resultBlock->addResultValuesBlock(24, 4))
{
return EXIT_FAILURE;
}
if (!db->writeBlock(resultBlock))
{
return EXIT_FAILURE;
}
}
// Write properties
// -------------------------------------------------------------------------
{
// Show result with id = 1 as fringes
cee::PtrRef<cee::PropertySet> scalarSelection = new cee::PropertySet("result_selection");
scalarSelection->setValue("fringes_result_id", 1);
vtfxProps->addPropertySet(scalarSelection.get());
// Set animation to run over all states
cee::PtrRef<cee::PropertySet> stateSelection = new cee::PropertySet("state_selection");
const cee::Variant STATE_IDS[] = { 1, 2, 3, 4};
std::vector<cee::Variant> stateIds(STATE_IDS, STATE_IDS + sizeof(STATE_IDS) / sizeof(STATE_IDS[0]));
stateSelection->setValue("state_ids", stateIds);
vtfxProps->addPropertySet(stateSelection.get());
// Start animation when opening file. Set frames per second to 3.0.
viewer->setValue("animation_fps", 3.0);
viewer->setValue("start_animation", true);
vtfxProps->addPropertySet(viewer.get());
// Set the camera to look at the model from negative y axis
camera->setValue("eye", cee::Vec3d(2.0, -7.5, 0.5));
camera->setValue("vrp", cee::Vec3d(2.0, -5.5, 0.5));
camera->setValue("vup", cee::Vec3d(0.0, 0.0, 1.0));
vtfxProps->addPropertySet(camera.get());
// Write the block
if (!file->fileCase(0)->setProperties(vtfxProps.get()))
{
return EXIT_FAILURE;
}
}
// Finally close the file
// -------------------------------------------------------------------------
if (!file->close())
{
return EXIT_FAILURE;
}
std::cout << "Exported successfully to file: " << fileName.toStdString() << std::endl;
std::cout << std::endl << "Press enter to exit..." << std::endl;
std::cin.ignore();
return EXIT_SUCCESS;