1 // SMESH DriverMED : tool to split groups on families
3 // Copyright (C) 2003 CEA
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : DriverMED_Family.cxx
24 // 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)
98 ::MemberOf(std::string theGroupName) const
100 return myGroupNames.find(theGroupName) != myGroupNames.end();
103 const MED::TStringSet&
105 ::GetGroupNames () const
113 ::GetGroupAttributVal() const
115 return myGroupAttributVal;
120 ::SetGroupAttributVal( int theValue)
122 myGroupAttributVal = theValue;
129 return myElements.empty();
132 bool CompareColors( const Quantity_Color& theColor, const Quantity_Color& theRefColor )
134 float aTolerance = 0.01;
135 if( fabs( theColor.Red() - theRefColor.Red() ) < aTolerance &&
136 fabs( theColor.Green() - theRefColor.Green() ) < aTolerance &&
137 fabs( theColor.Blue() - theRefColor.Blue() ) < aTolerance )
143 //=============================================================================
145 * Split each group from list <aGroups> on some parts (families)
146 * on the basis of the elements membership in other groups from this list.
147 * Resulting families have no common elements.
149 //=============================================================================
150 DriverMED_FamilyPtrList
152 ::MakeFamilies(const SMESHDS_SubMeshPtrMap& theSubMeshes,
153 const SMESHDS_GroupBasePtrList& theGroups,
154 const bool doGroupOfNodes,
155 const bool doGroupOfEdges,
156 const bool doGroupOfFaces,
157 const bool doGroupOfVolumes)
159 DriverMED_FamilyPtrList aFamilies;
161 string anAllNodesGroupName = "Group_Of_All_Nodes";
162 string anAllEdgesGroupName = "Group_Of_All_Edges";
163 string anAllFacesGroupName = "Group_Of_All_Faces";
164 string anAllVolumesGroupName = "Group_Of_All_Volumes";
166 // Reserve four ids for families of free elements
167 // (1 - nodes, -1 - edges, -2 - faces, -3 - volumes).
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;
219 for (aGroupsIter = theGroups.begin(); aGroupsIter != theGroups.end(); aGroupsIter++)
221 Quantity_Color aColor = (*aGroupsIter)->GetColor();
223 bool isFound = false;
224 for (ColorMap::iterator aColorIter = aColorMap.begin(); aColorIter != aColorMap.end(); aColorIter++)
226 Quantity_Color aRefColor = aColorIter->second;
227 if( CompareColors( aColor, aRefColor ) )
235 aColorMap[ id++ ] = aColor;
238 for (aGroupsIter = theGroups.begin(); aGroupsIter != theGroups.end(); aGroupsIter++)
240 DriverMED_FamilyPtr aFam2 (new DriverMED_Family);
241 aFam2->Init(*aGroupsIter, aColorMap);
243 DriverMED_FamilyPtrList::iterator aFamsIter = aFamilies.begin();
244 while (aFamsIter != aFamilies.end())
246 DriverMED_FamilyPtr aFam1 = *aFamsIter;
247 DriverMED_FamilyPtrList::iterator aCurrIter = aFamsIter++;
248 if (aFam1->myType == aFam2->myType)
250 DriverMED_FamilyPtr aCommon (new DriverMED_Family);
251 aFam1->Split(aFam2, aCommon);
252 if (!aCommon->IsEmpty())
254 aCommon->SetGroupAttributVal(0);
255 aFamilies.push_back(aCommon);
257 if (aFam1->IsEmpty())
259 aFamilies.erase(aCurrIter);
261 if (aFam2->IsEmpty())
265 // The rest elements of group
266 if (!aFam2->IsEmpty())
268 aFamilies.push_back(aFam2);
272 DriverMED_FamilyPtrList::iterator aFamsIter = aFamilies.begin();
273 for (; aFamsIter != aFamilies.end(); aFamsIter++)
275 DriverMED_FamilyPtr aFam = *aFamsIter;
276 if (aFam->myType == SMDSAbs_Node) {
277 aFam->SetId(aNodeFamId++);
278 if (doGroupOfNodes) aFam->myGroupNames.insert(anAllNodesGroupName);
281 aFam->SetId(aElemFamId--);
282 if (aFam->myType == SMDSAbs_Edge) {
283 if (doGroupOfEdges) aFam->myGroupNames.insert(anAllEdgesGroupName);
285 else if (aFam->myType == SMDSAbs_Face) {
286 if (doGroupOfFaces) aFam->myGroupNames.insert(anAllFacesGroupName);
288 else if (aFam->myType == SMDSAbs_Volume) {
289 if (doGroupOfVolumes) aFam->myGroupNames.insert(anAllVolumesGroupName);
294 // Create families for elements, not belonging to any group
297 DriverMED_FamilyPtr aFreeNodesFam (new DriverMED_Family);
298 aFreeNodesFam->SetId(REST_NODES_FAMILY);
299 aFreeNodesFam->myType = SMDSAbs_Node;
300 aFreeNodesFam->myGroupNames.insert(anAllNodesGroupName);
301 aFamilies.push_back(aFreeNodesFam);
306 DriverMED_FamilyPtr aFreeEdgesFam (new DriverMED_Family);
307 aFreeEdgesFam->SetId(REST_EDGES_FAMILY);
308 aFreeEdgesFam->myType = SMDSAbs_Edge;
309 aFreeEdgesFam->myGroupNames.insert(anAllEdgesGroupName);
310 aFamilies.push_back(aFreeEdgesFam);
315 DriverMED_FamilyPtr aFreeFacesFam (new DriverMED_Family);
316 aFreeFacesFam->SetId(REST_FACES_FAMILY);
317 aFreeFacesFam->myType = SMDSAbs_Face;
318 aFreeFacesFam->myGroupNames.insert(anAllFacesGroupName);
319 aFamilies.push_back(aFreeFacesFam);
322 if (doGroupOfVolumes)
324 DriverMED_FamilyPtr aFreeVolumesFam (new DriverMED_Family);
325 aFreeVolumesFam->SetId(REST_VOLUMES_FAMILY);
326 aFreeVolumesFam->myType = SMDSAbs_Volume;
327 aFreeVolumesFam->myGroupNames.insert(anAllVolumesGroupName);
328 aFamilies.push_back(aFreeVolumesFam);
331 DriverMED_FamilyPtr aNullFam (new DriverMED_Family);
333 aNullFam->myType = SMDSAbs_All;
334 aFamilies.push_back(aNullFam);
339 //=============================================================================
341 * Create TFamilyInfo for this family
343 //=============================================================================
345 DriverMED_Family::GetFamilyInfo(const MED::PWrapper& theWrapper,
346 const MED::PMeshInfo& theMeshInfo) const
349 aStr << "FAM_" << myId;
350 set<string>::const_iterator aGrIter = myGroupNames.begin();
351 for(; aGrIter != myGroupNames.end(); aGrIter++){
352 aStr << "_" << *aGrIter;
355 MED::PFamilyInfo anInfo;
356 string aValue = aStr.str();
357 if(myId == 0 || myGroupAttributVal == 0){
358 anInfo = theWrapper->CrFamilyInfo(theMeshInfo,
363 MED::TStringVector anAttrDescs (1, ""); // 1 attribute with empty description,
364 MED::TIntVector anAttrIds (1, myId); // Id=0,
365 MED::TIntVector anAttrVals (1, myGroupAttributVal);
366 anInfo = theWrapper->CrFamilyInfo(theMeshInfo,
376 // cout << "Groups: ";
377 // set<string>::iterator aGrIter = myGroupNames.begin();
378 // for (; aGrIter != myGroupNames.end(); aGrIter++)
380 // cout << " " << *aGrIter;
384 // cout << "Elements: ";
385 // set<const SMDS_MeshElement *>::iterator anIter = myElements.begin();
386 // for (; anIter != myElements.end(); anIter++)
388 // cout << " " << (*anIter)->GetID();
395 //=============================================================================
397 * Initialize the tool by SMESHDS_GroupBase
399 //=============================================================================
400 void DriverMED_Family::Init (SMESHDS_GroupBase* theGroup, const ColorMap& theColorMap)
404 SMDS_ElemIteratorPtr elemIt = theGroup->GetElements();
405 while (elemIt->more())
407 myElements.insert(elemIt->next());
411 myType = theGroup->GetType();
414 myGroupNames.clear();
415 myGroupNames.insert(string(theGroup->GetStoreName()));
417 myGroupAttributVal = 0;
419 ColorMap::const_iterator aColorIter = theColorMap.begin();
420 for (; aColorIter != theColorMap.end(); aColorIter++)
422 if( CompareColors( theGroup->GetColor(), aColorIter->second ) )
424 myGroupAttributVal = aColorIter->first;
430 //=============================================================================
432 * Split <theSubMesh> on some parts (families)
433 * on the basis of the elements type.
435 //=============================================================================
436 DriverMED_FamilyPtrList
438 ::SplitByType (SMESHDS_SubMesh* theSubMesh,
441 DriverMED_FamilyPtrList aFamilies;
442 DriverMED_FamilyPtr aNodesFamily (new DriverMED_Family);
443 DriverMED_FamilyPtr anEdgesFamily (new DriverMED_Family);
444 DriverMED_FamilyPtr aFacesFamily (new DriverMED_Family);
445 DriverMED_FamilyPtr aVolumesFamily (new DriverMED_Family);
447 char submeshGrpName[ 30 ];
448 sprintf( submeshGrpName, "SubMesh %d", theId );
450 SMDS_NodeIteratorPtr aNodesIter = theSubMesh->GetNodes();
451 while (aNodesIter->more())
453 const SMDS_MeshNode* aNode = aNodesIter->next();
454 aNodesFamily->AddElement(aNode);
457 SMDS_ElemIteratorPtr anElemsIter = theSubMesh->GetElements();
458 while (anElemsIter->more())
460 const SMDS_MeshElement* anElem = anElemsIter->next();
461 switch (anElem->GetType())
464 anEdgesFamily->AddElement(anElem);
467 aFacesFamily->AddElement(anElem);
470 aVolumesFamily->AddElement(anElem);
477 if (!aNodesFamily->IsEmpty()) {
478 aNodesFamily->SetType(SMDSAbs_Node);
479 aNodesFamily->AddGroupName(submeshGrpName);
480 aFamilies.push_back(aNodesFamily);
482 if (!anEdgesFamily->IsEmpty()) {
483 anEdgesFamily->SetType(SMDSAbs_Edge);
484 anEdgesFamily->AddGroupName(submeshGrpName);
485 aFamilies.push_back(anEdgesFamily);
487 if (!aFacesFamily->IsEmpty()) {
488 aFacesFamily->SetType(SMDSAbs_Face);
489 aFacesFamily->AddGroupName(submeshGrpName);
490 aFamilies.push_back(aFacesFamily);
492 if (!aVolumesFamily->IsEmpty()) {
493 aVolumesFamily->SetType(SMDSAbs_Volume);
494 aVolumesFamily->AddGroupName(submeshGrpName);
495 aFamilies.push_back(aVolumesFamily);
501 //=============================================================================
503 * Remove from <myElements> elements, common with <by>,
504 * Remove from <by> elements, common with <myElements>,
505 * Create family <common> from common elements, with combined groups list.
507 //=============================================================================
508 void DriverMED_Family::Split (DriverMED_FamilyPtr by,
509 DriverMED_FamilyPtr common)
512 ElementsSet::iterator anIter = by->myElements.begin();
513 while ( anIter != by->myElements.end())
515 if (myElements.find(*anIter) != myElements.end())
517 common->myElements.insert(*anIter);
518 myElements.erase(*anIter);
519 by->myElements.erase(anIter++);
525 if (!common->IsEmpty())
528 common->myGroupNames = myGroupNames;
529 MED::TStringSet::iterator aGrNamesIter = by->myGroupNames.begin();
530 for (; aGrNamesIter != by->myGroupNames.end(); aGrNamesIter++)
532 common->myGroupNames.insert(*aGrNamesIter);
536 common->myType = myType;