1
/***************************************************************************
2
* Copyright (c) Juergen Riegel <juergen.riegel@web.de> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
24
#include "PreCompiled.h"
33
#include "PointsAlgos.h"
36
#include <Base/Exception.h>
37
#include <Base/FileInfo.h>
38
#include <Base/Console.h>
39
#include <Base/Sequencer.h>
40
#include <Base/Stream.h>
42
#include <boost/regex.hpp>
44
using namespace Points;
46
void PointsAlgos::Load(PointKernel &points, const char *FileName)
48
Base::FileInfo File(FileName);
50
// checking on the file
51
if (!File.isReadable())
52
throw Base::Exception("File to load not existing or not readable");
54
if (File.extension() == "asc" ||File.extension() == "ASC")
55
LoadAscii(points,FileName);
57
throw Base::Exception("Unknown ending");
60
void PointsAlgos::LoadAscii(PointKernel &points, const char *FileName)
62
boost::regex rx("^\\s*([-+]?[0-9]*)\\.?([0-9]+([eE][-+]?[0-9]+)?)"
63
"\\s+([-+]?[0-9]*)\\.?([0-9]+([eE][-+]?[0-9]+)?)"
64
"\\s+([-+]?[0-9]*)\\.?([0-9]+([eE][-+]?[0-9]+)?)\\s*$");
65
//boost::regex rx("(\\b[0-9]+\\.([0-9]+\\b)?|\\.[0-9]+\\b)");
66
//boost::regex rx("^\\s*(-?[0-9]*)\\.([0-9]+)\\s+(-?[0-9]*)\\.([0-9]+)\\s+(-?[0-9]*)\\.([0-9]+)\\s*$");
72
Base::FileInfo fi(FileName);
74
Base::ifstream tmp_str(fi, std::ios::in);
77
while (std::getline(tmp_str,line))
80
// resize the PointKernel
81
points.resize(LineCnt);
83
Base::SequencerLauncher seq( "Loading points...", LineCnt );
85
// again to the beginning
86
Base::ifstream file(fi, std::ios::in);
91
while (std::getline(file, line)) {
92
if (boost::regex_match(line.c_str(), what, rx)) {
93
pt.x = std::atof(what[1].first);
94
pt.y = std::atof(what[4].first);
95
pt.z = std::atof(what[7].first);
97
points.setPoint(LineCnt,pt);
105
throw Base::Exception("Reading in points failed.");
108
// now remove the last points from the kernel
109
// Note: first we allocate memory corresponding to the number of lines (points and comments)
110
// and read in the file twice. But then the size of the kernel is too high
111
if (LineCnt < (int)points.size())
112
points.erase(LineCnt, points.size());