]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_CompleteCalcCase.cxx
Salome HOME
Merge branch 'BR_H2018_DRAFT' into BR_2018_V8_5
[modules/hydro.git] / src / HYDROData / HYDROData_CompleteCalcCase.cxx
1 // Copyright (C) 2014-2018  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 // File:     HYDROData_CompleteCalcCase.cxx
20 // Author:   Ilya SHCHEKIN
21
22 #include <HYDROData_CompleteCalcCase.h>
23
24 #include <gp_Pnt.hxx>
25 #include <HYDROData_SplitToZonesTool.h>
26 #include <HYDROData_SplitShapesGroup.h>
27 #include <BOPTools_AlgoTools3D.hxx>
28 #include <IntTools_Context.hxx>
29 #include <BRepBuilderAPI_MakeFace.hxx>
30 #include <TopTools_HSequenceOfShape.hxx>
31 #include <HYDROData_PolylineXY.h>
32 #include <TopoDS.hxx>
33 #include <HYDROData_Region.h>
34 #include <TopTools_IndexedMapOfShape.hxx>
35 #include <TopExp.hxx>
36 #include <HYDROData_Document.h>
37 #include <HYDROData_Tool.h>
38
39 static void GetModifToOrigHistory(BOPAlgo_Builder& theAlgo, TopTools_IndexedDataMapOfShapeListOfShape& theModifToOrigMap)
40 {
41   const TopTools_ListOfShape& args = theAlgo.Arguments();
42   TopTools_IndexedMapOfShape argsAllSh;
43   TopTools_ListOfShape::Iterator it(args);
44   for (;it.More();it.Next())
45     TopExp::MapShapes(it.Value(), argsAllSh);
46   for (int i=1;i<=argsAllSh.Extent();i++)
47   {
48     const TopoDS_Shape& arg_sh = argsAllSh(i);
49     const TopTools_ListOfShape& modif_ls = theAlgo.Modified(arg_sh);
50     for (TopTools_ListIteratorOfListOfShape itLS(modif_ls); itLS.More(); itLS.Next())
51     {
52       const TopoDS_Shape& val = itLS.Value();
53       TopTools_ListOfShape* LS = theModifToOrigMap.ChangeSeek(val);
54       if (LS)
55         LS->Append(arg_sh);
56       else
57       {
58         TopTools_ListOfShape newLS;
59         newLS.Append(arg_sh);
60         theModifToOrigMap.Add(val, newLS);
61       }
62     }
63   }
64 }
65
66
67 static bool CheckIntersection(BOPAlgo_Builder& theAlgo, const std::vector<TopoDS_Shape>& SHM,
68   const TopTools_MapOfShape& ShapesToAvoid)
69 {
70   TopTools_MapOfShape gmodif_m;
71   int calc_ext = 0;
72   for (int i=0; i<SHM.size();i++)
73   {
74     TopTools_MapOfShape modif_m;
75     const TopTools_ListOfShape& modif_ls = theAlgo.Modified(SHM[i]);
76     for (TopTools_ListIteratorOfListOfShape itLS(modif_ls); itLS.More(); itLS.Next())
77     {
78       const TopoDS_Shape& val = itLS.Value();
79       if (!ShapesToAvoid.Contains(val))
80         modif_m.Add(val);
81     }
82     calc_ext+=modif_m.Extent();
83     gmodif_m.Unite(modif_m);
84     if (gmodif_m.Extent() < calc_ext)
85       return true; //there is an intersection
86   }
87   return false;
88 }
89
90 bool HYDROData_CompleteCalcCase::AddObjects( const Handle(HYDROData_Document)& doc,
91                                              Handle(HYDROData_CalculationCase)& theCalcCase, 
92                                              NCollection_Sequence<Handle(HYDROData_Entity)> theNewObjects,
93                                              bool& IsIntersectionOfNewObj)
94 {
95   Handle(HYDROData_PolylineXY) aBndPolyline = theCalcCase->GetBoundaryPolyline();        
96   TopoDS_Wire aBndWire;
97   TopoDS_Face aLimFace;
98   bool UseBndPolyline = false;
99   QString CaseName = theCalcCase->GetName();
100   if (!aBndPolyline.IsNull())
101   {
102     Handle(TopTools_HSequenceOfShape) aConnectedWires = new TopTools_HSequenceOfShape;
103     int nbWires = aBndPolyline->GetNbConnectedWires(aConnectedWires);
104     if (nbWires > 0)
105     {
106       aBndWire = TopoDS::Wire(aConnectedWires->Value(1));
107       if(!aBndWire.IsNull()) 
108       {      
109         if(HYDROData_SplitToZonesTool::buildLimFace(aBndWire, aLimFace)) 
110         {
111           theNewObjects.Append(theCalcCase->GetBoundaryPolyline());
112           UseBndPolyline = true;
113         }
114       }
115     }
116   }
117
118   BOPAlgo_Builder anAlgo;
119   std::vector<TopoDS_Shape> newShapes;
120   std::vector<Handle(HYDROData_ShapesGroup)> newshapesGroups;
121   NCollection_DataMap<TopoDS_Shape, QStringList, TopTools_ShapeMapHasher> aShToRefObjects;
122   for (int i=1; i<= theNewObjects.Size();i++)
123   {
124     Handle(HYDROData_PolylineXY) aPolyXY = Handle(HYDROData_PolylineXY)::DownCast( theNewObjects(i) );
125     if (!aPolyXY.IsNull())
126     {
127       TopoDS_Shape aSh = aPolyXY->GetShape();
128       newShapes.push_back(aSh);
129       anAlgo.AddArgument(aSh);
130       QStringList aLS(aPolyXY->GetName());
131       aShToRefObjects.Bind(aSh, aLS);
132     }
133     else 
134     {
135       Handle(HYDROData_Object ) anObj = Handle(HYDROData_Object)::DownCast( theNewObjects(i) );
136       if (!anObj.IsNull())
137       {
138         TopoDS_Shape aSh = anObj->GetTopShape();
139         //
140         HYDROData_SequenceOfObjects groups = anObj->GetGroups();
141         for ( int k=1; k<=groups.Size(); k++ )
142         {
143           Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast(groups(k));
144           if ( aGroup.IsNull() )
145             continue;
146           newshapesGroups.push_back(aGroup);
147         }
148         //
149         newShapes.push_back(aSh);
150         anAlgo.AddArgument(aSh);
151         QStringList aLS(anObj->GetName());
152         aShToRefObjects.Bind(aSh, aLS);
153       }
154     }
155   }
156
157   HYDROData_SequenceOfObjects aRegions = theCalcCase->GetRegions();
158   for ( int i = 1; i <= aRegions.Size(); i++ )
159   {
160     Handle(HYDROData_Region) aRegion = Handle(HYDROData_Region)::DownCast( aRegions(i) );
161     if ( !aRegion.IsNull() )
162     {
163       HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
164       for ( int j = 1; j <= aZones.Size(); j++ )
165       {
166         Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( aZones(j) );
167         TopoDS_Shape aSh = aZone->GetShape();
168         anAlgo.AddArgument(aSh);
169         HYDROData_SequenceOfObjects aRefObjects = aZone->GetObjects();
170         QStringList aRefObjList;
171         for (int k=1;k<=aRefObjects.Size();k++)
172           aRefObjList << aRefObjects(k)->GetName();
173         aShToRefObjects.Bind(aSh, aRefObjList);
174       }
175     }
176   }
177
178   anAlgo.Perform(); 
179 #if OCC_VERSION_LARGE > 0x07020000
180   if (anAlgo.HasErrors())
181     return false;
182 #endif
183   TopoDS_Shape aRes = anAlgo.Shape();        
184   TopTools_MapOfShape UsedFaces;
185   ///
186   TopTools_IndexedDataMapOfShapeListOfShape theModifToOrigMap;
187   GetModifToOrigHistory(anAlgo, theModifToOrigMap);
188
189   //
190   if (UseBndPolyline)
191   {
192     TopTools_IndexedMapOfShape aResFaces;
193     TopExp::MapShapes(aRes, TopAbs_FACE, aResFaces);
194     Handle(IntTools_Context) aContext = new IntTools_Context();
195     for (int i=1; i<= aResFaces.Extent();i++)
196     {
197       gp_Pnt aP3D;
198       gp_Pnt2d aP2D;
199       TopoDS_Face aF = TopoDS::Face(aResFaces(i));
200       int err = BOPTools_AlgoTools3D::PointInFace(aF, aP3D, aP2D, aContext);
201       if (err)
202         continue;
203
204       TopAbs_State aState = HYDROData_Tool::ComputePointState(gp_XY(aP3D.X(), aP3D.Y()), aLimFace);
205       if (aState == TopAbs_OUT)
206       {
207         UsedFaces.Add(aF); //filter out the faces which is out of boundary polyline
208       }
209     }
210   }
211
212   ///
213   //check intersection between new objects => if it's present, the combining of zones into region will be depend on ordering
214   IsIntersectionOfNewObj = CheckIntersection(anAlgo, newShapes, UsedFaces);
215   //
216   std::vector<std::vector<TopoDS_Shape>> NREGV; //new regions vector (each vector is a region, subvector == zones) 
217   for (int i=0;i<newShapes.size();i++)
218   {
219     TopoDS_Shape aSh = newShapes[i];
220     if (aSh.ShapeType() != TopAbs_FACE)
221       continue;
222     TopTools_ListOfShape newShL = anAlgo.Modified(aSh);
223     if (newShL.IsEmpty()) //non-modified
224     {
225       if (!UsedFaces.Contains(aSh))
226       {
227         std::vector<TopoDS_Shape> vect;
228         vect.push_back(aSh);
229         NREGV.push_back(vect);
230         UsedFaces.Add(aSh);
231       }
232     }
233     else //was modified
234     {
235       std::vector<TopoDS_Shape> vect;
236       for (TopTools_ListIteratorOfListOfShape it(newShL); it.More(); it.Next())
237       {
238         TopoDS_Face nF = TopoDS::Face(it.Value());
239         if (!nF.IsNull() && !UsedFaces.Contains(nF))
240         {                                         
241           vect.push_back(nF);
242           UsedFaces.Add(nF);
243         }
244       }    
245       NREGV.push_back(vect);
246     }
247   }
248   
249   //iter through already existing zone
250   //substract new zones (NREGV) from old zones
251   for ( int i = 1; i <= aRegions.Size(); i++ )
252   {
253     Handle(HYDROData_Region) aRegion = Handle(HYDROData_Region)::DownCast( aRegions(i) );
254     if ( !aRegion.IsNull() )
255     {
256       HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
257       for ( int j = 1; j <= aZones.Size(); j++ )
258       {
259         Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast(aZones(j));
260         TopoDS_Shape aSh = aZone->GetShape();
261         TopTools_ListOfShape newShL = anAlgo.Modified(aSh);
262         if (newShL.IsEmpty() )
263           newShL.Append(aSh);
264         TopTools_MapOfShape newShM;
265         for (TopTools_ListIteratorOfListOfShape it(newShL); it.More(); it.Next())
266           newShM.Add(it.Value());
267         //
268         newShM.Subtract(UsedFaces); ///substract UsedFaces from newShM (since they have been taken by regions/object with higher priority)
269         //
270
271         if (newShM.Size() == 0)
272         {
273           //remove zone
274           aRegion->RemoveZone(aZone, true);
275         }
276         else if (newShM.Size() == 1)
277         {
278           TopoDS_Shape newS = *newShM.cbegin();
279           if (!newS.IsEqual(aSh))
280             aZone->SetShape(newS);
281         }
282         else ///newShM > 1
283         {
284           QString anOldZoneName = aZone->GetName();                
285           HYDROData_SequenceOfObjects aRefObjects = aZone->GetObjects();
286           aRegion->RemoveZone(aZone, false);
287           QStringList aRefObjList;
288           for (int k=1;k<=aRefObjects.Size();k++)
289             aRefObjList << aRefObjects(k)->GetName();
290           for (TopTools_MapIteratorOfMapOfShape it(newShM); it.More(); it.Next())
291           {
292             if (it.Value().ShapeType() == TopAbs_FACE)
293             {
294               TopoDS_Face F = TopoDS::Face(it.Value());
295               aRegion->addNewZone( doc, anOldZoneName, F, aRefObjList );
296             }
297           }
298         }
299       }
300     }
301   }
302
303   //create new regions/zones based on NREGV
304   QString aRegsPref = CaseName + "_Reg_";
305   QString aZonesPref = CaseName + "_Zone";
306   for ( int k=0;k<NREGV.size();k++ )
307   {
308     const std::vector<TopoDS_Shape>& sh_vec = NREGV[k];
309     Handle(HYDROData_Region) aRegion = theCalcCase->addNewRegion( doc, aRegsPref );
310     for (int i=0;i<sh_vec.size();i++)
311     {
312       TopoDS_Face nF = TopoDS::Face(sh_vec[i]);
313       QString zoneName = aZonesPref;
314       QStringList refObjList;
315       //
316       const TopTools_ListOfShape* origLS = theModifToOrigMap.Seek(nF);
317       if (origLS)
318       {
319         for (TopTools_ListIteratorOfListOfShape itLS1(*origLS); itLS1.More(); itLS1.Next())
320         {
321           const TopoDS_Shape& OrSh = itLS1.Value();
322           const QStringList* names = aShToRefObjects.Seek(OrSh);
323           if (names)
324             refObjList.append(*names);
325         }
326       }
327       //
328       Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( doc, aZonesPref, nF, refObjList);
329     }
330   }
331
332   //GROUPS
333   HYDROData_SequenceOfObjects aSplitGroups = theCalcCase->GetSplitGroups();
334
335   ///process boundary polyline group (if present)
336   Handle(HYDROData_ShapesGroup) aBndWireGroup;
337   QString BndWireGroupName;
338   TopTools_ListOfShape aNewGroupForBndWireLS;
339   if (UseBndPolyline)
340   {
341     TopTools_IndexedMapOfShape aBndWireEdges;
342     TopTools_IndexedDataMapOfShapeListOfShape aResEdgesToFaces;
343     TopExp::MapShapes(aBndWire, TopAbs_EDGE, aBndWireEdges);
344     TopExp::MapShapesAndAncestors(aRes, TopAbs_EDGE, TopAbs_FACE, aResEdgesToFaces);
345     TopTools_IndexedMapOfShape aNewGroupForBndWire;
346     for (int i=1;i<=aBndWireEdges.Extent();i++)
347     {
348       TopoDS_Shape E = aBndWireEdges(i);
349       TopTools_ListOfShape aMLS = anAlgo.Modified(E);
350       if (aMLS.IsEmpty())
351         aMLS.Append(E);
352       TopTools_ListIteratorOfListOfShape itLS(aMLS);
353       for (;itLS.More();itLS.Next())
354       {
355         TopoDS_Edge E1 = TopoDS::Edge(itLS.Value());
356         if (E1.IsNull())
357           continue;
358         if (!aResEdgesToFaces.Contains(E1)) //should contains E since it's a part of aRes
359           continue;
360         //skip free edges
361         if (aResEdgesToFaces.FindFromKey(E1).Extent() > 0) 
362           aNewGroupForBndWire.Add(E1);
363       }
364     }
365     //
366     BndWireGroupName = CaseName + "_" + aBndPolyline->GetName();
367     for (int i=1;i<=aNewGroupForBndWire.Extent();i++)
368       aNewGroupForBndWireLS.Append(aNewGroupForBndWire(i));
369   }
370
371   // UPDATE SPLIT GROUPS       
372   for ( int k=1; k<=aSplitGroups.Size(); k++ )
373   {
374     Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast(aSplitGroups(k));
375     if ( aGroup.IsNull() )
376       continue;
377
378     TopTools_SequenceOfShape GDefSeq, ModifedGDefSeq;
379
380     if (UseBndPolyline && aGroup->GetName() == BndWireGroupName)
381     {
382       aBndWireGroup = aGroup;
383       continue;
384     }
385
386     aGroup->GetShapes( GDefSeq );
387     for (int i=1;i<=GDefSeq.Length();i++)
388     {
389       const TopoDS_Shape& CSH = GDefSeq(i); 
390       TopTools_ListOfShape aMLS = anAlgo.Modified(CSH);
391       if (aMLS.IsEmpty())
392         aMLS.Append(CSH);
393       TopTools_ListIteratorOfListOfShape itLS(aMLS);
394       for (;itLS.More();itLS.Next())
395         ModifedGDefSeq.Append(itLS.Value());                     
396     }
397     aGroup->SetShapes(ModifedGDefSeq);
398   }
399
400   if (UseBndPolyline)
401   {
402     if (!aBndWireGroup.IsNull()) //modify group
403     {
404       aBndWireGroup->SetShapes(aNewGroupForBndWireLS);
405     }
406     else //add new group
407     {
408       Handle(HYDROData_SplitShapesGroup) aSplitGroup = theCalcCase->addNewSplitGroup( BndWireGroupName );
409       aSplitGroup->SetShapes(aNewGroupForBndWireLS);
410     }
411   }
412
413   ///Add new groups from newly added objects
414   for ( int k=0; k<newshapesGroups.size(); k++ )
415   {
416     Handle(HYDROData_ShapesGroup) aGroup = newshapesGroups[k];
417     QString aName = aGroup->GetName();
418     TopTools_SequenceOfShape aSeqSh, ModifedGDefSeq;
419     aGroup->GetShapes(aSeqSh);
420     Handle(HYDROData_SplitShapesGroup) aSplitGroup = theCalcCase->addNewSplitGroup( aName );
421
422     for (int i=1;i<=aSeqSh.Length();i++)
423     {
424       const TopoDS_Shape& CSH = aSeqSh(i); 
425       TopTools_ListOfShape aMLS = anAlgo.Modified(CSH);
426       if (aMLS.IsEmpty())
427         aMLS.Append(CSH);
428       TopTools_ListIteratorOfListOfShape itLS(aMLS);
429       for (;itLS.More();itLS.Next())
430         ModifedGDefSeq.Append(itLS.Value());                     
431     }
432     aSplitGroup->SetShapes(ModifedGDefSeq);
433   }
434   return true;
435 }