2 //=============================================================================
3 // File : SMESH_Gen.cxx
4 // Created : sam mai 18 09:34:35 CEST 2002
5 // Author : Paul RASCLE, EDF
7 // Copyright : EDF 2002
9 //=============================================================================
12 #include "SMESH_Gen.hxx"
14 #include "SMESH_subMesh.hxx"
16 #include "SMESHDS_ListOfPtrHypothesis.hxx"
17 #include "SMESHDS_ListIteratorOfListOfPtrHypothesis.hxx"
18 #include "SMDS_MeshElement.hxx"
19 #include "SMDS_MeshNode.hxx"
22 #include <BRep_Tool.hxx>
24 #include "utilities.h"
27 //=============================================================================
29 * default constructor:
31 //=============================================================================
33 SMESH_Gen::SMESH_Gen()
35 MESSAGE("SMESH_Gen::SMESH_Gen");
37 _hypothesisFactory.SetGen(this);
40 //=============================================================================
44 //=============================================================================
46 SMESH_Gen::~SMESH_Gen()
48 MESSAGE("SMESH_Gen::~SMESH_Gen");
51 //=============================================================================
55 //=============================================================================
57 SMESH_Hypothesis* SMESH_Gen::CreateHypothesis(const char* anHyp,
59 throw (SALOME_Exception)
61 MESSAGE("SMESH_Gen::CreateHypothesis");
63 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
65 StudyContextStruct* myStudyContext = GetStudyContext(studyId);
67 // create a new hypothesis object, store its ref. in studyContext
69 SMESH_Hypothesis* myHypothesis = _hypothesisFactory.Create(anHyp, studyId);
70 int hypId = myHypothesis->GetID();
71 myStudyContext->mapHypothesis[hypId] = myHypothesis;
75 // store hypothesis in SMESHDS document
77 myStudyContext->myDocument->AddHypothesis(myHypothesis);
80 //=============================================================================
84 //=============================================================================
86 SMESH_Mesh* SMESH_Gen::Init(int studyId, const TopoDS_Shape& aShape)
87 throw (SALOME_Exception)
89 MESSAGE("SMESH_Gen::Init");
90 // if (aShape.ShapeType() == TopAbs_COMPOUND)
92 // INFOS("Mesh Compound not yet implemented!");
93 // throw(SALOME_Exception(LOCALIZED("Mesh Compound not yet implemented!")));
96 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
98 StudyContextStruct* myStudyContext = GetStudyContext(studyId);
100 // create a new SMESH_mesh object
102 SMESH_Mesh* mesh = new SMESH_Mesh(_localId++,
105 myStudyContext->myDocument);
106 myStudyContext->mapMesh[_localId] = mesh;
108 // associate a TopoDS_Shape to the mesh
110 mesh->ShapeToMesh(aShape);
114 //=============================================================================
118 //=============================================================================
120 bool SMESH_Gen::Compute(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
121 throw (SALOME_Exception)
123 MESSAGE("SMESH_Gen::Compute");
124 // bool isDone = false;
126 Algo : s'appuie ou non sur une 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
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)
143 SMESH_subMesh* sm = aMesh.GetSubMesh(aShape);
145 SMESH_subMesh* smToCompute = sm->GetFirstToCompute();
148 TopoDS_Shape subShape = smToCompute->GetSubShape();
149 int dim = GetShapeDim(subShape);
153 bool ret1 = smToCompute->ComputeStateEngine(SMESH_subMesh::COMPUTE);
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);
173 smToCompute = sm->GetFirstToCompute();
179 //=============================================================================
183 //=============================================================================
185 SMESH_Algo* SMESH_Gen::GetAlgo(SMESH_Mesh& aMesh,
186 const TopoDS_Shape& aShape)
188 //MESSAGE("SMESH_Gen::GetAlgo");
190 SMESHDS_Hypothesis* theHyp = NULL;
191 SMESH_Algo* algo = NULL;
192 const Handle(SMESHDS_Mesh)& meshDS = aMesh.GetMeshDS();
197 // try shape first, then main shape
199 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
200 const TopoDS_Shape* shapeToTry[2] = {&aShape, &mainShape};
202 for (int iShape=0; iShape<2; iShape++)
204 TopoDS_Shape tryShape = (*shapeToTry[iShape]);
206 const SMESHDS_ListOfPtrHypothesis& listHyp
207 = meshDS->GetHypothesis(tryShape);
208 SMESHDS_ListIteratorOfListOfPtrHypothesis it(listHyp);
211 int shapeDim = GetShapeDim(aShape);
212 int typeOfShape = aShape.ShapeType();
216 SMESHDS_Hypothesis* anHyp = it.Value();
217 hypType = anHyp->GetType();
219 if (hypType > SMESHDS_Hypothesis::PARAM_ALGO)
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;
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
245 if (nb_algo > 1) return NULL; // more than one algo
248 if (nb_algo == 1) // one algo found : OK
249 break; // do not try a parent shape
252 if (!theHyp) return NULL; // no algo found
254 hypType = theHyp->GetType();
255 hypId = theHyp->GetID();
257 ASSERT(_mapAlgo.find(hypId) != _mapAlgo.end());
258 algo = _mapAlgo[hypId];
259 const char* algoName = algo->GetName();
260 //MESSAGE("Algo found " << algoName << " Id " << hypId);
264 //=============================================================================
268 //=============================================================================
270 StudyContextStruct* SMESH_Gen::GetStudyContext(int studyId)
272 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
274 if (_mapStudyContext.find(studyId) == _mapStudyContext.end())
276 _mapStudyContext[studyId] = new StudyContextStruct;
277 _mapStudyContext[studyId]->myDocument = new SMESHDS_Document(studyId);
279 StudyContextStruct* myStudyContext = _mapStudyContext[studyId];
280 // ASSERT(_mapStudyContext.find(studyId) != _mapStudyContext.end());
281 return myStudyContext;
284 //=============================================================================
288 //=============================================================================
290 void SMESH_Gen::Save(int studyId, const char *aUrlOfFile)
294 //=============================================================================
298 //=============================================================================
300 void SMESH_Gen::Load(int studyId, const char *aUrlOfFile)
304 //=============================================================================
308 //=============================================================================
310 void SMESH_Gen::Close(int studyId)
314 //=============================================================================
318 //=============================================================================
320 const char* SMESH_Gen::ComponentDataType()
325 //=============================================================================
329 //=============================================================================
331 const char* SMESH_Gen::IORToLocalPersistentID(const char* IORString,
336 //=============================================================================
340 //=============================================================================
342 const char* SMESH_Gen::LocalPersistentIDToIOR(const char* aLocalPersistentID)
346 //=============================================================================
350 //=============================================================================
352 int SMESH_Gen::GetShapeDim(const TopoDS_Shape& aShape)
354 int shapeDim = -1; // Shape dimension: 0D, 1D, 2D, 3D
355 int type = aShape.ShapeType();
358 // case TopAbs_COMPOUND:
362 case TopAbs_COMPOUND:
363 case TopAbs_COMPSOLID:
370 // case TopAbs_SHELL: