1 // Copyright (C) 2007-2010 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)
155 DriverMED_FamilyPtrList aFamilies;
157 string anAllNodesGroupName = "Group_Of_All_Nodes";
158 string anAllEdgesGroupName = "Group_Of_All_Edges";
159 string anAllFacesGroupName = "Group_Of_All_Faces";
160 string anAllVolumesGroupName = "Group_Of_All_Volumes";
162 // Reserve four ids for families of free elements
163 // (1 - nodes, -1 - edges, -2 - faces, -3 - volumes).
164 // 'Free' means here not belonging to any group.
165 int aNodeFamId = FIRST_NODE_FAMILY;
166 int aElemFamId = FIRST_ELEM_FAMILY;
168 // Process sub-meshes
169 SMESHDS_SubMeshPtrMap::const_iterator aSMIter = theSubMeshes.begin();
170 for (; aSMIter != theSubMeshes.end(); aSMIter++)
172 const int anId = aSMIter->first;
173 SMESHDS_SubMesh* aSubMesh = aSMIter->second;
174 if ( aSubMesh->IsComplexSubmesh() )
175 continue; // submesh containing other submeshs
176 DriverMED_FamilyPtrList aSMFams = SplitByType(aSubMesh,anId);
177 DriverMED_FamilyPtrList::iterator aSMFamsIter = aSMFams.begin();
178 for (; aSMFamsIter != aSMFams.end(); aSMFamsIter++)
180 DriverMED_FamilyPtr aFam2 = (*aSMFamsIter);
181 DriverMED_FamilyPtrList::iterator aFamsIter = aFamilies.begin();
182 while (aFamsIter != aFamilies.end())
184 DriverMED_FamilyPtr aFam1 = *aFamsIter;
185 DriverMED_FamilyPtrList::iterator aCurrIter = aFamsIter++;
186 if (aFam1->myType == aFam2->myType)
188 DriverMED_FamilyPtr aCommon (new DriverMED_Family);
189 aFam1->Split(aFam2, aCommon);
190 if (!aCommon->IsEmpty())
192 aFamilies.push_back(aCommon);
194 if (aFam1->IsEmpty())
196 aFamilies.erase(aCurrIter);
198 if (aFam2->IsEmpty())
202 // The rest elements of family
203 if (!aFam2->IsEmpty())
205 aFamilies.push_back(aFam2);
211 SMESHDS_GroupBasePtrList::const_iterator aGroupsIter = theGroups.begin();
212 for (; aGroupsIter != theGroups.end(); aGroupsIter++)
214 DriverMED_FamilyPtr aFam2 (new DriverMED_Family);
215 aFam2->Init(*aGroupsIter);
217 DriverMED_FamilyPtrList::iterator aFamsIter = aFamilies.begin();
218 while (aFamsIter != aFamilies.end())
220 DriverMED_FamilyPtr aFam1 = *aFamsIter;
221 DriverMED_FamilyPtrList::iterator aCurrIter = aFamsIter++;
222 if (aFam1->myType == aFam2->myType)
224 DriverMED_FamilyPtr aCommon (new DriverMED_Family);
225 aFam1->Split(aFam2, aCommon);
226 if (!aCommon->IsEmpty())
228 aCommon->SetGroupAttributVal(0);
229 aFamilies.push_back(aCommon);
231 if (aFam1->IsEmpty())
233 aFamilies.erase(aCurrIter);
235 if (aFam2->IsEmpty())
239 // The rest elements of group
240 if (!aFam2->IsEmpty())
242 aFamilies.push_back(aFam2);
246 DriverMED_FamilyPtrList::iterator aFamsIter = aFamilies.begin();
247 for (; aFamsIter != aFamilies.end(); aFamsIter++)
249 DriverMED_FamilyPtr aFam = *aFamsIter;
250 if (aFam->myType == SMDSAbs_Node) {
251 aFam->SetId(aNodeFamId++);
252 if (doGroupOfNodes) aFam->myGroupNames.insert(anAllNodesGroupName);
255 aFam->SetId(aElemFamId--);
256 if (aFam->myType == SMDSAbs_Edge) {
257 if (doGroupOfEdges) aFam->myGroupNames.insert(anAllEdgesGroupName);
259 else if (aFam->myType == SMDSAbs_Face) {
260 if (doGroupOfFaces) aFam->myGroupNames.insert(anAllFacesGroupName);
262 else if (aFam->myType == SMDSAbs_Volume) {
263 if (doGroupOfVolumes) aFam->myGroupNames.insert(anAllVolumesGroupName);
268 // Create families for elements, not belonging to any group
271 DriverMED_FamilyPtr aFreeNodesFam (new DriverMED_Family);
272 aFreeNodesFam->SetId(REST_NODES_FAMILY);
273 aFreeNodesFam->myType = SMDSAbs_Node;
274 aFreeNodesFam->myGroupNames.insert(anAllNodesGroupName);
275 aFamilies.push_back(aFreeNodesFam);
280 DriverMED_FamilyPtr aFreeEdgesFam (new DriverMED_Family);
281 aFreeEdgesFam->SetId(REST_EDGES_FAMILY);
282 aFreeEdgesFam->myType = SMDSAbs_Edge;
283 aFreeEdgesFam->myGroupNames.insert(anAllEdgesGroupName);
284 aFamilies.push_back(aFreeEdgesFam);
289 DriverMED_FamilyPtr aFreeFacesFam (new DriverMED_Family);
290 aFreeFacesFam->SetId(REST_FACES_FAMILY);
291 aFreeFacesFam->myType = SMDSAbs_Face;
292 aFreeFacesFam->myGroupNames.insert(anAllFacesGroupName);
293 aFamilies.push_back(aFreeFacesFam);
296 if (doGroupOfVolumes)
298 DriverMED_FamilyPtr aFreeVolumesFam (new DriverMED_Family);
299 aFreeVolumesFam->SetId(REST_VOLUMES_FAMILY);
300 aFreeVolumesFam->myType = SMDSAbs_Volume;
301 aFreeVolumesFam->myGroupNames.insert(anAllVolumesGroupName);
302 aFamilies.push_back(aFreeVolumesFam);
305 DriverMED_FamilyPtr aNullFam (new DriverMED_Family);
307 aNullFam->myType = SMDSAbs_All;
308 aFamilies.push_back(aNullFam);
313 //=============================================================================
315 * Create TFamilyInfo for this family
317 //=============================================================================
319 DriverMED_Family::GetFamilyInfo(const MED::PWrapper& theWrapper,
320 const MED::PMeshInfo& theMeshInfo) const
323 aStr << "FAM_" << myId;
324 set<string>::const_iterator aGrIter = myGroupNames.begin();
325 for(; aGrIter != myGroupNames.end(); aGrIter++){
326 aStr << "_" << *aGrIter;
328 string aValue = aStr.str();
329 // PAL19785,0019867 - med forbids whitespace to be the last char in the name
331 if ( theWrapper->GetVersion() == MED::eV2_1 )
332 maxSize = MED::GetNOMLength<MED::eV2_1>();
334 maxSize = MED::GetNOMLength<MED::eV2_2>();
335 int lastCharPos = min( maxSize, (int) aValue.size() ) - 1;
336 while ( isspace( aValue[ lastCharPos ] ))
337 aValue.resize( lastCharPos-- );
339 MED::PFamilyInfo anInfo;
340 if(myId == 0 || myGroupAttributVal == 0){
341 anInfo = theWrapper->CrFamilyInfo(theMeshInfo,
346 MED::TStringVector anAttrDescs (1, ""); // 1 attribute with empty description,
347 MED::TIntVector anAttrIds (1, myId); // Id=0,
348 MED::TIntVector anAttrVals (1, myGroupAttributVal);
349 anInfo = theWrapper->CrFamilyInfo(theMeshInfo,
359 // cout << "Groups: ";
360 // set<string>::iterator aGrIter = myGroupNames.begin();
361 // for (; aGrIter != myGroupNames.end(); aGrIter++)
363 // cout << " " << *aGrIter;
367 // cout << "Elements: ";
368 // set<const SMDS_MeshElement *>::iterator anIter = myElements.begin();
369 // for (; anIter != myElements.end(); anIter++)
371 // cout << " " << (*anIter)->GetID();
378 //=============================================================================
380 * Initialize the tool by SMESHDS_GroupBase
382 //=============================================================================
383 void DriverMED_Family::Init (SMESHDS_GroupBase* theGroup)
387 SMDS_ElemIteratorPtr elemIt = theGroup->GetElements();
388 while (elemIt->more())
390 myElements.insert(elemIt->next());
394 myType = theGroup->GetType();
397 myGroupNames.clear();
398 myGroupNames.insert(string(theGroup->GetStoreName()));
400 Quantity_Color aColor = theGroup->GetColor();
401 double aRed = aColor.Red();
402 double aGreen = aColor.Green();
403 double aBlue = aColor.Blue();
404 int aR = int( aRed*255 );
405 int aG = int( aGreen*255 );
406 int aB = int( aBlue*255 );
407 // cout << "aRed = " << aR << endl;
408 // cout << "aGreen = " << aG << endl;
409 // cout << "aBlue = " << aB << endl;
410 myGroupAttributVal = (int)(aR*1000000 + aG*1000 + aB);
411 //cout << "myGroupAttributVal = " << myGroupAttributVal << endl;
414 //=============================================================================
416 * Split <theSubMesh> on some parts (families)
417 * on the basis of the elements type.
419 //=============================================================================
420 DriverMED_FamilyPtrList
422 ::SplitByType (SMESHDS_SubMesh* theSubMesh,
425 DriverMED_FamilyPtrList aFamilies;
426 DriverMED_FamilyPtr aNodesFamily (new DriverMED_Family);
427 DriverMED_FamilyPtr anEdgesFamily (new DriverMED_Family);
428 DriverMED_FamilyPtr aFacesFamily (new DriverMED_Family);
429 DriverMED_FamilyPtr aVolumesFamily (new DriverMED_Family);
431 char submeshGrpName[ 30 ];
432 sprintf( submeshGrpName, "SubMesh %d", theId );
434 SMDS_NodeIteratorPtr aNodesIter = theSubMesh->GetNodes();
435 while (aNodesIter->more())
437 const SMDS_MeshNode* aNode = aNodesIter->next();
438 aNodesFamily->AddElement(aNode);
441 SMDS_ElemIteratorPtr anElemsIter = theSubMesh->GetElements();
442 while (anElemsIter->more())
444 const SMDS_MeshElement* anElem = anElemsIter->next();
445 switch (anElem->GetType())
448 anEdgesFamily->AddElement(anElem);
451 aFacesFamily->AddElement(anElem);
454 aVolumesFamily->AddElement(anElem);
461 if (!aNodesFamily->IsEmpty()) {
462 aNodesFamily->SetType(SMDSAbs_Node);
463 aNodesFamily->AddGroupName(submeshGrpName);
464 aFamilies.push_back(aNodesFamily);
466 if (!anEdgesFamily->IsEmpty()) {
467 anEdgesFamily->SetType(SMDSAbs_Edge);
468 anEdgesFamily->AddGroupName(submeshGrpName);
469 aFamilies.push_back(anEdgesFamily);
471 if (!aFacesFamily->IsEmpty()) {
472 aFacesFamily->SetType(SMDSAbs_Face);
473 aFacesFamily->AddGroupName(submeshGrpName);
474 aFamilies.push_back(aFacesFamily);
476 if (!aVolumesFamily->IsEmpty()) {
477 aVolumesFamily->SetType(SMDSAbs_Volume);
478 aVolumesFamily->AddGroupName(submeshGrpName);
479 aFamilies.push_back(aVolumesFamily);
485 //=============================================================================
487 * Remove from <myElements> elements, common with <by>,
488 * Remove from <by> elements, common with <myElements>,
489 * Create family <common> from common elements, with combined groups list.
491 //=============================================================================
492 void DriverMED_Family::Split (DriverMED_FamilyPtr by,
493 DriverMED_FamilyPtr common)
496 ElementsSet::iterator anIter = by->myElements.begin();
497 while ( anIter != by->myElements.end())
499 if (myElements.find(*anIter) != myElements.end())
501 common->myElements.insert(*anIter);
502 myElements.erase(*anIter);
503 by->myElements.erase(anIter++);
509 if (!common->IsEmpty())
512 common->myGroupNames = myGroupNames;
513 MED::TStringSet::iterator aGrNamesIter = by->myGroupNames.begin();
514 for (; aGrNamesIter != by->myGroupNames.end(); aGrNamesIter++)
516 common->myGroupNames.insert(*aGrNamesIter);
520 common->myType = myType;