ParseObj.cc
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 #include "Core.hh"
00057
00058 #include <ACG/Scenegraph/DrawModes.hh>
00059
00060 #include <OpenFlipper/common/GlobalOptions.hh>
00061 #include <OpenFlipper/common/RecentFiles.hh>
00062
00063 #include <OpenFlipper/BasePlugin/PluginFunctions.hh>
00064
00065 #include <QFile>
00066
00067 #include <OpenFlipper/ACGHelper/DrawModeConverter.hh>
00068
00069
00070
00071
00072 void Core::openObjFile(QString _filename)
00073 {
00074 std::string fname = _filename.toStdString();
00075 std::fstream in(fname.c_str(), std::ios_base::in);
00076
00077 if (!in)
00078 {
00079 emit log(LOGERR,tr("Cannot open to obj file") + _filename);
00080 return;
00081 }
00082
00083 if ( OpenFlipper::Options::gui() )
00084 {
00085 coreWidget_->statusMessage( tr("Loading obj File ") + _filename + " ...");
00086 coreWidget_->setStatus(ApplicationStatus::BLOCKED );
00087 }
00088
00089 OpenFlipper::Options::loadingSettings(true);
00090
00091
00092 while(!in.eof())
00093 {
00094 std::string keyword;
00095 in >> keyword;
00096
00097
00098 if (keyword.compare("call") == 0)
00099 {
00100
00101 std::string file;
00102 in >> file;
00103
00104 QString path(file.c_str());
00105
00106
00107 if (path.startsWith( "." + OpenFlipper::Options::dirSeparator() ))
00108 {
00109
00110 if (_filename.section(OpenFlipper::Options::dirSeparator(), 0, -2) != "")
00111 {
00112 path.remove(0,1);
00113 path = _filename.section(OpenFlipper::Options::dirSeparator(), 0, -2) + path;
00114 }
00115 }
00116
00117
00118 preprocessObjFile(path);
00119 }
00120
00121
00122
00123 }
00124
00125
00126 OpenFlipper::Options::loadingSettings(false);
00127
00128
00129 resetScenegraph( true );
00130
00131 if ( OpenFlipper::Options::gui() )
00132 {
00133 coreWidget_->statusMessage( tr("Loading obj File ") + _filename + tr(" ... done"), 4000);
00134 coreWidget_->setStatus(ApplicationStatus::READY );
00135 }
00136
00137 in.close();
00138
00139 }
00140
00141
00142
00143
00144
00145 void Core::preprocessObjFile(QString _filename)
00146 {
00147 bool newActiveObject = false;
00148
00149 std::cout << "[preprocessObjFile] filename: " << _filename.toStdString() << std::endl;
00150
00151 QString path = _filename;
00152
00153
00154 if (path.startsWith( "." + OpenFlipper::Options::dirSeparator() ))
00155 {
00156
00157 if (_filename.section(OpenFlipper::Options::dirSeparator(), 0, -2) != "")
00158 {
00159 path.remove(0,1);
00160 path = _filename.section(OpenFlipper::Options::dirSeparator(), 0, -2) + path;
00161 }
00162 }
00163
00164
00165
00166 DataType dataType = DATA_UNKNOWN;
00167
00168 std::string fname = _filename.toStdString();
00169 std::fstream in(fname.c_str(), std::ios_base::in);
00170 if (!in)
00171 {
00172 emit log(LOGERR,tr("Cannot open to obj file") + _filename);
00173 return;
00174 }
00175
00176
00177 std::string token;
00178 std::string tmp;
00179
00180 bool typeFound = false;
00181 bool meshFound = false;
00182
00183 while ( (!typeFound) && (!in.eof()) )
00184 {
00185
00186 in >> token;
00187
00188 if (token == "v")
00189 continue;
00190
00191 else if (token == "f")
00192 {
00193
00194 bool nextLine = false;
00195 int numVerticesPerFace = 0;
00196
00197 meshFound = true;
00198
00199 while((!nextLine) && (!in.eof()))
00200 {
00201 in >> tmp;
00202 if (tmp == "f") nextLine = true;
00203 else ++numVerticesPerFace;
00204 }
00205
00206 if (numVerticesPerFace > 3)
00207 {
00208 dataType = DATA_POLY_MESH;
00209 typeFound = true;
00210 }
00211 }
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221 else if (token == "cstype")
00222 {
00223 bool nextLine = false;
00224 in >> tmp;
00225 if (tmp == "bspline")
00226 {
00227
00228 while((!nextLine) && (!in.eof()))
00229 {
00230 in >> tmp;
00231 if (tmp == "curv")
00232 {
00233 nextLine = true;
00234 dataType = typeId("BSplineCurve");
00235 typeFound = true;
00236 }
00237 else if (tmp == "surf")
00238 {
00239 nextLine = true;
00240 dataType = typeId("BSplineSurface");
00241 typeFound = true;
00242 }
00243 }
00244
00245 }
00246 }
00247
00248 }
00249
00250 if (!typeFound)
00251 {
00252 dataType = DATA_TRIANGLE_MESH;
00253 emit log(LOGWARN, tr("Unable to get DataType for object ") + _filename + tr(" - assuming Triangle Mesh") );
00254 }
00255
00256
00257 if (dataType == DATA_POLY_MESH)
00258 std::cout << _filename.toStdString() << " is of type DATA_POLY_MESH" << std::endl;
00259 else if (dataType == DATA_TRIANGLE_MESH)
00260 std::cout << _filename.toStdString() << " is of type DATA_TRIANGLE_MESH" << std::endl;
00261 else if (dataType == typeId("BSplineCurve"))
00262 std::cout << _filename.toStdString() << " is of type DATA_BSPLINE_CURVE" << std::endl;
00263 else if (dataType == typeId("BSplineSurface"))
00264 std::cout << _filename.toStdString() << " is of type DATA_BSPLINE_SURFACE" << std::endl;
00265 else
00266 std::cout << "no data type found " << std::endl;
00267
00268
00269 int newObjectId = loadObject(dataType, path);
00270
00271 BaseObject* object = objectRoot_->childExists( newObjectId );
00272 if ( object == 0 )
00273 emit log(LOGERR,tr("Unable to open Object ") + path);
00274
00275 if ( newActiveObject )
00276 emit objectSelectionChanged(-1);
00277 }
00278
00279
00280
00281
00282 void Core::writeObjFile(QString _filename, bool _relativePaths, bool _targetOnly)
00283 {
00284
00285 std::string fname = _filename.toStdString();
00286 std::fstream out(fname.c_str(), std::ios_base::out);
00287 if (!out)
00288 {
00289 emit log(LOGERR,tr("Cannot open to obj file") + _filename);
00290 return;
00291 }
00292
00293
00294 if ( OpenFlipper::Options::gui() )
00295 {
00296 coreWidget_->statusMessage( tr("Saving obj File ") + _filename + " ...");
00297 coreWidget_->setStatus(ApplicationStatus::BLOCKED );
00298 }
00299
00300 PluginFunctions::IteratorRestriction restriction;
00301 if ( _targetOnly )
00302 restriction = PluginFunctions::TARGET_OBJECTS;
00303 else
00304 restriction = PluginFunctions::ALL_OBJECTS;
00305
00306
00307 for ( PluginFunctions::ObjectIterator o_it (restriction) ;
00308 o_it != PluginFunctions::objectsEnd(); ++o_it)
00309 {
00310 QString file = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name();
00311
00312 if (QFile(file).exists())
00313 {
00314
00315 if (_relativePaths)
00316 {
00317 int prefixLen = _filename.section(OpenFlipper::Options::dirSeparator(),0,-2).length();
00318 file.remove(0, prefixLen);
00319 file = "." + file;
00320 }
00321
00322
00323 out << "call " << file.toStdString() << "\n";
00324 }
00325 }
00326
00327 out.close();
00328
00329 if ( OpenFlipper::Options::gui() )
00330 {
00331 coreWidget_->statusMessage( tr("Saving obj File ") + _filename + tr(" ... done"), 4000);
00332 coreWidget_->setStatus(ApplicationStatus::READY );
00333 }
00334
00335 }
00336
00337
00338