1 // Copyright (C) 2007-2012 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.
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 // SMESH DriverMED : tool to split groups on families
24 // File : DriverMED_Family.cxx
25 // Author : Julia DOROVSKIKH
29 #include "DriverMED_Family.h"
30 #include "MED_Factory.hxx"
36 //=============================================================================
40 //=============================================================================
47 //=============================================================================
50 ::GetElements () const
64 ::SetId (const int theId)
71 ::AddElement(const SMDS_MeshElement* theElement)
73 myElements.insert(theElement);
78 ::AddGroupName(std::string theGroupName)
80 myGroupNames.insert(theGroupName);
85 ::SetType(const SMDSAbs_ElementType theType)
87 myTypes.insert( myType = theType );
97 const std::set< SMDSAbs_ElementType >&
106 ::MemberOf(std::string theGroupName) const
108 return myGroupNames.find(theGroupName) != myGroupNames.end();
111 const MED::TStringSet&
113 ::GetGroupNames () const
121 ::GetGroupAttributVal() const
123 return myGroupAttributVal;
128 ::SetGroupAttributVal( int theValue)
130 myGroupAttributVal = theValue;
137 return myElements.empty();
140 //=============================================================================
142 * Split each group from list <aGroups> on some parts (families)
143 * on the basis of the elements membership in other groups from this list.
144 * Resulting families have no common elements.
146 //=============================================================================
147 DriverMED_FamilyPtrList
149 ::MakeFamilies(const SMESHDS_SubMeshPtrMap& theSubMeshes,
150 const SMESHDS_GroupBasePtrList& theGroups,
151 const bool doGroupOfNodes,
152 const bool doGroupOfEdges,
153 const bool doGroupOfFaces,
154 const bool doGroupOfVolumes,
155 const bool doGroupOf0DElems,
156 const bool doGroupOfBalls)
158 DriverMED_FamilyPtrList aFamilies;
160 string anAllNodesGroupName = "Group_Of_All_Nodes";
161 string anAllEdgesGroupName = "Group_Of_All_Edges";
162 string anAllFacesGroupName = "Group_Of_All_Faces";
163 string anAllVolumesGroupName = "Group_Of_All_Volumes";
164 string anAll0DElemsGroupName = "Group_Of_All_0DElems";
165 string anAllBallsGroupName = "Group_Of_All_Balls";
167 // Reserve 6 ids for families of free elements
168 // (1 - nodes, -1 - edges, -2 - faces, -3 - volumes, -4 - 0D, -5 - balls).
169 // 'Free' means here not belonging to any group.
170 int aNodeFamId = FIRST_NODE_FAMILY;
171 int aElemFamId = FIRST_ELEM_FAMILY;
173 // Process sub-meshes
174 SMESHDS_SubMeshPtrMap::const_iterator aSMIter = theSubMeshes.begin();
175 for (; aSMIter != theSubMeshes.end(); aSMIter++)
177 const int anId = aSMIter->first;
178 SMESHDS_SubMesh* aSubMesh = aSMIter->second;
179 if ( aSubMesh->IsComplexSubmesh() )
180 continue; // submesh containing other submeshs
181 DriverMED_FamilyPtrList aSMFams = SplitByType(aSubMesh,anId);
182 DriverMED_FamilyPtrList::iterator aSMFamsIter = aSMFams.begin();
183 for (; aSMFamsIter != aSMFams.end(); aSMFamsIter++)
185 DriverMED_FamilyPtr aFam2 = (*aSMFamsIter);
186 DriverMED_FamilyPtrList::iterator aFamsIter = aFamilies.begin();
187 while (aFamsIter != aFamilies.end())
189 DriverMED_FamilyPtr aFam1 = *aFamsIter;
190 DriverMED_FamilyPtrList::iterator aCurrIter = aFamsIter++;
191 if (aFam1->myType == aFam2->myType)
193 DriverMED_FamilyPtr aCommon (new DriverMED_Family);
194 aFam1->Split(aFam2, aCommon);
195 if (!aCommon->IsEmpty())
197 aFamilies.push_back(aCommon);
199 if (aFam1->IsEmpty())
201 aFamilies.erase(aCurrIter);
203 if (aFam2->IsEmpty())
207 // The rest elements of family
208 if (!aFam2->IsEmpty())
210 aFamilies.push_back(aFam2);
216 SMESHDS_GroupBasePtrList::const_iterator aGroupsIter = theGroups.begin();
217 for (; aGroupsIter != theGroups.end(); aGroupsIter++)
219 DriverMED_FamilyPtr aFam2 (new DriverMED_Family);
220 aFam2->Init(*aGroupsIter);
222 DriverMED_FamilyPtrList::iterator aFamsIter = aFamilies.begin();
223 while (aFamsIter != aFamilies.end())
225 DriverMED_FamilyPtr aFam1 = *aFamsIter;
226 DriverMED_FamilyPtrList::iterator aCurrIter = aFamsIter++;
227 if (aFam1->myType == aFam2->myType)
229 DriverMED_FamilyPtr aCommon (new DriverMED_Family);
230 aFam1->Split(aFam2, aCommon);
231 if (!aCommon->IsEmpty())
233 aCommon->SetGroupAttributVal(0);
234 aFamilies.push_back(aCommon);
236 if (aFam1->IsEmpty())
238 aFamilies.erase(aCurrIter);
240 if (aFam2->IsEmpty())
244 // The rest elements of group
245 if (!aFam2->IsEmpty())
247 aFamilies.push_back(aFam2);
251 DriverMED_FamilyPtrList::iterator aFamsIter = aFamilies.begin();
252 for (; aFamsIter != aFamilies.end(); aFamsIter++)
254 DriverMED_FamilyPtr aFam = *aFamsIter;
255 if (aFam->myType == SMDSAbs_Node) {
256 aFam->SetId(aNodeFamId++);
257 if (doGroupOfNodes) aFam->myGroupNames.insert(anAllNodesGroupName);
260 aFam->SetId(aElemFamId--);
261 if (aFam->myType == SMDSAbs_Edge) {
262 if (doGroupOfEdges) aFam->myGroupNames.insert(anAllEdgesGroupName);
264 else if (aFam->myType == SMDSAbs_Face) {
265 if (doGroupOfFaces) aFam->myGroupNames.insert(anAllFacesGroupName);
267 else if (aFam->myType == SMDSAbs_Volume) {
268 if (doGroupOfVolumes) aFam->myGroupNames.insert(anAllVolumesGroupName);
270 else if (aFam->myType == SMDSAbs_0DElement) {
271 if (doGroupOfVolumes) aFam->myGroupNames.insert(anAll0DElemsGroupName);
273 else if (aFam->myType == SMDSAbs_Ball) {
274 if (doGroupOfVolumes) aFam->myGroupNames.insert(anAllBallsGroupName);
279 // Create families for elements, not belonging to any group
282 DriverMED_FamilyPtr aFreeNodesFam (new DriverMED_Family);
283 aFreeNodesFam->SetId(REST_NODES_FAMILY);
284 aFreeNodesFam->myType = SMDSAbs_Node;
285 aFreeNodesFam->myGroupNames.insert(anAllNodesGroupName);
286 aFamilies.push_back(aFreeNodesFam);
291 DriverMED_FamilyPtr aFreeEdgesFam (new DriverMED_Family);
292 aFreeEdgesFam->SetId(REST_EDGES_FAMILY);
293 aFreeEdgesFam->myType = SMDSAbs_Edge;
294 aFreeEdgesFam->myGroupNames.insert(anAllEdgesGroupName);
295 aFamilies.push_back(aFreeEdgesFam);
300 DriverMED_FamilyPtr aFreeFacesFam (new DriverMED_Family);
301 aFreeFacesFam->SetId(REST_FACES_FAMILY);
302 aFreeFacesFam->myType = SMDSAbs_Face;
303 aFreeFacesFam->myGroupNames.insert(anAllFacesGroupName);
304 aFamilies.push_back(aFreeFacesFam);
307 if (doGroupOfVolumes)
309 DriverMED_FamilyPtr aFreeVolumesFam (new DriverMED_Family);
310 aFreeVolumesFam->SetId(REST_VOLUMES_FAMILY);
311 aFreeVolumesFam->myType = SMDSAbs_Volume;
312 aFreeVolumesFam->myGroupNames.insert(anAllVolumesGroupName);
313 aFamilies.push_back(aFreeVolumesFam);
316 if (doGroupOf0DElems)
318 DriverMED_FamilyPtr aFree0DFam (new DriverMED_Family);
319 aFree0DFam->SetId(REST_0DELEM_FAMILY);
320 aFree0DFam->myType = SMDSAbs_0DElement;
321 aFree0DFam->myGroupNames.insert(anAll0DElemsGroupName);
322 aFamilies.push_back(aFree0DFam);
327 DriverMED_FamilyPtr aFreeBallsFam (new DriverMED_Family);
328 aFreeBallsFam->SetId(REST_BALL_FAMILY);
329 aFreeBallsFam->myType = SMDSAbs_Ball;
330 aFreeBallsFam->myGroupNames.insert(anAllBallsGroupName);
331 aFamilies.push_back(aFreeBallsFam);
334 DriverMED_FamilyPtr aNullFam (new DriverMED_Family);
336 aNullFam->myType = SMDSAbs_All;
337 aFamilies.push_back(aNullFam);
342 //=============================================================================
344 * Create TFamilyInfo for this family
346 //=============================================================================
348 DriverMED_Family::GetFamilyInfo(const MED::PWrapper& theWrapper,
349 const MED::PMeshInfo& theMeshInfo) const
352 aStr << "FAM_" << myId;
353 set<string>::const_iterator aGrIter = myGroupNames.begin();
354 for(; aGrIter != myGroupNames.end(); aGrIter++){
355 aStr << "_" << *aGrIter;
357 string aValue = aStr.str();
358 // PAL19785,0019867 - med forbids whitespace to be the last char in the name
360 if ( theWrapper->GetVersion() == MED::eV2_1 )
361 maxSize = MED::GetNOMLength<MED::eV2_1>();
363 maxSize = MED::GetNOMLength<MED::eV2_2>();
364 int lastCharPos = min( maxSize, (int) aValue.size() ) - 1;
365 while ( isspace( aValue[ lastCharPos ] ))
366 aValue.resize( lastCharPos-- );
368 MED::PFamilyInfo anInfo;
369 if(myId == 0 || myGroupAttributVal == 0){
370 anInfo = theWrapper->CrFamilyInfo(theMeshInfo,
375 MED::TStringVector anAttrDescs (1, ""); // 1 attribute with empty description,
376 MED::TIntVector anAttrIds (1, myId); // Id=0,
377 MED::TIntVector anAttrVals (1, myGroupAttributVal);
378 anInfo = theWrapper->CrFamilyInfo(theMeshInfo,
388 // cout << "Groups: ";
389 // set<string>::iterator aGrIter = myGroupNames.begin();
390 // for (; aGrIter != myGroupNames.end(); aGrIter++)
392 // cout << " " << *aGrIter;
396 // cout << "Elements: ";
397 // set<const SMDS_MeshElement *>::iterator anIter = myElements.begin();
398 // for (; anIter != myElements.end(); anIter++)
400 // cout << " " << (*anIter)->GetID();
407 //=============================================================================
409 * Initialize the tool by SMESHDS_GroupBase
411 //=============================================================================
412 void DriverMED_Family::Init (SMESHDS_GroupBase* theGroup)
416 SMDS_ElemIteratorPtr elemIt = theGroup->GetElements();
417 while (elemIt->more())
419 myElements.insert(elemIt->next());
423 myType = theGroup->GetType();
426 myGroupNames.clear();
427 myGroupNames.insert(string(theGroup->GetStoreName()));
429 Quantity_Color aColor = theGroup->GetColor();
430 double aRed = aColor.Red();
431 double aGreen = aColor.Green();
432 double aBlue = aColor.Blue();
433 int aR = int( aRed*255 );
434 int aG = int( aGreen*255 );
435 int aB = int( aBlue*255 );
436 // cout << "aRed = " << aR << endl;
437 // cout << "aGreen = " << aG << endl;
438 // cout << "aBlue = " << aB << endl;
439 myGroupAttributVal = (int)(aR*1000000 + aG*1000 + aB);
440 //cout << "myGroupAttributVal = " << myGroupAttributVal << endl;
443 //=============================================================================
445 * Split <theSubMesh> on some parts (families)
446 * on the basis of the elements type.
448 //=============================================================================
449 DriverMED_FamilyPtrList
451 ::SplitByType (SMESHDS_SubMesh* theSubMesh,
454 DriverMED_FamilyPtrList aFamilies;
455 DriverMED_FamilyPtr aNodesFamily (new DriverMED_Family);
456 DriverMED_FamilyPtr anEdgesFamily (new DriverMED_Family);
457 DriverMED_FamilyPtr aFacesFamily (new DriverMED_Family);
458 DriverMED_FamilyPtr aVolumesFamily (new DriverMED_Family);
459 // DriverMED_FamilyPtr a0DElemsFamily (new DriverMED_Family);
460 // DriverMED_FamilyPtr aBallsFamily (new DriverMED_Family);
462 char submeshGrpName[ 30 ];
463 sprintf( submeshGrpName, "SubMesh %d", theId );
465 SMDS_NodeIteratorPtr aNodesIter = theSubMesh->GetNodes();
466 while (aNodesIter->more())
468 const SMDS_MeshNode* aNode = aNodesIter->next();
469 aNodesFamily->AddElement(aNode);
472 SMDS_ElemIteratorPtr anElemsIter = theSubMesh->GetElements();
473 while (anElemsIter->more())
475 const SMDS_MeshElement* anElem = anElemsIter->next();
476 switch (anElem->GetType())
479 anEdgesFamily->AddElement(anElem);
482 aFacesFamily->AddElement(anElem);
485 aVolumesFamily->AddElement(anElem);
492 if (!aNodesFamily->IsEmpty()) {
493 aNodesFamily->SetType(SMDSAbs_Node);
494 aNodesFamily->AddGroupName(submeshGrpName);
495 aFamilies.push_back(aNodesFamily);
497 if (!anEdgesFamily->IsEmpty()) {
498 anEdgesFamily->SetType(SMDSAbs_Edge);
499 anEdgesFamily->AddGroupName(submeshGrpName);
500 aFamilies.push_back(anEdgesFamily);
502 if (!aFacesFamily->IsEmpty()) {
503 aFacesFamily->SetType(SMDSAbs_Face);
504 aFacesFamily->AddGroupName(submeshGrpName);
505 aFamilies.push_back(aFacesFamily);
507 if (!aVolumesFamily->IsEmpty()) {
508 aVolumesFamily->SetType(SMDSAbs_Volume);
509 aVolumesFamily->AddGroupName(submeshGrpName);
510 aFamilies.push_back(aVolumesFamily);
516 //=============================================================================
518 * Remove from <myElements> elements, common with <by>,
519 * Remove from <by> elements, common with <myElements>,
520 * Create family <common> from common elements, with combined groups list.
522 //=============================================================================
523 void DriverMED_Family::Split (DriverMED_FamilyPtr by,
524 DriverMED_FamilyPtr common)
527 ElementsSet::iterator anIter = by->myElements.begin(), elemInMe;
528 while ( anIter != by->myElements.end())
530 elemInMe = myElements.find(*anIter);
531 if (elemInMe != myElements.end())
533 common->myElements.insert(*anIter);
534 myElements.erase(elemInMe);
535 by->myElements.erase(anIter++);
541 if (!common->IsEmpty())
544 common->myGroupNames = myGroupNames;
545 common->myGroupNames.insert( by->myGroupNames.begin(), by->myGroupNames.end() );
548 common->myType = myType;