Salome HOME
Porting Salome to OCCT 7.7.0
[modules/smesh.git] / src / SMESH / SMESH_Mesh.hxx
1 // Copyright (C) 2007-2022  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 //  File   : SMESH_Mesh.hxx
24 //  Author : Paul RASCLE, EDF
25 //  Module : SMESH
26 //
27 #ifndef _SMESH_MESH_HXX_
28 #define _SMESH_MESH_HXX_
29
30 #include "SMESH_SMESH.hxx"
31
32 #include "SMDSAbs_ElementType.hxx"
33 #include "SMESH_ComputeError.hxx"
34 #include "SMESH_Controls.hxx"
35 #include "SMESH_Hypothesis.hxx"
36 #include "SMDS_Iterator.hxx"
37
38 #include "Utils_SALOME_Exception.hxx"
39
40 #include <TopoDS_Shape.hxx>
41 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
42 #include <TopTools_ListOfShape.hxx>
43
44 #include "MEDCouplingMemArray.hxx"
45
46 #include <map>
47 #include <list>
48 #include <vector>
49 #include <ostream>
50
51 #ifndef WIN32
52 #include <boost/filesystem.hpp>
53 #include <boost/asio/thread_pool.hpp>
54 #endif
55 #include <boost/thread.hpp>
56
57 #ifdef WIN32
58 #pragma warning(disable:4251) // Warning DLL Interface ...
59 #pragma warning(disable:4290) // Warning Exception ...
60 #endif
61
62 class SMESHDS_Command;
63 class SMESHDS_Document;
64 class SMESHDS_GroupBase;
65 class SMESHDS_Hypothesis;
66 class SMESHDS_Mesh;
67 class SMESH_Gen;
68 class SMESH_Group;
69 class SMESH_HypoFilter;
70 class SMESH_subMesh;
71 class TopoDS_Solid;
72
73 class DriverMED_W_SMESHDS_Mesh;
74
75 typedef std::list<int> TListOfInt;
76 typedef std::list<TListOfInt> TListOfListOfInt;
77
78 class SMESH_EXPORT SMESH_Mesh
79 {
80  public:
81   SMESH_Mesh(int               theLocalId,
82              SMESH_Gen*        theGen,
83              bool              theIsEmbeddedMode,
84              SMESHDS_Document* theDocument);
85
86   virtual ~SMESH_Mesh();
87
88   /*!
89    * \brief Set geometry to be meshed
90    */
91   void ShapeToMesh(const TopoDS_Shape & aShape);
92   /*!
93    * \brief Return geometry to be meshed. (It may be a PseudoShape()!)
94    */
95   TopoDS_Shape GetShapeToMesh() const;
96   /*!
97    * \brief Return true if there is a geometry to be meshed, not PseudoShape()
98    */
99   bool HasShapeToMesh() const { return _isShapeToMesh; }
100
101   void UndefShapeToMesh() { _isShapeToMesh = false; }
102
103   /*!
104    * \brief Return diagonal size of bounding box of shape to mesh.
105    */
106   double GetShapeDiagonalSize() const;
107   /*!
108    * \brief Return diagonal size of bounding box of a shape.
109    */
110   static double GetShapeDiagonalSize(const TopoDS_Shape & aShape);
111   /*!
112    * \brief Return a solid which is returned by GetShapeToMesh() if
113    *        a real geometry to be meshed was not set
114    */
115   static const TopoDS_Solid& PseudoShape();
116
117   /*!
118    * \brief Load mesh from study file
119    */
120   void Load();
121   /*!
122    * \brief Remove all nodes and elements
123    */
124   void Clear();
125   /*!
126    * \brief Remove all nodes and elements of indicated shape
127    */
128   void ClearSubMesh(const int theShapeId);
129
130   /*!
131    * consult DriverMED_R_SMESHDS_Mesh::ReadStatus for returned value
132    */
133   int UNVToMesh(const char* theFileName);
134
135   int MEDToMesh(const char* theFileName, const char* theMeshName);
136
137   std::string STLToMesh(const char* theFileName);
138
139   int CGNSToMesh(const char* theFileName, const int theMeshIndex, std::string& theMeshName);
140
141   SMESH_ComputeErrorPtr GMFToMesh(const char* theFileName,
142                                   bool        theMakeRequiredGroups = true );
143
144   SMESH_Hypothesis::Hypothesis_Status
145   AddHypothesis(const TopoDS_Shape & aSubShape, int anHypId, std::string* error=0);
146
147   SMESH_Hypothesis::Hypothesis_Status
148   RemoveHypothesis(const TopoDS_Shape & aSubShape, int anHypId);
149
150   const std::list <const SMESHDS_Hypothesis * >&
151   GetHypothesisList(const TopoDS_Shape & aSubShape) const;
152
153   const SMESH_Hypothesis * GetHypothesis(const TopoDS_Shape &    aSubShape,
154                                          const SMESH_HypoFilter& aFilter,
155                                          const bool              andAncestors,
156                                          TopoDS_Shape*           assignedTo=0) const;
157
158   int GetHypotheses(const TopoDS_Shape &                     aSubShape,
159                     const SMESH_HypoFilter&                  aFilter,
160                     std::list< const SMESHDS_Hypothesis * >& aHypList,
161                     const bool                               andAncestors,
162                     std::list< TopoDS_Shape > *              assignedTo=0) const;
163
164   const SMESH_Hypothesis * GetHypothesis(const SMESH_subMesh *   aSubMesh,
165                                          const SMESH_HypoFilter& aFilter,
166                                          const bool              andAncestors,
167                                          TopoDS_Shape*           assignedTo=0) const;
168
169   int GetHypotheses(const SMESH_subMesh *                    aSubMesh,
170                     const SMESH_HypoFilter&                  aFilter,
171                     std::list< const SMESHDS_Hypothesis * >& aHypList,
172                     const bool                               andAncestors,
173                     std::list< TopoDS_Shape > *              assignedTo=0) const;
174
175   SMESH_Hypothesis * GetHypothesis(const int aHypID) const;
176
177   const std::list<SMESHDS_Command*> & GetLog();
178
179   void ClearLog();
180
181   int GetId() const          { return _id; }
182
183   bool MeshExists( int meshId ) const;
184
185   SMESH_Mesh* FindMesh( int meshId ) const;
186
187   SMESHDS_Mesh * GetMeshDS() { return _meshDS; }
188
189   const SMESHDS_Mesh * GetMeshDS() const { return _meshDS; }
190
191   SMESH_Gen *GetGen()        { return _gen; }
192
193   SMESH_subMesh *GetSubMesh(const TopoDS_Shape & aSubShape);
194
195   SMESH_subMesh *GetSubMeshContaining(const TopoDS_Shape & aSubShape) const;
196
197   SMESH_subMesh *GetSubMeshContaining(const int aShapeID) const;
198   /*!
199    * \brief Return submeshes of groups containing the given subshape
200    */
201   std::list<SMESH_subMesh*> GetGroupSubMeshesContaining(const TopoDS_Shape & shape) const;
202   /*!
203    * \brief Say all submeshes that theChangedHyp has been modified
204    */
205   void NotifySubMeshesHypothesisModification(const SMESH_Hypothesis* theChangedHyp);
206
207   // const std::list < SMESH_subMesh * >&
208   // GetSubMeshUsingHypothesis(SMESHDS_Hypothesis * anHyp) throw(SALOME_Exception);
209   /*!
210    * \brief Return True if anHyp is used to mesh aSubShape
211    */
212   bool IsUsedHypothesis(SMESHDS_Hypothesis *  anHyp,
213                         const SMESH_subMesh * aSubMesh);
214   /*!
215    * \brief check if a hypothesis allowing notconform mesh is present
216    */
217   bool IsNotConformAllowed() const;
218
219   bool IsMainShape(const TopoDS_Shape& theShape) const;
220
221   TopoDS_Shape GetShapeByEntry(const std::string& entry) const;
222
223   /*!
224    * \brief Return list of ancestors of theSubShape in the order
225    *        that lower dimension shapes come first
226    */
227   const TopTools_ListOfShape& GetAncestors(const TopoDS_Shape& theSubShape) const;
228
229   void SetAutoColor(bool theAutoColor);
230
231   bool GetAutoColor();
232
233   /*!
234    * \brief Set the flag meaning that the mesh has been edited "manually".
235    * It is to set to false after Clear() and to set to true by MeshEditor
236    */
237   void SetIsModified(bool isModified);
238
239   bool GetIsModified() const { return _isModified; }
240
241   /*!
242    * \brief Return true if the mesh has been edited since a total re-compute
243    *        and those modifications may prevent successful partial re-compute.
244    *        As a side effect reset _isModified flag if mesh is empty
245    */
246   bool HasModificationsToDiscard() const;
247
248   /*!
249    * \brief Return true if all sub-meshes are computed OK - to update an icon
250    */
251   bool IsComputedOK();
252
253   /*!
254    * \brief Return data map of descendant to ancestor shapes
255    */
256   typedef TopTools_IndexedDataMapOfShapeListOfShape TAncestorMap;
257   const TAncestorMap& GetAncestorMap() const { return _mapAncestors; }
258
259   /*!
260    * \brief Check group names for duplications.
261    *  Consider maximum group name length stored in MED file
262    */
263   bool HasDuplicatedGroupNamesMED();
264
265   /*!
266    * \brief Exception thrown by Export*() in case if a mesh is too large for export
267    *        due to limitation of a format
268    */
269   struct TooLargeForExport : public std::runtime_error
270   {
271     TooLargeForExport(const char* format):runtime_error(format) {}
272   };
273
274   MEDCoupling::MCAuto<MEDCoupling::DataArrayByte>
275     ExportMEDCoupling(const char*         theMeshName = NULL,
276                       bool                theAutoGroups = true,
277                       const SMESHDS_Mesh* theMeshPart = 0,
278                       bool                theAutoDimension = false,
279                       bool                theAddODOnVertices = false,
280                       double              theZTolerance = -1.,
281                       bool                theSaveNumbers = true);
282
283   void ExportMED(const char *        theFile,
284                  const char*         theMeshName = NULL,
285                  bool                theAutoGroups = true,
286                  int                 theVersion = -1,
287                  const SMESHDS_Mesh* theMeshPart = 0,
288                  bool                theAutoDimension = false,
289                  bool                theAddODOnVertices = false,
290                  double              theZTolerance = -1.,
291                  bool                theSaveNumbers = true);
292
293   void ExportDAT(const char *        file,
294                  const SMESHDS_Mesh* meshPart = 0,
295                  const bool          renumber = true);
296   void ExportUNV(const char *        file,
297                  const SMESHDS_Mesh* meshPart = 0,
298                  const bool          renumber = true);
299   void ExportSTL(const char *        file,
300                  const bool          isascii,
301                  const char *        name = 0,
302                  const SMESHDS_Mesh* meshPart = 0);
303   void ExportCGNS(const char *        file,
304                   const SMESHDS_Mesh* mesh,
305                   const char *        meshName = 0,
306                   const bool          groupElemsByType = false);
307   void ExportGMF(const char *        file,
308                  const SMESHDS_Mesh* mesh,
309                  bool                withRequiredGroups = true );
310
311   double GetComputeProgress() const;
312
313   smIdType NbNodes() const;
314   smIdType Nb0DElements() const;
315   smIdType NbBalls() const;
316
317   smIdType NbEdges(SMDSAbs_ElementOrder order = ORDER_ANY) const;
318
319   smIdType NbFaces(SMDSAbs_ElementOrder order = ORDER_ANY) const;
320   smIdType NbTriangles(SMDSAbs_ElementOrder order = ORDER_ANY) const;
321   smIdType NbQuadrangles(SMDSAbs_ElementOrder order = ORDER_ANY) const;
322   smIdType NbBiQuadQuadrangles() const;
323   smIdType NbBiQuadTriangles() const;
324   smIdType NbPolygons(SMDSAbs_ElementOrder order = ORDER_ANY) const;
325
326   smIdType NbVolumes(SMDSAbs_ElementOrder order = ORDER_ANY) const;
327   smIdType NbTetras(SMDSAbs_ElementOrder order = ORDER_ANY) const;
328   smIdType NbHexas(SMDSAbs_ElementOrder order = ORDER_ANY) const;
329   smIdType NbTriQuadraticHexas() const;
330   smIdType NbPyramids(SMDSAbs_ElementOrder order = ORDER_ANY) const;
331   smIdType NbPrisms(SMDSAbs_ElementOrder order = ORDER_ANY) const;
332   smIdType NbQuadPrisms() const;
333   smIdType NbBiQuadPrisms() const;
334   smIdType NbHexagonalPrisms() const;
335   smIdType NbPolyhedrons() const;
336
337   smIdType NbSubMesh() const;
338
339   size_t NbGroup() const { return _mapGroup.size(); }
340
341   int NbMeshes() const; // nb meshes in the Study
342
343   SMESH_Group* AddGroup (const SMDSAbs_ElementType theType,
344                          const char*               theName,
345                          const int                 theId = -1,
346                          const TopoDS_Shape&       theShape = TopoDS_Shape(),
347                          const SMESH_PredicatePtr& thePredicate = SMESH_PredicatePtr());
348
349   SMESH_Group* AddGroup (SMESHDS_GroupBase* groupDS);
350
351   typedef boost::shared_ptr< SMDS_Iterator<SMESH_Group*> > GroupIteratorPtr;
352   GroupIteratorPtr GetGroups() const;
353
354   std::list<int> GetGroupIds() const;
355
356   SMESH_Group* GetGroup (const int theGroupID) const;
357
358   bool RemoveGroup (const int theGroupID);
359
360   SMESH_Group* ConvertToStandalone ( int theGroupID );
361
362   struct TCallUp // callback from SMESH to SMESH_I level
363   {
364     virtual void RemoveGroup( const int theGroupID )=0;
365     virtual void HypothesisModified( int hypID, bool updateIcons )=0;
366     virtual void Load()=0;
367     virtual bool IsLoaded()=0;
368     virtual TopoDS_Shape GetShapeByEntry(const std::string& entry)=0;
369     virtual ~TCallUp() {}
370   };
371   void SetCallUp( TCallUp * upCaller );
372
373   bool SynchronizeGroups();
374
375
376   SMDSAbs_ElementType GetElementType( const smIdType id, const bool iselem );
377
378   void ClearMeshOrder();
379   void SetMeshOrder(const TListOfListOfInt& theOrder );
380   const TListOfListOfInt& GetMeshOrder() const;
381
382   // sort submeshes according to stored mesh order
383   bool SortByMeshOrder(std::vector<SMESH_subMesh*>& theListToSort) const;
384
385   // return true if given order of sub-meshes is OK
386   bool IsOrderOK( const SMESH_subMesh* smBefore,
387                   const SMESH_subMesh* smAfter ) const;
388
389   std::ostream& Dump(std::ostream & save);
390
391   // Parallel computation functions
392
393 #ifdef WIN32
394   void Lock() {};
395   void Unlock() {};
396
397   int GetNbThreads(){return _NbThreads;};
398   void SetNbThreads(long nbThreads){std::cout << "Warning Parallel Meshing is disabled on Windows it will behave as a slower normal compute" << std::endl;_NbThreads=nbThreads;};
399
400   void InitPoolThreads(){};
401   void DeletePoolThreads(){};
402   void wait(){}
403
404   bool IsParallel(){return _NbThreads > 0;}
405 #else
406   void Lock() {_my_lock.lock();};
407   void Unlock() {_my_lock.unlock();};
408
409   int GetNbThreads(){return _NbThreads;};
410   void SetNbThreads(long nbThreads){_NbThreads=nbThreads;};
411
412   void InitPoolThreads(){_pool = new boost::asio::thread_pool(_NbThreads);};
413   void DeletePoolThreads(){delete _pool;};
414
415   void wait(){_pool->join(); DeletePoolThreads(); InitPoolThreads(); }
416
417   bool IsParallel(){return _NbThreads > 0;}
418 #endif
419
420   void CreateTmpFolder();
421   void DeleteTmpFolder();
422
423   // Temporary folder used during parallel Computation
424 #ifndef WIN32
425   boost::filesystem::path tmp_folder;
426   boost::asio::thread_pool *     _pool = nullptr; //thread pool for computation
427 #else
428   std::string tmp_folder;
429   bool _pool = false;
430 #endif
431
432
433 private:
434
435   void exportMEDCommmon(DriverMED_W_SMESHDS_Mesh& myWriter,
436                         const char*               theMeshName,
437                         bool                      theAutoGroups,
438                         const SMESHDS_Mesh*       meshPart,
439                         bool                      theAutoDimension,
440                         bool                      theAddODOnVertices,
441                         double                    theZTolerance,
442                         bool                      theSaveNumbers);
443
444  private:
445   void fillAncestorsMap(const TopoDS_Shape& theShape);
446   void getAncestorsSubMeshes(const TopoDS_Shape&            theSubShape,
447                              std::vector< SMESH_subMesh* >& theSubMeshes) const;
448
449 protected:
450   int                        _id;           // id given by creator (unique within the creator instance)
451   int                        _groupId;      // id generator for group objects
452   int                        _nbSubShapes;  // initial nb of subshapes in the shape to mesh
453   bool                       _isShapeToMesh;// set to true when a shape is given (only once)
454   SMESHDS_Document *         _document;
455   SMESHDS_Mesh *             _meshDS;
456   SMESH_Gen *                _gen;
457   std::map <int, SMESH_Group*> _mapGroup;
458
459   class SubMeshHolder;
460   SubMeshHolder*             _subMeshHolder;
461
462   bool                       _isAutoColor;
463   bool                       _isModified; //!< modified since last total re-compute, issue 0020693
464
465   double                     _shapeDiagonal; //!< diagonal size of bounding box of shape to mesh
466
467   TopTools_IndexedDataMapOfShapeListOfShape _mapAncestors;
468
469   mutable std::vector<SMESH_subMesh*> _ancestorSubMeshes; // to speed up GetHypothes[ei]s()
470
471   TListOfListOfInt           _subMeshOrder;
472
473   // Struct calling methods at CORBA API implementation level, used to
474   // 1) make an upper level (SMESH_I) be consistent with a lower one (SMESH)
475   // when group removal is invoked by hyp modification (issue 0020918)
476   // 2) to forget not loaded mesh data at hyp modification
477   TCallUp*                    _callUp;
478
479   // Mutex for multhitreading write in SMESH_Mesh
480 #ifndef WIN32
481   boost::mutex _my_lock;
482 #endif
483   int _NbThreads=0;
484
485 protected:
486   SMESH_Mesh();
487   SMESH_Mesh(const SMESH_Mesh&) {};
488 };
489 #endif