Salome HOME
Merge branch 'V9_7_BR'
[modules/smesh.git] / src / DriverMED / DriverMED_Family.h
1 // Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
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 GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SMESH DriverMED : tool to split groups on families
24 //  File   : DriverMED_Family.hxx
25 //  Author : Julia DOROVSKIKH
26 //  Module : SMESH
27 //
28 #ifndef _INCLUDE_DRIVERMED_FAMILY
29 #define _INCLUDE_DRIVERMED_FAMILY
30
31 #include "SMESH_DriverMED.hxx"
32 #include "DriverMED.hxx"
33
34 #include "SMDS_Mesh.hxx"
35 #include "SMESHDS_GroupBase.hxx"
36 #include "SMESHDS_SubMesh.hxx"
37 #include "MED_Common.hxx"
38
39 #include <boost/container/flat_set.hpp>
40 #include <boost/shared_ptr.hpp>
41 #include <set>
42 #include <limits>
43
44 #define REST_NODES_FAMILY 1
45 #define FIRST_NODE_FAMILY 2
46
47 #define REST_EDGES_FAMILY   -1
48 #define REST_FACES_FAMILY   -2
49 #define REST_VOLUMES_FAMILY -3
50 #define REST_0DELEM_FAMILY  -4
51 #define REST_BALL_FAMILY    -5
52 #define FIRST_ELEM_FAMILY   -6
53
54 // Not In Group families
55 #define NIG_EDGES_FAMILY   INT_MAX-1
56 #define NIG_FACES_FAMILY   INT_MAX-2
57 #define NIG_VOLS_FAMILY    INT_MAX-3
58 #define NIG_0DELEM_FAMILY  INT_MAX-4
59 #define NIG_BALL_FAMILY    INT_MAX-5
60 #define NIG_GROUP_PREFIX "NOT_IN_GRP"
61
62 typedef std::list<DriverMED_FamilyPtr                   > DriverMED_FamilyPtrList;
63 typedef std::map<int,SMESHDS_SubMesh*                   > SMESHDS_SubMeshPtrMap;
64 typedef std::list<SMESHDS_GroupBase*                    > SMESHDS_GroupBasePtrList;
65 typedef std::set<const SMDS_MeshElement*,TIDCompare     > ElementsSet;
66 typedef boost::container::flat_set< SMDSAbs_ElementType > ElemTypeSet;
67
68 class MESHDRIVERMED_EXPORT DriverMED_Family
69 {
70  public:
71
72   DriverMED_Family();
73
74   //! Methods for groups storing to MED
75   /*!
76     Split each group from list <theGroups> and each sub-mesh from list <theSubMeshes>
77     on some parts (families) on the basis of the elements membership in other groups
78     from <theGroups> and other sub-meshes from <theSubMeshes>.
79     Resulting families have no common elements.
80   */
81   static 
82   DriverMED_FamilyPtrList
83   MakeFamilies (SMESHDS_SubMeshIteratorPtr      theSubMeshes,
84                 const SMESHDS_GroupBasePtrList& theGroups,
85                 const bool doGroupOfNodes,
86                 const bool doGroupOfEdges,
87                 const bool doGroupOfFaces,
88                 const bool doGroupOfVolumes,
89                 const bool doGroupOf0DElems,
90                 const bool doGroupOfBalls,
91                 const bool doAllInGroups);
92
93   //! Create TFamilyInfo for this family
94   template<class LowLevelWriter>
95   MED::PFamilyInfo 
96   GetFamilyInfo(const LowLevelWriter& theWrapper, 
97                 const MED::PMeshInfo& theMeshInfo) const;
98
99   //! Returns elements of this family
100   const ElementsSet& GetElements () const;
101
102   //! Returns a family ID
103   int GetId () const;
104
105   //! Sets a family ID
106   void SetId (const int theId);
107
108  public:
109
110   // Methods for groups reading from MED
111
112   void AddElement(const SMDS_MeshElement* theElement);
113
114   const MED::TStringSet& GetGroupNames() const;
115   void AddGroupName(std::string theGroupName);
116
117   void SetType(const SMDSAbs_ElementType theType);
118   SMDSAbs_ElementType GetType();
119   const ElemTypeSet&  GetTypes() const;
120
121   bool MemberOf(std::string theGroupName) const;
122
123   int  GetGroupAttributVal() const;
124   void SetGroupAttributVal( int theValue);
125
126   size_t NbElements( SMDSAbs_ElementType ) const;
127
128  private:
129   //! Initialize the tool by SMESHDS_GroupBase
130   void Init (SMESHDS_GroupBase* group);
131
132   //! Split <theSubMesh> on some parts (families) on the basis of the elements type.
133   static
134   DriverMED_FamilyPtrList SplitByType(SMESHDS_SubMesh* theSubMesh,
135                                       const int        theId);
136
137
138   /*! Remove from <Elements> elements, common with <by>,
139     Remove from <by> elements, common with <Elements>,
140     Create family <common> from common elements, with combined groups list.
141   */
142   void Split (DriverMED_FamilyPtr by,
143               DriverMED_FamilyPtr common);
144
145   //! Check, if this family has empty list of elements
146   bool IsEmpty () const;
147
148
149  private:
150   int                 myId;
151   SMDSAbs_ElementType myType;
152   ElementsSet         myElements;
153   MED::TStringSet     myGroupNames;
154   int                 myGroupAttributVal;
155   ElemTypeSet         myTypes; // Issue 0020576
156 };
157
158 #include "MED_Factory.hxx"
159
160 #include <set>
161 #include <string>
162 //=============================================================================
163 /*!
164  *  Create TFamilyInfo for this family
165  */
166 //=============================================================================
167 template<class LowLevelWriter>
168 MED::PFamilyInfo
169 DriverMED_Family::GetFamilyInfo(const LowLevelWriter& theWrapper, 
170                                 const MED::PMeshInfo& theMeshInfo) const
171 {
172   std::ostringstream aStr;
173   aStr << "FAM_" << myId;
174   std::set<std::string>::const_iterator aGrIter = myGroupNames.begin();
175   for(; aGrIter != myGroupNames.end(); aGrIter++){
176     aStr << "_" << *aGrIter;
177   }
178   std::string aValue = aStr.str();
179   // PAL19785,0019867 - med forbids whitespace to be the last char in the name
180   int maxSize = MED::GetNOMLength();
181   int lastCharPos = std::min( maxSize, (int) aValue.size() ) - 1;
182   while ( isspace( aValue[ lastCharPos ] ))
183     aValue.resize( lastCharPos-- );
184
185   MED::PFamilyInfo anInfo;
186   if(myId == 0 || myGroupAttributVal == 0){
187     anInfo = theWrapper->CrFamilyInfo(theMeshInfo,
188                                       aValue,
189                                       myId,
190                                       myGroupNames);
191   }else{
192     MED::TStringVector anAttrDescs (1, "");  // 1 attribute with empty description,
193     MED::TIntVector anAttrIds (1, myId);        // Id=0,
194     MED::TIntVector anAttrVals (1, myGroupAttributVal);
195     anInfo = theWrapper->CrFamilyInfo(theMeshInfo,
196                                       aValue,
197                                       myId,
198                                       myGroupNames,
199                                       anAttrDescs,
200                                       anAttrIds,
201                                       anAttrVals);
202   }
203
204 //  cout << endl;
205 //  cout << "Groups: ";
206 //  set<string>::iterator aGrIter = myGroupNames.begin();
207 //  for (; aGrIter != myGroupNames.end(); aGrIter++)
208 //  {
209 //    cout << " " << *aGrIter;
210 //  }
211 //  cout << endl;
212 //
213 //  cout << "Elements: ";
214 //  set<const SMDS_MeshElement *>::iterator anIter = myElements.begin();
215 //  for (; anIter != myElements.end(); anIter++)
216 //  {
217 //    cout << " " << (*anIter)->GetID();
218 //  }
219 //  cout << endl;
220
221   return anInfo;
222 }
223
224 #endif