Salome HOME
Copyright update 2020
[modules/homard.git] / src / FrontTrack / FrontTrack_Utils.cxx
1 // Copyright (C) 2017-2020  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // File      : FrontTrack_Utils.cxx
20 // Created   : Tue Apr 25 17:28:58 2017
21 // Author    : Edward AGAPOV (eap)
22
23 #include "FrontTrack_Utils.hxx"
24
25 #include <XAO_Xao.hxx>
26 #include <XAO_Group.hxx>
27
28 #include <fcntl.h>
29 #include <boost/filesystem.hpp>
30
31 namespace boofs = boost::filesystem;
32
33 //================================================================================
34 /*
35  * \brief Check if a file exists
36  */
37 //================================================================================
38
39 bool FT_Utils::fileExists( const std::string& path )
40 {
41   if ( path.empty() )
42     return false;
43
44   boost::system::error_code err;
45   bool res = boofs::exists( path, err );
46
47   return err ? false : res;
48 }
49
50 //================================================================================
51 /*!
52  * \brief Check if a file can be created/overwritten
53  */
54 //================================================================================
55
56 bool FT_Utils::canWrite( const std::string& path )
57 {
58   if ( path.empty() )
59     return false;
60
61   bool can = false;
62 #ifdef WIN32
63
64   HANDLE file = CreateFile( path.c_str(),           // name of the write
65                             GENERIC_WRITE,          // open for writing
66                             0,                      // do not share
67                             NULL,                   // default security
68                             OPEN_ALWAYS,            // CREATE NEW or OPEN EXISTING
69                             FILE_ATTRIBUTE_NORMAL,  // normal file
70                             NULL);                  // no attr. template
71   can = ( file != INVALID_HANDLE_VALUE );
72   CloseHandle( file );
73
74 #else
75
76   int file = ::open( path.c_str(),
77                      O_WRONLY | O_CREAT,
78                      S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ); // rw-r--r--
79   can = ( file >= 0 );
80
81 #endif
82
83   return can;
84 }
85
86 //================================================================================
87 /*!
88  * \brief Make a map of XAO groups
89  */
90 //================================================================================
91
92 FT_Utils::XaoGroups::XaoGroups( const XAO::Xao* theXao )
93 {
94   XAO::Xao* xao = const_cast< XAO::Xao* >( theXao );
95
96   for ( int iG = 0; iG < theXao->countGroups(); ++iG )
97   {
98     XAO::Group* group = xao->getGroup( iG );
99
100     if ( group->getDimension() == 1 )
101       
102       _xaoGroups[ 0 ].insert( std::make_pair( group->getName(), group ));
103
104     else if ( group->getDimension() == 2 )
105
106       _xaoGroups[ 1 ].insert( std::make_pair( group->getName(), group ));
107   }
108 }
109
110 //================================================================================
111 /*!
112  * \brief Return FT_Projector's by a group name
113  *  \param [in] groupName - the group name
114  *  \param [in] dim - the group dimension
115  *  \param [in] allProjectors - the projector of all shapes of \a dim dimension
116  *  \param [out] groupProjectors - projectors to shapes of the group
117  *  \return int - number of found shapes
118  */
119 //================================================================================
120
121 int FT_Utils::XaoGroups::getProjectors( const std::string&                   groupName,
122                                         const int                            dim,
123                                         const std::vector< FT_Projector > &  allProjectors,
124                                         std::vector< const FT_Projector* > & groupProjectors) const
125 {
126   // get namesake groups
127
128   const TGroupByNameMap* groupMap = 0;
129   if ( dim == 1 )
130     groupMap = &_xaoGroups[ 0 ];
131   else if ( dim == 2 )
132     groupMap = &_xaoGroups[ 1 ];
133   else
134     return 0;
135
136   TGroupByNameMap::const_iterator name2gr = groupMap->find( groupName );
137   if ( name2gr == groupMap->end() )
138     return 0;
139
140   std::vector< XAO::Group* > groups;
141   groups.push_back( name2gr->second );
142
143   for ( ++name2gr; name2gr != groupMap->end(); ++name2gr )
144   {
145     if ( name2gr->second->getName() == groupName )
146       groups.push_back( name2gr->second );
147     else
148       break;
149   }
150
151   // get projectors
152
153   int nbFound = 0;
154   for ( size_t i = 0; i < groups.size(); ++i )
155   {
156     // IDs in XAO correspond to indices of allProjectors
157     std::set<int>::iterator id = groups[i]->begin(), end = groups[i]->end();
158     for ( ; id != end; ++id, ++nbFound )
159       if ( *id < (int) allProjectors.size() )
160         groupProjectors.push_back ( & allProjectors[ *id ]);
161   }
162
163   return nbFound;
164 }