The Importing/Exporting Scenarios from Different File Formats example demonstrates the following:
- How to add a scenario importer/exporter to the back-end
- How to import a scenario file of comma delimited format
- How to export a scenario into a comma delimited format
- How to write a front-end example that works in conjuction with a back-end example
The Importing/Exporting scenarios example installs a new scenario importer/exporter that will read a comma delimited file and restore entity/aggregate laydown as well as tactical graphic objects.
Since this example is loaded as a plugin, it can be used in conjunction with the released VR-Forces application. Make sure to enable the csvScenarioImporterExporter example from the launcher application
Adding a New Scenario Importer/Exporter
To create a new scenario importer/exporter it is necessary to inherit from the DtScenarioDescriptionImporterExporter class.
The new importer/exporter is registered using the extension it supports.
The supportedExtensions() Function
The supportedExtensions () method is used to define which file extensions are supported by the importer/exporter.
Exporting Scenarios
Exporting a scenario consists of two parts: first getting the objects from the simulation and then writing them out to disk.
DtExCSVScenarioDescriptionImporterExporter handles writing out the simulation data to disk through the saveScenarioDescription method:
{
std::ofstream ifs(file.c_str());
if(!ifs.is_open())
{
myError = std::string(
"Could not open file for writing ") + std::string(file.c_str());
return false;
}
ifs.precision(9);
ifs << "NAME,ENUMERATION,UUID,FORCE,SUPERIOR OR OVERLAY,POINTS" << std::endl;
{
ifs << iter->second->name.c_str() << ",";
ifs << iter->second->type.string() << ",";
ifs << iter->second->uuid.uuidString() << ",";
ifs << iter->second->force << ",";
ifs << iter->second->parent << ",";
std::vector<DtVector>::const_iterator pointIter = iter->second->points.begin();
while (pointIter != iter->second->points.end())
{
DtGeodeticCoord geod;
geod.setGeocentric(*pointIter);
ifs << DtRad2Deg(geod.lat()) << ",";
ifs << DtRad2Deg(geod.lon()) << ",";
ifs << geod.alt();
++pointIter;
if (pointIter != iter->second->points.end())
{
ifs << ",";
}
}
ifs << std::endl;
++iter;
}
return true;
}
It obtains the simulation data from the importFromSimulation method that is implemented by the subclass DtVrfCSVScenarioDescriptionImporterExporter
Importing Scenarios
Like exporting scenarios, importing scenarios is a two step process: first is loading the scenario data from disk and second is instantiating that data into the simulation.
DtExCSVScenarioDescriptionImporterExporter implements the method loadScenarioDescription to read in the simulation data stored in the CSV file.
const std::map<std::string, std::string>& importData)
{
std::ifstream ifs(file.c_str());
if(!ifs.is_open())
{
myError = std::string(
"Could not open file for reading ") + std::string(file.c_str());
return false;
}
std::string s;
getline(ifs, s);
while (!ifs.eof())
{
getline(ifs, s);
if (s.length())
{
std::vector<std::string> results;
boost::algorithm::split(results, s, boost::algorithm::is_any_of(","));
information->
name = results[0].c_str();
information->
type = DtEntityType(results[1].c_str());
information->uuid =
DtUUID(results[2].c_str());
information->
force = atoi(results[3].c_str());
information->
parent = results[4].c_str();
int lastFixedColumn = 5;
int size = results.size();
int i;
for (i = size - 1; i > -1; --i)
{
{
break;
}
}
int lastCell = i;
int numPoints = ((lastCell + 1) - 4) / 3;
for (i = 0; i < numPoints; i++)
{
DtGeodeticCoord geod;
geod.setLat(DtDeg2Rad(atof(results[i * 3 + lastFixedColumn].c_str())));
geod.setLon(DtDeg2Rad(atof(results[i * 3 + lastFixedColumn + 1].c_str())));
geod.setAlt(atof(results[i * 3 + lastFixedColumn + 2].c_str()));
information->
points.push_back(geod.geocentric());
}
WaitingMap::iterator waitingIter =
myToBeParented.find(information->uuid);
{
information->children = waitingIter->second;
}
if (information->
type.kind() < 16)
{
if (parentUUID.isValid())
{
ObjectMap::const_iterator superiorIter =
myObjectMap.find(parentUUID);
{
superiorIter->second->children.push_back(information);
}
else
{
}
}
}
}
}
return true;
}
The subclass DtVrfCSVScenarioDescriptionImporterExporter converts the data read in from disk to simulation data through the importIntoSimulation method.
{
{
if (!iter->second->parent.length() || (iter->second->type.kind() >= 16))
{
}
++iter;
}
{
}
return true;
}
How to Run the Example
Usage
In the Launcher, click the Plug-ins button to bring up the Plug-ins Selection dialog. Enable the plugin.
To view the new behavior:
-
Start VR-Forces GUI and SIM.
-
Load the BombMission.scnx scenario file.
-
Click on File → Export Scenario Laydown...
-
Enter a filename and select “CSV Scenario File (*.csv *.txt)” as the Save as type and press the Save button.
-
Click on File → Close Scenario and the entities and tactical overlays should disappear
-
Click on File → Import Scenario Objects... and select “CSV Scenario File (*.csv *.txt)” as the Files of type option
-
If the File -> Import Scenario Objects menu option appears to be disabled. Create a new blank scenario using the same (Ala Moana) terrain. The Import Scenario Objects menu option should be enabled to import the previously exported .csv scenario file.
-
Select the scenario file that was just exported and press the Open button
-
The objects that were exported should reload;verify that objects were exported correctly
Notes
Scenario Import/Export does not export VR Forces specific data, such as Plans and Tasks.
Classes
Plugin Entry Points
extern "C" {
{
info.
pluginName =
"csvScenarioImporterExporterSim";
info.
pluginDescription =
"An example of how to take a scenario imported from a file and incorporate it into an existing scenario";
}
{
return true;
}
}