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 Quantity_Color aQColor = (*aGroupsIter)->GetColor();
221 SALOMEDS::Color aColor;
222 aColor.R = aQColor.Red();
223 aColor.G = aQColor.Green();
224 aColor.B = aQColor.Blue();
226 bool isFound = false;
227 for (ColorMap::iterator aColorIter = aColorMap.begin(); aColorIter != aColorMap.end(); aColorIter++)
229 SALOMEDS::Color aRefColor = aColorIter->second;
230 if( CompareColors( aColor, aRefColor ) )
238 aColorMap[ id++ ] = aColor;
241 for (aGroupsIter = theGroups.begin(); aGroupsIter != theGroups.end(); aGroupsIter++)
243 DriverMED_FamilyPtr aFam2 (new DriverMED_Family);
244 aFam2->Init(*aGroupsIter, aColorMap);
246 DriverMED_FamilyPtrList::iterator aFamsIter = aFamilies.begin();
247 while (aFamsIter != aFamilies.end())
249 DriverMED_FamilyPtr aFam1 = *aFamsIter;
250 DriverMED_FamilyPtrList::iterator aCurrIter = aFamsIter++;
251 if (aFam1->myType == aFam2->myType)
253 DriverMED_FamilyPtr aCommon (new DriverMED_Family);
254 aFam1->Split(aFam2, aCommon);
255 if (!aCommon->IsEmpty())
257 aCommon->SetGroupAttributVal(0);
258 aFamilies.push_back(aCommon);
260 if (aFam1->IsEmpty())
262 aFamilies.erase(aCurrIter);
264 if (aFam2->IsEmpty())
268 // The rest elements of group
269 if (!aFam2->IsEmpty())
271 aFamilies.push_back(aFam2);
275 DriverMED_FamilyPtrList::iterator aFamsIter = aFamilies.begin();
276 for (; aFamsIter != aFamilies.end(); aFamsIter++)
278 DriverMED_FamilyPtr aFam = *aFamsIter;
279 if (aFam->myType == SMDSAbs_Node) {
280 aFam->SetId(aNodeFamId++);
281 if (doGroupOfNodes) aFam->myGroupNames.insert(anAllNodesGroupName);
284 aFam->SetId(aElemFamId--);
285 if (aFam->myType == SMDSAbs_Edge) {
286 if (doGroupOfEdges) aFam->myGroupNames.insert(anAllEdgesGroupName);
288 else if (aFam->myType == SMDSAbs_Face) {
289 if (doGroupOfFaces) aFam->myGroupNames.insert(anAllFacesGroupName);
291 else if (aFam->myType == SMDSAbs_Volume) {
292 if (doGroupOfVolumes) aFam->myGroupNames.insert(anAllVolumesGroupName);
297 // Create families for elements, not belonging to any group
300 DriverMED_FamilyPtr aFreeNodesFam (new DriverMED_Family);
301 aFreeNodesFam->SetId(REST_NODES_FAMILY);
302 aFreeNodesFam->myType = SMDSAbs_Node;
303 aFreeNodesFam->myGroupNames.insert(anAllNodesGroupName);
304 aFamilies.push_back(aFreeNodesFam);
309 DriverMED_FamilyPtr aFreeEdgesFam (new DriverMED_Family);
310 aFreeEdgesFam->SetId(REST_EDGES_FAMILY);
311 aFreeEdgesFam->myType = SMDSAbs_Edge;
312 aFreeEdgesFam->myGroupNames.insert(anAllEdgesGroupName);
313 aFamilies.push_back(aFreeEdgesFam);
318 DriverMED_FamilyPtr aFreeFacesFam (new DriverMED_Family);
319 aFreeFacesFam->SetId(REST_FACES_FAMILY);
320 aFreeFacesFam->myType = SMDSAbs_Face;
321 aFreeFacesFam->myGroupNames.insert(anAllFacesGroupName);
322 aFamilies.push_back(aFreeFacesFam);
325 if (doGroupOfVolumes)
327 DriverMED_FamilyPtr aFreeVolumesFam (new DriverMED_Family);
328 aFreeVolumesFam->SetId(REST_VOLUMES_FAMILY);
329 aFreeVolumesFam->myType = SMDSAbs_Volume;
330 aFreeVolumesFam->myGroupNames.insert(anAllVolumesGroupName);
331 aFamilies.push_back(aFreeVolumesFam);
334 DriverMED_FamilyPtr aNullFam (new DriverMED_Family);
336 aNullFam->myType = SMDSAbs_All;
337 aFamilies.push_back(aNullFam);
342 //=============================================================================
344 * Create TFamilyInfo for this family
346 //=============================================================================
348 DriverMED_Family::GetFamilyInfo(const MED::PWrapper& theWrapper,
349 const MED::PMeshInfo& theMeshInfo) const
352 aStr << "FAM_" << myId;
353 set<string>::const_iterator aGrIter = myGroupNames.begin();
354 for(; aGrIter != myGroupNames.end(); aGrIter++){
355 aStr << "_" << *aGrIter;
358 MED::PFamilyInfo anInfo;
359 string aValue = aStr.str();
360 if(myId == 0 || myGroupAttributVal == 0){
361 anInfo = theWrapper->CrFamilyInfo(theMeshInfo,
366 MED::TStringVector anAttrDescs (1, ""); // 1 attribute with empty description,
367 MED::TIntVector anAttrIds (1, myId); // Id=0,
368 MED::TIntVector anAttrVals (1, myGroupAttributVal);
369 anInfo = theWrapper->CrFamilyInfo(theMeshInfo,
379 // cout << "Groups: ";
380 // set<string>::iterator aGrIter = myGroupNames.begin();
381 // for (; aGrIter != myGroupNames.end(); aGrIter++)
383 // cout << " " << *aGrIter;
387 // cout << "Elements: ";
388 // set<const SMDS_MeshElement *>::iterator anIter = myElements.begin();
389 // for (; anIter != myElements.end(); anIter++)
391 // cout << " " << (*anIter)->GetID();
398 //=============================================================================
400 * Initialize the tool by SMESHDS_GroupBase
402 //=============================================================================
403 void DriverMED_Family::Init (SMESHDS_GroupBase* theGroup, const ColorMap& theColorMap)
407 SMDS_ElemIteratorPtr elemIt = theGroup->GetElements();
408 while (elemIt->more())
410 myElements.insert(elemIt->next());
414 myType = theGroup->GetType();
417 myGroupNames.clear();
418 myGroupNames.insert(string(theGroup->GetStoreName()));
420 myGroupAttributVal = 0;
422 ColorMap::const_iterator aColorIter = theColorMap.begin();
423 for (; aColorIter != theColorMap.end(); aColorIter++)
425 Quantity_Color aGroupQColor = theGroup->GetColor();
426 SALOMEDS::Color aGroupColor;
427 aGroupColor.R = aGroupQColor.Red();
428 aGroupColor.G = aGroupQColor.Green();
429 aGroupColor.B = aGroupQColor.Blue();
431 SALOMEDS::Color aColor = aColorIter->second;
432 if( CompareColors( aGroupColor, aColor ) )
434 myGroupAttributVal = aColorIter->first;
440 //=============================================================================
442 * Split <theSubMesh> on some parts (families)
443 * on the basis of the elements type.
445 //=============================================================================
446 DriverMED_FamilyPtrList
448 ::SplitByType (SMESHDS_SubMesh* theSubMesh,
451 DriverMED_FamilyPtrList aFamilies;
452 DriverMED_FamilyPtr aNodesFamily (new DriverMED_Family);
453 DriverMED_FamilyPtr anEdgesFamily (new DriverMED_Family);
454 DriverMED_FamilyPtr aFacesFamily (new DriverMED_Family);
455 DriverMED_FamilyPtr aVolumesFamily (new DriverMED_Family);
457 char submeshGrpName[ 30 ];
458 sprintf( submeshGrpName, "SubMesh %d", theId );
460 SMDS_NodeIteratorPtr aNodesIter = theSubMesh->GetNodes();
461 while (aNodesIter->more())
463 const SMDS_MeshNode* aNode = aNodesIter->next();
464 aNodesFamily->AddElement(aNode);
467 SMDS_ElemIteratorPtr anElemsIter = theSubMesh->GetElements();
468 while (anElemsIter->more())
470 const SMDS_MeshElement* anElem = anElemsIter->next();
471 switch (anElem->GetType())
474 anEdgesFamily->AddElement(anElem);
477 aFacesFamily->AddElement(anElem);
480 aVolumesFamily->AddElement(anElem);
487 if (!aNodesFamily->IsEmpty()) {
488 aNodesFamily->SetType(SMDSAbs_Node);
489 aNodesFamily->AddGroupName(submeshGrpName);
490 aFamilies.push_back(aNodesFamily);
492 if (!anEdgesFamily->IsEmpty()) {
493 anEdgesFamily->SetType(SMDSAbs_Edge);
494 anEdgesFamily->AddGroupName(submeshGrpName);
495 aFamilies.push_back(anEdgesFamily);
497 if (!aFacesFamily->IsEmpty()) {
498 aFacesFamily->SetType(SMDSAbs_Face);
499 aFacesFamily->AddGroupName(submeshGrpName);
500 aFamilies.push_back(aFacesFamily);
502 if (!aVolumesFamily->IsEmpty()) {
503 aVolumesFamily->SetType(SMDSAbs_Volume);
504 aVolumesFamily->AddGroupName(submeshGrpName);
505 aFamilies.push_back(aVolumesFamily);
511 //=============================================================================
513 * Remove from <myElements> elements, common with <by>,
514 * Remove from <by> elements, common with <myElements>,
515 * Create family <common> from common elements, with combined groups list.
517 //=============================================================================
518 void DriverMED_Family::Split (DriverMED_FamilyPtr by,
519 DriverMED_FamilyPtr common)
522 ElementsSet::iterator anIter = by->myElements.begin();
523 while ( anIter != by->myElements.end())
525 if (myElements.find(*anIter) != myElements.end())
527 common->myElements.insert(*anIter);
528 myElements.erase(*anIter);
529 by->myElements.erase(anIter++);
535 if (!common->IsEmpty())
538 common->myGroupNames = myGroupNames;
539 MED::TStringSet::iterator aGrNamesIter = by->myGroupNames.begin();
540 for (; aGrNamesIter != by->myGroupNames.end(); aGrNamesIter++)
542 common->myGroupNames.insert(*aGrNamesIter);
546 common->myType = myType;