]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_Region.cxx
Salome HOME
Merge branch 'BR_H2018_3' into BR_2018_V8_5
[modules/hydro.git] / src / HYDROData / HYDROData_Region.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROData_Region.h"
20
21 #include "HYDROData_CalculationCase.h"
22 #include "HYDROData_Document.h"
23 #include "HYDROData_Iterator.h"
24 #include "HYDROData_Object.h"
25 #include "HYDROData_ShapesTool.h"
26 #include "HYDROData_Zone.h"
27 #include "HYDROData_Tool.h"
28
29 #include <TopoDS.hxx>
30 #include <TopoDS_Shape.hxx>
31 #include <TopoDS_Shell.hxx>
32 #include <TopoDS_Face.hxx>
33
34 #include <TopExp.hxx>
35
36 #include <TopTools_ListOfShape.hxx>
37 #include <TopTools_SequenceOfShape.hxx>
38 #include <TopTools_ListIteratorOfListOfShape.hxx>
39 #include <TopTools_IndexedMapOfShape.hxx>
40
41
42 #include <BRep_Builder.hxx>
43 #include <BRepAlgoAPI_Fuse.hxx>
44
45 #include <ShapeUpgrade_UnifySameDomain.hxx>
46
47 #include <QStringList>
48 #include <QColor>
49
50 #include "Geom_Plane.hxx"
51 #include "gp_Pln.hxx"
52 #include "BRepTools_ReShape.hxx"
53
54 //#define _DEVDEBUG_
55 #include "HYDRO_trace.hxx"
56
57 //#define DEB_GET_REGION_SHAPE
58
59 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Region, HYDROData_Entity)
60
61
62 HYDROData_Region::HYDROData_Region()
63  : HYDROData_Entity( Geom_2d )
64 {
65 }
66
67 HYDROData_Region::~HYDROData_Region()
68 {
69 }
70
71 bool HYDROData_Region::CanBeUpdated() const
72 {
73   return false;
74 }
75
76 void HYDROData_Region::Remove()
77 {
78   Handle(HYDROData_CalculationCase) aFatherCalc = 
79     Handle(HYDROData_CalculationCase)::DownCast( GetFatherObject() );
80
81   HYDROData_Entity::Remove();
82
83   if ( !aFatherCalc.IsNull() )
84     aFatherCalc->UpdateRegionsOrder();
85 }
86
87 bool HYDROData_Region::CanRemove()
88 {
89   return false;
90 }
91
92 HYDROData_SequenceOfObjects HYDROData_Region::GetAllReferenceObjects() const
93 {
94   HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
95
96   HYDROData_SequenceOfObjects aSeqOfZones = GetZones();
97   aResSeq.Append( aSeqOfZones );
98
99   return aResSeq;
100 }
101
102 bool HYDROData_Region::AddZone( const Handle(HYDROData_Zone)& theZone )
103 {
104   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
105
106   if ( theZone.IsNull() )
107     return false;
108   
109   if ( HasReference( theZone, DataTag_Zone ) )
110     return false; // Object is already in reference list
111
112   // Move the zone from other region
113   Handle(HYDROData_Region) aFatherRegion = 
114     Handle(HYDROData_Region)::DownCast( theZone->GetFatherObject() );
115   if ( !aFatherRegion.IsNull() && aFatherRegion->Label() != myLab )
116   {
117     Handle(HYDROData_Zone) aNewZone = addNewZone( aDocument, "", TopoDS_Face(), QStringList() );
118     theZone->CopyTo( aNewZone, false );
119
120     // To prevent changing of stored shape
121     aNewZone->SetShape( theZone->GetShape() );
122
123     aFatherRegion->RemoveZone( theZone );
124
125     theZone->SetLabel( aNewZone->Label() );
126   }
127   else
128   {
129     AddReferenceObject( theZone, DataTag_Zone );
130   }
131
132   return true;
133 }
134
135 HYDROData_SequenceOfObjects HYDROData_Region::GetZones() const
136 {
137   return GetReferenceObjects( DataTag_Zone );
138 }
139
140 void HYDROData_Region::RemoveZone( const Handle(HYDROData_Zone)& theZone )
141 {
142   if ( theZone.IsNull() )
143     return;
144
145   RemoveReferenceObject( theZone->Label(), DataTag_Zone );
146
147   // Remove zone from data model
148   Handle(HYDROData_Region) aFatherRegion = 
149     Handle(HYDROData_Region)::DownCast( theZone->GetFatherObject() );
150   if ( !aFatherRegion.IsNull() && aFatherRegion->Label() == myLab )
151     theZone->Remove();
152
153   // If the last zone has been removed from region we remove this region
154   HYDROData_SequenceOfObjects aRefZones = GetZones();
155   if ( aRefZones.IsEmpty() )
156     Remove();
157 }
158
159 void HYDROData_Region::RemoveZones()
160 {
161   ClearReferenceObjects( DataTag_Zone );
162   myLab.FindChild( DataTag_ChildZone ).ForgetAllAttributes( true );
163 }
164
165 Handle(HYDROData_Zone) HYDROData_Region::addNewZone( const Handle(HYDROData_Document)& theDoc,
166                                                      const QString& thePrefix,
167                                                      const TopoDS_Face& theFace,
168                                                      const QStringList& theRefObjects )
169 {
170   TDF_Label aNewLab = myLab.FindChild( DataTag_ChildZone ).NewChild();
171
172   Handle(HYDROData_Zone) aNewZone =
173     Handle(HYDROData_Zone)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_ZONE ) );
174   AddZone( aNewZone );
175
176   QString aZoneName = HYDROData_Tool::GenerateObjectName( theDoc, thePrefix );
177   aNewZone->SetName( aZoneName );
178
179   aNewZone->SetShape( theFace );
180
181   // Add the reference object for zone
182   for ( int i = 0, n = theRefObjects.length(); i < n; ++i )
183   {
184     const QString& anObjName = theRefObjects.at( i );
185     Handle(HYDROData_Entity) aRefObject = theDoc->FindObjectByName( anObjName );
186     if ( aRefObject.IsNull() )
187       continue;
188
189     aNewZone->AddObject( aRefObject );
190   }
191
192   return aNewZone;
193 }
194
195 void getUsedGroups( const TopoDS_Shape&                     theShape,
196                     HYDROData_ShapesGroup::SeqOfGroupsDefs& theOriGroups,
197                     HYDROData_ShapesGroup::SeqOfGroupsDefs& theUsedGroups )
198 {
199 #ifdef DEB_GET_REGION_SHAPE
200   HYDROData_ShapesTool::DumpShapeSubShapes( std::cout, "Zone face edges:", theShape, TopAbs_EDGE );
201 #endif
202
203   TopTools_IndexedMapOfShape aMapOfSubShapes;
204   TopExp::MapShapes( theShape, TopAbs_EDGE, aMapOfSubShapes );
205
206   HYDROData_ShapesGroup::SeqOfGroupsDefs::Iterator anIter( theOriGroups );
207   for ( ; anIter.More(); anIter.Next() )
208   {
209     HYDROData_ShapesGroup::GroupDefinition& anOriGroupDef = anIter.ChangeValue();
210     if ( anOriGroupDef.Shapes.IsEmpty() )
211       continue;
212
213     for ( int i = 1; i <= anOriGroupDef.Shapes.Length(); ++i )
214     {
215       TopoDS_Shape aGroupEdge = anOriGroupDef.Shapes.Value( i );
216       
217       int aShapeIndex = aMapOfSubShapes.FindIndex( aGroupEdge );
218       if ( aShapeIndex <= 0 )
219         continue;
220
221       anOriGroupDef.Shapes.Remove( i );
222       --i;
223
224       bool anIsAdded = false;
225
226       HYDROData_ShapesGroup::SeqOfGroupsDefs::Iterator aUsedIter( theUsedGroups );
227       for ( ; aUsedIter.More(); aUsedIter.Next() )
228       {
229         HYDROData_ShapesGroup::GroupDefinition& aUsedGroupDef = aUsedIter.ChangeValue();
230         if ( aUsedGroupDef.Name != anOriGroupDef.Name )
231           continue;
232
233         aUsedGroupDef.Shapes.Append( aGroupEdge );
234         anIsAdded = true;
235         break;
236       }
237
238       if ( !anIsAdded )
239       {
240         HYDROData_ShapesGroup::GroupDefinition aUsedGroupDef;
241         aUsedGroupDef.Name = anOriGroupDef.Name;
242         aUsedGroupDef.Shapes.Append( aGroupEdge );
243         theUsedGroups.Append( aUsedGroupDef );
244       }
245     }
246   }
247 }
248
249
250 TopoDS_Shape HYDROData_Region::GetShape( HYDROData_ShapesGroup::SeqOfGroupsDefs* theSeqOfGroups, const TopTools_SequenceOfShape* IntSh ) const
251 {
252   DEBTRACE("GetShape");
253   HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroups;
254   HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfUsedGroups;
255   if ( theSeqOfGroups )
256     aSeqOfGroups = *theSeqOfGroups;
257
258 #ifdef DEB_GET_REGION_SHAPE
259   HYDROData_ShapesGroup::GroupDefinition::Dump( std::cout, aSeqOfGroups );
260 #endif
261
262   TopoDS_Shape aResShape;
263
264   // Unite the region zones (each zone is a face) into one face (united face)
265   // If the zones can't be united into the single face - unite them into shell
266
267   // Collect the list of region faces
268   TopTools_ListOfShape aRegionFacesList;
269
270   HYDROData_SequenceOfObjects aZones = GetZones();
271   HYDROData_SequenceOfObjects::Iterator aZoneIter( aZones );
272   TopTools_IndexedMapOfShape AllE;
273   TopTools_IndexedMapOfShape IE; //int edges
274
275   for ( ; aZoneIter.More(); aZoneIter.Next() )
276   {
277     Handle(HYDROData_Zone) aZone =
278       Handle(HYDROData_Zone)::DownCast( aZoneIter.Value() );
279     if ( aZone.IsNull() )
280       continue;
281
282     TopoDS_Shape aZoneShape = aZone->GetShape();
283     if ( aZoneShape.IsNull() || aZoneShape.ShapeType() != TopAbs_FACE )
284       continue;
285
286     TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
287     aRegionFacesList.Append( aZoneFace );
288     TopExp::MapShapes(aZoneFace, TopAbs_EDGE, AllE); // collect all edges
289     getUsedGroups( aZoneFace, aSeqOfGroups, aSeqOfUsedGroups );
290   } // zones iterator
291
292   DEBTRACE("--- IntSh->Length():" << IntSh->Length());
293   for (int i = 1; i <= IntSh->Length(); i++)
294   {
295     const TopoDS_Shape& CS = (*IntSh)(i);
296     if (AllE.Contains(CS))
297       IE.Add(CS);
298   }
299   
300   if ( aRegionFacesList.IsEmpty() )
301     return aResShape;
302
303   TopoDS_Face aRegionFace;
304
305   if ( aRegionFacesList.Extent() == 1 )
306   {
307     DEBTRACE("--- aRegionFacesList.Extent() == 1 ");
308     aResShape = TopoDS::Face( aRegionFacesList.First() );
309   }
310   else
311   {
312 #ifdef DEB_GET_REGION_SHAPE
313     HYDROData_ShapesGroup::GroupDefinition::Dump( std::cout, aSeqOfUsedGroups );
314 #endif
315
316     // Try to fuse all region faces into one common face
317     TopoDS_Shape aFuseShape;
318     TopTools_ListIteratorOfListOfShape aFaceIter( aRegionFacesList );
319     for ( ; aFaceIter.More(); aFaceIter.Next() )
320     {
321       if ( aFuseShape.IsNull() )
322       {
323         aFuseShape = aFaceIter.Value();
324         continue;
325       }
326
327       BRepAlgoAPI_Fuse aFuse( aFuseShape, aFaceIter.Value() );
328       if ( !aFuse.IsDone() )
329       {
330         aFuseShape.Nullify();
331         break;
332       }
333
334       aFuseShape = aFuse.Shape();
335
336       //update history of internal edges
337       TopTools_IndexedMapOfShape DIE;
338       TopTools_ListOfShape newSh1, newSh2;
339       DEBTRACE("IE.Extent():" << IE.Extent());
340       for (int i = 1; i <= IE.Extent(); i++)
341       {
342         const TopoDS_Shape& CSH = IE(i);
343         newSh1.Clear();
344         newSh2.Clear();
345         newSh1 = aFuse.Modified(CSH);
346         if (newSh1.IsEmpty())
347         {
348           newSh2 = aFuse.Generated(CSH);
349           if (newSh2.IsEmpty())
350             DIE.Add(CSH);
351           else
352             for (TopTools_ListIteratorOfListOfShape lt(newSh2); lt.More(); lt.Next())
353               if (!lt.Value().IsNull())
354                  DIE.Add(lt.Value());
355         }
356         else
357         {
358           for (TopTools_ListIteratorOfListOfShape lt(newSh1); lt.More(); lt.Next())
359             if (!lt.Value().IsNull())
360               DIE.Add(lt.Value());
361         }
362       }
363       IE = DIE;
364       //update groups
365       HYDROData_ShapesGroup::GroupDefinition::Update( &aSeqOfUsedGroups, &aFuse );
366     } // faces iterator
367
368 #ifdef DEB_GET_REGION_SHAPE
369     HYDROData_ShapesTool::DumpShapeSubShapes( std::cout, "Fused face edges:", aFuseShape, TopAbs_EDGE );
370 #endif
371
372     aFuseShape = HYDROData_Tool::RebuildCmp(aFuseShape);
373
374     ShapeUpgrade_UnifySameDomain unif( aFuseShape, Standard_False, Standard_True, Standard_False );
375     if (!IE.IsEmpty())
376     {
377       TopTools_MapOfShape IEM;
378       for (int i = 1; i <= IE.Extent(); i++)
379         IEM.Add(IE(i));
380       unif.KeepShapes(IEM);
381     }
382
383     unif.Build();
384     TopoDS_Shape anUnitedShape;
385     anUnitedShape = unif.Shape();
386
387     HYDROData_ShapesGroup::GroupDefinition::Update( &aSeqOfUsedGroups, &unif );
388
389     //cout << "inif:   fuseshape: " << aFuseShape.TShape().get() << "unifS " << anUnitedShape.TShape().get() << endl;
390 #ifdef DEB_GET_REGION_SHAPE
391     HYDROData_ShapesGroup::GroupDefinition::Dump( std::cout, aSeqOfUsedGroups );
392 #endif
393
394     TopTools_SequenceOfShape aShapeFaces;
395     HYDROData_ShapesTool::ExploreShapeToShapes( anUnitedShape, TopAbs_FACE, aShapeFaces );
396     if ( aShapeFaces.Length() == 1 ) //it should be either face or compound of faces (?)
397     {
398       const TopoDS_Face& CF = TopoDS::Face( aShapeFaces.Value( 1 ));
399       aResShape = CF;
400     }
401     else
402     {
403       TopTools_SequenceOfShape aShapeShells;
404       HYDROData_ShapesTool::ExploreShapeToShapes( anUnitedShape, TopAbs_SHELL, aShapeShells );
405       if (aShapeShells.Length() == 1)
406         aResShape = TopoDS::Shell(aShapeShells(1));
407       else
408         aResShape = anUnitedShape;
409     }
410
411     // Update the sequence of groups
412     if ( theSeqOfGroups )
413     {
414       HYDROData_ShapesGroup::SeqOfGroupsDefs::Iterator aUsedIter( aSeqOfUsedGroups );
415       for ( ; aUsedIter.More(); aUsedIter.Next() )
416       {
417         const HYDROData_ShapesGroup::GroupDefinition& aUsedGroupDef = aUsedIter.Value();
418         if ( aUsedGroupDef.Shapes.IsEmpty() )
419           continue;
420
421         HYDROData_ShapesGroup::SeqOfGroupsDefs::Iterator anOriIter( aSeqOfGroups );
422         for ( ; anOriIter.More(); anOriIter.Next() )
423         {
424           HYDROData_ShapesGroup::GroupDefinition& anOriGroupDef = anOriIter.ChangeValue();
425           if ( anOriGroupDef.Name != aUsedGroupDef.Name )
426             continue;
427
428           HYDROData_ShapesTool::AddShapes( anOriGroupDef.Shapes, aUsedGroupDef.Shapes );
429           break;
430         }
431       }
432
433       *theSeqOfGroups = aSeqOfGroups;
434     }
435   }
436                                                      
437   return aResShape;
438 }
439
440 QStringList HYDROData_Region::DumpToPython( const QString&       thePyScriptPath,
441                                             MapOfTreatedObjects& theTreatedObjects,
442                                             QString              defRegName ) const
443 {
444   QStringList aResList;
445
446   // Find region
447   findPythonReferenceObject( aResList, defRegName );
448
449   // Add zones
450   HYDROData_SequenceOfObjects aZones = GetZones();
451   HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
452   for ( ; aZonesIter.More(); aZonesIter.Next() ) {
453     Handle(HYDROData_Zone) aZone =
454       Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
455     if ( aZone.IsNull() ) {
456       continue;
457     }
458     
459     // find zone
460     aZone->findPythonReferenceObject( aResList );
461     theTreatedObjects.insert( aZone->GetName(), aZone );
462     
463     // set zone merge type
464     QString aMergeTypeStr;
465     HYDROData_Zone::MergeType aMergeType = aZone->GetMergeType();
466     if ( aMergeType == HYDROData_Zone::Merge_ZMIN ) {
467       aMergeTypeStr = "HYDROData_Zone.Merge_ZMIN";
468     } else if ( aMergeType == HYDROData_Zone::Merge_ZMAX ) {
469       aMergeTypeStr = "HYDROData_Zone.Merge_ZMAX";
470     } else if ( aMergeType == HYDROData_Zone::Merge_Object ) {
471       aMergeTypeStr = "HYDROData_Zone.Merge_Object";
472     }
473
474     if ( !aMergeTypeStr.isEmpty() ) {
475       aResList << QString( "%1.SetMergeType( %2 )" ).arg( aZone->GetObjPyName() ).arg( aMergeTypeStr );
476     }
477     if ( aMergeType == HYDROData_Zone::Merge_Object ) {
478       Handle(HYDROData_Entity) aMergeObject = aZone->GetMergeObject();
479       if ( !aMergeObject.IsNull() ) {
480         aMergeObject->findPythonReferenceObject( aResList );
481         aResList << QString( "%1.SetMergeObject( %2 )" ).arg( aZone->GetObjPyName() )
482                                                         .arg( aMergeObject->GetObjPyName() );
483       }
484     }
485     // set color
486     QColor zoneColor = aZone->GetColor(Qt::darkBlue);
487     aResList << QString( "%1.SetColor( QColor( %2, %3, %4 ))" )
488                  .arg( aZone->GetObjPyName() ).arg( zoneColor.red() ).arg( zoneColor.green() ).arg( zoneColor.blue() );
489     // add zone
490     setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aZone, "AddZone" );
491
492   }
493
494   return aResList;
495 }
496
497 bool HYDROData_Region::IsSubmersible() const
498 {
499   HYDROData_SequenceOfObjects aZones = GetZones();
500   HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
501   for ( ; aZonesIter.More(); aZonesIter.Next() )
502   {
503     Handle(HYDROData_Zone) aZone =
504       Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
505     if ( !aZone->IsSubmersible() )
506       return false; //if one of zones is not submersible the region is considered as not submersible
507   }
508   return true;
509 }