1 // Copyright (C) 2007-2013 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
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(const SMESHDS_SubMeshPtrMap& 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 SMESHDS_SubMeshPtrMap::const_iterator aSMIter = theSubMeshes.begin();
174 for (; aSMIter != theSubMeshes.end(); aSMIter++)
176 const int anId = aSMIter->first;
177 SMESHDS_SubMesh* aSubMesh = aSMIter->second;
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);
335 aNullFam->myType = SMDSAbs_All;
336 aFamilies.push_back(aNullFam);
341 //=============================================================================
343 * Create TFamilyInfo for this family
345 //=============================================================================
347 DriverMED_Family::GetFamilyInfo(const MED::PWrapper& theWrapper,
348 const MED::PMeshInfo& theMeshInfo) const
351 aStr << "FAM_" << myId;
352 set<string>::const_iterator aGrIter = myGroupNames.begin();
353 for(; aGrIter != myGroupNames.end(); aGrIter++){
354 aStr << "_" << *aGrIter;
356 string aValue = aStr.str();
357 // PAL19785,0019867 - med forbids whitespace to be the last char in the name
359 //if ( theWrapper->GetVersion() == MED::eV2_1 )
360 // maxSize = MED::GetNOMLength<MED::eV2_1>();
362 maxSize = MED::GetNOMLength<MED::eV2_2>();
363 int lastCharPos = min( maxSize, (int) aValue.size() ) - 1;
364 while ( isspace( aValue[ lastCharPos ] ))
365 aValue.resize( lastCharPos-- );
367 MED::PFamilyInfo anInfo;
368 if(myId == 0 || myGroupAttributVal == 0){
369 anInfo = theWrapper->CrFamilyInfo(theMeshInfo,
374 MED::TStringVector anAttrDescs (1, ""); // 1 attribute with empty description,
375 MED::TIntVector anAttrIds (1, myId); // Id=0,
376 MED::TIntVector anAttrVals (1, myGroupAttributVal);
377 anInfo = theWrapper->CrFamilyInfo(theMeshInfo,
387 // cout << "Groups: ";
388 // set<string>::iterator aGrIter = myGroupNames.begin();
389 // for (; aGrIter != myGroupNames.end(); aGrIter++)
391 // cout << " " << *aGrIter;
395 // cout << "Elements: ";
396 // set<const SMDS_MeshElement *>::iterator anIter = myElements.begin();
397 // for (; anIter != myElements.end(); anIter++)
399 // cout << " " << (*anIter)->GetID();
406 //=============================================================================
408 * Initialize the tool by SMESHDS_GroupBase
410 //=============================================================================
411 void DriverMED_Family::Init (SMESHDS_GroupBase* theGroup)
415 SMDS_ElemIteratorPtr elemIt = theGroup->GetElements();
416 while (elemIt->more())
418 myElements.insert(elemIt->next());
422 myType = theGroup->GetType();
425 myGroupNames.clear();
426 myGroupNames.insert(string(theGroup->GetStoreName()));
428 Quantity_Color aColor = theGroup->GetColor();
429 double aRed = aColor.Red();
430 double aGreen = aColor.Green();
431 double aBlue = aColor.Blue();
432 int aR = int( aRed*255 );
433 int aG = int( aGreen*255 );
434 int aB = int( aBlue*255 );
435 // cout << "aRed = " << aR << endl;
436 // cout << "aGreen = " << aG << endl;
437 // cout << "aBlue = " << aB << endl;
438 myGroupAttributVal = (int)(aR*1000000 + aG*1000 + aB);
439 //cout << "myGroupAttributVal = " << myGroupAttributVal << endl;
442 //=============================================================================
444 * Split <theSubMesh> on some parts (families)
445 * on the basis of the elements type.
447 //=============================================================================
448 DriverMED_FamilyPtrList
450 ::SplitByType (SMESHDS_SubMesh* theSubMesh,
453 DriverMED_FamilyPtrList aFamilies;
454 DriverMED_FamilyPtr aNodesFamily (new DriverMED_Family);
455 DriverMED_FamilyPtr anEdgesFamily (new DriverMED_Family);
456 DriverMED_FamilyPtr aFacesFamily (new DriverMED_Family);
457 DriverMED_FamilyPtr aVolumesFamily (new DriverMED_Family);
458 // DriverMED_FamilyPtr a0DElemsFamily (new DriverMED_Family);
459 // DriverMED_FamilyPtr aBallsFamily (new DriverMED_Family);
461 char submeshGrpName[ 30 ];
462 sprintf( submeshGrpName, "SubMesh %d", theId );
464 SMDS_NodeIteratorPtr aNodesIter = theSubMesh->GetNodes();
465 while (aNodesIter->more())
467 const SMDS_MeshNode* aNode = aNodesIter->next();
468 aNodesFamily->AddElement(aNode);
471 SMDS_ElemIteratorPtr anElemsIter = theSubMesh->GetElements();
472 while (anElemsIter->more())
474 const SMDS_MeshElement* anElem = anElemsIter->next();
475 switch (anElem->GetType())
478 anEdgesFamily->AddElement(anElem);
481 aFacesFamily->AddElement(anElem);
484 aVolumesFamily->AddElement(anElem);
491 if (!aNodesFamily->IsEmpty()) {
492 aNodesFamily->SetType(SMDSAbs_Node);
493 aNodesFamily->AddGroupName(submeshGrpName);
494 aFamilies.push_back(aNodesFamily);
496 if (!anEdgesFamily->IsEmpty()) {
497 anEdgesFamily->SetType(SMDSAbs_Edge);
498 anEdgesFamily->AddGroupName(submeshGrpName);
499 aFamilies.push_back(anEdgesFamily);
501 if (!aFacesFamily->IsEmpty()) {
502 aFacesFamily->SetType(SMDSAbs_Face);
503 aFacesFamily->AddGroupName(submeshGrpName);
504 aFamilies.push_back(aFacesFamily);
506 if (!aVolumesFamily->IsEmpty()) {
507 aVolumesFamily->SetType(SMDSAbs_Volume);
508 aVolumesFamily->AddGroupName(submeshGrpName);
509 aFamilies.push_back(aVolumesFamily);
515 //=============================================================================
517 * Remove from <myElements> elements, common with <by>,
518 * Remove from <by> elements, common with <myElements>,
519 * Create family <common> from common elements, with combined groups list.
521 //=============================================================================
522 void DriverMED_Family::Split (DriverMED_FamilyPtr by,
523 DriverMED_FamilyPtr common)
526 ElementsSet::iterator anIter = by->myElements.begin(), elemInMe;
527 while ( anIter != by->myElements.end())
529 elemInMe = myElements.find(*anIter);
530 if (elemInMe != myElements.end())
532 common->myElements.insert(*anIter);
533 myElements.erase(elemInMe);
534 by->myElements.erase(anIter++);
540 if (!common->IsEmpty())
543 common->myGroupNames = myGroupNames;
544 common->myGroupNames.insert( by->myGroupNames.begin(), by->myGroupNames.end() );
547 common->myType = myType;