ChangeDyna.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2007                                                    *
00003  *   Joachim Zettler <Joachim.Zettler@gmx.de>                              *
00004  *                                                                         *
00005  *   This file is part of the FreeCAD CAx development system.              *
00006  *                                                                         *
00007  *   This library is free software; you can redistribute it and/or         *
00008  *   modify it under the terms of the GNU Library General Public           *
00009  *   License as published by the Free Software Foundation; either          *
00010  *   version 2 of the License, or (at your option) any later version.      *
00011  *                                                                         *
00012  *   This library  is distributed in the hope that it will be useful,      *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU Library General Public License for more details.                  *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU Library General Public     *
00018  *   License along with this library; see the file COPYING.LIB. If not,    *
00019  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00020  *   Suite 330, Boston, MA  02111-1307, USA                                *
00021  *                                                                         *
00022  ***************************************************************************/
00023 
00024 
00025 #include "PreCompiled.h"
00026 #include "ChangeDyna.h"
00027 #include <sstream>
00028 #include <vector>
00029 #include <fstream>
00030 #include <stdexcept>
00031 
00032 ChangeDyna::ChangeDyna()
00033 {
00034     m_ProperTime.clear();
00035 }
00036 bool ChangeDyna::Read( const std::string & _filename)
00037 {
00038     // open file for reading
00039     std::ifstream input( _filename.c_str() );
00040     std::ifstream input2("CurveTimes.k");
00041     if (!input2.is_open()) return false;
00042     if (!ReadTimes(input2)) return false;
00043     std::ofstream output("dyna2.str");
00044     if ( !input.is_open() )
00045     {
00046         std::cout << "failed to open file" << std::endl;
00047         return false;
00048     }
00049     if ( !ReadCurve( input, output ) )
00050     {
00051         std::cout << "failed to read curve_data" << std::endl;
00052         return false;
00053     }
00054 
00055     input.close();
00056     output.close();
00057     input2.close();
00058     return true;
00059 }
00060 
00061 
00062 bool ChangeDyna::ReadTimes(std::ifstream &input2)
00063 {
00064     input2.seekg(std::ifstream::beg);
00065     std::string line;
00066     unsigned int i=0;
00067     std::pair<float,float> tempPair;
00068     std::stringstream astream1;
00069     do
00070     {
00071         std::getline(input2,line);
00072         if (line.size() == 0) continue;
00073         astream1.str(line);
00074         astream1 >> tempPair.first >> tempPair.second;
00075         m_ProperTime.push_back(tempPair);
00076         astream1.str("");
00077         astream1.clear();
00078 
00079     }
00080     while (input2.good());
00081     return true;
00082 }
00083 
00084 
00085 bool ChangeDyna::ReadCurve(std::ifstream &input,std::ofstream &output)
00086 {
00087     input.seekg( std::ifstream::beg );
00088     std::string line,subline1;
00089     bool found = false;
00090     int current_index;
00091     do
00092     {
00093         std::getline( input, line );
00094         if (line.size() == 0) //if we have an empty line
00095         {
00096             continue;
00097         }
00098         if (found)
00099         {
00100             //If we find a new Keyword description
00101             if (line.size() > 3 && line.at(0) == '$' && (line.find("nid") == std::string::npos))
00102             {
00103                 found = false;
00104                 output << line << std::endl;
00105                 continue; //go directly to the next line
00106             }
00107             else if (line.at(0) == '$')
00108             {
00109                 output << line << std::endl;
00110                 continue;
00111             }
00112             else // Now we change the Birth and Death-Times
00113             {
00114                 std::stringstream astream1,astream2;
00115                 astream1.precision(20);
00116                 astream2.precision(20);
00117                 if (line.at(9) != '0') //If we are at the first line
00118                 {
00119                     //Extract the Curve-Index
00120                     astream1.str(line.substr(10,5));
00121                     astream1 >> current_index;
00122                     //Exchange the Death time. We need a vector of pairs (birth,death)
00123                     if ((current_index-2) < 0)
00124                         return false;
00125                     astream2 << m_ProperTime[current_index-2].second;
00126                     //Now we have to reformat the string to fit exactly 9 digits
00127                     try
00128                     {
00129                         ReformatStream(astream2,subline1);
00130                         output << line.substr(0,66) << subline1 << line.substr(75,5) << std::endl;
00131                     }
00132                     catch (const std::out_of_range&)
00133                     {
00134                         output << line << std::endl;
00135                         return false;
00136                     }
00137 
00138                     continue;
00139                 }
00140                 else //we are at the second line and can exchange the Birth-Time
00141                 {
00142 
00143                     astream2 << m_ProperTime[current_index-2].first;
00144                     try
00145                     {
00146                         ReformatStream(astream2,subline1);
00147                         output << line.substr(0,31) << subline1 << std::endl;
00148                     }
00149                     catch (std::out_of_range)
00150                     {
00151                         output << line << std::endl;
00152                         return false;
00153                     }
00154 
00155                     continue;
00156                 }
00157             }
00158         }
00159         else
00160         {
00161             std::string search("Velocity/Acceleration/");
00162             if (line.find(search)!=std::string::npos)
00163                 found = true;
00164             output << line << std::endl;
00165             continue;
00166         }
00167     }
00168     while ( input.good());
00169     return true;
00170 }
00171 
00172 
00173 bool ChangeDyna::ReformatStream(const std::stringstream& astream, std::string& astring)
00174 {
00175     astring.clear();
00176     std::string tempstring(astream.str());
00177     unsigned int found=tempstring.find('.');
00178     astring = tempstring.substr(0,found);
00179     astring += '.';
00180     //Now add the rest. We have only space for 9 digits in total (the '-' is already there)
00181     astring += tempstring.substr(found+1,9-astring.size());
00182     while (astring.size() < 9)
00183         astring += '0'; //We add '0' until we have a size of 9
00184     return true;
00185 }

Generated on Wed Nov 23 19:00:00 2011 for FreeCAD by  doxygen 1.6.1