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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // File: HYDROData_CompleteCalcCase.cxx
20 // Author: Ilya SHCHEKIN
22 #include <HYDROData_CompleteCalcCase.h>
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>
33 #include <HYDROData_Region.h>
34 #include <TopTools_IndexedMapOfShape.hxx>
36 #include <HYDROData_Document.h>
37 #include <HYDROData_Tool.h>
39 static void GetModifToOrigHistory(BOPAlgo_Builder& theAlgo, TopTools_IndexedDataMapOfShapeListOfShape& theModifToOrigMap)
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++)
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())
52 const TopoDS_Shape& val = itLS.Value();
53 TopTools_ListOfShape* LS = theModifToOrigMap.ChangeSeek(val);
58 TopTools_ListOfShape newLS;
60 theModifToOrigMap.Add(val, newLS);
67 static bool CheckIntersection(BOPAlgo_Builder& theAlgo, const std::vector<TopoDS_Shape>& SHM,
68 const TopTools_MapOfShape& ShapesToAvoid)
70 TopTools_MapOfShape gmodif_m;
72 for (int i=0; i<SHM.size();i++)
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())
78 const TopoDS_Shape& val = itLS.Value();
79 if (!ShapesToAvoid.Contains(val))
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
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)
95 Handle(HYDROData_PolylineXY) aBndPolyline = theCalcCase->GetBoundaryPolyline();
98 bool UseBndPolyline = false;
99 QString CaseName = theCalcCase->GetName();
100 if (!aBndPolyline.IsNull())
102 Handle(TopTools_HSequenceOfShape) aConnectedWires = new TopTools_HSequenceOfShape;
103 int nbWires = aBndPolyline->GetNbConnectedWires(aConnectedWires);
106 aBndWire = TopoDS::Wire(aConnectedWires->Value(1));
107 if(!aBndWire.IsNull())
109 if(HYDROData_SplitToZonesTool::buildLimFace(aBndWire, aLimFace))
111 theNewObjects.Append(theCalcCase->GetBoundaryPolyline());
112 UseBndPolyline = true;
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++)
124 Handle(HYDROData_PolylineXY) aPolyXY = Handle(HYDROData_PolylineXY)::DownCast( theNewObjects(i) );
125 if (!aPolyXY.IsNull())
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);
135 Handle(HYDROData_Object ) anObj = Handle(HYDROData_Object)::DownCast( theNewObjects(i) );
138 TopoDS_Shape aSh = anObj->GetTopShape();
140 HYDROData_SequenceOfObjects groups = anObj->GetGroups();
141 for ( int k=1; k<=groups.Size(); k++ )
143 Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast(groups(k));
144 if ( aGroup.IsNull() )
146 newshapesGroups.push_back(aGroup);
149 newShapes.push_back(aSh);
150 anAlgo.AddArgument(aSh);
151 QStringList aLS(anObj->GetName());
152 aShToRefObjects.Bind(aSh, aLS);
157 HYDROData_SequenceOfObjects aRegions = theCalcCase->GetRegions();
158 for ( int i = 1; i <= aRegions.Size(); i++ )
160 Handle(HYDROData_Region) aRegion = Handle(HYDROData_Region)::DownCast( aRegions(i) );
161 if ( !aRegion.IsNull() )
163 HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
164 for ( int j = 1; j <= aZones.Size(); j++ )
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);
179 #if OCC_VERSION_LARGE > 0x07020000
180 if (anAlgo.HasErrors())
183 TopoDS_Shape aRes = anAlgo.Shape();
184 TopTools_MapOfShape UsedFaces;
186 TopTools_IndexedDataMapOfShapeListOfShape theModifToOrigMap;
187 GetModifToOrigHistory(anAlgo, theModifToOrigMap);
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++)
199 TopoDS_Face aF = TopoDS::Face(aResFaces(i));
200 int err = BOPTools_AlgoTools3D::PointInFace(aF, aP3D, aP2D, aContext);
204 TopAbs_State aState = HYDROData_Tool::ComputePointState(gp_XY(aP3D.X(), aP3D.Y()), aLimFace);
205 if (aState == TopAbs_OUT)
207 UsedFaces.Add(aF); //filter out the faces which is out of boundary polyline
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);
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++)
219 TopoDS_Shape aSh = newShapes[i];
220 if (aSh.ShapeType() != TopAbs_FACE)
222 TopTools_ListOfShape newShL = anAlgo.Modified(aSh);
223 if (newShL.IsEmpty()) //non-modified
225 if (!UsedFaces.Contains(aSh))
227 std::vector<TopoDS_Shape> vect;
229 NREGV.push_back(vect);
235 std::vector<TopoDS_Shape> vect;
236 for (TopTools_ListIteratorOfListOfShape it(newShL); it.More(); it.Next())
238 TopoDS_Face nF = TopoDS::Face(it.Value());
239 if (!nF.IsNull() && !UsedFaces.Contains(nF))
245 NREGV.push_back(vect);
249 //iter through already existing zone
250 //substract new zones (NREGV) from old zones
251 for ( int i = 1; i <= aRegions.Size(); i++ )
253 Handle(HYDROData_Region) aRegion = Handle(HYDROData_Region)::DownCast( aRegions(i) );
254 if ( !aRegion.IsNull() )
256 HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
257 for ( int j = 1; j <= aZones.Size(); j++ )
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() )
264 TopTools_MapOfShape newShM;
265 for (TopTools_ListIteratorOfListOfShape it(newShL); it.More(); it.Next())
266 newShM.Add(it.Value());
268 newShM.Subtract(UsedFaces); ///substract UsedFaces from newShM (since they have been taken by regions/object with higher priority)
271 if (newShM.Size() == 0)
274 aRegion->RemoveZone(aZone, true);
276 else if (newShM.Size() == 1)
278 TopoDS_Shape newS = *newShM.cbegin();
279 if (!newS.IsEqual(aSh))
280 aZone->SetShape(newS);
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())
292 if (it.Value().ShapeType() == TopAbs_FACE)
294 TopoDS_Face F = TopoDS::Face(it.Value());
295 aRegion->addNewZone( doc, anOldZoneName, F, aRefObjList );
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++ )
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++)
312 TopoDS_Face nF = TopoDS::Face(sh_vec[i]);
313 QString zoneName = aZonesPref;
314 QStringList refObjList;
316 const TopTools_ListOfShape* origLS = theModifToOrigMap.Seek(nF);
319 for (TopTools_ListIteratorOfListOfShape itLS1(*origLS); itLS1.More(); itLS1.Next())
321 const TopoDS_Shape& OrSh = itLS1.Value();
322 const QStringList* names = aShToRefObjects.Seek(OrSh);
324 refObjList.append(*names);
328 Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( doc, aZonesPref, nF, refObjList);
333 HYDROData_SequenceOfObjects aSplitGroups = theCalcCase->GetSplitGroups();
335 ///process boundary polyline group (if present)
336 Handle(HYDROData_ShapesGroup) aBndWireGroup;
337 QString BndWireGroupName;
338 TopTools_ListOfShape aNewGroupForBndWireLS;
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++)
348 TopoDS_Shape E = aBndWireEdges(i);
349 TopTools_ListOfShape aMLS = anAlgo.Modified(E);
352 TopTools_ListIteratorOfListOfShape itLS(aMLS);
353 for (;itLS.More();itLS.Next())
355 TopoDS_Edge E1 = TopoDS::Edge(itLS.Value());
358 if (!aResEdgesToFaces.Contains(E1)) //should contains E since it's a part of aRes
361 if (aResEdgesToFaces.FindFromKey(E1).Extent() > 0)
362 aNewGroupForBndWire.Add(E1);
366 BndWireGroupName = CaseName + "_" + aBndPolyline->GetName();
367 for (int i=1;i<=aNewGroupForBndWire.Extent();i++)
368 aNewGroupForBndWireLS.Append(aNewGroupForBndWire(i));
371 // UPDATE SPLIT GROUPS
372 for ( int k=1; k<=aSplitGroups.Size(); k++ )
374 Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast(aSplitGroups(k));
375 if ( aGroup.IsNull() )
378 TopTools_SequenceOfShape GDefSeq, ModifedGDefSeq;
380 if (UseBndPolyline && aGroup->GetName() == BndWireGroupName)
382 aBndWireGroup = aGroup;
386 aGroup->GetShapes( GDefSeq );
387 for (int i=1;i<=GDefSeq.Length();i++)
389 const TopoDS_Shape& CSH = GDefSeq(i);
390 TopTools_ListOfShape aMLS = anAlgo.Modified(CSH);
393 TopTools_ListIteratorOfListOfShape itLS(aMLS);
394 for (;itLS.More();itLS.Next())
395 ModifedGDefSeq.Append(itLS.Value());
397 aGroup->SetShapes(ModifedGDefSeq);
402 if (!aBndWireGroup.IsNull()) //modify group
404 aBndWireGroup->SetShapes(aNewGroupForBndWireLS);
408 Handle(HYDROData_SplitShapesGroup) aSplitGroup = theCalcCase->addNewSplitGroup( BndWireGroupName );
409 aSplitGroup->SetShapes(aNewGroupForBndWireLS);
413 ///Add new groups from newly added objects
414 for ( int k=0; k<newshapesGroups.size(); k++ )
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 );
422 for (int i=1;i<=aSeqSh.Length();i++)
424 const TopoDS_Shape& CSH = aSeqSh(i);
425 TopTools_ListOfShape aMLS = anAlgo.Modified(CSH);
428 TopTools_ListIteratorOfListOfShape itLS(aMLS);
429 for (;itLS.More();itLS.Next())
430 ModifedGDefSeq.Append(itLS.Value());
432 aSplitGroup->SetShapes(ModifedGDefSeq);