1 // Copyright (C) 2007-2019 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 // SMESH DriverMED : tool to split groups on families
24 // File : DriverMED_Family.cxx
25 // Author : Julia DOROVSKIKH
28 #include "DriverMED_Family.h"
29 #include "MED_Factory.hxx"
35 //=============================================================================
39 //=============================================================================
46 //=============================================================================
49 ::GetElements () const
63 ::SetId (const int theId)
70 ::AddElement(const SMDS_MeshElement* theElement)
72 myElements.insert( myElements.end(), theElement );
77 ::AddGroupName(std::string theGroupName)
79 myGroupNames.insert(theGroupName);
84 ::SetType(const SMDSAbs_ElementType theType)
86 myTypes.insert( myType = theType );
105 ::MemberOf(std::string theGroupName) const
107 return myGroupNames.find(theGroupName) != myGroupNames.end();
110 const MED::TStringSet&
112 ::GetGroupNames () const
120 ::GetGroupAttributVal() const
122 return myGroupAttributVal;
127 ::SetGroupAttributVal( int theValue)
129 myGroupAttributVal = theValue;
136 return myElements.empty();
139 //=============================================================================
141 * Split each group from list <aGroups> on some parts (families)
142 * on the basis of the elements membership in other groups from this list.
143 * Resulting families have no common elements.
145 //=============================================================================
146 DriverMED_FamilyPtrList
148 ::MakeFamilies(SMESHDS_SubMeshIteratorPtr theSubMeshes,
149 const SMESHDS_GroupBasePtrList& theGroups,
150 const bool doGroupOfNodes,
151 const bool doGroupOfEdges,
152 const bool doGroupOfFaces,
153 const bool doGroupOfVolumes,
154 const bool doGroupOf0DElems,
155 const bool doGroupOfBalls,
156 const bool doAllInGroups)
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 while ( theSubMeshes->more() )
176 SMESHDS_SubMesh* aSubMesh = const_cast< SMESHDS_SubMesh* >( theSubMeshes->next() );
177 const int anId = aSubMesh->GetID();
178 if ( aSubMesh->IsComplexSubmesh() )
179 continue; // submesh containing other submeshs
180 DriverMED_FamilyPtrList aSMFams = SplitByType(aSubMesh,anId);
181 DriverMED_FamilyPtrList::iterator aSMFamsIter = aSMFams.begin();
182 for (; aSMFamsIter != aSMFams.end(); aSMFamsIter++)
184 DriverMED_FamilyPtr aFam2 = (*aSMFamsIter);
185 DriverMED_FamilyPtrList::iterator aFamsIter = aFamilies.begin();
186 while (aFamsIter != aFamilies.end())
188 DriverMED_FamilyPtr aFam1 = *aFamsIter;
189 DriverMED_FamilyPtrList::iterator aCurrIter = aFamsIter++;
190 if (aFam1->myType == aFam2->myType)
192 DriverMED_FamilyPtr aCommon (new DriverMED_Family);
193 aFam1->Split(aFam2, aCommon);
194 if (!aCommon->IsEmpty())
196 aFamilies.push_back(aCommon);
198 if (aFam1->IsEmpty())
200 aFamilies.erase(aCurrIter);
202 if (aFam2->IsEmpty())
206 // The rest elements of family
207 if (!aFam2->IsEmpty())
209 aFamilies.push_back(aFam2);
215 SMESHDS_GroupBasePtrList::const_iterator aGroupsIter = theGroups.begin();
216 for (; aGroupsIter != theGroups.end(); aGroupsIter++)
218 DriverMED_FamilyPtr aFam2 (new DriverMED_Family);
219 aFam2->Init(*aGroupsIter);
221 DriverMED_FamilyPtrList::iterator aFamsIter = aFamilies.begin();
222 while (aFamsIter != aFamilies.end())
224 DriverMED_FamilyPtr aFam1 = *aFamsIter;
225 DriverMED_FamilyPtrList::iterator aCurrIter = aFamsIter++;
226 if (aFam1->myType == aFam2->myType)
228 DriverMED_FamilyPtr aCommon (new DriverMED_Family);
229 aFam1->Split(aFam2, aCommon);
230 if (!aCommon->IsEmpty())
232 aCommon->SetGroupAttributVal(0);
233 aFamilies.push_back(aCommon);
235 if (aFam1->IsEmpty())
237 aFamilies.erase(aCurrIter);
239 if (aFam2->IsEmpty())
243 // The rest elements of group
244 if (!aFam2->IsEmpty())
246 aFamilies.push_back(aFam2);
250 DriverMED_FamilyPtrList::iterator aFamsIter = aFamilies.begin();
251 for (; aFamsIter != aFamilies.end(); aFamsIter++)
253 DriverMED_FamilyPtr aFam = *aFamsIter;
254 if (aFam->myType == SMDSAbs_Node) {
255 aFam->SetId(aNodeFamId++);
256 if (doGroupOfNodes) aFam->myGroupNames.insert(anAllNodesGroupName);
259 aFam->SetId(aElemFamId--);
260 if (aFam->myType == SMDSAbs_Edge) {
261 if (doGroupOfEdges) aFam->myGroupNames.insert(anAllEdgesGroupName);
263 else if (aFam->myType == SMDSAbs_Face) {
264 if (doGroupOfFaces) aFam->myGroupNames.insert(anAllFacesGroupName);
266 else if (aFam->myType == SMDSAbs_Volume) {
267 if (doGroupOfVolumes) aFam->myGroupNames.insert(anAllVolumesGroupName);
269 else if (aFam->myType == SMDSAbs_0DElement) {
270 if (doGroupOfVolumes) aFam->myGroupNames.insert(anAll0DElemsGroupName);
272 else if (aFam->myType == SMDSAbs_Ball) {
273 if (doGroupOfVolumes) aFam->myGroupNames.insert(anAllBallsGroupName);
278 // Create families for elements, not belonging to any group
281 DriverMED_FamilyPtr aFreeNodesFam (new DriverMED_Family);
282 aFreeNodesFam->SetId(REST_NODES_FAMILY);
283 aFreeNodesFam->myType = SMDSAbs_Node;
284 aFreeNodesFam->myGroupNames.insert(anAllNodesGroupName);
285 aFamilies.push_back(aFreeNodesFam);
290 DriverMED_FamilyPtr aFreeEdgesFam (new DriverMED_Family);
291 aFreeEdgesFam->SetId(REST_EDGES_FAMILY);
292 aFreeEdgesFam->myType = SMDSAbs_Edge;
293 aFreeEdgesFam->myGroupNames.insert(anAllEdgesGroupName);
294 aFamilies.push_back(aFreeEdgesFam);
299 DriverMED_FamilyPtr aFreeFacesFam (new DriverMED_Family);
300 aFreeFacesFam->SetId(REST_FACES_FAMILY);
301 aFreeFacesFam->myType = SMDSAbs_Face;
302 aFreeFacesFam->myGroupNames.insert(anAllFacesGroupName);
303 aFamilies.push_back(aFreeFacesFam);
306 if (doGroupOfVolumes)
308 DriverMED_FamilyPtr aFreeVolumesFam (new DriverMED_Family);
309 aFreeVolumesFam->SetId(REST_VOLUMES_FAMILY);
310 aFreeVolumesFam->myType = SMDSAbs_Volume;
311 aFreeVolumesFam->myGroupNames.insert(anAllVolumesGroupName);
312 aFamilies.push_back(aFreeVolumesFam);
315 if (doGroupOf0DElems)
317 DriverMED_FamilyPtr aFree0DFam (new DriverMED_Family);
318 aFree0DFam->SetId(REST_0DELEM_FAMILY);
319 aFree0DFam->myType = SMDSAbs_0DElement;
320 aFree0DFam->myGroupNames.insert(anAll0DElemsGroupName);
321 aFamilies.push_back(aFree0DFam);
326 DriverMED_FamilyPtr aFreeBallsFam (new DriverMED_Family);
327 aFreeBallsFam->SetId(REST_BALL_FAMILY);
328 aFreeBallsFam->myType = SMDSAbs_Ball;
329 aFreeBallsFam->myGroupNames.insert(anAllBallsGroupName);
330 aFamilies.push_back(aFreeBallsFam);
333 DriverMED_FamilyPtr aNullFam (new DriverMED_Family);
334 aNullFam->SetId( 0 );
335 aNullFam->myType = SMDSAbs_All;
336 aFamilies.push_back(aNullFam);
340 if ( !doGroupOfEdges )
342 DriverMED_FamilyPtr aNigEdgeFam (new DriverMED_Family);
343 aNigEdgeFam->SetId( NIG_EDGES_FAMILY );
344 aNigEdgeFam->myType = SMDSAbs_Edge;
345 aNigEdgeFam->myGroupNames.insert( NIG_GROUP_PREFIX "_EDGES" );
346 aFamilies.push_back(aNigEdgeFam);
348 if ( !doGroupOfFaces )
350 DriverMED_FamilyPtr aNigFaceFam (new DriverMED_Family);
351 aNigFaceFam->SetId( NIG_FACES_FAMILY );
352 aNigFaceFam->myType = SMDSAbs_Face;
353 aNigFaceFam->myGroupNames.insert( NIG_GROUP_PREFIX "_FACES" );
354 aFamilies.push_back(aNigFaceFam);
356 if ( !doGroupOfVolumes )
358 DriverMED_FamilyPtr aNigVolFam (new DriverMED_Family);
359 aNigVolFam->SetId( NIG_VOLS_FAMILY );
360 aNigVolFam->myType = SMDSAbs_Volume;
361 aNigVolFam->myGroupNames.insert( NIG_GROUP_PREFIX "_VOLS" );
362 aFamilies.push_back(aNigVolFam);
369 //=============================================================================
371 * Create TFamilyInfo for this family
373 //=============================================================================
375 DriverMED_Family::GetFamilyInfo(const MED::PWrapper& theWrapper,
376 const MED::PMeshInfo& theMeshInfo) const
379 aStr << "FAM_" << myId;
380 set<string>::const_iterator aGrIter = myGroupNames.begin();
381 for(; aGrIter != myGroupNames.end(); aGrIter++){
382 aStr << "_" << *aGrIter;
384 string aValue = aStr.str();
385 // PAL19785,0019867 - med forbids whitespace to be the last char in the name
386 int maxSize = MED::GetNOMLength();
387 int lastCharPos = min( maxSize, (int) aValue.size() ) - 1;
388 while ( isspace( aValue[ lastCharPos ] ))
389 aValue.resize( lastCharPos-- );
391 MED::PFamilyInfo anInfo;
392 if(myId == 0 || myGroupAttributVal == 0){
393 anInfo = theWrapper->CrFamilyInfo(theMeshInfo,
398 MED::TStringVector anAttrDescs (1, ""); // 1 attribute with empty description,
399 MED::TIntVector anAttrIds (1, myId); // Id=0,
400 MED::TIntVector anAttrVals (1, myGroupAttributVal);
401 anInfo = theWrapper->CrFamilyInfo(theMeshInfo,
411 // cout << "Groups: ";
412 // set<string>::iterator aGrIter = myGroupNames.begin();
413 // for (; aGrIter != myGroupNames.end(); aGrIter++)
415 // cout << " " << *aGrIter;
419 // cout << "Elements: ";
420 // set<const SMDS_MeshElement *>::iterator anIter = myElements.begin();
421 // for (; anIter != myElements.end(); anIter++)
423 // cout << " " << (*anIter)->GetID();
430 //=============================================================================
432 * Initialize the tool by SMESHDS_GroupBase
434 //=============================================================================
435 void DriverMED_Family::Init (SMESHDS_GroupBase* theGroup)
439 SMDS_ElemIteratorPtr elemIt = theGroup->GetElements();
440 while (elemIt->more())
442 myElements.insert( myElements.end(), elemIt->next() );
446 myType = theGroup->GetType();
449 myGroupNames.clear();
450 myGroupNames.insert(string(theGroup->GetStoreName()));
452 Quantity_Color aColor = theGroup->GetColor();
453 double aRed = aColor.Red();
454 double aGreen = aColor.Green();
455 double aBlue = aColor.Blue();
456 int aR = int( aRed*255 );
457 int aG = int( aGreen*255 );
458 int aB = int( aBlue*255 );
459 // cout << "aRed = " << aR << endl;
460 // cout << "aGreen = " << aG << endl;
461 // cout << "aBlue = " << aB << endl;
462 myGroupAttributVal = (int)(aR*1000000 + aG*1000 + aB);
463 //cout << "myGroupAttributVal = " << myGroupAttributVal << endl;
466 //=============================================================================
468 * Split <theSubMesh> on some parts (families)
469 * on the basis of the elements type.
471 //=============================================================================
472 DriverMED_FamilyPtrList
473 DriverMED_Family::SplitByType (SMESHDS_SubMesh* theSubMesh,
476 DriverMED_FamilyPtrList aFamilies;
477 DriverMED_FamilyPtr aNodesFamily (new DriverMED_Family);
478 DriverMED_FamilyPtr anEdgesFamily (new DriverMED_Family);
479 DriverMED_FamilyPtr aFacesFamily (new DriverMED_Family);
480 DriverMED_FamilyPtr aVolumesFamily (new DriverMED_Family);
481 // DriverMED_FamilyPtr a0DElemsFamily (new DriverMED_Family);
482 // DriverMED_FamilyPtr aBallsFamily (new DriverMED_Family);
484 char submeshGrpName[ 30 ];
485 sprintf( submeshGrpName, "SubMesh %d", theId );
487 SMDS_NodeIteratorPtr aNodesIter = theSubMesh->GetNodes();
488 while (aNodesIter->more())
490 const SMDS_MeshNode* aNode = aNodesIter->next();
491 aNodesFamily->AddElement(aNode);
494 SMDS_ElemIteratorPtr anElemsIter = theSubMesh->GetElements();
495 while (anElemsIter->more())
497 const SMDS_MeshElement* anElem = anElemsIter->next();
498 switch (anElem->GetType())
501 anEdgesFamily->AddElement(anElem);
504 aFacesFamily->AddElement(anElem);
507 aVolumesFamily->AddElement(anElem);
514 if (!aNodesFamily->IsEmpty()) {
515 aNodesFamily->SetType(SMDSAbs_Node);
516 aNodesFamily->AddGroupName(submeshGrpName);
517 aFamilies.push_back(aNodesFamily);
519 if (!anEdgesFamily->IsEmpty()) {
520 anEdgesFamily->SetType(SMDSAbs_Edge);
521 anEdgesFamily->AddGroupName(submeshGrpName);
522 aFamilies.push_back(anEdgesFamily);
524 if (!aFacesFamily->IsEmpty()) {
525 aFacesFamily->SetType(SMDSAbs_Face);
526 aFacesFamily->AddGroupName(submeshGrpName);
527 aFamilies.push_back(aFacesFamily);
529 if (!aVolumesFamily->IsEmpty()) {
530 aVolumesFamily->SetType(SMDSAbs_Volume);
531 aVolumesFamily->AddGroupName(submeshGrpName);
532 aFamilies.push_back(aVolumesFamily);
538 //=============================================================================
540 * Remove from <myElements> elements, common with <by>,
541 * Remove from <by> elements, common with <myElements>,
542 * Create family <common> from common elements, with combined groups list.
544 //=============================================================================
545 void DriverMED_Family::Split (DriverMED_FamilyPtr by,
546 DriverMED_FamilyPtr common)
549 ElementsSet::iterator anIter = by->myElements.begin(), elemInMe;
550 while ( anIter != by->myElements.end())
552 elemInMe = myElements.find(*anIter);
553 if (elemInMe != myElements.end())
555 common->myElements.insert(*anIter);
556 myElements.erase(elemInMe);
557 by->myElements.erase(anIter++);
563 if (!common->IsEmpty())
566 common->myGroupNames = myGroupNames;
567 common->myGroupNames.insert( by->myGroupNames.begin(), by->myGroupNames.end() );
570 common->myType = myType;
574 //================================================================================
576 * \brief Return a number of elements of a given type
578 //================================================================================
580 size_t DriverMED_Family::NbElements( SMDSAbs_ElementType theType ) const
582 if ( myTypes.size() < 2 )
583 return myElements.size();
586 for ( ElementsSet::iterator e = myElements.begin(); e != myElements.end(); ++e )
587 nb += ( theType == (*e)->GetType() );