1 // Copyright (C) 2007-2014 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(theElement);
77 ::AddGroupName(std::string theGroupName)
79 myGroupNames.insert(theGroupName);
84 ::SetType(const SMDSAbs_ElementType theType)
86 myTypes.insert( myType = theType );
96 const std::set< SMDSAbs_ElementType >&
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)
157 DriverMED_FamilyPtrList aFamilies;
159 string anAllNodesGroupName = "Group_Of_All_Nodes";
160 string anAllEdgesGroupName = "Group_Of_All_Edges";
161 string anAllFacesGroupName = "Group_Of_All_Faces";
162 string anAllVolumesGroupName = "Group_Of_All_Volumes";
163 string anAll0DElemsGroupName = "Group_Of_All_0DElems";
164 string anAllBallsGroupName = "Group_Of_All_Balls";
166 // Reserve 6 ids for families of free elements
167 // (1 - nodes, -1 - edges, -2 - faces, -3 - volumes, -4 - 0D, -5 - balls).
168 // 'Free' means here not belonging to any group.
169 int aNodeFamId = FIRST_NODE_FAMILY;
170 int aElemFamId = FIRST_ELEM_FAMILY;
172 // Process sub-meshes
173 while ( theSubMeshes->more() )
175 SMESHDS_SubMesh* aSubMesh = const_cast< SMESHDS_SubMesh* >( theSubMeshes->next() );
176 const int anId = aSubMesh->GetID();
177 if ( aSubMesh->IsComplexSubmesh() )
178 continue; // submesh containing other submeshs
179 DriverMED_FamilyPtrList aSMFams = SplitByType(aSubMesh,anId);
180 DriverMED_FamilyPtrList::iterator aSMFamsIter = aSMFams.begin();
181 for (; aSMFamsIter != aSMFams.end(); aSMFamsIter++)
183 DriverMED_FamilyPtr aFam2 = (*aSMFamsIter);
184 DriverMED_FamilyPtrList::iterator aFamsIter = aFamilies.begin();
185 while (aFamsIter != aFamilies.end())
187 DriverMED_FamilyPtr aFam1 = *aFamsIter;
188 DriverMED_FamilyPtrList::iterator aCurrIter = aFamsIter++;
189 if (aFam1->myType == aFam2->myType)
191 DriverMED_FamilyPtr aCommon (new DriverMED_Family);
192 aFam1->Split(aFam2, aCommon);
193 if (!aCommon->IsEmpty())
195 aFamilies.push_back(aCommon);
197 if (aFam1->IsEmpty())
199 aFamilies.erase(aCurrIter);
201 if (aFam2->IsEmpty())
205 // The rest elements of family
206 if (!aFam2->IsEmpty())
208 aFamilies.push_back(aFam2);
214 SMESHDS_GroupBasePtrList::const_iterator aGroupsIter = theGroups.begin();
215 for (; aGroupsIter != theGroups.end(); aGroupsIter++)
217 DriverMED_FamilyPtr aFam2 (new DriverMED_Family);
218 aFam2->Init(*aGroupsIter);
220 DriverMED_FamilyPtrList::iterator aFamsIter = aFamilies.begin();
221 while (aFamsIter != aFamilies.end())
223 DriverMED_FamilyPtr aFam1 = *aFamsIter;
224 DriverMED_FamilyPtrList::iterator aCurrIter = aFamsIter++;
225 if (aFam1->myType == aFam2->myType)
227 DriverMED_FamilyPtr aCommon (new DriverMED_Family);
228 aFam1->Split(aFam2, aCommon);
229 if (!aCommon->IsEmpty())
231 aCommon->SetGroupAttributVal(0);
232 aFamilies.push_back(aCommon);
234 if (aFam1->IsEmpty())
236 aFamilies.erase(aCurrIter);
238 if (aFam2->IsEmpty())
242 // The rest elements of group
243 if (!aFam2->IsEmpty())
245 aFamilies.push_back(aFam2);
249 DriverMED_FamilyPtrList::iterator aFamsIter = aFamilies.begin();
250 for (; aFamsIter != aFamilies.end(); aFamsIter++)
252 DriverMED_FamilyPtr aFam = *aFamsIter;
253 if (aFam->myType == SMDSAbs_Node) {
254 aFam->SetId(aNodeFamId++);
255 if (doGroupOfNodes) aFam->myGroupNames.insert(anAllNodesGroupName);
258 aFam->SetId(aElemFamId--);
259 if (aFam->myType == SMDSAbs_Edge) {
260 if (doGroupOfEdges) aFam->myGroupNames.insert(anAllEdgesGroupName);
262 else if (aFam->myType == SMDSAbs_Face) {
263 if (doGroupOfFaces) aFam->myGroupNames.insert(anAllFacesGroupName);
265 else if (aFam->myType == SMDSAbs_Volume) {
266 if (doGroupOfVolumes) aFam->myGroupNames.insert(anAllVolumesGroupName);
268 else if (aFam->myType == SMDSAbs_0DElement) {
269 if (doGroupOfVolumes) aFam->myGroupNames.insert(anAll0DElemsGroupName);
271 else if (aFam->myType == SMDSAbs_Ball) {
272 if (doGroupOfVolumes) aFam->myGroupNames.insert(anAllBallsGroupName);
277 // Create families for elements, not belonging to any group
280 DriverMED_FamilyPtr aFreeNodesFam (new DriverMED_Family);
281 aFreeNodesFam->SetId(REST_NODES_FAMILY);
282 aFreeNodesFam->myType = SMDSAbs_Node;
283 aFreeNodesFam->myGroupNames.insert(anAllNodesGroupName);
284 aFamilies.push_back(aFreeNodesFam);
289 DriverMED_FamilyPtr aFreeEdgesFam (new DriverMED_Family);
290 aFreeEdgesFam->SetId(REST_EDGES_FAMILY);
291 aFreeEdgesFam->myType = SMDSAbs_Edge;
292 aFreeEdgesFam->myGroupNames.insert(anAllEdgesGroupName);
293 aFamilies.push_back(aFreeEdgesFam);
298 DriverMED_FamilyPtr aFreeFacesFam (new DriverMED_Family);
299 aFreeFacesFam->SetId(REST_FACES_FAMILY);
300 aFreeFacesFam->myType = SMDSAbs_Face;
301 aFreeFacesFam->myGroupNames.insert(anAllFacesGroupName);
302 aFamilies.push_back(aFreeFacesFam);
305 if (doGroupOfVolumes)
307 DriverMED_FamilyPtr aFreeVolumesFam (new DriverMED_Family);
308 aFreeVolumesFam->SetId(REST_VOLUMES_FAMILY);
309 aFreeVolumesFam->myType = SMDSAbs_Volume;
310 aFreeVolumesFam->myGroupNames.insert(anAllVolumesGroupName);
311 aFamilies.push_back(aFreeVolumesFam);
314 if (doGroupOf0DElems)
316 DriverMED_FamilyPtr aFree0DFam (new DriverMED_Family);
317 aFree0DFam->SetId(REST_0DELEM_FAMILY);
318 aFree0DFam->myType = SMDSAbs_0DElement;
319 aFree0DFam->myGroupNames.insert(anAll0DElemsGroupName);
320 aFamilies.push_back(aFree0DFam);
325 DriverMED_FamilyPtr aFreeBallsFam (new DriverMED_Family);
326 aFreeBallsFam->SetId(REST_BALL_FAMILY);
327 aFreeBallsFam->myType = SMDSAbs_Ball;
328 aFreeBallsFam->myGroupNames.insert(anAllBallsGroupName);
329 aFamilies.push_back(aFreeBallsFam);
332 DriverMED_FamilyPtr aNullFam (new DriverMED_Family);
334 aNullFam->myType = SMDSAbs_All;
335 aFamilies.push_back(aNullFam);
340 //=============================================================================
342 * Create TFamilyInfo for this family
344 //=============================================================================
346 DriverMED_Family::GetFamilyInfo(const MED::PWrapper& theWrapper,
347 const MED::PMeshInfo& theMeshInfo) const
350 aStr << "FAM_" << myId;
351 set<string>::const_iterator aGrIter = myGroupNames.begin();
352 for(; aGrIter != myGroupNames.end(); aGrIter++){
353 aStr << "_" << *aGrIter;
355 string aValue = aStr.str();
356 // PAL19785,0019867 - med forbids whitespace to be the last char in the name
358 //if ( theWrapper->GetVersion() == MED::eV2_1 )
359 // maxSize = MED::GetNOMLength<MED::eV2_1>();
361 maxSize = MED::GetNOMLength<MED::eV2_2>();
362 int lastCharPos = min( maxSize, (int) aValue.size() ) - 1;
363 while ( isspace( aValue[ lastCharPos ] ))
364 aValue.resize( lastCharPos-- );
366 MED::PFamilyInfo anInfo;
367 if(myId == 0 || myGroupAttributVal == 0){
368 anInfo = theWrapper->CrFamilyInfo(theMeshInfo,
373 MED::TStringVector anAttrDescs (1, ""); // 1 attribute with empty description,
374 MED::TIntVector anAttrIds (1, myId); // Id=0,
375 MED::TIntVector anAttrVals (1, myGroupAttributVal);
376 anInfo = theWrapper->CrFamilyInfo(theMeshInfo,
386 // cout << "Groups: ";
387 // set<string>::iterator aGrIter = myGroupNames.begin();
388 // for (; aGrIter != myGroupNames.end(); aGrIter++)
390 // cout << " " << *aGrIter;
394 // cout << "Elements: ";
395 // set<const SMDS_MeshElement *>::iterator anIter = myElements.begin();
396 // for (; anIter != myElements.end(); anIter++)
398 // cout << " " << (*anIter)->GetID();
405 //=============================================================================
407 * Initialize the tool by SMESHDS_GroupBase
409 //=============================================================================
410 void DriverMED_Family::Init (SMESHDS_GroupBase* theGroup)
414 SMDS_ElemIteratorPtr elemIt = theGroup->GetElements();
415 while (elemIt->more())
417 myElements.insert(elemIt->next());
421 myType = theGroup->GetType();
424 myGroupNames.clear();
425 myGroupNames.insert(string(theGroup->GetStoreName()));
427 Quantity_Color aColor = theGroup->GetColor();
428 double aRed = aColor.Red();
429 double aGreen = aColor.Green();
430 double aBlue = aColor.Blue();
431 int aR = int( aRed*255 );
432 int aG = int( aGreen*255 );
433 int aB = int( aBlue*255 );
434 // cout << "aRed = " << aR << endl;
435 // cout << "aGreen = " << aG << endl;
436 // cout << "aBlue = " << aB << endl;
437 myGroupAttributVal = (int)(aR*1000000 + aG*1000 + aB);
438 //cout << "myGroupAttributVal = " << myGroupAttributVal << endl;
441 //=============================================================================
443 * Split <theSubMesh> on some parts (families)
444 * on the basis of the elements type.
446 //=============================================================================
447 DriverMED_FamilyPtrList
449 ::SplitByType (SMESHDS_SubMesh* theSubMesh,
452 DriverMED_FamilyPtrList aFamilies;
453 DriverMED_FamilyPtr aNodesFamily (new DriverMED_Family);
454 DriverMED_FamilyPtr anEdgesFamily (new DriverMED_Family);
455 DriverMED_FamilyPtr aFacesFamily (new DriverMED_Family);
456 DriverMED_FamilyPtr aVolumesFamily (new DriverMED_Family);
457 // DriverMED_FamilyPtr a0DElemsFamily (new DriverMED_Family);
458 // DriverMED_FamilyPtr aBallsFamily (new DriverMED_Family);
460 char submeshGrpName[ 30 ];
461 sprintf( submeshGrpName, "SubMesh %d", theId );
463 SMDS_NodeIteratorPtr aNodesIter = theSubMesh->GetNodes();
464 while (aNodesIter->more())
466 const SMDS_MeshNode* aNode = aNodesIter->next();
467 aNodesFamily->AddElement(aNode);
470 SMDS_ElemIteratorPtr anElemsIter = theSubMesh->GetElements();
471 while (anElemsIter->more())
473 const SMDS_MeshElement* anElem = anElemsIter->next();
474 switch (anElem->GetType())
477 anEdgesFamily->AddElement(anElem);
480 aFacesFamily->AddElement(anElem);
483 aVolumesFamily->AddElement(anElem);
490 if (!aNodesFamily->IsEmpty()) {
491 aNodesFamily->SetType(SMDSAbs_Node);
492 aNodesFamily->AddGroupName(submeshGrpName);
493 aFamilies.push_back(aNodesFamily);
495 if (!anEdgesFamily->IsEmpty()) {
496 anEdgesFamily->SetType(SMDSAbs_Edge);
497 anEdgesFamily->AddGroupName(submeshGrpName);
498 aFamilies.push_back(anEdgesFamily);
500 if (!aFacesFamily->IsEmpty()) {
501 aFacesFamily->SetType(SMDSAbs_Face);
502 aFacesFamily->AddGroupName(submeshGrpName);
503 aFamilies.push_back(aFacesFamily);
505 if (!aVolumesFamily->IsEmpty()) {
506 aVolumesFamily->SetType(SMDSAbs_Volume);
507 aVolumesFamily->AddGroupName(submeshGrpName);
508 aFamilies.push_back(aVolumesFamily);
514 //=============================================================================
516 * Remove from <myElements> elements, common with <by>,
517 * Remove from <by> elements, common with <myElements>,
518 * Create family <common> from common elements, with combined groups list.
520 //=============================================================================
521 void DriverMED_Family::Split (DriverMED_FamilyPtr by,
522 DriverMED_FamilyPtr common)
525 ElementsSet::iterator anIter = by->myElements.begin(), elemInMe;
526 while ( anIter != by->myElements.end())
528 elemInMe = myElements.find(*anIter);
529 if (elemInMe != myElements.end())
531 common->myElements.insert(*anIter);
532 myElements.erase(elemInMe);
533 by->myElements.erase(anIter++);
539 if (!common->IsEmpty())
542 common->myGroupNames = myGroupNames;
543 common->myGroupNames.insert( by->myGroupNames.begin(), by->myGroupNames.end() );
546 common->myType = myType;