2 #include "HYDROData_CalculationCase.h"
4 #include "HYDROData_ArtificialObject.h"
5 #include "HYDROData_Document.h"
6 #include "HYDROData_Iterator.h"
7 #include "HYDROData_NaturalObject.h"
8 #include "HYDROData_PolylineXY.h"
9 #include "HYDROData_SplitToZonesTool.h"
10 #include "HYDROData_Region.h"
11 #include "HYDROData_Tool.h"
12 #include "HYDROData_Zone.h"
15 #include <TopoDS_Shell.hxx>
16 #include <BRep_Builder.hxx>
17 #include <BRepBuilderAPI_Sewing.hxx>
18 #include <TopExp_Explorer.hxx>
20 #include <TopTools_ListOfShape.hxx>
21 #include <TopTools_ListIteratorOfListOfShape.hxx>
23 #define CALCULATION_REGIONS_PREF GetName() + "_Reg"
24 #define CALCULATION_ZONES_PREF GetName() + "_Zone"
26 #define PYTHON_CALCULATION_ID "KIND_CALCULATION"
28 IMPLEMENT_STANDARD_HANDLE(HYDROData_CalculationCase, HYDROData_Entity)
29 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_CalculationCase, HYDROData_Entity)
31 HYDROData_CalculationCase::HYDROData_CalculationCase()
36 HYDROData_CalculationCase::~HYDROData_CalculationCase()
40 void HYDROData_CalculationCase::SetName( const QString& theName )
42 QString anOldCaseName = GetName();
43 if ( anOldCaseName != theName )
45 HYDROData_SequenceOfObjects aRegions = GetRegions();
47 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
48 for ( ; anIter.More(); anIter.Next() )
50 Handle(HYDROData_Region) aRegion =
51 Handle(HYDROData_Region)::DownCast( anIter.Value() );
52 if ( aRegion.IsNull() )
55 QString aRegionName = aRegion->GetName();
56 if ( aRegionName.startsWith( anOldCaseName ) )
58 aRegionName.replace( anOldCaseName, theName );
59 aRegion->SetName( aRegionName );
62 HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
63 HYDROData_SequenceOfObjects::Iterator anIter( aZones );
64 for ( ; anIter.More(); anIter.Next() )
66 Handle(HYDROData_Zone) aRegZone =
67 Handle(HYDROData_Zone)::DownCast( anIter.Value() );
68 if ( aRegZone.IsNull() )
71 QString aRegionZoneName = aRegZone->GetName();
72 if ( aRegionZoneName.startsWith( anOldCaseName ) )
74 aRegionZoneName.replace( anOldCaseName, theName );
75 aRegZone->SetName( aRegionZoneName );
81 HYDROData_Entity::SetName( theName );
84 QStringList HYDROData_CalculationCase::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
88 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
89 if ( aDocument.IsNull() )
92 QString aDocName = aDocument->GetDocPyName();
93 QString aCalculName = GetName();
95 aResList << QString( "%1 = %2.CreateObject( %3 );" )
96 .arg( aCalculName ).arg( aDocName ).arg( PYTHON_CALCULATION_ID );
97 aResList << QString( "%1.SetName( \"%2\" );" )
98 .arg( aCalculName ).arg( aCalculName );
99 aResList << QString( "" );
101 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
102 HYDROData_SequenceOfObjects::Iterator anIter( aGeomObjects );
103 for ( ; anIter.More(); anIter.Next() )
105 Handle(HYDROData_Object) aRefGeomObj =
106 Handle(HYDROData_Object)::DownCast( anIter.Value() );
107 if ( !aRefGeomObj.IsNull() )
108 setPythonReferenceObject( theTreatedObjects, aResList, aRefGeomObj, "AddGeometryObject" );
110 aResList << QString( "" );
112 aResList << QString( "%1.SplitGeometryObjects();" ).arg( aCalculName );
113 aResList << QString( "" );
115 // Now we restore the regions and zones order
116 HYDROData_SequenceOfObjects aRegions = GetRegions();
117 anIter.Init( aRegions );
118 for ( ; anIter.More(); anIter.Next() )
120 Handle(HYDROData_Region) aRegion =
121 Handle(HYDROData_Region)::DownCast( anIter.Value() );
122 if ( aRegion.IsNull() )
125 QString aRegionName = aRegion->GetName();
132 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetAllReferenceObjects() const
134 HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
136 HYDROData_SequenceOfObjects aSeqOfRegions = GetRegions();
137 aResSeq.Append( aSeqOfRegions );
142 void HYDROData_CalculationCase::Update()
144 HYDROData_Entity::Update();
146 // At first we remove previously created regions
149 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
150 if ( aDocument.IsNull() )
153 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
154 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
155 if ( aGeomObjects.IsEmpty() )
158 HYDROData_SplitToZonesTool::SplitDataList aSplitedZones =
159 HYDROData_SplitToZonesTool::SplitToZones( aGeomObjects, aBoundaryPolyline );
160 if ( aSplitedZones.isEmpty() )
163 QString aRegsPref = CALCULATION_REGIONS_PREF;
164 QString aZonesPref = CALCULATION_ZONES_PREF;
166 // Create result regions for case, by default one zone for one region
167 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplitedZones );
168 while( anIter.hasNext() )
170 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
173 Handle(HYDROData_Region) aRegion = addNewRegion();
175 QString aRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
176 aRegion->SetName( aRegionName );
178 // Add the zone for region
179 Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone();
181 QString aZoneName = HYDROData_Tool::GenerateObjectName( aDocument, aZonesPref );
182 aRegionZone->SetName( aZoneName );
184 aRegionZone->SetShape( aSplitData.Face() );
186 // Add the reference object for zone
187 for ( int i = 0, n = aSplitData.ObjectNames.length(); i < n; ++i )
189 const QString& anObjName = aSplitData.ObjectNames.at( i );
191 Handle(HYDROData_Object) aRefObject = Handle(HYDROData_Object)::DownCast(
192 HYDROData_Tool::FindObjectByName( aDocument, anObjName ) );
193 if ( aRefObject.IsNull() )
196 aRegionZone->AddGeometryObject( aRefObject );
201 bool HYDROData_CalculationCase::AddGeometryObject( const Handle(HYDROData_Object)& theObject )
203 if ( !HYDROData_Tool::IsGeometryObject( theObject ) )
204 return false; // Wrong type of object
206 if ( HasReference( theObject, DataTag_GeometryObject ) )
207 return false; // Object is already in reference list
209 AddReferenceObject( theObject, DataTag_GeometryObject );
211 // Indicate model of the need to update zones splitting
217 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryObjects() const
219 return GetReferenceObjects( DataTag_GeometryObject );
222 void HYDROData_CalculationCase::RemoveGeometryObject( const Handle(HYDROData_Object)& theObject )
224 if ( theObject.IsNull() )
227 RemoveReferenceObject( theObject->Label(), DataTag_GeometryObject );
229 // Indicate model of the need to update zones splitting
233 void HYDROData_CalculationCase::RemoveGeometryObjects()
235 ClearReferenceObjects( DataTag_GeometryObject );
237 // Indicate model of the need to update zones splitting
241 void HYDROData_CalculationCase::SetBoundaryPolyline( const Handle(HYDROData_PolylineXY)& thePolyline )
243 Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
245 SetReferenceObject( thePolyline, DataTag_Polyline );
247 // Indicate model of the need to update zones splitting
248 SetToUpdate( !IsEqual( aPrevPolyline, thePolyline ) || IsMustBeUpdated() );
251 Handle(HYDROData_PolylineXY) HYDROData_CalculationCase::GetBoundaryPolyline() const
253 return Handle(HYDROData_PolylineXY)::DownCast(
254 GetReferenceObject( DataTag_Polyline ) );
257 void HYDROData_CalculationCase::RemoveBoundaryPolyline()
259 Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
261 ClearReferenceObjects( DataTag_Polyline );
263 // Indicate model of the need to update zones splitting
264 SetToUpdate( !aPrevPolyline.IsNull() || IsMustBeUpdated() );
267 Handle(HYDROData_Region) HYDROData_CalculationCase::AddNewRegion( const Handle(HYDROData_Zone)& theZone )
269 Handle(HYDROData_Region) aNewRegion = addNewRegion();
270 if ( aNewRegion.IsNull() )
273 // Generate new name for new region
274 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
275 if ( !aDocument.IsNull() )
277 QString aRegsPref = CALCULATION_REGIONS_PREF;
279 QString aNewRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
280 aNewRegion->SetName( aNewRegionName );
283 aNewRegion->AddZone( theZone );
288 bool HYDROData_CalculationCase::AddRegion( const Handle(HYDROData_Region)& theRegion )
290 if ( theRegion.IsNull() )
293 if ( HasReference( theRegion, DataTag_Region ) )
294 return false; // Object is already in reference list
296 // Move the region from other calculation
297 Handle(HYDROData_CalculationCase) aFatherCalc =
298 Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
299 if ( !aFatherCalc.IsNull() && aFatherCalc->Label() != myLab )
301 Handle(HYDROData_Region) aNewRegion = addNewRegion();
302 theRegion->CopyTo( aNewRegion );
304 aFatherCalc->RemoveRegion( theRegion );
306 theRegion->SetLabel( aNewRegion->Label() );
310 AddReferenceObject( theRegion, DataTag_Region );
316 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetRegions() const
318 return GetReferenceObjects( DataTag_Region );
321 void HYDROData_CalculationCase::UpdateRegionsOrder()
323 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
324 if ( aDocument.IsNull() )
327 HYDROData_SequenceOfObjects aRegions = GetRegions();
329 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
330 for ( ; anIter.More(); anIter.Next() )
332 Handle(HYDROData_Region) aRegion =
333 Handle(HYDROData_Region)::DownCast( anIter.Value() );
334 if ( aRegion.IsNull() )
337 aRegion->SetName( "" );
340 QString aRegsPref = CALCULATION_REGIONS_PREF;
342 anIter.Init( aRegions );
343 for ( ; anIter.More(); anIter.Next() )
345 Handle(HYDROData_Region) aRegion =
346 Handle(HYDROData_Region)::DownCast( anIter.Value() );
347 if ( aRegion.IsNull() )
350 QString aRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
351 aRegion->SetName( aRegionName );
355 void HYDROData_CalculationCase::RemoveRegion( const Handle(HYDROData_Region)& theRegion )
357 if ( theRegion.IsNull() )
360 RemoveReferenceObject( theRegion->Label(), DataTag_Region );
362 // Remove region from data model
363 Handle(HYDROData_CalculationCase) aFatherCalc =
364 Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
365 if ( !aFatherCalc.IsNull() && aFatherCalc->Label() == myLab )
369 void HYDROData_CalculationCase::RemoveRegions()
371 ClearReferenceObjects( DataTag_Region );
372 myLab.FindChild( DataTag_ChildRegion ).ForgetAllAttributes( true );
375 Handle(HYDROData_Region) HYDROData_CalculationCase::addNewRegion()
377 TDF_Label aNewLab = myLab.FindChild( DataTag_ChildRegion ).NewChild();
379 Handle(HYDROData_Region) aNewRegion =
380 Handle(HYDROData_Region)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_REGION ) );
381 AddRegion( aNewRegion );
386 TopoDS_Shell HYDROData_CalculationCase::GetShell()
390 TopTools_ListOfShape aFacesList;
392 // Make shell containing all region shapes
393 BRepBuilderAPI_Sewing aSewing( Precision::Confusion()*10.0 );
395 HYDROData_SequenceOfObjects aCaseRegions = GetRegions();
396 HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
397 for ( ; aRegionIter.More(); aRegionIter.Next() ) {
398 Handle(HYDROData_Region) aRegion =
399 Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
400 if( aRegion.IsNull() ) {
404 TopoDS_Shape aRegionShape = aRegion->GetShape();
405 if( !aRegionShape.IsNull() ) {
406 if ( aRegionShape.ShapeType() == TopAbs_FACE ) {
407 TopoDS_Face aFace = TopoDS::Face( aRegionShape );
408 if ( !aFace.IsNull() ) {
409 aFacesList.Append( aFace );
410 aSewing.Add( aFace );
413 TopExp_Explorer anExp( aRegionShape, TopAbs_FACE );
414 for ( ; anExp.More(); anExp.Next() ) {
415 TopoDS_Face aFace = TopoDS::Face( anExp.Current() );
416 if ( !aFace.IsNull() ) {
417 aFacesList.Append( aFace );
418 aSewing.Add( aFace );
423 } // regions iterator
426 TopoDS_Shape aSewedShape = aSewing.SewedShape();
428 if ( !aSewedShape.IsNull() )
430 if ( aSewedShape.ShapeType() == TopAbs_FACE && aCaseRegions.Length() ==1 ) {
431 // create shell from one face
432 BRep_Builder aBuilder;
433 aBuilder.MakeShell( aShell );
434 aBuilder.Add( aShell, aSewedShape);
436 TopExp_Explorer anExpShells( aSewedShape, TopAbs_SHELL );
437 Standard_Integer aNbOfShells = 0;
438 for ( ; anExpShells.More(); anExpShells.Next() ) {
439 aShell = TopoDS::Shell( anExpShells.Current() );
443 if ( aNbOfShells != 1 ) {
445 BRep_Builder aBuilder;
446 aBuilder.MakeShell( aShell );
448 TopExp_Explorer anExpFaces( aSewedShape, TopAbs_FACE );
449 for ( ; anExpFaces.More(); anExpFaces.Next() ) {
450 TopoDS_Face aFace = TopoDS::Face( anExpFaces.Current() );
451 if ( !aFace.IsNull() ) {
452 aBuilder.Add( aShell, aFace );
459 if ( !aShell.IsNull() ) {
460 TopTools_IndexedMapOfShape aMapOfFaces;
461 TopExp::MapShapes( aShell, TopAbs_FACE, aMapOfFaces );
462 if ( aMapOfFaces.Extent() != aFacesList.Extent() ) {
464 BRep_Builder aBuilder;
465 aBuilder.MakeShell( aShell );
467 TopTools_ListIteratorOfListOfShape anIter( aFacesList );
468 for ( ; anIter.More(); anIter.Next() ) {
469 TopoDS_Face aFace = TopoDS::Face( anIter.Value() );
470 aBuilder.Add( aShell, aFace );
477 BRep_Builder aBuilder;
478 aBuilder.MakeShell( aShell );
480 // Make shell containing all region shapes
481 HYDROData_SequenceOfObjects aCaseRegions = GetRegions();
482 HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
483 for ( ; aRegionIter.More(); aRegionIter.Next() ) {
484 Handle(HYDROData_Region) aRegion =
485 Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
486 if( aRegion.IsNull() ) {
490 TopoDS_Shape aRegionShape = aRegion->GetShape();
492 // Add shape (face or shell) corresponding to the region into the shell
493 if( !aRegionShape.IsNull() ) {
494 if ( aRegionShape.ShapeType() == TopAbs_FACE ) {
495 aBuilder.Add( aShell, aRegionShape );
497 TopExp_Explorer anExp( aRegionShape, TopAbs_FACE );
498 for( ; anExp.More(); anExp.Next() ) {
499 TopoDS_Face aFace = TopoDS::Face( anExp.Current() );
500 if( !aFace.IsNull() ) {
501 aBuilder.Add( aShell, aFace );
506 } // regions iterator
509 // Nullify shell if it is empty
510 if ( !aShell.IsNull() && !TopoDS_Iterator(aShell).More() ) {