Salome HOME
This commit was generated by cvs2git to track changes on a CVS vendor
[modules/smesh.git] / src / SMESH / SMESH_Gen.cxx
1 using namespace std;
2 //=============================================================================
3 // File      : SMESH_Gen.cxx
4 // Created   : sam mai 18 09:34:35 CEST 2002
5 // Author    : Paul RASCLE, EDF
6 // Project   : SALOME
7 // Copyright : EDF 2002
8 // $Header$
9 //=============================================================================
10 using namespace std;
11
12 #include "SMESH_Gen.hxx"
13
14 #include "SMESH_subMesh.hxx"
15
16 #include "SMESHDS_ListOfPtrHypothesis.hxx"
17 #include "SMESHDS_ListIteratorOfListOfPtrHypothesis.hxx"
18 #include "SMDS_MeshElement.hxx"
19 #include "SMDS_MeshNode.hxx"
20
21 #include <gp_Pnt.hxx>
22 #include <BRep_Tool.hxx>
23
24 #include "utilities.h"
25 #include "OpUtil.hxx"
26
27 //=============================================================================
28 /*!
29  *  default constructor:
30  */
31 //=============================================================================
32
33 SMESH_Gen::SMESH_Gen()
34 {
35   MESSAGE("SMESH_Gen::SMESH_Gen");
36   _localId = 0;
37   _hypothesisFactory.SetGen(this);
38 }
39
40 //=============================================================================
41 /*!
42  * 
43  */
44 //=============================================================================
45
46 SMESH_Gen::~SMESH_Gen()
47 {
48   MESSAGE("SMESH_Gen::~SMESH_Gen");
49 }
50
51 //=============================================================================
52 /*!
53  * 
54  */
55 //=============================================================================
56
57 SMESH_Hypothesis* SMESH_Gen::CreateHypothesis(const char* anHyp,
58                                               int studyId)
59   throw (SALOME_Exception)
60 {
61   MESSAGE("SMESH_Gen::CreateHypothesis");
62
63   // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
64
65   StudyContextStruct* myStudyContext = GetStudyContext(studyId);
66
67   // create a new hypothesis object, store its ref. in studyContext
68
69   SMESH_Hypothesis* myHypothesis = _hypothesisFactory.Create(anHyp, studyId);
70   int hypId = myHypothesis->GetID();
71   myStudyContext->mapHypothesis[hypId] = myHypothesis;
72   SCRUTE(studyId);
73   SCRUTE(hypId);
74
75   // store hypothesis in SMESHDS document
76
77   myStudyContext->myDocument->AddHypothesis(myHypothesis);
78 }
79
80 //=============================================================================
81 /*!
82  * 
83  */
84 //=============================================================================
85
86 SMESH_Mesh* SMESH_Gen::Init(int studyId, const TopoDS_Shape& aShape)
87   throw (SALOME_Exception)
88 {
89   MESSAGE("SMESH_Gen::Init");
90 //   if (aShape.ShapeType() == TopAbs_COMPOUND)
91 //     {
92 //       INFOS("Mesh Compound not yet implemented!");
93 //       throw(SALOME_Exception(LOCALIZED("Mesh Compound not yet implemented!")));
94 //     }
95
96   // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
97
98   StudyContextStruct* myStudyContext = GetStudyContext(studyId);
99
100   // create a new SMESH_mesh object 
101
102   SMESH_Mesh* mesh = new SMESH_Mesh(_localId++,
103                                     studyId,
104                                     this,
105                                     myStudyContext->myDocument);
106   myStudyContext->mapMesh[_localId] = mesh;
107
108   // associate a TopoDS_Shape to the mesh
109
110   mesh->ShapeToMesh(aShape);
111   return mesh;
112 }
113
114 //=============================================================================
115 /*!
116  * 
117  */
118 //=============================================================================
119
120 bool SMESH_Gen::Compute(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
121   throw (SALOME_Exception)
122 {
123   MESSAGE("SMESH_Gen::Compute");
124 //   bool isDone = false;
125 /* 
126 Algo : s'appuie ou non sur une geometrie
127 Si geometrie:
128 Vertex : rien à faire (range le point)
129 Edge, Wire, collection d'edge et wire : 1D
130 Face, Shell, collection de Face et Shells : 2D
131 Solid, Collection de Solid : 3D
132 */
133 // *** corriger commentaires
134   // check hypothesis associated to the mesh :
135   // - only one algo : type compatible with the type of the shape
136   // - hypothesis = compatible with algo
137   //    - check if hypothesis are applicable to this algo
138   //    - check contradictions within hypothesis
139   //    (test if enough hypothesis is done further)
140
141   bool ret = true;
142
143   SMESH_subMesh* sm = aMesh.GetSubMesh(aShape);
144 //   SCRUTE(sm);
145   SMESH_subMesh* smToCompute = sm->GetFirstToCompute();
146   while (smToCompute)
147     {
148       TopoDS_Shape subShape = smToCompute->GetSubShape();
149       int dim = GetShapeDim(subShape);
150       //SCRUTE(dim);
151       if (dim > 0)
152         {
153           bool ret1 = smToCompute->ComputeStateEngine(SMESH_subMesh::COMPUTE);
154           ret = ret && ret1;
155         }
156       else
157         {
158           ASSERT(dim == 0);
159           ASSERT(smToCompute->_vertexSet == false);
160           TopoDS_Vertex V1 = TopoDS::Vertex(subShape);
161           gp_Pnt P1 = BRep_Tool::Pnt(V1);
162           const Handle(SMESHDS_Mesh)& meshDS = aMesh.GetMeshDS();
163           int nodeId = meshDS->AddNode(P1.X(), P1.Y(), P1.Z());
164           //MESSAGE("point "<<nodeId<<" "<<P1.X()<<" "<<P1.Y()<<" "<<P1.Z());
165           Handle (SMDS_MeshElement) elt = meshDS->FindNode(nodeId);
166           Handle (SMDS_MeshNode) node = meshDS->GetNode(1, elt);
167           meshDS->SetNodeOnVertex(node, V1);
168           const Handle(SMESHDS_SubMesh)& subMeshDS
169             = smToCompute->GetSubMeshDS();
170           smToCompute->_vertexSet = true;
171           bool ret1 = smToCompute->ComputeStateEngine(SMESH_subMesh::COMPUTE);
172         }
173       smToCompute = sm->GetFirstToCompute();
174     }
175
176   return ret;
177 }
178
179 //=============================================================================
180 /*!
181  * 
182  */
183 //=============================================================================
184
185 SMESH_Algo* SMESH_Gen::GetAlgo(SMESH_Mesh& aMesh,
186                                const TopoDS_Shape& aShape)
187 {
188   //MESSAGE("SMESH_Gen::GetAlgo");
189
190   SMESHDS_Hypothesis* theHyp = NULL;
191   SMESH_Algo* algo = NULL;
192   const Handle(SMESHDS_Mesh)& meshDS = aMesh.GetMeshDS();
193   int hypType;
194   int hypId;
195   int algoDim;
196
197   // try shape first, then main shape
198
199   TopoDS_Shape mainShape = meshDS->ShapeToMesh();
200   const TopoDS_Shape* shapeToTry[2] = {&aShape, &mainShape};
201
202   for (int iShape=0; iShape<2; iShape++)
203     {
204       TopoDS_Shape tryShape = (*shapeToTry[iShape]);
205
206       const SMESHDS_ListOfPtrHypothesis& listHyp
207         = meshDS->GetHypothesis(tryShape);
208       SMESHDS_ListIteratorOfListOfPtrHypothesis it(listHyp);
209
210       int nb_algo = 0;
211       int shapeDim = GetShapeDim(aShape);
212       int typeOfShape = aShape.ShapeType();
213
214       while (it.More())
215         {
216           SMESHDS_Hypothesis* anHyp = it.Value();
217           hypType = anHyp->GetType();
218 //        SCRUTE(hypType);
219           if (hypType > SMESHDS_Hypothesis::PARAM_ALGO)
220             {
221               switch (hypType)
222                 {
223                 case SMESHDS_Hypothesis::ALGO_1D: algoDim=1; break;
224                 case SMESHDS_Hypothesis::ALGO_2D: algoDim=2; break;
225                 case SMESHDS_Hypothesis::ALGO_3D: algoDim=3; break;
226                 default: algoDim=0; break;
227                 }
228 //            SCRUTE(algoDim);
229 //            SCRUTE(shapeDim);
230 //            SCRUTE(typeOfShape);
231               if (shapeDim == algoDim)        // count only algos of shape dim.
232                 {                             // discard algos for subshapes
233                   hypId = anHyp->GetID();     // (of lower dim.)
234                   ASSERT(_mapAlgo.find(hypId) != _mapAlgo.end());
235                   SMESH_Algo* anAlgo = _mapAlgo[hypId];
236                   //SCRUTE(anAlgo->GetShapeType());
237 //                if (anAlgo->GetShapeType() == typeOfShape)
238                   if ((anAlgo->GetShapeType()) & (1 << typeOfShape))
239                     {                         // only specific TopoDS_Shape
240                       nb_algo++;                  
241                       theHyp = anHyp;
242                     }
243                 }
244             }
245           if (nb_algo > 1) return NULL;  // more than one algo
246           it.Next();
247         }
248       if (nb_algo == 1)                  // one algo found : OK
249         break;                           // do not try a parent shape
250     }
251
252   if (!theHyp) return NULL;              // no algo found
253
254   hypType = theHyp->GetType();
255   hypId = theHyp->GetID();
256
257   ASSERT(_mapAlgo.find(hypId) != _mapAlgo.end());
258   algo = _mapAlgo[hypId];
259   const char* algoName = algo->GetName();
260   //MESSAGE("Algo found " << algoName << " Id " << hypId);
261   return algo;
262 }
263
264 //=============================================================================
265 /*!
266  * 
267  */
268 //=============================================================================
269
270 StudyContextStruct* SMESH_Gen::GetStudyContext(int studyId)
271 {
272   // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
273
274   if (_mapStudyContext.find(studyId) == _mapStudyContext.end())
275     {
276       _mapStudyContext[studyId] = new StudyContextStruct;
277       _mapStudyContext[studyId]->myDocument = new SMESHDS_Document(studyId);
278     }
279   StudyContextStruct* myStudyContext = _mapStudyContext[studyId];
280 //   ASSERT(_mapStudyContext.find(studyId) != _mapStudyContext.end());
281   return myStudyContext;
282 }
283
284 //=============================================================================
285 /*!
286  * 
287  */
288 //=============================================================================
289
290 void SMESH_Gen::Save(int studyId, const char *aUrlOfFile)
291 {
292 }
293
294 //=============================================================================
295 /*!
296  * 
297  */
298 //=============================================================================
299
300 void SMESH_Gen::Load(int studyId, const char *aUrlOfFile)
301 {
302
303
304 //=============================================================================
305 /*!
306  * 
307  */
308 //=============================================================================
309
310 void SMESH_Gen::Close(int studyId)
311 {
312 }
313  
314 //=============================================================================
315 /*!
316  * 
317  */
318 //=============================================================================
319
320 const char* SMESH_Gen::ComponentDataType()
321 {
322 }
323
324
325 //=============================================================================
326 /*!
327  * 
328  */
329 //=============================================================================
330
331 const char* SMESH_Gen::IORToLocalPersistentID(const char* IORString,
332                                               bool& IsAFile)
333 {
334 }
335
336 //=============================================================================
337 /*!
338  * 
339  */
340 //=============================================================================
341
342 const char* SMESH_Gen::LocalPersistentIDToIOR(const char* aLocalPersistentID)
343 {
344 }
345
346 //=============================================================================
347 /*!
348  * 
349  */
350 //=============================================================================
351
352 int SMESH_Gen::GetShapeDim(const TopoDS_Shape& aShape)
353 {
354   int shapeDim = -1;              // Shape dimension: 0D, 1D, 2D, 3D
355   int type = aShape.ShapeType();
356   switch (type)
357     {
358 //     case TopAbs_COMPOUND:
359 //       {
360 //      break;
361 //       }
362     case TopAbs_COMPOUND:
363     case TopAbs_COMPSOLID:
364     case TopAbs_SOLID:
365     case TopAbs_SHELL:
366       {
367         shapeDim = 3;
368         break;
369       }
370       //    case TopAbs_SHELL:
371     case TopAbs_FACE:
372       {
373         shapeDim = 2;
374         break;
375       }
376     case TopAbs_WIRE:
377     case TopAbs_EDGE:
378       {
379         shapeDim = 1;
380         break;
381       }
382     case TopAbs_VERTEX:
383       {
384         shapeDim = 0;
385         break;
386       }
387     }
388 //   SCRUTE(shapeDim);
389   return shapeDim;
390 }