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_Polyline.h"
9 #include "HYDROData_SplitToZonesTool.h"
10 #include "HYDROData_Region.h"
11 #include "HYDROData_Tool.h"
12 #include "HYDROData_Zone.h"
14 #include <TopoDS_Shell.hxx>
15 #include <BRep_Builder.hxx>
17 #define CALCULATION_REGIONS_PREF GetName() + "_Reg"
18 #define CALCULATION_ZONES_PREF GetName() + "_Zone"
20 #define PYTHON_CALCULATION_ID "KIND_CALCULATION"
22 IMPLEMENT_STANDARD_HANDLE(HYDROData_CalculationCase, HYDROData_Entity)
23 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_CalculationCase, HYDROData_Entity)
25 HYDROData_CalculationCase::HYDROData_CalculationCase()
30 HYDROData_CalculationCase::~HYDROData_CalculationCase()
34 QStringList HYDROData_CalculationCase::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
38 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
39 if ( aDocument.IsNull() )
42 QString aDocName = aDocument->GetDocPyName();
43 QString aCalculName = GetName();
45 aResList << QString( "%1 = %2.CreateObject( %3 );" )
46 .arg( aCalculName ).arg( aDocName ).arg( PYTHON_CALCULATION_ID );
47 aResList << QString( "%1.SetName( \"%2\" );" )
48 .arg( aCalculName ).arg( aCalculName );
49 aResList << QString( "" );
51 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
52 HYDROData_SequenceOfObjects::Iterator anIter( aGeomObjects );
53 for ( ; anIter.More(); anIter.Next() )
55 Handle(HYDROData_Object) aRefGeomObj =
56 Handle(HYDROData_Object)::DownCast( anIter.Value() );
57 if ( !aRefGeomObj.IsNull() )
58 setPythonReferenceObject( theTreatedObjects, aResList, aRefGeomObj, "AddGeometryObject" );
60 aResList << QString( "" );
62 aResList << QString( "%1.SplitGeometryObjects();" ).arg( aCalculName );
63 aResList << QString( "" );
65 // Now we restore the regions and zones order
66 HYDROData_SequenceOfObjects aRegions = GetRegions();
67 anIter.Init( aRegions );
68 for ( ; anIter.More(); anIter.Next() )
70 Handle(HYDROData_Region) aRegion =
71 Handle(HYDROData_Region)::DownCast( anIter.Value() );
72 if ( aRegion.IsNull() )
75 QString aRegionName = aRegion->GetName();
82 void HYDROData_CalculationCase::SplitGeometryObjects()
84 // At first we remove previously created regions
87 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
88 if ( aDocument.IsNull() )
91 Handle(HYDROData_Polyline) aBoundaryPolyline = GetBoundaryPolyline();
92 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
93 if ( aGeomObjects.IsEmpty() )
96 HYDROData_SplitToZonesTool::SplitDataList aSplitedZones =
97 HYDROData_SplitToZonesTool::SplitToZones( aGeomObjects, aBoundaryPolyline );
98 if ( aSplitedZones.isEmpty() )
101 QString aRegsPref = CALCULATION_REGIONS_PREF;
102 QString aZonesPref = CALCULATION_ZONES_PREF;
104 // Create result regions for case, by default one zone for one region
105 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplitedZones );
106 while( anIter.hasNext() )
108 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
111 Handle(HYDROData_Region) aRegion = addNewRegion();
113 QString aRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
114 aRegion->SetName( aRegionName );
116 // Add the zone for region
117 Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone();
119 QString aZoneName = HYDROData_Tool::GenerateObjectName( aDocument, aZonesPref );
120 aRegionZone->SetName( aZoneName );
122 aRegionZone->SetShape( aSplitData.Face() );
124 // Add the reference object for zone
125 for ( int i = 0, n = aSplitData.ObjectNames.length(); i < n; ++i )
127 const QString& anObjName = aSplitData.ObjectNames.at( i );
129 Handle(HYDROData_Object) aRefObject = Handle(HYDROData_Object)::DownCast(
130 HYDROData_Tool::FindObjectByName( aDocument, anObjName ) );
131 if ( aRefObject.IsNull() )
134 aRegionZone->AddGeometryObject( aRefObject );
138 // The splitted data is up to date
139 SetToUpdate( false );
142 bool HYDROData_CalculationCase::AddGeometryObject( const Handle(HYDROData_Object)& theObject )
144 if ( !HYDROData_Tool::IsGeometryObject( theObject ) )
145 return false; // Wrong type of object
147 if ( HasReference( theObject, DataTag_GeometryObject ) )
148 return false; // Object is already in reference list
150 AddReferenceObject( theObject, DataTag_GeometryObject );
152 // Indicate model of the need to update zones splitting
158 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryObjects() const
160 return GetReferenceObjects( DataTag_GeometryObject );
163 void HYDROData_CalculationCase::RemoveGeometryObject( const Handle(HYDROData_Object)& theObject )
165 if ( theObject.IsNull() )
168 RemoveReferenceObject( theObject->Label(), DataTag_GeometryObject );
170 // Indicate model of the need to update zones splitting
174 void HYDROData_CalculationCase::RemoveGeometryObjects()
176 ClearReferenceObjects( DataTag_GeometryObject );
178 // Indicate model of the need to update zones splitting
182 void HYDROData_CalculationCase::SetBoundaryPolyline( const Handle(HYDROData_Polyline)& thePolyline )
184 Handle(HYDROData_Polyline) aPrevPolyline = GetBoundaryPolyline();
186 SetReferenceObject( thePolyline, DataTag_Polyline );
188 // Indicate model of the need to update zones splitting
189 SetToUpdate( !IsEqual( aPrevPolyline, thePolyline ) || IsMustBeUpdated() );
192 Handle(HYDROData_Polyline) HYDROData_CalculationCase::GetBoundaryPolyline() const
194 return Handle(HYDROData_Polyline)::DownCast(
195 GetReferenceObject( DataTag_Polyline ) );
198 void HYDROData_CalculationCase::RemoveBoundaryPolyline()
200 Handle(HYDROData_Polyline) aPrevPolyline = GetBoundaryPolyline();
202 ClearReferenceObjects( DataTag_Polyline );
204 // Indicate model of the need to update zones splitting
205 SetToUpdate( !aPrevPolyline.IsNull() || IsMustBeUpdated() );
208 Handle(HYDROData_Region) HYDROData_CalculationCase::AddNewRegion( const Handle(HYDROData_Zone)& theZone )
210 Handle(HYDROData_Region) aNewRegion = addNewRegion();
211 if ( aNewRegion.IsNull() )
214 // Generate new name for new region
215 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
216 if ( !aDocument.IsNull() )
218 QString aRegsPref = CALCULATION_REGIONS_PREF;
220 QString aNewRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
221 aNewRegion->SetName( aNewRegionName );
224 aNewRegion->AddZone( theZone );
229 bool HYDROData_CalculationCase::AddRegion( const Handle(HYDROData_Region)& theRegion )
231 if ( theRegion.IsNull() )
234 if ( HasReference( theRegion, DataTag_Region ) )
235 return false; // Object is already in reference list
237 // Move the region from other calculation
238 Handle(HYDROData_CalculationCase) aFatherCalc =
239 Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
240 if ( !aFatherCalc.IsNull() && aFatherCalc->Label() != myLab )
242 Handle(HYDROData_Region) aNewRegion = addNewRegion();
243 theRegion->CopyTo( aNewRegion );
245 aFatherCalc->RemoveRegion( theRegion );
247 theRegion->SetLabel( aNewRegion->Label() );
251 AddReferenceObject( theRegion, DataTag_Region );
257 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetRegions() const
259 return GetReferenceObjects( DataTag_Region );
262 void HYDROData_CalculationCase::UpdateRegionsOrder()
264 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
265 if ( aDocument.IsNull() )
268 HYDROData_SequenceOfObjects aRegions = GetRegions();
270 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
271 for ( ; anIter.More(); anIter.Next() )
273 Handle(HYDROData_Region) aRegion =
274 Handle(HYDROData_Region)::DownCast( anIter.Value() );
275 if ( aRegion.IsNull() )
278 aRegion->SetName( "" );
281 QString aRegsPref = CALCULATION_REGIONS_PREF;
283 anIter.Init( aRegions );
284 for ( ; anIter.More(); anIter.Next() )
286 Handle(HYDROData_Region) aRegion =
287 Handle(HYDROData_Region)::DownCast( anIter.Value() );
288 if ( aRegion.IsNull() )
291 QString aRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
292 aRegion->SetName( aRegionName );
296 void HYDROData_CalculationCase::RemoveRegion( const Handle(HYDROData_Region)& theRegion )
298 if ( theRegion.IsNull() )
301 RemoveReferenceObject( theRegion->Label(), DataTag_Region );
303 // Remove region from data model
304 Handle(HYDROData_CalculationCase) aFatherCalc =
305 Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
306 if ( !aFatherCalc.IsNull() && aFatherCalc->Label() == myLab )
310 void HYDROData_CalculationCase::RemoveRegions()
312 ClearReferenceObjects( DataTag_Region );
313 myLab.FindChild( DataTag_ChildRegion ).ForgetAllAttributes( true );
316 Handle(HYDROData_Region) HYDROData_CalculationCase::addNewRegion()
318 TDF_Label aNewLab = myLab.FindChild( DataTag_ChildRegion ).NewChild();
320 Handle(HYDROData_Region) aNewRegion =
321 Handle(HYDROData_Region)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_REGION ) );
322 AddRegion( aNewRegion );
327 TopoDS_Shell HYDROData_CalculationCase::GetShell()
332 BRep_Builder aBuilder;
333 aBuilder.MakeShell( aShell );
335 bool isShellEmpty = true;
337 // Make shell containing all region shapes
338 HYDROData_SequenceOfObjects aCaseRegions = GetRegions();
339 HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
340 for ( ; aRegionIter.More(); aRegionIter.Next() ) {
341 Handle(HYDROData_Region) aRegion =
342 Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
343 if( aRegion.IsNull() ) {
347 TopoDS_Shape aRegionShape = aRegion->GetShape();
349 // Add shape (face or shell) corresponding to the region into the shell
350 if( !aRegionShape.IsNull() ) {
351 aBuilder.Add( aShell, aRegionShape );
352 isShellEmpty = false;
354 } // regions iterator
356 // Check if no shapes were added to the shell
357 if ( isShellEmpty ) {