📄 factory.cc
字号:
/* * Gazebo - Outdoor Multi-Robot Simulator * Copyright (C) 2003 * Nate Koenig & Andrew Howard * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * *//* Desc: Model for a Factory * Author: Nate Koenig * Date: 14 Nov 2003 * CVS: $Id: Factory.cc,v 1.17 2006/02/22 14:58:57 natepak Exp $ *//// @addtogroup models/// @{ /** @defgroup Factory FactoryThe Factory model maintains no physical characteristics, but insteadallows the user to dynamically create models at runtime. A model isinstantiated by passing an XML character string to a Factory model.The character string uses the same model format as the .world files. Notethat the string should only contain the model description, and noother tags. The XML string is passed into Gazebo through the %gz_factoryinterface which has the same interface as the Player speech device. @par libgazebo interfacesThis model supports the @ref factory interface.@par Player driversA new model is created using the %gz_factory driver.@par AttributesThe following attributes are supported.- id (string) - The model ID string (used by libgazebo to identify models). - Default: empty@par BodiesThis model does not have a physical representation.@par Example@verbatim<model:Factory> <id>factory0</id></model:Factory>@endverbatim@par AuthorsChris Jones*//// @}#include <assert.h>#include "gazebo.h"#include "Body.hh"#include "World.hh"#include "WorldFile.hh"#include "ModelFactory.hh"#include "Factory.hh"//////////////////////////////////////////////////////////////////////////////// Register this classGZ_REGISTER_STATIC("Factory", Factory);//////////////////////////////////////////////////////////////////////////////// ConstructorFactory::Factory( World *world ) : Model( world ){ // Prefix and suffix for xml files this->xmlPrefix = "<?xml version='1.0'?> <gz:world xmlns:gz='http://playerstage.sourceforge.net/gazebo/xmlschema/#gz' xmlns:model='http://playerstage.sourceforge.net/gazebo/xmlschema/#model'> "; this->xmlSuffix = "</gz:world>"; this->body = NULL; return;}//////////////////////////////////////////////////////////////////////////////// DestructorFactory::~Factory(){ delete [] this->xmlPrefix; delete [] this->xmlSuffix; if (this->body) delete this->body; this->xmlPrefix = NULL; this->xmlSuffix = NULL; this->body = NULL; return;}//////////////////////////////////////////////////////////////////////////////// Load the modelint Factory::Load( WorldFile *file, WorldFileNode *node ){ // Create a dummy body to which to attach geoms this->body = new Body(this->world, NULL, true); this->AddBody(this->body, true); return 0;}//////////////////////////////////////////////////////////////////////////////// Initialize the modelint Factory::Init( WorldFile *file, WorldFileNode *node ){ // Initialize external interface if (this->IfaceInit() != 0) return -1; return 0;}//////////////////////////////////////////////////////////////////////////////// Finalize the modelint Factory::Fini(){ // Finalize external interface this->IfaceFini(); return 0;}//////////////////////////////////////////////////////////////////////////////// Update modelvoid Factory::Update( double /*step*/ ){ // Get commands from the external interface this->IfaceGetCmd(); return;}//////////////////////////////////////////////////////////////////////////////// Initialize the external interfaceint Factory::IfaceInit(){ this->iface = gz_factory_alloc(); assert(this->iface); if (gz_factory_create(this->iface, this->world->gz_server, this->GetId(), "Factory", this->GetIntId(), this->GetParentIntId()) != 0) return -1; return 0;}//////////////////////////////////////////////////////////////////////////////// Finalize the external interfaceint Factory::IfaceFini(){ gz_factory_destroy( this->iface ); gz_factory_free( this->iface ); this->iface = NULL; return 0;}//////////////////////////////////////////////////////////////////////////////// Get commands from the external interfacevoid Factory::IfaceGetCmd(){ // If there is a string, then add the contents to the world if (strcmp((const char*)this->iface->data->string,"")!=0) { // Create the complete xml string char *xmlString = new char[strlen(this->xmlPrefix) + strlen(this->xmlSuffix) + strlen((const char*)this->iface->data->string)]; // Create the world file WorldFile *worldFile = new WorldFile(); // Copy in all the data to the xml string strcpy(xmlString,this->xmlPrefix); strcat(xmlString,(const char*)this->iface->data->string); strcat(xmlString,this->xmlSuffix); // Load the XML tree from the given string worldFile->LoadString( xmlString ); // Add the new models into the World this->world->LoadModel( worldFile->GetRootNode(), NULL ); // Cleanup delete worldFile; worldFile = NULL; delete [] xmlString; xmlString = NULL; //strcpy((char*)this->iface->data,""); strcpy((char*)this->iface->data->string,""); } return;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -