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 SALOMEDS::Color& theColor, const SALOMEDS::Color& theRefColor )
134 if( fabs( theColor.R - theRefColor.R ) < 0.01 &&
135 fabs( theColor.G - theRefColor.G ) < 0.01 &&
136 fabs( theColor.B - theRefColor.B ) < 0.01 )
142 //=============================================================================
144 * Split each group from list <aGroups> on some parts (families)
145 * on the basis of the elements membership in other groups from this list.
146 * Resulting families have no common elements.
148 //=============================================================================
149 DriverMED_FamilyPtrList
151 ::MakeFamilies(const SMESHDS_SubMeshPtrMap& theSubMeshes,
152 const SMESHDS_GroupBasePtrList& theGroups,
153 const bool doGroupOfNodes,
154 const bool doGroupOfEdges,
155 const bool doGroupOfFaces,
156 const bool doGroupOfVolumes)
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";
165 // Reserve four ids for families of free elements
166 // (1 - nodes, -1 - edges, -2 - faces, -3 - volumes).
167 // 'Free' means here not belonging to any group.
168 int aNodeFamId = FIRST_NODE_FAMILY;
169 int aElemFamId = FIRST_ELEM_FAMILY;
171 // Process sub-meshes
172 SMESHDS_SubMeshPtrMap::const_iterator aSMIter = theSubMeshes.begin();
173 for (; aSMIter != theSubMeshes.end(); aSMIter++)
175 const int anId = aSMIter->first;
176 SMESHDS_SubMesh* aSubMesh = aSMIter->second;
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;
218 for (aGroupsIter = theGroups.begin(); aGroupsIter != theGroups.end(); aGroupsIter++)
220 SALOMEDS::Color aColor = (*aGroupsIter)->GetColor();
222 bool isFound = false;
223 for (ColorMap::iterator aColorIter = aColorMap.begin(); aColorIter != aColorMap.end(); aColorIter++)
225 SALOMEDS::Color aRefColor = aColorIter->second;
226 if( CompareColors( aColor, aRefColor ) )
234 aColorMap[ id++ ] = aColor;
237 for (aGroupsIter = theGroups.begin(); aGroupsIter != theGroups.end(); aGroupsIter++)
239 DriverMED_FamilyPtr aFam2 (new DriverMED_Family);
240 aFam2->Init(*aGroupsIter, aColorMap);
242 DriverMED_FamilyPtrList::iterator aFamsIter = aFamilies.begin();
243 while (aFamsIter != aFamilies.end())
245 DriverMED_FamilyPtr aFam1 = *aFamsIter;
246 DriverMED_FamilyPtrList::iterator aCurrIter = aFamsIter++;
247 if (aFam1->myType == aFam2->myType)
249 DriverMED_FamilyPtr aCommon (new DriverMED_Family);
250 aFam1->Split(aFam2, aCommon);
251 if (!aCommon->IsEmpty())
253 aCommon->SetGroupAttributVal(0);
254 aFamilies.push_back(aCommon);
256 if (aFam1->IsEmpty())
258 aFamilies.erase(aCurrIter);
260 if (aFam2->IsEmpty())
264 // The rest elements of group
265 if (!aFam2->IsEmpty())
267 aFamilies.push_back(aFam2);
271 DriverMED_FamilyPtrList::iterator aFamsIter = aFamilies.begin();
272 for (; aFamsIter != aFamilies.end(); aFamsIter++)
274 DriverMED_FamilyPtr aFam = *aFamsIter;
275 if (aFam->myType == SMDSAbs_Node) {
276 aFam->SetId(aNodeFamId++);
277 if (doGroupOfNodes) aFam->myGroupNames.insert(anAllNodesGroupName);
280 aFam->SetId(aElemFamId--);
281 if (aFam->myType == SMDSAbs_Edge) {
282 if (doGroupOfEdges) aFam->myGroupNames.insert(anAllEdgesGroupName);
284 else if (aFam->myType == SMDSAbs_Face) {
285 if (doGroupOfFaces) aFam->myGroupNames.insert(anAllFacesGroupName);
287 else if (aFam->myType == SMDSAbs_Volume) {
288 if (doGroupOfVolumes) aFam->myGroupNames.insert(anAllVolumesGroupName);
293 // Create families for elements, not belonging to any group
296 DriverMED_FamilyPtr aFreeNodesFam (new DriverMED_Family);
297 aFreeNodesFam->SetId(REST_NODES_FAMILY);
298 aFreeNodesFam->myType = SMDSAbs_Node;
299 aFreeNodesFam->myGroupNames.insert(anAllNodesGroupName);
300 aFamilies.push_back(aFreeNodesFam);
305 DriverMED_FamilyPtr aFreeEdgesFam (new DriverMED_Family);
306 aFreeEdgesFam->SetId(REST_EDGES_FAMILY);
307 aFreeEdgesFam->myType = SMDSAbs_Edge;
308 aFreeEdgesFam->myGroupNames.insert(anAllEdgesGroupName);
309 aFamilies.push_back(aFreeEdgesFam);
314 DriverMED_FamilyPtr aFreeFacesFam (new DriverMED_Family);
315 aFreeFacesFam->SetId(REST_FACES_FAMILY);
316 aFreeFacesFam->myType = SMDSAbs_Face;
317 aFreeFacesFam->myGroupNames.insert(anAllFacesGroupName);
318 aFamilies.push_back(aFreeFacesFam);
321 if (doGroupOfVolumes)
323 DriverMED_FamilyPtr aFreeVolumesFam (new DriverMED_Family);
324 aFreeVolumesFam->SetId(REST_VOLUMES_FAMILY);
325 aFreeVolumesFam->myType = SMDSAbs_Volume;
326 aFreeVolumesFam->myGroupNames.insert(anAllVolumesGroupName);
327 aFamilies.push_back(aFreeVolumesFam);
330 DriverMED_FamilyPtr aNullFam (new DriverMED_Family);
332 aNullFam->myType = SMDSAbs_All;
333 aFamilies.push_back(aNullFam);
338 //=============================================================================
340 * Create TFamilyInfo for this family
342 //=============================================================================
344 DriverMED_Family::GetFamilyInfo(const MED::PWrapper& theWrapper,
345 const MED::PMeshInfo& theMeshInfo) const
348 aStr << "FAM_" << myId;
349 set<string>::const_iterator aGrIter = myGroupNames.begin();
350 for(; aGrIter != myGroupNames.end(); aGrIter++){
351 aStr << "_" << *aGrIter;
354 MED::PFamilyInfo anInfo;
355 string aValue = aStr.str();
356 if(myId == 0 || myGroupAttributVal == 0){
357 anInfo = theWrapper->CrFamilyInfo(theMeshInfo,
362 MED::TStringVector anAttrDescs (1, ""); // 1 attribute with empty description,
363 MED::TIntVector anAttrIds (1, myId); // Id=0,
364 MED::TIntVector anAttrVals (1, myGroupAttributVal);
365 anInfo = theWrapper->CrFamilyInfo(theMeshInfo,
375 // cout << "Groups: ";
376 // set<string>::iterator aGrIter = myGroupNames.begin();
377 // for (; aGrIter != myGroupNames.end(); aGrIter++)
379 // cout << " " << *aGrIter;
383 // cout << "Elements: ";
384 // set<const SMDS_MeshElement *>::iterator anIter = myElements.begin();
385 // for (; anIter != myElements.end(); anIter++)
387 // cout << " " << (*anIter)->GetID();
394 //=============================================================================
396 * Initialize the tool by SMESHDS_GroupBase
398 //=============================================================================
399 void DriverMED_Family::Init (SMESHDS_GroupBase* theGroup, const ColorMap& theColorMap)
403 SMDS_ElemIteratorPtr elemIt = theGroup->GetElements();
404 while (elemIt->more())
406 myElements.insert(elemIt->next());
410 myType = theGroup->GetType();
413 myGroupNames.clear();
414 myGroupNames.insert(string(theGroup->GetStoreName()));
416 myGroupAttributVal = 0;
418 ColorMap::const_iterator aColorIter = theColorMap.begin();
419 for (; aColorIter != theColorMap.end(); aColorIter++)
421 SALOMEDS::Color aColor = aColorIter->second;
422 if( CompareColors( theGroup->GetColor(), aColor ) )
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;