1 // Copyright (C) 2007-2008 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
22 // SMESH DriverMED : tool to split groups on families
23 // File : DriverMED_Family.cxx
24 // Author : Julia DOROVSKIKH
27 #include "DriverMED_Family.h"
28 #include "MED_Factory.hxx"
34 //=============================================================================
38 //=============================================================================
45 //=============================================================================
48 ::GetElements () const
62 ::SetId (const int theId)
69 ::AddElement(const SMDS_MeshElement* theElement)
71 myElements.insert(theElement);
76 ::AddGroupName(std::string theGroupName)
78 myGroupNames.insert(theGroupName);
83 ::SetType(const SMDSAbs_ElementType theType)
85 myTypes.insert( myType = theType );
95 const std::set< SMDSAbs_ElementType >&
104 ::MemberOf(std::string theGroupName) const
106 return myGroupNames.find(theGroupName) != myGroupNames.end();
109 const MED::TStringSet&
111 ::GetGroupNames () const
119 ::GetGroupAttributVal() const
121 return myGroupAttributVal;
126 ::SetGroupAttributVal( int theValue)
128 myGroupAttributVal = theValue;
135 return myElements.empty();
138 //=============================================================================
140 * Split each group from list <aGroups> on some parts (families)
141 * on the basis of the elements membership in other groups from this list.
142 * Resulting families have no common elements.
144 //=============================================================================
145 DriverMED_FamilyPtrList
147 ::MakeFamilies(const SMESHDS_SubMeshPtrMap& theSubMeshes,
148 const SMESHDS_GroupBasePtrList& theGroups,
149 const bool doGroupOfNodes,
150 const bool doGroupOfEdges,
151 const bool doGroupOfFaces,
152 const bool doGroupOfVolumes)
154 DriverMED_FamilyPtrList aFamilies;
156 string anAllNodesGroupName = "Group_Of_All_Nodes";
157 string anAllEdgesGroupName = "Group_Of_All_Edges";
158 string anAllFacesGroupName = "Group_Of_All_Faces";
159 string anAllVolumesGroupName = "Group_Of_All_Volumes";
161 // Reserve four ids for families of free elements
162 // (1 - nodes, -1 - edges, -2 - faces, -3 - volumes).
163 // 'Free' means here not belonging to any group.
164 int aNodeFamId = FIRST_NODE_FAMILY;
165 int aElemFamId = FIRST_ELEM_FAMILY;
167 // Process sub-meshes
168 SMESHDS_SubMeshPtrMap::const_iterator aSMIter = theSubMeshes.begin();
169 for (; aSMIter != theSubMeshes.end(); aSMIter++)
171 const int anId = aSMIter->first;
172 SMESHDS_SubMesh* aSubMesh = aSMIter->second;
173 if ( aSubMesh->IsComplexSubmesh() )
174 continue; // submesh containing other submeshs
175 DriverMED_FamilyPtrList aSMFams = SplitByType(aSubMesh,anId);
176 DriverMED_FamilyPtrList::iterator aSMFamsIter = aSMFams.begin();
177 for (; aSMFamsIter != aSMFams.end(); aSMFamsIter++)
179 DriverMED_FamilyPtr aFam2 = (*aSMFamsIter);
180 DriverMED_FamilyPtrList::iterator aFamsIter = aFamilies.begin();
181 while (aFamsIter != aFamilies.end())
183 DriverMED_FamilyPtr aFam1 = *aFamsIter;
184 DriverMED_FamilyPtrList::iterator aCurrIter = aFamsIter++;
185 if (aFam1->myType == aFam2->myType)
187 DriverMED_FamilyPtr aCommon (new DriverMED_Family);
188 aFam1->Split(aFam2, aCommon);
189 if (!aCommon->IsEmpty())
191 aFamilies.push_back(aCommon);
193 if (aFam1->IsEmpty())
195 aFamilies.erase(aCurrIter);
197 if (aFam2->IsEmpty())
201 // The rest elements of family
202 if (!aFam2->IsEmpty())
204 aFamilies.push_back(aFam2);
210 SMESHDS_GroupBasePtrList::const_iterator aGroupsIter = theGroups.begin();
211 for (; aGroupsIter != theGroups.end(); aGroupsIter++)
213 DriverMED_FamilyPtr aFam2 (new DriverMED_Family);
214 aFam2->Init(*aGroupsIter);
216 DriverMED_FamilyPtrList::iterator aFamsIter = aFamilies.begin();
217 while (aFamsIter != aFamilies.end())
219 DriverMED_FamilyPtr aFam1 = *aFamsIter;
220 DriverMED_FamilyPtrList::iterator aCurrIter = aFamsIter++;
221 if (aFam1->myType == aFam2->myType)
223 DriverMED_FamilyPtr aCommon (new DriverMED_Family);
224 aFam1->Split(aFam2, aCommon);
225 if (!aCommon->IsEmpty())
227 aCommon->SetGroupAttributVal(0);
228 aFamilies.push_back(aCommon);
230 if (aFam1->IsEmpty())
232 aFamilies.erase(aCurrIter);
234 if (aFam2->IsEmpty())
238 // The rest elements of group
239 if (!aFam2->IsEmpty())
241 aFamilies.push_back(aFam2);
245 DriverMED_FamilyPtrList::iterator aFamsIter = aFamilies.begin();
246 for (; aFamsIter != aFamilies.end(); aFamsIter++)
248 DriverMED_FamilyPtr aFam = *aFamsIter;
249 if (aFam->myType == SMDSAbs_Node) {
250 aFam->SetId(aNodeFamId++);
251 if (doGroupOfNodes) aFam->myGroupNames.insert(anAllNodesGroupName);
254 aFam->SetId(aElemFamId--);
255 if (aFam->myType == SMDSAbs_Edge) {
256 if (doGroupOfEdges) aFam->myGroupNames.insert(anAllEdgesGroupName);
258 else if (aFam->myType == SMDSAbs_Face) {
259 if (doGroupOfFaces) aFam->myGroupNames.insert(anAllFacesGroupName);
261 else if (aFam->myType == SMDSAbs_Volume) {
262 if (doGroupOfVolumes) aFam->myGroupNames.insert(anAllVolumesGroupName);
267 // Create families for elements, not belonging to any group
270 DriverMED_FamilyPtr aFreeNodesFam (new DriverMED_Family);
271 aFreeNodesFam->SetId(REST_NODES_FAMILY);
272 aFreeNodesFam->myType = SMDSAbs_Node;
273 aFreeNodesFam->myGroupNames.insert(anAllNodesGroupName);
274 aFamilies.push_back(aFreeNodesFam);
279 DriverMED_FamilyPtr aFreeEdgesFam (new DriverMED_Family);
280 aFreeEdgesFam->SetId(REST_EDGES_FAMILY);
281 aFreeEdgesFam->myType = SMDSAbs_Edge;
282 aFreeEdgesFam->myGroupNames.insert(anAllEdgesGroupName);
283 aFamilies.push_back(aFreeEdgesFam);
288 DriverMED_FamilyPtr aFreeFacesFam (new DriverMED_Family);
289 aFreeFacesFam->SetId(REST_FACES_FAMILY);
290 aFreeFacesFam->myType = SMDSAbs_Face;
291 aFreeFacesFam->myGroupNames.insert(anAllFacesGroupName);
292 aFamilies.push_back(aFreeFacesFam);
295 if (doGroupOfVolumes)
297 DriverMED_FamilyPtr aFreeVolumesFam (new DriverMED_Family);
298 aFreeVolumesFam->SetId(REST_VOLUMES_FAMILY);
299 aFreeVolumesFam->myType = SMDSAbs_Volume;
300 aFreeVolumesFam->myGroupNames.insert(anAllVolumesGroupName);
301 aFamilies.push_back(aFreeVolumesFam);
304 DriverMED_FamilyPtr aNullFam (new DriverMED_Family);
306 aNullFam->myType = SMDSAbs_All;
307 aFamilies.push_back(aNullFam);
312 //=============================================================================
314 * Create TFamilyInfo for this family
316 //=============================================================================
318 DriverMED_Family::GetFamilyInfo(const MED::PWrapper& theWrapper,
319 const MED::PMeshInfo& theMeshInfo) const
322 aStr << "FAM_" << myId;
323 set<string>::const_iterator aGrIter = myGroupNames.begin();
324 for(; aGrIter != myGroupNames.end(); aGrIter++){
325 aStr << "_" << *aGrIter;
327 string aValue = aStr.str();
328 // PAL19785,0019867 - med forbids whitespace to be the last char in the name
330 if ( theWrapper->GetVersion() == MED::eV2_1 )
331 maxSize = MED::GetNOMLength<MED::eV2_1>();
333 maxSize = MED::GetNOMLength<MED::eV2_2>();
334 int lastCharPos = min( maxSize, (int) aValue.size() ) - 1;
335 while ( isspace( aValue[ lastCharPos ] ))
336 aValue.resize( lastCharPos-- );
338 MED::PFamilyInfo anInfo;
339 if(myId == 0 || myGroupAttributVal == 0){
340 anInfo = theWrapper->CrFamilyInfo(theMeshInfo,
345 MED::TStringVector anAttrDescs (1, ""); // 1 attribute with empty description,
346 MED::TIntVector anAttrIds (1, myId); // Id=0,
347 MED::TIntVector anAttrVals (1, myGroupAttributVal);
348 anInfo = theWrapper->CrFamilyInfo(theMeshInfo,
358 // cout << "Groups: ";
359 // set<string>::iterator aGrIter = myGroupNames.begin();
360 // for (; aGrIter != myGroupNames.end(); aGrIter++)
362 // cout << " " << *aGrIter;
366 // cout << "Elements: ";
367 // set<const SMDS_MeshElement *>::iterator anIter = myElements.begin();
368 // for (; anIter != myElements.end(); anIter++)
370 // cout << " " << (*anIter)->GetID();
377 //=============================================================================
379 * Initialize the tool by SMESHDS_GroupBase
381 //=============================================================================
382 void DriverMED_Family::Init (SMESHDS_GroupBase* theGroup)
386 SMDS_ElemIteratorPtr elemIt = theGroup->GetElements();
387 while (elemIt->more())
389 myElements.insert(elemIt->next());
393 myType = theGroup->GetType();
396 myGroupNames.clear();
397 myGroupNames.insert(string(theGroup->GetStoreName()));
399 Quantity_Color aColor = theGroup->GetColor();
400 double aRed = aColor.Red();
401 double aGreen = aColor.Green();
402 double aBlue = aColor.Blue();
403 int aR = int( aRed*255 );
404 int aG = int( aGreen*255 );
405 int aB = int( aBlue*255 );
406 // cout << "aRed = " << aR << endl;
407 // cout << "aGreen = " << aG << endl;
408 // cout << "aBlue = " << aB << endl;
409 myGroupAttributVal = (int)(aR*1000000 + aG*1000 + aB);
410 //cout << "myGroupAttributVal = " << myGroupAttributVal << endl;
413 //=============================================================================
415 * Split <theSubMesh> on some parts (families)
416 * on the basis of the elements type.
418 //=============================================================================
419 DriverMED_FamilyPtrList
421 ::SplitByType (SMESHDS_SubMesh* theSubMesh,
424 DriverMED_FamilyPtrList aFamilies;
425 DriverMED_FamilyPtr aNodesFamily (new DriverMED_Family);
426 DriverMED_FamilyPtr anEdgesFamily (new DriverMED_Family);
427 DriverMED_FamilyPtr aFacesFamily (new DriverMED_Family);
428 DriverMED_FamilyPtr aVolumesFamily (new DriverMED_Family);
430 char submeshGrpName[ 30 ];
431 sprintf( submeshGrpName, "SubMesh %d", theId );
433 SMDS_NodeIteratorPtr aNodesIter = theSubMesh->GetNodes();
434 while (aNodesIter->more())
436 const SMDS_MeshNode* aNode = aNodesIter->next();
437 aNodesFamily->AddElement(aNode);
440 SMDS_ElemIteratorPtr anElemsIter = theSubMesh->GetElements();
441 while (anElemsIter->more())
443 const SMDS_MeshElement* anElem = anElemsIter->next();
444 switch (anElem->GetType())
447 anEdgesFamily->AddElement(anElem);
450 aFacesFamily->AddElement(anElem);
453 aVolumesFamily->AddElement(anElem);
460 if (!aNodesFamily->IsEmpty()) {
461 aNodesFamily->SetType(SMDSAbs_Node);
462 aNodesFamily->AddGroupName(submeshGrpName);
463 aFamilies.push_back(aNodesFamily);
465 if (!anEdgesFamily->IsEmpty()) {
466 anEdgesFamily->SetType(SMDSAbs_Edge);
467 anEdgesFamily->AddGroupName(submeshGrpName);
468 aFamilies.push_back(anEdgesFamily);
470 if (!aFacesFamily->IsEmpty()) {
471 aFacesFamily->SetType(SMDSAbs_Face);
472 aFacesFamily->AddGroupName(submeshGrpName);
473 aFamilies.push_back(aFacesFamily);
475 if (!aVolumesFamily->IsEmpty()) {
476 aVolumesFamily->SetType(SMDSAbs_Volume);
477 aVolumesFamily->AddGroupName(submeshGrpName);
478 aFamilies.push_back(aVolumesFamily);
484 //=============================================================================
486 * Remove from <myElements> elements, common with <by>,
487 * Remove from <by> elements, common with <myElements>,
488 * Create family <common> from common elements, with combined groups list.
490 //=============================================================================
491 void DriverMED_Family::Split (DriverMED_FamilyPtr by,
492 DriverMED_FamilyPtr common)
495 ElementsSet::iterator anIter = by->myElements.begin();
496 while ( anIter != by->myElements.end())
498 if (myElements.find(*anIter) != myElements.end())
500 common->myElements.insert(*anIter);
501 myElements.erase(*anIter);
502 by->myElements.erase(anIter++);
508 if (!common->IsEmpty())
511 common->myGroupNames = myGroupNames;
512 MED::TStringSet::iterator aGrNamesIter = by->myGroupNames.begin();
513 for (; aGrNamesIter != by->myGroupNames.end(); aGrNamesIter++)
515 common->myGroupNames.insert(*aGrNamesIter);
519 common->myType = myType;