Salome HOME
Merge branch 'V9_9_BR'
[modules/smesh.git] / src / SMESHDS / SMESHDS_GroupOnGeom.cxx
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 //  SMESH SMESHDS : idl implementation based on 'SMESH' unit's classes
24 //  File   : SMESHDS_GroupOnGeom.cxx
25 //  Module : SMESH
26 //
27 #include "SMESHDS_GroupOnGeom.hxx"
28 #include "SMESHDS_Mesh.hxx"
29 #include "utilities.h"
30
31 using namespace std;
32
33 //=============================================================================
34 /*!
35  *  
36  */
37 //=============================================================================
38
39 SMESHDS_GroupOnGeom::SMESHDS_GroupOnGeom (const int                 theID,
40                                           const SMESHDS_Mesh*       theMesh,
41                                           const SMDSAbs_ElementType theType,
42                                           const TopoDS_Shape&       theShape)
43      : SMESHDS_GroupBase(theID,theMesh,theType)
44 {
45   SetShape( theShape );
46 }
47
48 void SMESHDS_GroupOnGeom::SetShape( const TopoDS_Shape& theShape)
49 {
50   SMESHDS_Mesh* aMesh = const_cast<SMESHDS_Mesh*>( GetMesh() );
51   mySubMesh = aMesh->MeshElements( aMesh->AddCompoundSubmesh( theShape ));
52   myShape   = theShape;
53 }
54
55 // =====================
56 // class MyGroupIterator
57 // =====================
58
59 class MyIterator: public SMDS_ElemIterator
60 {
61   SMDSAbs_ElementType     myType;
62   SMDS_ElemIteratorPtr    myElemIt;
63   SMDS_NodeIteratorPtr    myNodeIt;
64   const SMDS_MeshElement* myElem;
65  public:
66   MyIterator(SMDSAbs_ElementType type, const SMESHDS_SubMesh* subMesh)
67     : myType(type), myElem(0)
68   {
69     if ( subMesh )
70     {
71       if ( myType == SMDSAbs_Node ||
72            myType == SMDSAbs_0DElement ||
73            myType == SMDSAbs_Ball )
74         myNodeIt = subMesh->GetNodes();
75       else
76         myElemIt = subMesh->GetElements();
77
78       if ( myType != SMDSAbs_Node )
79         next();
80     }
81   }
82   bool more()
83   {
84     if ( myType == SMDSAbs_Node && myNodeIt )
85       return myNodeIt->more();
86     return ( myElem != 0 );
87   }
88   const SMDS_MeshElement* next()
89   {
90     if ( myType == SMDSAbs_Node && myNodeIt )
91       return myNodeIt->next();
92
93     const SMDS_MeshElement* res = myElem;
94     myElem = 0;
95
96     if ( myElemIt )
97       while ( myElemIt->more() ) {
98         myElem = myElemIt->next();
99         if ( myElem && myElem->GetType() == myType )
100           break;
101         else
102           myElem = 0;
103       }
104
105     if ( !myElem && myNodeIt ) // look for a 0D element
106       while ( myNodeIt->more() ) {
107         const SMDS_MeshNode* n = myNodeIt->next();
108         if (( myElemIt = n->GetInverseElementIterator( myType )) &&
109             ( myElemIt->more() ))
110         {
111           myElem = myElemIt->next();
112           break;
113         }
114       }
115
116     return res;
117   }
118 };
119
120 //=======================================================================
121 //function : GetElements
122 //purpose  : 
123 //=======================================================================
124
125 SMDS_ElemIteratorPtr SMESHDS_GroupOnGeom::GetElements() const
126 {
127   return SMDS_ElemIteratorPtr( new MyIterator ( GetType(), mySubMesh ));
128 }
129
130 //=======================================================================
131 //function : Contains
132 //purpose  : 
133 //=======================================================================
134
135 bool SMESHDS_GroupOnGeom::Contains (const smIdType theID)
136 {
137   return mySubMesh->Contains( findInMesh( theID ));
138 }
139
140 //=======================================================================
141 //function : Contains
142 //purpose  : 
143 //=======================================================================
144
145 bool SMESHDS_GroupOnGeom::Contains (const SMDS_MeshElement* elem)
146 {
147   if ( GetType() == SMDSAbs_0DElement ||
148        GetType() == SMDSAbs_Ball )
149     return elem ? mySubMesh->Contains( elem->GetNode(0) ) : false;
150
151   return mySubMesh->Contains( elem );
152 }
153
154 //================================================================================
155 /*!
156  * \brief Return a value allowing to find out if a group has changed or not
157  */
158 //================================================================================
159
160 int SMESHDS_GroupOnGeom::GetTic() const
161 {
162   return GetMesh()->GetMTime();
163 }
164