1 // Copyright (C) 2007-2022 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : SMESH_Mesh.hxx
24 // Author : Paul RASCLE, EDF
27 #ifndef _SMESH_MESH_HXX_
28 #define _SMESH_MESH_HXX_
30 #include "SMESH_SMESH.hxx"
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"
38 #include "Utils_SALOME_Exception.hxx"
40 #include <TopoDS_Shape.hxx>
41 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
42 #include <TopTools_ListOfShape.hxx>
44 #include "MEDCouplingMemArray.hxx"
52 #include <boost/filesystem.hpp>
53 #include <boost/asio/thread_pool.hpp>
55 #include <boost/thread.hpp>
58 #pragma warning(disable:4251) // Warning DLL Interface ...
59 #pragma warning(disable:4290) // Warning Exception ...
62 class SMESHDS_Command;
63 class SMESHDS_Document;
64 class SMESHDS_GroupBase;
65 class SMESHDS_Hypothesis;
69 class SMESH_HypoFilter;
73 class DriverMED_W_SMESHDS_Mesh;
75 typedef std::list<int> TListOfInt;
76 typedef std::list<TListOfInt> TListOfListOfInt;
78 class SMESH_EXPORT SMESH_Mesh
81 SMESH_Mesh(int theLocalId,
83 bool theIsEmbeddedMode,
84 SMESHDS_Document* theDocument);
86 virtual ~SMESH_Mesh();
89 * \brief Set geometry to be meshed
91 void ShapeToMesh(const TopoDS_Shape & aShape);
93 * \brief Return geometry to be meshed. (It may be a PseudoShape()!)
95 TopoDS_Shape GetShapeToMesh() const;
97 * \brief Return true if there is a geometry to be meshed, not PseudoShape()
99 bool HasShapeToMesh() const { return _isShapeToMesh; }
101 void UndefShapeToMesh() { _isShapeToMesh = false; }
104 * \brief Return diagonal size of bounding box of shape to mesh.
106 double GetShapeDiagonalSize() const;
108 * \brief Return diagonal size of bounding box of a shape.
110 static double GetShapeDiagonalSize(const TopoDS_Shape & aShape);
112 * \brief Return a solid which is returned by GetShapeToMesh() if
113 * a real geometry to be meshed was not set
115 static const TopoDS_Solid& PseudoShape();
118 * \brief Load mesh from study file
122 * \brief Remove all nodes and elements
126 * \brief Remove all nodes and elements of indicated shape
128 void ClearSubMesh(const int theShapeId);
131 * consult DriverMED_R_SMESHDS_Mesh::ReadStatus for returned value
133 int UNVToMesh(const char* theFileName);
135 int MEDToMesh(const char* theFileName, const char* theMeshName);
137 std::string STLToMesh(const char* theFileName);
139 int CGNSToMesh(const char* theFileName, const int theMeshIndex, std::string& theMeshName);
141 SMESH_ComputeErrorPtr GMFToMesh(const char* theFileName,
142 bool theMakeRequiredGroups = true );
144 SMESH_Hypothesis::Hypothesis_Status
145 AddHypothesis(const TopoDS_Shape & aSubShape, int anHypId, std::string* error=0);
147 SMESH_Hypothesis::Hypothesis_Status
148 RemoveHypothesis(const TopoDS_Shape & aSubShape, int anHypId);
150 const std::list <const SMESHDS_Hypothesis * >&
151 GetHypothesisList(const TopoDS_Shape & aSubShape) const;
153 const SMESH_Hypothesis * GetHypothesis(const TopoDS_Shape & aSubShape,
154 const SMESH_HypoFilter& aFilter,
155 const bool andAncestors,
156 TopoDS_Shape* assignedTo=0) const;
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;
164 const SMESH_Hypothesis * GetHypothesis(const SMESH_subMesh * aSubMesh,
165 const SMESH_HypoFilter& aFilter,
166 const bool andAncestors,
167 TopoDS_Shape* assignedTo=0) const;
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;
175 SMESH_Hypothesis * GetHypothesis(const int aHypID) const;
177 const std::list<SMESHDS_Command*> & GetLog();
181 int GetId() const { return _id; }
183 bool MeshExists( int meshId ) const;
185 SMESH_Mesh* FindMesh( int meshId ) const;
187 SMESHDS_Mesh * GetMeshDS() { return _meshDS; }
189 const SMESHDS_Mesh * GetMeshDS() const { return _meshDS; }
191 SMESH_Gen *GetGen() { return _gen; }
193 SMESH_subMesh *GetSubMesh(const TopoDS_Shape & aSubShape);
195 SMESH_subMesh *GetSubMeshContaining(const TopoDS_Shape & aSubShape) const;
197 SMESH_subMesh *GetSubMeshContaining(const int aShapeID) const;
199 * \brief Return submeshes of groups containing the given subshape
201 std::list<SMESH_subMesh*> GetGroupSubMeshesContaining(const TopoDS_Shape & shape) const;
203 * \brief Say all submeshes that theChangedHyp has been modified
205 void NotifySubMeshesHypothesisModification(const SMESH_Hypothesis* theChangedHyp);
207 // const std::list < SMESH_subMesh * >&
208 // GetSubMeshUsingHypothesis(SMESHDS_Hypothesis * anHyp) throw(SALOME_Exception);
210 * \brief Return True if anHyp is used to mesh aSubShape
212 bool IsUsedHypothesis(SMESHDS_Hypothesis * anHyp,
213 const SMESH_subMesh * aSubMesh);
215 * \brief check if a hypothesis allowing notconform mesh is present
217 bool IsNotConformAllowed() const;
219 bool IsMainShape(const TopoDS_Shape& theShape) const;
221 TopoDS_Shape GetShapeByEntry(const std::string& entry) const;
224 * \brief Return list of ancestors of theSubShape in the order
225 * that lower dimension shapes come first
227 const TopTools_ListOfShape& GetAncestors(const TopoDS_Shape& theSubShape) const;
229 void SetAutoColor(bool theAutoColor);
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
237 void SetIsModified(bool isModified);
239 bool GetIsModified() const { return _isModified; }
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
246 bool HasModificationsToDiscard() const;
249 * \brief Return true if all sub-meshes are computed OK - to update an icon
254 * \brief Return data map of descendant to ancestor shapes
256 typedef TopTools_IndexedDataMapOfShapeListOfShape TAncestorMap;
257 const TAncestorMap& GetAncestorMap() const { return _mapAncestors; }
260 * \brief Check group names for duplications.
261 * Consider maximum group name length stored in MED file
263 bool HasDuplicatedGroupNamesMED();
266 * \brief Exception thrown by Export*() in case if a mesh is too large for export
267 * due to limitation of a format
269 struct TooLargeForExport : public std::runtime_error
271 TooLargeForExport(const char* format):runtime_error(format) {}
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);
283 void ExportMED(const char * theFile,
284 const char* theMeshName = NULL,
285 bool theAutoGroups = true,
287 const SMESHDS_Mesh* theMeshPart = 0,
288 bool theAutoDimension = false,
289 bool theAddODOnVertices = false,
290 double theZTolerance = -1.,
291 bool theSaveNumbers = true);
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,
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 );
311 double GetComputeProgress() const;
313 smIdType NbNodes() const;
314 smIdType Nb0DElements() const;
315 smIdType NbBalls() const;
317 smIdType NbEdges(SMDSAbs_ElementOrder order = ORDER_ANY) const;
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;
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;
337 smIdType NbSubMesh() const;
339 size_t NbGroup() const { return _mapGroup.size(); }
341 int NbMeshes() const; // nb meshes in the Study
343 SMESH_Group* AddGroup (const SMDSAbs_ElementType theType,
345 const int theId = -1,
346 const TopoDS_Shape& theShape = TopoDS_Shape(),
347 const SMESH_PredicatePtr& thePredicate = SMESH_PredicatePtr());
349 SMESH_Group* AddGroup (SMESHDS_GroupBase* groupDS);
351 typedef boost::shared_ptr< SMDS_Iterator<SMESH_Group*> > GroupIteratorPtr;
352 GroupIteratorPtr GetGroups() const;
354 std::list<int> GetGroupIds() const;
356 SMESH_Group* GetGroup (const int theGroupID) const;
358 bool RemoveGroup (const int theGroupID);
360 SMESH_Group* ConvertToStandalone ( int theGroupID );
362 struct TCallUp // callback from SMESH to SMESH_I level
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() {}
371 void SetCallUp( TCallUp * upCaller );
373 bool SynchronizeGroups();
376 SMDSAbs_ElementType GetElementType( const smIdType id, const bool iselem );
378 void ClearMeshOrder();
379 void SetMeshOrder(const TListOfListOfInt& theOrder );
380 const TListOfListOfInt& GetMeshOrder() const;
382 // sort submeshes according to stored mesh order
383 bool SortByMeshOrder(std::vector<SMESH_subMesh*>& theListToSort) const;
385 // return true if given order of sub-meshes is OK
386 bool IsOrderOK( const SMESH_subMesh* smBefore,
387 const SMESH_subMesh* smAfter ) const;
389 std::ostream& Dump(std::ostream & save);
391 // Parallel computation functions
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;};
400 void InitPoolThreads(){};
401 void DeletePoolThreads(){};
404 bool IsParallel(){return _NbThreads > 0;}
406 void Lock() {_my_lock.lock();};
407 void Unlock() {_my_lock.unlock();};
409 int GetNbThreads(){return _NbThreads;};
410 void SetNbThreads(long nbThreads){_NbThreads=nbThreads;};
412 void InitPoolThreads(){_pool = new boost::asio::thread_pool(_NbThreads);};
413 void DeletePoolThreads(){delete _pool;};
415 void wait(){_pool->join(); DeletePoolThreads(); InitPoolThreads(); }
417 bool IsParallel(){return _NbThreads > 0;}
420 void CreateTmpFolder();
421 void DeleteTmpFolder();
423 // Temporary folder used during parallel Computation
425 boost::filesystem::path tmp_folder;
426 boost::asio::thread_pool * _pool = nullptr; //thread pool for computation
428 std::string tmp_folder;
435 void exportMEDCommmon(DriverMED_W_SMESHDS_Mesh& myWriter,
436 const char* theMeshName,
438 const SMESHDS_Mesh* meshPart,
439 bool theAutoDimension,
440 bool theAddODOnVertices,
441 double theZTolerance,
442 bool theSaveNumbers);
445 void fillAncestorsMap(const TopoDS_Shape& theShape);
446 void getAncestorsSubMeshes(const TopoDS_Shape& theSubShape,
447 std::vector< SMESH_subMesh* >& theSubMeshes) const;
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;
457 std::map <int, SMESH_Group*> _mapGroup;
460 SubMeshHolder* _subMeshHolder;
463 bool _isModified; //!< modified since last total re-compute, issue 0020693
465 double _shapeDiagonal; //!< diagonal size of bounding box of shape to mesh
467 TopTools_IndexedDataMapOfShapeListOfShape _mapAncestors;
469 mutable std::vector<SMESH_subMesh*> _ancestorSubMeshes; // to speed up GetHypothes[ei]s()
471 TListOfListOfInt _subMeshOrder;
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
479 // Mutex for multhitreading write in SMESH_Mesh
481 boost::mutex _my_lock;
487 SMESH_Mesh(const SMESH_Mesh&) {};