FreeCAD/src/Mod/TechDraw/App/DrawUtil.cpp
WandererFan 9a93185f7e Py binding for EdgeWalker
refactor EdgeWalker code from DVP,DVS
2016-09-01 12:14:42 -04:00

112 lines
4.1 KiB
C++

/***************************************************************************
* Copyright (c) 2015 WandererFan <wandererfan@gmail.com> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <sstream>
# include <cstring>
# include <cstdlib>
# include <exception>
# include <boost/regex.hpp>
# include <QString>
# include <QStringList>
# include <QRegExp>
//#include <TopoDS_Vertex.hxx>
#include <BRep_Tool.hxx>
#include <gp_Pnt.hxx>
#include <Precision.hxx>
#endif
#include <App/Application.h>
#include <Base/Console.h>
#include <Base/Exception.h>
#include <Base/Parameter.h>
#include "DrawUtil.h"
using namespace TechDraw;
/*static*/ int DrawUtil::getIndexFromName(std::string geomName)
{
boost::regex re("\\d+$"); // one of more digits at end of string
boost::match_results<std::string::const_iterator> what;
boost::match_flag_type flags = boost::match_default;
char* endChar;
std::string::const_iterator begin = geomName.begin();
std::string::const_iterator end = geomName.end();
std::stringstream ErrorMsg;
if (!geomName.empty()) {
if (boost::regex_search(begin, end, what, re, flags)) {
return int (std::strtol(what.str().c_str(), &endChar, 10)); //TODO: use std::stoi() in c++11
} else {
ErrorMsg << "getIndexFromName: malformed geometry name - " << geomName;
throw Base::Exception(ErrorMsg.str());
}
} else {
throw Base::Exception("getIndexFromName - empty geometry name");
}
}
std::string DrawUtil::getGeomTypeFromName(std::string geomName)
{
boost::regex re("^[a-zA-Z]*"); //one or more letters at start of string
boost::match_results<std::string::const_iterator> what;
boost::match_flag_type flags = boost::match_default;
std::string::const_iterator begin = geomName.begin();
std::string::const_iterator end = geomName.end();
std::stringstream ErrorMsg;
if (!geomName.empty()) {
if (boost::regex_search(begin, end, what, re, flags)) {
return what.str(); //TODO: use std::stoi() in c++11
} else {
ErrorMsg << "In getGeomTypeFromName: malformed geometry name - " << geomName;
throw Base::Exception(ErrorMsg.str());
}
} else {
throw Base::Exception("getGeomTypeFromName - empty geometry name");
}
}
std::string DrawUtil::makeGeomName(std::string geomType, int index)
{
std::stringstream newName;
newName << geomType << index;
return newName.str();
}
bool DrawUtil::isSamePoint(TopoDS_Vertex v1, TopoDS_Vertex v2)
{
bool result = false;
gp_Pnt p1 = BRep_Tool::Pnt(v1);
gp_Pnt p2 = BRep_Tool::Pnt(v2);
if (p1.IsEqual(p2,Precision::Confusion())) {
result = true;
}
return result;
}