void OnlineViewerServerImpl::load(string name, string url) { string filepath; QRegExp filePattern("(\\w+)://(.+)"); if(filePattern.exactMatch(url.c_str())){ string protocol = filePattern.cap(1).toStdString(); if(protocol == "file"){ filepath = filePattern.cap(2).toStdString(); } else { mv->putln( fmt(_("OnlineViewer: The model file at \"%1%\" cannot be read. %2% protocol is not supported.")) % url % protocol); return; } } else { filepath = url; } // search for registered body items BodyItemInfo* info = findInfo(name); if(info && info->bodyItem->filePath() == filepath){ info->needToSelectLogItem = true; // mv->putln(fmt(_("OnlineViewer: \"%1%\" at \"%2%\" has already been loaded.")) % name % url); return; } // search for existing body items RootItem* rootItem = RootItem::instance(); ItemList<BodyItem> bodyItems; bodyItems.extractChildItems(rootItem); for(int i=0; i < bodyItems.size(); ++i){ BodyItemPtr bodyItem = bodyItems[i]; if(bodyItem->name() == name && bodyItem->filePath() == filepath){ registerBodyItem(bodyItem); return; } } // load a new body item BodyItemPtr bodyItem = new BodyItem(); mv->putln(fmt(_("OnlineViewer: Loading \"%1%\" at \"%2%\".")) % name % url); mv->flush(); if(!bodyItem->load(filepath)){ mv->putln(fmt(_("OnlineViewer: Loading \"%1%\" failed.")) % name); } else { bodyItem->setName(name); ItemList<WorldItem> worldItems; if(worldItems.extractChildItems(rootItem)){ worldItems.front()->addChildItem(bodyItem); } else { rootItem->addChildItem(bodyItem); } ItemTreeView::instance()->checkItem(bodyItem, true); registerBodyItem(bodyItem); } }
bool WorldRosItem::spawnModel(gazebo_msgs::SpawnModel::Request &req, gazebo_msgs::SpawnModel::Response &res) { std::string model_name = req.model_name; std::string model_xml = req.model_xml; cnoid::Vector3 trans; cnoid::Quat R; trans(0) = req.initial_pose.position.x; trans(1) = req.initial_pose.position.y; trans(2) = req.initial_pose.position.z; R.w() = req.initial_pose.orientation.w; R.x() = req.initial_pose.orientation.x; R.y() = req.initial_pose.orientation.y; R.z() = req.initial_pose.orientation.z; BodyItemPtr body = new BodyItem(); body->setName(req.model_name); const char *fname = tmpnam(NULL); std::ofstream ofs(fname); ofs << model_xml << std::endl; ofs.close(); body->loadModelFile(fname); remove(fname); body->body()->rootLink()->setTranslation(trans); body->body()->rootLink()->setRotation(R.matrix()); world->addChildItem(body); BodyRosItemPtr bodyros = new BodyRosItem(); bodyros->setName(req.model_name + "_ROS"); body->addChildItem(bodyros); ItemTreeView::instance()->checkItem(body); ItemTreeView::instance()->checkItem(bodyros); return true; }