const std::string& Gui::SoFCDB::writeNodesToString(SoNode * root) { SoOutput out; buffer = (char *)malloc(1024); buffer_size = 1024; out.setBuffer(buffer, buffer_size, buffer_realloc); if (root && root->getTypeId().isDerivedFrom(SoVRMLParent::getClassTypeId())) out.setHeaderString("#VRML V2.0 utf8"); SoWriteAction wa(&out); wa.apply(root); cReturnString = buffer; free(buffer); return cReturnString; }
void MainWindow::saveScene() { SoOutput output; if (!output.openFile(QString("coach-" + QDateTime::currentDateTime().toString("yyyyMMdd-HHmmsszzz") + ".wrl").toStdString().c_str())) { return; } output.setHeaderString("#VRML V2.0 utf8"); SoWriteAction writeAction(&output); writeAction.apply(this->scene->root); output.closeFile(); }
void Viewer::saveScene() { QString filename = "rlPlanDemo-" + QDateTime::currentDateTime().toString("yyyyMMdd-HHmmsszzz") + ".wrl"; SoOutput output; if (!output.openFile(filename.toStdString().c_str())) { return; } output.setHeaderString("#VRML V2.0 utf8"); SoWriteAction writeAction(&output); writeAction.apply(this->root); output.closeFile(); MainWindow::instance()->statusBar()->showMessage("Successfully saved scene in '" + filename + "'.", 1000); }
osgDB::ReaderWriter::WriteResult ReaderWriterIV::writeNode(const osg::Node& node, const std::string& fileName, const osgDB::ReaderWriter::Options* /*options*/) const { // accept extension std::string ext = osgDB::getLowerCaseFileExtension(fileName); if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED; bool useVRML1 = !isInventorExtension(osgDB::getFileExtension(fileName)); OSG_NOTICE << "osgDB::ReaderWriterIV::writeNode() Writing file " << fileName.data() << std::endl; // Convert OSG graph to Inventor graph ConvertToInventor osg2iv; osg2iv.setVRML1Conversion(useVRML1); (const_cast<osg::Node*>(&node))->accept(osg2iv); SoNode *ivRoot = osg2iv.getIvSceneGraph(); if (ivRoot == NULL) return WriteResult::ERROR_IN_WRITING_FILE; ivRoot->ref(); // Change prefix according to VRML spec: // Node names must not begin with a digit, and must not contain spaces or // control characters, single or double quote characters, backslashes, curly braces, // the sharp (#) character, the plus (+) character or the period character. if (useVRML1) SoBase::setInstancePrefix("_"); // Write Inventor graph to file SoOutput out; out.setHeaderString((useVRML1) ? "#VRML V1.0 ascii" : "#Inventor V2.1 ascii"); if (!out.openFile(fileName.c_str())) return WriteResult::ERROR_IN_WRITING_FILE; SoWriteAction wa(&out); wa.apply(ivRoot); ivRoot->unref(); return WriteResult::FILE_SAVED; }
int main(int argc, char ** argv) { fprintf(stderr, "ivcp v0.1\n"); SoDB::init(); SoNodeKit::init(); SoInteraction::init(); if (argc != 3 ) { fprintf(stdout, "Usage: %s infile outfile\n", argv[0]); return 0; } SoInput * in = new SoInput; if (!in->openFile(argv[1])) { fprintf(stderr, "error: could not open file '%s'\n", argv[1]); delete in; SoDB::cleanup(); return -1; } SoNode * scene = SoDB::readAll(in); if (!scene) { fprintf(stderr, "error: could not read file '%s'\n", argv[1]); delete in; SoDB::cleanup(); return -1; } FileType inputFileType; if (in->isFileVRML1()) inputFileType = VRML1; else if (in->isFileVRML2()) inputFileType = VRML2; else inputFileType = INVENTOR; delete in; scene->ref(); SoNode * firstChild = static_cast<SoSeparator*>(scene)->getNumChildren()? static_cast<SoSeparator*>(scene)->getChild(0) :NULL; if (firstChild && firstChild->isOfType(SoForeignFileKit::getClassTypeId())) { SoForeignFileKit * kit = (SoForeignFileKit *) firstChild; if (kit->canWriteScene() ) { SoNode * subscene = NULL; kit->writeScene(subscene); if (!subscene ) { return -1; } subscene->ref(); scene->unref(); scene = subscene; } } SoOutput * out = new SoOutput; if (!out->openFile(argv[2])) { fprintf(stderr, "error: could not open file '%s' for writing\n"); scene->unref(); delete out; SoDB::cleanup(); return -1; } switch (inputFileType) { case VRML1: out->setHeaderString("#VRML V1.0 ascii"); break; case VRML2: out->setHeaderString("#VRML V2.0 utf8"); } SoWriteAction wa(out); wa.apply(scene); out->closeFile(); delete out; scene->unref(); // with actions on the stack, cleanup can't be called... // SoDB::cleanup(); return 0; }
int main(int argc, char** argv) { if (argc != 3) { std::cerr << "Usage: tris2wrl [input.tris] [output.wrl]" << std::endl; return EXIT_FAILURE; } SoDB::init(); std::fstream file; file.open(argv[1]); std::string line; std::vector<std::string> tokens; std::getline(file, line); if (!boost::starts_with(line, "#")) { file.close(); return EXIT_FAILURE; } SoVRMLTransform* root = new SoVRMLTransform(); root->ref(); SoVRMLShape* shape = new SoVRMLShape(); root->addChild(shape); SoVRMLAppearance* appearance = new SoVRMLAppearance(); shape->appearance = appearance; SoVRMLMaterial* material = new SoVRMLMaterial(); appearance->material = material; SoVRMLIndexedFaceSet* indexedFaceSet = new SoVRMLIndexedFaceSet(); shape->geometry = indexedFaceSet; SoVRMLCoordinate* coordinate = new SoVRMLCoordinate(); indexedFaceSet->coord = coordinate; SoVRMLColor* color = new SoVRMLColor(); indexedFaceSet->color = color; indexedFaceSet->colorPerVertex.setValue(false); indexedFaceSet->solid.setValue(false); while (-1 != file.peek()) { std::getline(file, line); if (line.empty()) { continue; } else if (boost::starts_with(line, "#")) { break; } boost::trim(line); boost::split(tokens, line, boost::is_space(), boost::token_compress_on); coordinate->point.set1Value(coordinate->point.getNum(), std::stof(tokens[0]), std::stof(tokens[1]), std::stof(tokens[2])); } while (-1 != file.peek()) { std::getline(file, line); if (line.empty()) { continue; } else if (boost::starts_with(line, "#")) { break; } boost::trim(line); boost::split(tokens, line, boost::is_space(), boost::token_compress_on); for (std::size_t i = 0; i < tokens.size(); ++i) { indexedFaceSet->coordIndex.set1Value(indexedFaceSet->coordIndex.getNum(), std::stoi(tokens[i])); } indexedFaceSet->coordIndex.set1Value(indexedFaceSet->coordIndex.getNum(), -1); } while (-1 != file.peek()) { std::getline(file, line); if (line.empty()) { continue; } else if (boost::starts_with(line, "#")) { break; } boost::trim(line); boost::split(tokens, line, boost::is_space(), boost::token_compress_on); if (tokens.size() > 5) { color->color.set1Value(color->color.getNum(), std::stof(tokens[3]), std::stof(tokens[4]), std::stof(tokens[5])); } } while (-1 != file.peek()) { std::getline(file, line); if (line.empty()) { continue; } else if (boost::starts_with(line, "#")) { break; } boost::trim_if(line, boost::is_any_of(", \t\n\v\f\r")); boost::split(tokens, line, boost::is_any_of(", "), boost::token_compress_on); for (std::size_t i = 0; i < tokens.size(); ++i) { indexedFaceSet->colorIndex.set1Value(indexedFaceSet->colorIndex.getNum(), std::stoi(tokens[i])); } } std::cout << "coordinates: " << coordinate->point.getNum() << std::endl; std::cout << "faces: " << indexedFaceSet->coordIndex.getNum() / 4 << std::endl; std::cout << "materials: " << color->color.getNum() << std::endl; std::cout << "material indices: " << indexedFaceSet->colorIndex.getNum() << std::endl; if (0 == indexedFaceSet->colorIndex.getNum()) { color->ref(); indexedFaceSet->color.setValue(nullptr); color->unref(); } file.close(); SoOutput out; out.openFile(argv[2]); out.setHeaderString("#VRML V2.0 utf8"); SoWriteAction wra(&out); wra.apply(root); out.closeFile(); root->unref(); return EXIT_SUCCESS; }