Salome HOME
mask traces
[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();
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,  bool removeRegion )
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 (removeRegion)
154   {
155     // If the last zone has been removed from region we remove this region
156     HYDROData_SequenceOfObjects aRefZones = GetZones();
157     if ( aRefZones.IsEmpty() )
158       Remove();
159   }
160 }
161
162 void HYDROData_Region::RemoveZones()
163 {
164   ClearReferenceObjects( DataTag_Zone );
165   myLab.FindChild( DataTag_ChildZone ).ForgetAllAttributes( true );
166 }
167
168 Handle(HYDROData_Zone) HYDROData_Region::addNewZone( const Handle(HYDROData_Document)& theDoc,
169                                                      const QString& thePrefix,
170                                                      const TopoDS_Face& theFace,
171                                                      const QStringList& theRefObjects )
172 {
173   TDF_Label aNewLab = myLab.FindChild( DataTag_ChildZone ).NewChild();
174
175   Handle(HYDROData_Zone) aNewZone =
176     Handle(HYDROData_Zone)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_ZONE ) );
177   AddZone( aNewZone );
178
179   QString aZoneName = HYDROData_Tool::GenerateObjectName( theDoc, thePrefix );
180   aNewZone->SetName( aZoneName );
181
182   aNewZone->SetShape( theFace );
183
184   // Add the reference object for zone
185   for ( int i = 0, n = theRefObjects.length(); i < n; ++i )
186   {
187     const QString& anObjName = theRefObjects.at( i );
188     Handle(HYDROData_Entity) aRefObject = theDoc->FindObjectByName( anObjName );
189     if ( aRefObject.IsNull() )
190       continue;
191     DEBTRACE("aRefObject name: " << aRefObject->GetName());
192     aNewZone->AddObject( aRefObject );
193   }
194
195   return aNewZone;
196 }
197
198 void getUsedGroups( const TopoDS_Shape&                     theShape,
199                     HYDROData_ShapesGroup::SeqOfGroupsDefs& theOriGroups,
200                     HYDROData_ShapesGroup::SeqOfGroupsDefs& theUsedGroups )
201 {
202 #ifdef DEB_GET_REGION_SHAPE
203   HYDROData_ShapesTool::DumpShapeSubShapes( std::cout, "Zone face edges:", theShape, TopAbs_EDGE );
204 #endif
205
206   TopTools_IndexedMapOfShape aMapOfSubShapes;
207   TopExp::MapShapes( theShape, TopAbs_EDGE, aMapOfSubShapes );
208
209   HYDROData_ShapesGroup::SeqOfGroupsDefs::Iterator anIter( theOriGroups );
210   for ( ; anIter.More(); anIter.Next() )
211   {
212     HYDROData_ShapesGroup::GroupDefinition& anOriGroupDef = anIter.ChangeValue();
213     if ( anOriGroupDef.Shapes.IsEmpty() )
214       continue;
215
216     for ( int i = 1; i <= anOriGroupDef.Shapes.Length(); ++i )
217     {
218       TopoDS_Shape aGroupEdge = anOriGroupDef.Shapes.Value( i );
219       
220       int aShapeIndex = aMapOfSubShapes.FindIndex( aGroupEdge );
221       if ( aShapeIndex <= 0 )
222         continue;
223
224       anOriGroupDef.Shapes.Remove( i );
225       --i;
226
227       bool anIsAdded = false;
228
229       HYDROData_ShapesGroup::SeqOfGroupsDefs::Iterator aUsedIter( theUsedGroups );
230       for ( ; aUsedIter.More(); aUsedIter.Next() )
231       {
232         HYDROData_ShapesGroup::GroupDefinition& aUsedGroupDef = aUsedIter.ChangeValue();
233         if ( aUsedGroupDef.Name != anOriGroupDef.Name )
234           continue;
235
236         aUsedGroupDef.Shapes.Append( aGroupEdge );
237         anIsAdded = true;
238         break;
239       }
240
241       if ( !anIsAdded )
242       {
243         HYDROData_ShapesGroup::GroupDefinition aUsedGroupDef;
244         aUsedGroupDef.Name = anOriGroupDef.Name;
245         aUsedGroupDef.Shapes.Append( aGroupEdge );
246         theUsedGroups.Append( aUsedGroupDef );
247       }
248     }
249   }
250 }
251
252
253 TopoDS_Shape HYDROData_Region::GetShape( HYDROData_ShapesGroup::SeqOfGroupsDefs* theSeqOfGroups, const TopTools_SequenceOfShape* IntSh ) const
254 {
255   DEBTRACE("GetShape");
256   HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroups;
257   HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfUsedGroups;
258   if ( theSeqOfGroups )
259     aSeqOfGroups = *theSeqOfGroups;
260
261 #ifdef DEB_GET_REGION_SHAPE
262   HYDROData_ShapesGroup::GroupDefinition::Dump( std::cout, aSeqOfGroups );
263 #endif
264
265   TopoDS_Shape aResShape;
266
267   // Unite the region zones (each zone is a face) into one face (united face)
268   // If the zones can't be united into the single face - unite them into shell
269
270   // Collect the list of region faces
271   TopTools_ListOfShape aRegionFacesList;
272
273   HYDROData_SequenceOfObjects aZones = GetZones();
274   HYDROData_SequenceOfObjects::Iterator aZoneIter( aZones );
275   TopTools_IndexedMapOfShape AllE;
276   TopTools_IndexedMapOfShape IE; //int edges
277
278   for ( ; aZoneIter.More(); aZoneIter.Next() )
279   {
280     Handle(HYDROData_Zone) aZone =
281       Handle(HYDROData_Zone)::DownCast( aZoneIter.Value() );
282     if ( aZone.IsNull() )
283       continue;
284
285     TopoDS_Shape aZoneShape = aZone->GetShape();
286     if ( aZoneShape.IsNull() || aZoneShape.ShapeType() != TopAbs_FACE )
287       continue;
288
289     TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
290     aRegionFacesList.Append( aZoneFace );
291     TopExp::MapShapes(aZoneFace, TopAbs_EDGE, AllE); // collect all edges
292     getUsedGroups( aZoneFace, aSeqOfGroups, aSeqOfUsedGroups );
293   } // zones iterator
294
295   DEBTRACE("--- IntSh->Length():" << IntSh->Length());
296   if (IntSh)
297   {
298     for (int i = 1; i <= IntSh->Length(); i++)
299     {
300       const TopoDS_Shape& CS = (*IntSh)(i);
301       if (AllE.Contains(CS))
302         IE.Add(CS);
303     }
304   }
305   
306   if ( aRegionFacesList.IsEmpty() )
307     return aResShape;
308
309   TopoDS_Face aRegionFace;
310
311   if ( aRegionFacesList.Extent() == 1 )
312   {
313     DEBTRACE("--- aRegionFacesList.Extent() == 1 ");
314     aResShape = TopoDS::Face( aRegionFacesList.First() );
315   }
316   else
317   {
318 #ifdef DEB_GET_REGION_SHAPE
319     HYDROData_ShapesGroup::GroupDefinition::Dump( std::cout, aSeqOfUsedGroups );
320 #endif
321
322     // Try to fuse all region faces into one common face
323     TopoDS_Shape aFuseShape;
324     TopTools_ListIteratorOfListOfShape aFaceIter( aRegionFacesList );
325     for ( ; aFaceIter.More(); aFaceIter.Next() )
326     {
327       if ( aFuseShape.IsNull() )
328       {
329         aFuseShape = aFaceIter.Value();
330         continue;
331       }
332
333       BRepAlgoAPI_Fuse aFuse( aFuseShape, aFaceIter.Value() );
334       if ( !aFuse.IsDone() )
335       {
336         aFuseShape.Nullify();
337         break;
338       }
339
340       aFuseShape = aFuse.Shape();
341
342       //update history of internal edges
343       TopTools_IndexedMapOfShape DIE;
344       TopTools_ListOfShape newSh1, newSh2;
345       DEBTRACE("IE.Extent():" << IE.Extent());
346       for (int i = 1; i <= IE.Extent(); i++)
347       {
348         const TopoDS_Shape& CSH = IE(i);
349         newSh1.Clear();
350         newSh2.Clear();
351         newSh1 = aFuse.Modified(CSH);
352         if (newSh1.IsEmpty())
353         {
354           newSh2 = aFuse.Generated(CSH);
355           if (newSh2.IsEmpty())
356             DIE.Add(CSH);
357           else
358             for (TopTools_ListIteratorOfListOfShape lt(newSh2); lt.More(); lt.Next())
359               if (!lt.Value().IsNull())
360                  DIE.Add(lt.Value());
361         }
362         else
363         {
364           for (TopTools_ListIteratorOfListOfShape lt(newSh1); lt.More(); lt.Next())
365             if (!lt.Value().IsNull())
366               DIE.Add(lt.Value());
367         }
368       }
369       IE = DIE;
370       //update groups
371       HYDROData_ShapesGroup::GroupDefinition::Update( &aSeqOfUsedGroups, &aFuse );
372     } // faces iterator
373
374 #ifdef DEB_GET_REGION_SHAPE
375     HYDROData_ShapesTool::DumpShapeSubShapes( std::cout, "Fused face edges:", aFuseShape, TopAbs_EDGE );
376 #endif
377
378     aFuseShape = HYDROData_Tool::RebuildCmp(aFuseShape);
379
380     ShapeUpgrade_UnifySameDomain unif( aFuseShape, Standard_False, Standard_True, Standard_False );
381     if (!IE.IsEmpty())
382     {
383       TopTools_MapOfShape IEM;
384       for (int i = 1; i <= IE.Extent(); i++)
385         IEM.Add(IE(i));
386       unif.KeepShapes(IEM);
387     }
388
389     unif.Build();
390     TopoDS_Shape anUnitedShape;
391     anUnitedShape = unif.Shape();
392
393     HYDROData_ShapesGroup::GroupDefinition::Update( &aSeqOfUsedGroups, &unif );
394
395     //cout << "inif:   fuseshape: " << aFuseShape.TShape().get() << "unifS " << anUnitedShape.TShape().get() << endl;
396 #ifdef DEB_GET_REGION_SHAPE
397     HYDROData_ShapesGroup::GroupDefinition::Dump( std::cout, aSeqOfUsedGroups );
398 #endif
399
400     TopTools_SequenceOfShape aShapeFaces;
401     HYDROData_ShapesTool::ExploreShapeToShapes( anUnitedShape, TopAbs_FACE, aShapeFaces );
402     if ( aShapeFaces.Length() == 1 ) //it should be either face or compound of faces (?)
403     {
404       const TopoDS_Face& CF = TopoDS::Face( aShapeFaces.Value( 1 ));
405       aResShape = CF;
406     }
407     else
408     {
409       TopTools_SequenceOfShape aShapeShells;
410       HYDROData_ShapesTool::ExploreShapeToShapes( anUnitedShape, TopAbs_SHELL, aShapeShells );
411       if (aShapeShells.Length() == 1)
412         aResShape = TopoDS::Shell(aShapeShells(1));
413       else
414         aResShape = anUnitedShape;
415     }
416
417     // Update the sequence of groups
418     if ( theSeqOfGroups )
419     {
420       HYDROData_ShapesGroup::SeqOfGroupsDefs::Iterator aUsedIter( aSeqOfUsedGroups );
421       for ( ; aUsedIter.More(); aUsedIter.Next() )
422       {
423         const HYDROData_ShapesGroup::GroupDefinition& aUsedGroupDef = aUsedIter.Value();
424         if ( aUsedGroupDef.Shapes.IsEmpty() )
425           continue;
426
427         HYDROData_ShapesGroup::SeqOfGroupsDefs::Iterator anOriIter( aSeqOfGroups );
428         for ( ; anOriIter.More(); anOriIter.Next() )
429         {
430           HYDROData_ShapesGroup::GroupDefinition& anOriGroupDef = anOriIter.ChangeValue();
431           if ( anOriGroupDef.Name != aUsedGroupDef.Name )
432             continue;
433
434           HYDROData_ShapesTool::AddShapes( anOriGroupDef.Shapes, aUsedGroupDef.Shapes );
435           break;
436         }
437       }
438
439       *theSeqOfGroups = aSeqOfGroups;
440     }
441   }
442                                                      
443   return aResShape;
444 }
445
446 QStringList HYDROData_Region::DumpToPython( const QString&       thePyScriptPath,
447                                             MapOfTreatedObjects& theTreatedObjects,
448                                             QString              defRegName ) const
449 {
450   QStringList aResList;
451
452   // Find region
453   findPythonReferenceObject( aResList, defRegName );
454
455   // Add zones
456   HYDROData_SequenceOfObjects aZones = GetZones();
457   HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
458   for ( ; aZonesIter.More(); aZonesIter.Next() ) {
459     Handle(HYDROData_Zone) aZone =
460       Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
461     if ( aZone.IsNull() ) {
462       continue;
463     }
464     
465     // find zone
466     aZone->findPythonReferenceObject( aResList );
467     theTreatedObjects.insert( aZone->GetName(), aZone );
468     
469     // set zone merge type
470     QString aMergeTypeStr;
471     HYDROData_Zone::MergeType aMergeType = aZone->GetMergeType();
472     if ( aMergeType == HYDROData_Zone::Merge_ZMIN ) {
473       aMergeTypeStr = "HYDROData_Zone.Merge_ZMIN";
474     } else if ( aMergeType == HYDROData_Zone::Merge_ZMAX ) {
475       aMergeTypeStr = "HYDROData_Zone.Merge_ZMAX";
476     } else if ( aMergeType == HYDROData_Zone::Merge_Object ) {
477       aMergeTypeStr = "HYDROData_Zone.Merge_Object";
478     }
479
480     if ( !aMergeTypeStr.isEmpty() ) {
481       aResList << QString( "%1.SetMergeType( %2 )" ).arg( aZone->GetObjPyName() ).arg( aMergeTypeStr );
482     }
483     if ( aMergeType == HYDROData_Zone::Merge_Object ) {
484       Handle(HYDROData_Entity) aMergeObject = aZone->GetMergeObject();
485       if ( !aMergeObject.IsNull() ) {
486         aMergeObject->findPythonReferenceObject( aResList );
487         aResList << QString( "%1.SetMergeObject( %2 )" ).arg( aZone->GetObjPyName() )
488                                                         .arg( aMergeObject->GetObjPyName() );
489       }
490     }
491     // set color
492     QColor zoneColor = aZone->GetColor(Qt::darkBlue);
493     aResList << QString( "%1.SetColor( QColor( %2, %3, %4 ))" )
494                  .arg( aZone->GetObjPyName() ).arg( zoneColor.red() ).arg( zoneColor.green() ).arg( zoneColor.blue() );
495     // add zone
496     setPythonReferenceObject( thePyScriptPath, theTreatedObjects, aResList, aZone, "AddZone" );
497
498   }
499
500   return aResList;
501 }
502
503 bool HYDROData_Region::IsSubmersible() const
504 {
505   HYDROData_SequenceOfObjects aZones = GetZones();
506   HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
507   for ( ; aZonesIter.More(); aZonesIter.Next() )
508   {
509     Handle(HYDROData_Zone) aZone =
510       Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
511     if ( !aZone->IsSubmersible() )
512       return false; //if one of zones is not submersible the region is considered as not submersible
513   }
514   return true;
515 }