From: Yoann Audouin Date: Thu, 15 Sep 2022 13:12:22 +0000 (+0200) Subject: Adding support for BRep for shape X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=4607a95f5d775421f4fb621e9001f442926a8870;p=modules%2Fsmesh.git Adding support for BRep for shape --- diff --git a/src/SMESH/CMakeLists.txt b/src/SMESH/CMakeLists.txt index d08f4d78e..2ae6a4545 100644 --- a/src/SMESH/CMakeLists.txt +++ b/src/SMESH/CMakeLists.txt @@ -91,7 +91,7 @@ SET(SMESHimpl_HEADERS MG_ADAPT.hxx SMESH_Homard.hxx SMESH_DriverMesh.hxx - SMESH_DriverStep.hxx + SMESH_DriverShape.hxx ) # --- sources --- @@ -113,7 +113,7 @@ SET(SMESHimpl_SOURCES MG_ADAPT.cxx SMESH_Homard.cxx SMESH_DriverMesh.cxx - SMESH_DriverStep.cxx + SMESH_DriverShape.cxx ) # --- rules --- diff --git a/src/SMESH/SMESH_DriverShape.cxx b/src/SMESH/SMESH_DriverShape.cxx new file mode 100644 index 000000000..572da3e1e --- /dev/null +++ b/src/SMESH/SMESH_DriverShape.cxx @@ -0,0 +1,177 @@ +// Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE +// +// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 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 +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +// File : SMESH_DriverShape.cxx +// Author : Yoann AUDOUIN, EDF +// Module : SMESH +// + +#include "SMESH_DriverShape.hxx" + +// step include +#include +#include +#include + +// Brep include +#include +#include + +//Occ include +#include + +#include +#include +namespace fs = boost::filesystem; + +/** + * @brief Import the content of a shape file (STEP) into a TopDS_Shape object + * + * @param shape_file the shape file + * @param aShape the object + * + * @return error code + */ +int importSTEPShape(const std::string shape_file, TopoDS_Shape& aShape){ + + std::cout << "Importing STEP shape from " << shape_file << std::endl; + STEPControl_Reader reader; + // Forcing Unit in meter + Interface_Static::SetCVal("xstep.cascade.unit","M"); + Interface_Static::SetIVal("read.step.ideas", 1); + Interface_Static::SetIVal("read.step.nonmanifold", 1); + IFSelect_ReturnStatus aStat = reader.ReadFile(shape_file.c_str()); + if(aStat != IFSelect_RetDone) + std::cout << "Reading error for " << shape_file << std::endl; + + int NbTrans = reader.TransferRoots(); + // There should be only one shape within the file + assert(NbTrans==1); + aShape = reader.OneShape(); + + return true; +} + +/** + * @brief Export the content of a TopoDS_Shape into a shape file (STEP) + * + * @param shape_file the shape file + * @param aShape the object + * + * @return error code + */ +int exportSTEPShape(const std::string shape_file, const TopoDS_Shape& aShape){ + + std::cout << "Exporting STEP shape to " << shape_file << std::endl; + + STEPControl_Writer aWriter; + // Forcing Unit in meter + Interface_Static::SetCVal("xstep.cascade.unit","M"); + Interface_Static::SetCVal("write.step.unit","M"); + Interface_Static::SetIVal("write.step.nonmanifold", 1); + + IFSelect_ReturnStatus aStat = aWriter.Transfer(aShape,STEPControl_AsIs); + if(aStat != IFSelect_RetDone) + std::cout << "Transfer error for " << shape_file << std::endl; + + aStat = aWriter.Write(shape_file.c_str()); + + if(aStat != IFSelect_RetDone) + std::cout << "Writing error for " << shape_file << std::endl; + + return aStat; +} + +/** + * @brief Import the content of a shape file (BREP) into a TopDS_Shape object + * + * @param shape_file the shape file + * @param aShape the object + * + * @return error code + */ +int importBREPShape(const std::string shape_file, TopoDS_Shape& aShape){ + + std::cout << "Importing BREP shape from " << shape_file << std::endl; + BRep_Builder builder; + BRepTools::Read(aShape, shape_file.c_str(), builder); + + return true; +} + +/** + * @brief Export the content of a TopoDS_Shape into a shape file (BREP) + * + * @param shape_file the shape file + * @param aShape the object + * + * @return error code + */ +int exportBREPShape(const std::string shape_file, const TopoDS_Shape& aShape){ + + std::cout << "Exporting BREP shape to " << shape_file << std::endl; + BRepTools::Write(aShape, shape_file.c_str()); + + return true; +} + +/** + * @brief Import the content of a shape file into a TopDS_Shape object + * + * @param shape_file the shape file + * @param aShape the object + * + * @return error code + */ +int importShape(const std::string shape_file, TopoDS_Shape& aShape){ + std::string type = fs::path(shape_file).extension().string(); + boost::algorithm::to_lower(type); + if (type == ".brep"){ + return importBREPShape(shape_file, aShape); + } else if (type == ".step"){ + return importSTEPShape(shape_file, aShape); + } else { + std::cout << "Unknow format: " << type << std::endl; + return false; + } +} + +/** + * @brief Import the content of a shape file into a TopDS_Shape object + * + * @param shape_file the shape file + * @param aShape the object + * + * @return error code + */ +int exportShape(const std::string shape_file, const TopoDS_Shape& aShape){ + std::string type = fs::path(shape_file).extension().string(); + boost::algorithm::to_lower(type); + if (type == ".brep"){ + return exportBREPShape(shape_file, aShape); + } else if (type == ".step"){ + return exportSTEPShape(shape_file, aShape); + } else { + std::cout << "Unknow format: " << type << std::endl; + return false; + } +} diff --git a/src/SMESH/SMESH_DriverShape.hxx b/src/SMESH/SMESH_DriverShape.hxx new file mode 100644 index 000000000..b9e3a360e --- /dev/null +++ b/src/SMESH/SMESH_DriverShape.hxx @@ -0,0 +1,39 @@ +// Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE +// +// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 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 +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +// File : SMESH_DriverShape.hxx +// Author : Yoann AUDOUIN, EDF +// Module : SMESH +// + +#ifndef _SMESH_DRIVERSHAPE_HXX_ +#define _SMESH_DRIVERSHAPE_HXX_ + +#include +#include + +class TopoDS_Shape; + +int importShape(const std::string shape_file, TopoDS_Shape& aShape); +int exportShape(const std::string shape_file, const TopoDS_Shape& aShape); + +#endif \ No newline at end of file diff --git a/src/SMESH/SMESH_DriverStep.cxx b/src/SMESH/SMESH_DriverStep.cxx deleted file mode 100644 index b19be4891..000000000 --- a/src/SMESH/SMESH_DriverStep.cxx +++ /dev/null @@ -1,128 +0,0 @@ -// Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE -// -// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, -// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public -// License as published by the Free Software Foundation; either -// version 2.1 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 -// Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// -// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com -// - -// File : SMESH_DriverStep.cxx -// Author : Yoann AUDOUIN, EDF -// Module : SMESH -// - -#include "SMESH_DriverStep.hxx" - -#include -#include -#include - -//Occ include -#include - - -/** - * @brief Compares two shape file (STEP) - * - * @param file1 first file - * @param file2 second file - * - * @return true if the files are the same - */ -bool diffStepFile(std::string file1, std::string file2){ - std::ifstream sfile1(file1); - std::ifstream sfile2(file2); - std::string line1, line2; - int nb_lines = 0; - - while(!sfile1.eof() && !sfile2.eof()){ - std::getline(sfile1, line1); - std::getline(sfile2, line2); - nb_lines++; - // Skipping 4th line contain date of creation - if (nb_lines==4){ - std::cout << "Skipping line" << std::endl; - continue; - } - - // if lines are different end of read - if(line1 != line2){ - return false; - } - - } - // True if we reached the end of both files - return sfile1.eof() && sfile2.eof(); -} - -/** - * @brief Import the content of a shape file (STEP) into a TopDS_Shape object - * - * @param shape_file the shape file - * @param aShape the object - * - * @return error code - */ -int importShape(const std::string shape_file, TopoDS_Shape& aShape){ - - std::cout << "Importing shape from " << shape_file << std::endl; - STEPControl_Reader reader; - // Forcing Unit in meter - Interface_Static::SetCVal("xstep.cascade.unit","M"); - Interface_Static::SetIVal("read.step.ideas", 1); - Interface_Static::SetIVal("read.step.nonmanifold", 1); - IFSelect_ReturnStatus aStat = reader.ReadFile(shape_file.c_str()); - if(aStat != IFSelect_RetDone) - std::cout << "Reading error for " << shape_file << std::endl; - - int NbTrans = reader.TransferRoots(); - // There should be only one shape within the file - assert(NbTrans==1); - aShape = reader.OneShape(); - - return true; -} - -/** - * @brief Export the content of a TopoDS_Shape into a shape file (STEP) - * - * @param shape_file the shape file - * @param aShape the object - * - * @return error code - */ -int exportShape(const std::string shape_file, const TopoDS_Shape& aShape){ - - std::cout << "Exporting shape to " << shape_file << std::endl; - - STEPControl_Writer aWriter; - // Forcing Unit in meter - Interface_Static::SetCVal("xstep.cascade.unit","M"); - Interface_Static::SetCVal("write.step.unit","M"); - Interface_Static::SetIVal("write.step.nonmanifold", 1); - - IFSelect_ReturnStatus aStat = aWriter.Transfer(aShape,STEPControl_AsIs); - if(aStat != IFSelect_RetDone) - std::cout << "Transfer error for " << shape_file << std::endl; - - aStat = aWriter.Write(shape_file.c_str()); - - if(aStat != IFSelect_RetDone) - std::cout << "Writing error for " << shape_file << std::endl; - - return aStat; -} diff --git a/src/SMESH/SMESH_DriverStep.hxx b/src/SMESH/SMESH_DriverStep.hxx deleted file mode 100644 index 18158ae49..000000000 --- a/src/SMESH/SMESH_DriverStep.hxx +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE -// -// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, -// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public -// License as published by the Free Software Foundation; either -// version 2.1 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 -// Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// -// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com -// - -// File : SMESH_DriverStep.hxx -// Author : Yoann AUDOUIN, EDF -// Module : SMESH -// - -#ifndef _SMESH_DRIVERSTEP_HXX_ -#define _SMESH_DRIVERSTEP_HXX_ - -#include -#include - -class TopoDS_Shape; - -int importShape(const std::string shape_file, TopoDS_Shape& aShape); -int exportShape(const std::string shape_file, const TopoDS_Shape& aShape); -bool diffStepFile(std::string file1, std::string file2); - - -#endif \ No newline at end of file diff --git a/src/SMESH/SMESH_Mesh.cxx b/src/SMESH/SMESH_Mesh.cxx index 6690f0377..14255f568 100644 --- a/src/SMESH/SMESH_Mesh.cxx +++ b/src/SMESH/SMESH_Mesh.cxx @@ -87,7 +87,7 @@ namespace fs=boost::filesystem; #define MAX_MED_GROUP_NAME_LENGTH 80 #ifdef _DEBUG_ -static int MYDEBUG = 0; +static int MYDEBUG = 1; #else static int MYDEBUG = 0; #endif @@ -241,7 +241,7 @@ SMESH_Mesh::~SMESH_Mesh() int result=pthread_create(&thread, NULL, deleteMeshDS, (void*)_meshDS); #endif - fs::remove_all(tmp_folder); + //fs::remove_all(tmp_folder); } }