00001 /****************************************************************************** 00002 * SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3 * 00003 * (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS * 00004 * * 00005 * This library is free software; you can redistribute it and/or modify it * 00006 * under the terms of the GNU Lesser General Public License as published by * 00007 * the Free Software Foundation; either version 2.1 of the License, or (at * 00008 * your option) any later version. * 00009 * * 00010 * This library is distributed in the hope that it will be useful, but WITHOUT * 00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * 00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * 00013 * for more details. * 00014 * * 00015 * You should have received a copy of the GNU Lesser General Public License * 00016 * along with this library; if not, write to the Free Software Foundation, * 00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 00018 ******************************************************************************* 00019 * SOFA :: Modules * 00020 * * 00021 * Authors: The SOFA Team and external contributors (see Authors.txt) * 00022 * * 00023 * Contact information: contact@sofa-framework.org * 00024 ******************************************************************************/ 00025 #include <sofa/component/misc/WriteState.inl> 00026 #include <sofa/core/ObjectFactory.h> 00027 00028 namespace sofa 00029 { 00030 00031 namespace component 00032 { 00033 00034 namespace misc 00035 { 00036 00037 SOFA_DECL_CLASS(WriteState) 00038 00039 using namespace defaulttype; 00040 00041 00042 00043 int WriteStateClass = core::RegisterObject("Write State vectors to file at each timestep") 00044 .add< WriteState >(); 00045 00046 00047 00048 00049 //Create a Write State component each time a mechanical state is found 00050 simulation::Visitor::Result WriteStateCreator::processNodeTopDown( simulation::Node* gnode) 00051 { 00052 sofa::core::componentmodel::behavior::BaseMechanicalState * mstate = dynamic_cast<sofa::core::componentmodel::behavior::BaseMechanicalState *>( gnode->getMechanicalState()); 00053 if (!mstate) return simulation::Visitor::RESULT_CONTINUE; 00054 //We have a mechanical state 00055 addWriteState(mstate, gnode); 00056 return simulation::Visitor::RESULT_CONTINUE; 00057 } 00058 00059 00060 void WriteStateCreator::addWriteState(sofa::core::componentmodel::behavior::BaseMechanicalState *ms, simulation::Node* gnode) 00061 { 00062 sofa::core::objectmodel::BaseContext* context = gnode->getContext(); 00063 sofa::core::BaseMapping *mapping; 00064 context->get(mapping); 00065 if ( createInMapping || mapping == NULL) 00066 { 00067 sofa::component::misc::WriteState *ws; 00068 context->get(ws); 00069 if ( ws == NULL ) 00070 { 00071 ws = new sofa::component::misc::WriteState(); gnode->addObject(ws); 00072 } 00073 00074 std::ostringstream ofilename; 00075 ofilename << sceneName << "_" << counterWriteState << "_" << ms->getName() << "_mstate.txt" ; 00076 00077 ws->f_filename.setValue(ofilename.str()); ws->init(); ws->f_listening.setValue(true); //Activated at init 00078 00079 ++counterWriteState; 00080 } 00081 } 00082 00083 00084 00085 //if state is true, we activate all the write states present in the scene. 00086 simulation::Visitor::Result WriteStateActivator::processNodeTopDown( simulation::Node* gnode) 00087 { 00088 sofa::component::misc::WriteState *ws = gnode->get< sofa::component::misc::WriteState >(); 00089 if (ws != NULL) { changeStateWriter(ws);} 00090 return simulation::Visitor::RESULT_CONTINUE; 00091 } 00092 00093 void WriteStateActivator::changeStateWriter(sofa::component::misc::WriteState*ws) 00094 { 00095 if (!state) ws->reset(); 00096 ws->f_listening.setValue(state); 00097 } 00098 00099 00100 00101 00102 00103 00104 } // namespace misc 00105 00106 } // namespace component 00107 00108 } // namespace sofa
1.5.7.1