Commit 03388819 authored by Dirk Wilden's avatar Dirk Wilden
Browse files

load/save Flipper selections

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@6388 383ad7c9-94d9-4d36-a494-682f7c89f535
parent 138383fc
......@@ -198,15 +198,33 @@ void SelectionPlugin::loadFlipperModelingSelection( int _objectId , QString _fil
header = input.readLine();
// bool ok = false;
bool ok = false;
// uint vertexCount = header.toUInt(&ok);
//
// if ( !ok ) {
// emit log(LOGERR,"Unable to parse header. Cant get vertex count from string : " + header );
// return;
// }
uint vertexCount = header.toUInt(&ok);
if ( !ok ) {
emit log(LOGERR,"Unable to parse header. Cant get vertex count from string : " + header );
return;
}
//compare VertexCount
BaseObjectData* object;
if ( ! PluginFunctions::getObject(_objectId, object) ) {
emit log(LOGERR,"loadSelection : unable to get object" );
return;
}
if ( object->dataType() == DATA_TRIANGLE_MESH ){
if ( PluginFunctions::triMesh(object)->n_vertices() != vertexCount )
return;
} else if ( object->dataType() == DATA_POLY_MESH ){
if ( PluginFunctions::polyMesh(object)->n_vertices() != vertexCount )
return;
} else {
return;
}
//get selected handles
idList handleVertices;
idList modelingVertices;
......@@ -239,6 +257,73 @@ void SelectionPlugin::loadFlipperModelingSelection( int _objectId , QString _fil
} else
emit log(LOGERR,"Unable to open selection file!");
}
//=========================================================
void SelectionPlugin::saveFlipperModelingSelection( int _objectId , QString _filename ) {
QFile file(_filename);
if (file.open(QFile::WriteOnly)) {
QTextStream input(&file);
//get the object
BaseObjectData* object;
if ( ! PluginFunctions::getObject(_objectId,object) ) {
emit log(LOGERR,"saveFlipperModelingSelection : unable to get object" );
return;
}
if ( object->dataType() == DATA_TRIANGLE_MESH ) {
TriMesh* mesh = PluginFunctions::triMesh(object);
//header
input << "Selection" << endl;
input << mesh->n_vertices() << endl;
std::vector< int > modelingVertices = MeshSelection::getArea(mesh, AREA);
std::vector< int > handleVertices = MeshSelection::getArea(mesh, HANDLEAREA);
std::vector< bool > modelingAll(mesh->n_vertices(), false);
std::vector< bool > handleAll(mesh->n_vertices(), false);
for (uint i=0; i < modelingVertices.size(); i++)
modelingAll[ modelingVertices[i] ] = true;
for (uint i=0; i < handleVertices.size(); i++)
handleAll[ handleVertices[i] ] = true;
for (uint i=0; i < mesh->n_vertices(); i++)
input << (int) modelingAll[i] << " " << (int) handleAll[i] << endl;
} else if ( object->dataType() == DATA_POLY_MESH){
PolyMesh* mesh = PluginFunctions::polyMesh(object);
//header
input << "Selection" << endl;
input << mesh->n_vertices() << endl;
std::vector< int > modelingVertices = MeshSelection::getArea(mesh, AREA);
std::vector< int > handleVertices = MeshSelection::getArea(mesh, HANDLEAREA);
std::vector< bool > modelingAll(mesh->n_vertices(), false);
std::vector< bool > handleAll(mesh->n_vertices(), false);
for (uint i=0; i < modelingVertices.size(); i++)
modelingAll[ modelingVertices[i] ] = true;
for (uint i=0; i < handleVertices.size(); i++)
handleAll[ handleVertices[i] ] = true;
for (uint i=0; i < mesh->n_vertices(); i++)
input << (int) modelingAll[i] << " " << (int) handleAll[i] << endl;
} else {
emit log(LOGERR, "saveFlipperModelingSelection : Unsupported Type.");
}
} else
emit log(LOGERR,"Unable to open selection file!");
}
......@@ -398,6 +398,9 @@ class SelectionPlugin : public QObject, BaseInterface , MouseInterface, KeyInter
/// Load a selection from an Flipper selection file for the given object
void loadFlipperModelingSelection( int _objectId , QString _filename );
/// Save a selection in Flipper Selection Format
void saveFlipperModelingSelection( int _objectId , QString _filename );
//==========================================
/// Set vertices to be part of the modeling area
......
......@@ -180,22 +180,41 @@ void SelectionPlugin::slotGrowSelection() {
void SelectionPlugin::slotLoadSelection(){
QString fileName = QFileDialog::getOpenFileName(0, tr("Open File"),"", tr("INI files (*.ini)"));
QString fileName = QFileDialog::getOpenFileName(0, tr("Open File"),"", tr("Selections (*.ini *.sel)"));
if ( !fileName.isEmpty() ){
PluginFunctions::IteratorRestriction restriction;
if ( !tool_->restrictOnTargets->isChecked() )
restriction = PluginFunctions::ALL_OBJECTS;
else
restriction = PluginFunctions::TARGET_OBJECTS;
//first check if it's a Flipper Selection
QFile file(fileName);
if (file.open(QFile::ReadOnly)) {
QTextStream input(&file);
if ( input.readLine().contains("Selection") ) {
//load a Flipper Modeling Selection
for ( PluginFunctions::ObjectIterator o_it(restriction,DataType( DATA_TRIANGLE_MESH | DATA_POLY_MESH )) ;
o_it != PluginFunctions::objectsEnd(); ++o_it)
if ( o_it->visible() )
loadFlipperModelingSelection(o_it->id(), fileName );
return;
}
}
INIFile ini;
if ( ! ini.connect(fileName,false) ) {
emit log(LOGERR,"Failed to load ini file " + fileName);
return;
}
PluginFunctions::IteratorRestriction restriction;
if ( !tool_->restrictOnTargets->isChecked() )
restriction = PluginFunctions::ALL_OBJECTS;
else
restriction = PluginFunctions::TARGET_OBJECTS;
for ( PluginFunctions::ObjectIterator o_it(restriction,DataType( DATA_TRIANGLE_MESH | DATA_POLY_MESH )) ;
o_it != PluginFunctions::objectsEnd(); ++o_it)
......@@ -215,31 +234,64 @@ void SelectionPlugin::slotLoadSelection(){
*/
void SelectionPlugin::slotSaveSelection(){
QString fileName = QFileDialog::getSaveFileName(0, tr("Save File"),"", tr("INI files (*.ini)"));
QStringList filters;
filters << "OpenFlipper Selection (*.ini)" << "Flipper Selection (*.sel)";
if ( !fileName.isEmpty() ){
QFileDialog dialog(0);
INIFile ini;
dialog.setAcceptMode(QFileDialog::AcceptSave);
dialog.setNameFilters(filters);
dialog.setDefaultSuffix("ini");
if ( ! ini.connect(fileName,true) ) {
emit log(LOGERR,"Failed to save ini file " + fileName);
return;
}
if ( !dialog.exec() )
return;
PluginFunctions::IteratorRestriction restriction;
if ( !tool_->restrictOnTargets->isChecked() )
restriction = PluginFunctions::ALL_OBJECTS;
else
restriction = PluginFunctions::TARGET_OBJECTS;
for ( PluginFunctions::ObjectIterator o_it(restriction,DataType( DATA_TRIANGLE_MESH | DATA_POLY_MESH )) ;
o_it != PluginFunctions::objectsEnd(); ++o_it)
if ( o_it->visible() ){
QString fileName = dialog.selectedFiles()[0];
if ( !fileName.isEmpty() ){
ini.add_section( o_it->name() );
saveIniFile(ini, o_it->id() );
//write OpenFlipper Selection
if (dialog.selectedNameFilter() == filters[0] ){
INIFile ini;
if ( ! ini.connect(fileName,true) ) {
emit log(LOGERR,"Failed to save ini file " + fileName);
return;
}
ini.disconnect();
PluginFunctions::IteratorRestriction restriction;
if ( !tool_->restrictOnTargets->isChecked() )
restriction = PluginFunctions::ALL_OBJECTS;
else
restriction = PluginFunctions::TARGET_OBJECTS;
for ( PluginFunctions::ObjectIterator o_it(restriction,DataType( DATA_TRIANGLE_MESH | DATA_POLY_MESH )) ;
o_it != PluginFunctions::objectsEnd(); ++o_it)
if ( o_it->visible() ){
ini.add_section( o_it->name() );
saveIniFile(ini, o_it->id() );
}
ini.disconnect();
} else {
// write Flipper Selection
bool saved = false;
for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::TARGET_OBJECTS,DataType( DATA_TRIANGLE_MESH | DATA_POLY_MESH ));
o_it != PluginFunctions::objectsEnd(); ++o_it)
if ( o_it->visible() ){
if ( !saved ){
saveFlipperModelingSelection(o_it->id(), fileName );
saved = true;
} else {
emit log(LOGWARN, "SaveSelection: Only selection of the first target was saved.");
return;
}
}
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment