2 #include "HYDROData_CalculationCase.h"
4 #include "HYDROData_ArtificialObject.h"
5 #include "HYDROData_IAltitudeObject.h"
6 #include "HYDROData_Document.h"
7 #include "HYDROData_ShapesGroup.h"
8 #include "HYDROData_Iterator.h"
9 #include "HYDROData_NaturalObject.h"
10 #include "HYDROData_PolylineXY.h"
11 #include "HYDROData_SplittedShapesGroup.h"
12 #include "HYDROData_Region.h"
13 #include "HYDROData_Tool.h"
20 #include <TopoDS_Shell.hxx>
21 #include <TopoDS_Edge.hxx>
23 #include <BRep_Builder.hxx>
24 #include <BRepBuilderAPI_Sewing.hxx>
25 #include <BRepTopAdaptor_FClass2d.hxx>
27 #include <BRepTools.hxx>
30 #include <TopExp_Explorer.hxx>
32 #include <TopTools_ListOfShape.hxx>
33 #include <TopTools_ListIteratorOfListOfShape.hxx>
34 #include <TDataStd_Integer.hxx>
36 //#define DEB_CALCULATION 1
37 #ifdef DEB_CALCULATION
38 #include <BRepTools.hxx>
39 #include <TopLoc_Location.hxx>
41 #define CALCULATION_REGIONS_PREF GetName() + "_Reg"
42 #define CALCULATION_ZONES_PREF GetName() + "_Zone"
43 #define CALCULATION_GROUPS_PREF GetName() + "_"
44 //#define DEB_CLASS2D 1
46 #include <BRepBuilderAPI_MakeVertex.hxx>
49 #define EXPORT_NAME "HYDRO_" + GetName()
51 IMPLEMENT_STANDARD_HANDLE(HYDROData_CalculationCase, HYDROData_Entity)
52 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_CalculationCase, HYDROData_Entity)
54 HYDROData_CalculationCase::HYDROData_CalculationCase()
59 HYDROData_CalculationCase::~HYDROData_CalculationCase()
63 void HYDROData_CalculationCase::SetName( const QString& theName )
65 QString anOldCaseName = GetName();
66 if ( anOldCaseName != theName )
68 HYDROData_SequenceOfObjects aRegions = GetRegions();
70 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
71 for ( ; anIter.More(); anIter.Next() )
73 Handle(HYDROData_Region) aRegion =
74 Handle(HYDROData_Region)::DownCast( anIter.Value() );
75 if ( aRegion.IsNull() )
78 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aRegion );
80 HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
81 HYDROData_SequenceOfObjects::Iterator anIter( aZones );
82 for ( ; anIter.More(); anIter.Next() )
84 Handle(HYDROData_Zone) aRegZone =
85 Handle(HYDROData_Zone)::DownCast( anIter.Value() );
86 if ( aRegZone.IsNull() )
89 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aRegZone );
93 HYDROData_SequenceOfObjects aGroups = GetSplittedGroups();
95 anIter.Init( aGroups );
96 for ( ; anIter.More(); anIter.Next() )
98 Handle(HYDROData_SplittedShapesGroup) aGroup =
99 Handle(HYDROData_SplittedShapesGroup)::DownCast( anIter.Value() );
100 if ( aGroup.IsNull() )
103 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aGroup );
107 HYDROData_Entity::SetName( theName );
110 QStringList HYDROData_CalculationCase::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
112 QStringList aResList = dumpObjectCreation( theTreatedObjects );
114 QString aCalculName = GetObjPyName();
116 AssignmentMode aMode = GetAssignmentMode();
117 QString aModeStr = aMode==MANUAL ? "HYDROData_CalculationCase.MANUAL" : "HYDROData_CalculationCase.AUTOMATIC";
118 aResList << QString( "%0.SetAssignmentMode( %1 )" ).arg( aCalculName ).arg( aModeStr );
120 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
121 HYDROData_SequenceOfObjects::Iterator anIter( aGeomObjects );
122 for ( ; anIter.More(); anIter.Next() )
124 Handle(HYDROData_Object) aRefGeomObj =
125 Handle(HYDROData_Object)::DownCast( anIter.Value() );
126 setPythonReferenceObject( theTreatedObjects, aResList, aRefGeomObj, "AddGeometryObject" );
128 aResList << QString( "" );
130 QString aGroupName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "case_geom_group" );
132 HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
133 anIter.Init( aGeomGroups );
134 for ( ; anIter.More(); anIter.Next() )
136 Handle(HYDROData_ShapesGroup) aGeomGroup =
137 Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
138 if ( aGeomGroup.IsNull() )
141 Handle(HYDROData_Object) aFatherGeom =
142 Handle(HYDROData_Object)::DownCast( aGeomGroup->GetFatherObject() );
143 if ( aFatherGeom.IsNull() )
146 int aGroupId = aFatherGeom->GetGroupId( aGeomGroup );
147 aResList << QString( "%1 = %2.GetGroup( %3 );" )
148 .arg( aGroupName ).arg( aFatherGeom->GetObjPyName() ).arg( aGroupId );
150 aResList << QString( "%1.AddGeometryGroup( %2 );" ).arg( aCalculName ).arg( aGroupName );
152 aResList << QString( "" );
154 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
155 setPythonReferenceObject( theTreatedObjects, aResList, aBoundaryPolyline, "SetBoundaryPolyline" );
156 aResList << QString( "" );
158 if( aMode==AUTOMATIC )
159 DumpRulesToPython( aCalculName, aResList );
161 aResList << QString( "" );
162 aResList << QString( "%1.Update();" ).arg( aCalculName );
163 aResList << QString( "" );
167 // Now we restore the regions and zones order
168 HYDROData_SequenceOfObjects aRegions = GetRegions();
169 anIter.Init( aRegions );
170 for ( ; anIter.More(); anIter.Next() )
172 Handle(HYDROData_Region) aRegion =
173 Handle(HYDROData_Region)::DownCast( anIter.Value() );
174 if ( aRegion.IsNull() )
177 QString aRegionName = aRegion->GetName();
179 HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
180 HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
181 for ( ; aZonesIter.More(); aZonesIter.Next() )
183 Handle(HYDROData_Zone) aRegZone =
184 Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
185 if ( aRegZone.IsNull() )
193 aResList << QString( "" );
197 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetAllReferenceObjects() const
199 HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
201 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
202 if ( !aBoundaryPolyline.IsNull() )
203 aResSeq.Append( aBoundaryPolyline );
205 HYDROData_SequenceOfObjects aSeqOfRegions = GetRegions();
206 aResSeq.Append( aSeqOfRegions );
211 void HYDROData_CalculationCase::Update()
213 HYDROData_Entity::Update();
216 // At first we remove previously created objects
218 RemoveSplittedGroups();
220 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
221 if ( aDocument.IsNull() )
224 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
225 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
226 if ( aGeomObjects.IsEmpty() )
229 HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
231 HYDROData_SplitToZonesTool::SplitDataList aSplitObjects =
232 HYDROData_SplitToZonesTool::Split( aGeomObjects, aGeomGroups, aBoundaryPolyline );
233 if ( aSplitObjects.isEmpty() )
236 HYDROData_SplitToZonesTool::SplitDataList aZonesList, anEdgesList;
238 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplitObjects );
239 while( anIter.hasNext() )
241 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
242 if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Zone )
243 aZonesList.append( aSplitData );
244 else if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Edge )
245 anEdgesList.append( aSplitData );
248 switch( GetAssignmentMode() )
251 CreateRegionsDef( aDocument, aZonesList );
254 CreateRegionsAuto( aDocument, aZonesList );
257 CreateEdgeGroupsDef( aDocument, anEdgesList );
260 void HYDROData_CalculationCase::CreateRegionsDef( const Handle(HYDROData_Document)& theDoc,
261 const HYDROData_SplitToZonesTool::SplitDataList& theZones )
263 // Create result regions for case, by default one zone for one region
264 QString aRegsPref = CALCULATION_REGIONS_PREF;
265 QString aZonesPref = CALCULATION_ZONES_PREF;
267 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theZones );
268 while( anIter.hasNext() )
270 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
272 Handle(HYDROData_Region) aRegion = addNewRegion( theDoc, aRegsPref );
274 // Add the zone for region
275 Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( theDoc, aZonesPref, aSplitData.Face(), aSplitData.ObjectNames );
279 void HYDROData_CalculationCase::CreateRegionsAuto( const Handle(HYDROData_Document)& theDoc,
280 const HYDROData_SplitToZonesTool::SplitDataList& theZones )
282 QMap<QString, Handle(HYDROData_Region)> aRegionsMap; //object name to region
283 QMap<QString, QString> aRegionNameToObjNameMap;
284 QString aZonesPref = CALCULATION_ZONES_PREF;
285 HYDROData_PriorityQueue aPr( this );
287 // 1. First we create a default region for each object included into the calculation case
288 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
289 for( int i=aGeomObjects.Lower(), n=aGeomObjects.Upper(); i<=n; i++ )
291 Handle(HYDROData_Object) anObj = Handle(HYDROData_Object)::DownCast( aGeomObjects.Value( i ) );
294 QString anObjName = anObj->GetName();
295 QString aRegName = anObjName + "_reg";
296 Handle(HYDROData_Region) aRegion = addNewRegion( theDoc, aRegName, false );
297 aRegionsMap.insert( anObjName, aRegion );
298 aRegionNameToObjNameMap.insert( aRegName, anObjName );
301 // 2. Now for each zone it is necessary to determine the most priority object
302 // and assign to zone to corresponding region
303 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theZones );
304 while( anIter.hasNext() )
306 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
307 HYDROData_Zone::MergeAltitudesType aMergeType;
308 Handle(HYDROData_Object) aRegObj = aPr.GetMostPriorityObject( aSplitData.ObjectNames, aMergeType );
309 if( aRegObj.IsNull() )
311 Handle(HYDROData_Region) aRegion = aRegionsMap[aRegObj->GetName()];
312 if( aRegion.IsNull() )
314 Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( theDoc, aZonesPref, aSplitData.Face(), aSplitData.ObjectNames );
316 if( aSplitData.ObjectNames.count() > 1 && aMergeType==HYDROData_Zone::Merge_UNKNOWN )
318 qDebug( "Error in algorithm: unresolved conflicts" );
323 case HYDROData_Zone::Merge_ZMIN:
324 case HYDROData_Zone::Merge_ZMAX:
325 aRegionZone->SetMergeType( aMergeType );
327 case HYDROData_Zone::Merge_Object:
328 aRegionZone->SetMergeType( aMergeType );
329 aRegionZone->RemoveMergeAltitude();
330 aRegionZone->SetMergeAltitude( aRegObj->GetAltitudeObject() );
335 QStringList anObjectsWithEmptyRegions;
336 QMap<QString, Handle(HYDROData_Region)>::const_iterator
337 anIt = aRegionsMap.begin(), aLast = aRegionsMap.end();
338 for( ; anIt!=aLast; anIt++ )
340 Handle(HYDROData_Region) aRegion = anIt.value();
341 if( aRegion->GetZones().IsEmpty() )
343 QString aRegName = aRegion->GetName();
344 QString anObjName = aRegionNameToObjNameMap[aRegName];
345 anObjectsWithEmptyRegions.append( anObjName );
349 if( !anObjectsWithEmptyRegions.empty() )
351 QString aData = anObjectsWithEmptyRegions.join( ", " );
352 SetWarning( WARN_EMPTY_REGIONS, aData );
356 void HYDROData_CalculationCase::CreateEdgeGroupsDef( const Handle(HYDROData_Document)& theDoc,
357 const HYDROData_SplitToZonesTool::SplitDataList& theEdges )
359 QMap<QString,Handle(HYDROData_SplittedShapesGroup)> aSplittedEdgesGroupsMap;
361 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theEdges );
362 while( anIter.hasNext() )
364 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
365 // Create new edges group
366 if ( aSplitData.ObjectNames.isEmpty() || aSplitData.Shape.IsNull() )
369 QString anObjName = aSplitData.ObjectNames.first();
370 if ( anObjName.isEmpty() )
372 #ifdef DEB_CALCULATION
373 QString aStr = aSplitData.ObjectNames.join(" ");
374 cout << " CCase: Names = "<<aStr.toStdString() << " size = " <<aSplitData.ObjectNames.size() <<endl;
376 Handle(HYDROData_SplittedShapesGroup) aSplittedGroup;
377 if ( !aSplittedEdgesGroupsMap.contains( anObjName ) )
379 aSplittedGroup = addNewSplittedGroup( CALCULATION_GROUPS_PREF + anObjName );
380 aSplittedEdgesGroupsMap.insert( anObjName, aSplittedGroup );
384 aSplittedGroup = aSplittedEdgesGroupsMap[ anObjName ];
386 if ( aSplittedGroup.IsNull() )
389 aSplittedGroup->AddShape( aSplitData.Shape );
393 bool HYDROData_CalculationCase::AddGeometryObject( const Handle(HYDROData_Object)& theObject )
395 if ( !HYDROData_Tool::IsGeometryObject( theObject ) )
396 return false; // Wrong type of object
398 if ( HasReference( theObject, DataTag_GeometryObject ) )
399 return false; // Object is already in reference list
401 AddReferenceObject( theObject, DataTag_GeometryObject );
403 // Indicate model of the need to update splitting
409 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryObjects() const
411 return GetReferenceObjects( DataTag_GeometryObject );
414 void HYDROData_CalculationCase::RemoveGeometryObject( const Handle(HYDROData_Object)& theObject )
416 if ( theObject.IsNull() )
419 RemoveReferenceObject( theObject->Label(), DataTag_GeometryObject );
421 // Indicate model of the need to update splitting
425 void HYDROData_CalculationCase::RemoveGeometryObjects()
427 ClearReferenceObjects( DataTag_GeometryObject );
429 // Indicate model of the need to update splitting
433 bool HYDROData_CalculationCase::AddGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
435 if ( theGroup.IsNull() )
438 if ( HasReference( theGroup, DataTag_GeometryGroup ) )
439 return false; // Object is already in reference list
441 AddReferenceObject( theGroup, DataTag_GeometryGroup );
443 // Indicate model of the need to update splitting
449 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryGroups() const
451 return GetReferenceObjects( DataTag_GeometryGroup );
454 void HYDROData_CalculationCase::RemoveGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
456 if ( theGroup.IsNull() )
459 RemoveReferenceObject( theGroup->Label(), DataTag_GeometryGroup );
461 // Indicate model of the need to update splitting
465 void HYDROData_CalculationCase::RemoveGeometryGroups()
467 ClearReferenceObjects( DataTag_GeometryGroup );
469 // Indicate model of the need to update splitting
473 void HYDROData_CalculationCase::SetBoundaryPolyline( const Handle(HYDROData_PolylineXY)& thePolyline )
475 Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
477 SetReferenceObject( thePolyline, DataTag_Polyline );
479 // Indicate model of the need to update zones splitting
480 SetToUpdate( !IsEqual( aPrevPolyline, thePolyline ) || IsMustBeUpdated() );
483 Handle(HYDROData_PolylineXY) HYDROData_CalculationCase::GetBoundaryPolyline() const
485 return Handle(HYDROData_PolylineXY)::DownCast(
486 GetReferenceObject( DataTag_Polyline ) );
489 void HYDROData_CalculationCase::RemoveBoundaryPolyline()
491 Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
493 ClearReferenceObjects( DataTag_Polyline );
495 // Indicate model of the need to update zones splitting
496 SetToUpdate( !aPrevPolyline.IsNull() || IsMustBeUpdated() );
499 Handle(HYDROData_Region) HYDROData_CalculationCase::AddNewRegion( const Handle(HYDROData_Zone)& theZone )
501 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
502 Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
503 if ( aNewRegion.IsNull() )
506 aNewRegion->AddZone( theZone );
511 bool HYDROData_CalculationCase::AddRegion( const Handle(HYDROData_Region)& theRegion )
513 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
515 if ( theRegion.IsNull() )
518 if ( HasReference( theRegion, DataTag_Region ) )
519 return false; // Object is already in reference list
521 // Move the region from other calculation
522 Handle(HYDROData_CalculationCase) aFatherCalc =
523 Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
524 if ( !aFatherCalc.IsNull() && aFatherCalc->Label() != myLab )
526 Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
527 theRegion->CopyTo( aNewRegion );
529 aFatherCalc->RemoveRegion( theRegion );
531 theRegion->SetLabel( aNewRegion->Label() );
535 AddReferenceObject( theRegion, DataTag_Region );
541 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetRegions() const
543 return GetReferenceObjects( DataTag_Region );
546 void HYDROData_CalculationCase::UpdateRegionsOrder()
548 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
549 if ( aDocument.IsNull() )
552 HYDROData_SequenceOfObjects aRegions = GetRegions();
554 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
555 for ( ; anIter.More(); anIter.Next() )
557 Handle(HYDROData_Region) aRegion =
558 Handle(HYDROData_Region)::DownCast( anIter.Value() );
559 if ( aRegion.IsNull() )
562 aRegion->SetName( "" );
565 QString aRegsPref = CALCULATION_REGIONS_PREF;
567 anIter.Init( aRegions );
568 for ( ; anIter.More(); anIter.Next() )
570 Handle(HYDROData_Region) aRegion =
571 Handle(HYDROData_Region)::DownCast( anIter.Value() );
572 if ( aRegion.IsNull() )
575 QString aRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
576 aRegion->SetName( aRegionName );
580 void HYDROData_CalculationCase::RemoveRegion( const Handle(HYDROData_Region)& theRegion )
582 if ( theRegion.IsNull() )
585 RemoveReferenceObject( theRegion->Label(), DataTag_Region );
587 // Remove region from data model
588 Handle(HYDROData_CalculationCase) aFatherCalc =
589 Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
590 if ( !aFatherCalc.IsNull() && aFatherCalc->Label() == myLab )
594 void HYDROData_CalculationCase::RemoveRegions()
596 myLab.FindChild( DataTag_ChildRegion ).ForgetAllAttributes();
599 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetSplittedGroups() const
601 return GetReferenceObjects( DataTag_SplittedGroups );
604 void HYDROData_CalculationCase::RemoveSplittedGroups()
606 myLab.FindChild( DataTag_SplittedGroups ).ForgetAllAttributes();
609 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint ) const
611 Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
612 return GetAltitudeForPoint( thePoint, aZone );
615 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint,
616 const Handle(HYDROData_Region)& theRegion ) const
618 double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
620 Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
621 if ( !aZone.IsNull() )
623 Handle(HYDROData_Region) aRefRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
624 if ( IsEqual( aRefRegion, theRegion ) )
625 aResAltitude = GetAltitudeForPoint( thePoint, aZone );
631 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint,
632 const Handle(HYDROData_Zone)& theZone ) const
634 double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
635 if ( theZone.IsNull() )
638 HYDROData_Zone::MergeAltitudesType aZoneMergeType = theZone->GetMergeType();
639 if ( !theZone->IsMergingNeed() )
641 aZoneMergeType = HYDROData_Zone::Merge_UNKNOWN;
643 else if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
648 HYDROData_IInterpolator* aZoneInterpolator = theZone->GetInterpolator();
649 if ( aZoneMergeType == HYDROData_Zone::Merge_Object )
651 Handle(HYDROData_IAltitudeObject) aMergeAltitude = theZone->GetMergeAltitude();
652 if ( !aMergeAltitude.IsNull() )
654 if ( aZoneInterpolator != NULL )
656 aZoneInterpolator->SetAltitudeObject( aMergeAltitude );
657 aResAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
660 aResAltitude = aMergeAltitude->GetAltitudeForPoint( thePoint );
665 HYDROData_SequenceOfObjects aZoneObjects = theZone->GetGeometryObjects();
666 HYDROData_SequenceOfObjects::Iterator anIter( aZoneObjects );
667 for ( ; anIter.More(); anIter.Next() )
669 Handle(HYDROData_Object) aZoneObj =
670 Handle(HYDROData_Object)::DownCast( anIter.Value() );
671 if ( aZoneObj.IsNull() )
674 Handle(HYDROData_IAltitudeObject) anObjAltitude = aZoneObj->GetAltitudeObject();
675 if ( anObjAltitude.IsNull() )
678 double aPointAltitude = 0.0;
679 if ( aZoneInterpolator != NULL )
681 aZoneInterpolator->SetAltitudeObject( anObjAltitude );
682 aPointAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
685 aPointAltitude = anObjAltitude->GetAltitudeForPoint( thePoint );
687 if ( ValuesEquals( aPointAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) )
690 if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
692 aResAltitude = aPointAltitude;
695 else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMIN )
697 if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
698 aResAltitude > aPointAltitude )
700 aResAltitude = aPointAltitude;
703 else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMAX )
705 if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
706 aResAltitude < aPointAltitude )
708 aResAltitude = aPointAltitude;
717 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints(
718 const NCollection_Sequence<gp_XY>& thePoints,
719 const Handle(HYDROData_Region)& theRegion ) const
721 NCollection_Sequence<double> aResSeq;
723 for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
725 const gp_XY& thePnt = thePoints.Value( i );
727 double anAltitude = GetAltitudeForPoint( thePnt, theRegion );
728 aResSeq.Append( anAltitude );
734 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints(
735 const NCollection_Sequence<gp_XY>& thePoints,
736 const Handle(HYDROData_Zone)& theZone ) const
738 NCollection_Sequence<double> aResSeq;
740 for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
742 const gp_XY& thePnt = thePoints.Value( i );
744 double anAltitude = GetAltitudeForPoint( thePnt, theZone );
745 aResSeq.Append( anAltitude );
751 Handle(HYDROData_Region) HYDROData_CalculationCase::GetRegionFromPoint( const gp_XY& thePoint ) const
753 Handle(HYDROData_Region) aResRegion;
755 Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
756 if ( !aZone.IsNull() )
757 aResRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
762 Handle(HYDROData_Zone) HYDROData_CalculationCase::GetZoneFromPoint( const gp_XY& thePoint ) const
764 Handle(HYDROData_Zone) aResZone;
766 HYDROData_SequenceOfObjects aRegions = GetRegions();
768 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
769 for ( ; anIter.More() && aResZone.IsNull(); anIter.Next() )
771 Handle(HYDROData_Region) aRegion =
772 Handle(HYDROData_Region)::DownCast( anIter.Value() );
773 if ( aRegion.IsNull() )
776 HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
777 HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
778 for ( ; aZonesIter.More() && aResZone.IsNull(); aZonesIter.Next() )
780 Handle(HYDROData_Zone) aRegZone =
781 Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
782 if ( aRegZone.IsNull() )
785 PointClassification aPointRelation = GetPointClassification( thePoint, aRegZone );
786 if ( aPointRelation != POINT_OUT )
787 aResZone = aRegZone; // We found the desired zone
794 HYDROData_CalculationCase::PointClassification HYDROData_CalculationCase::GetPointClassification(
795 const gp_XY& thePoint,
796 const Handle(HYDROData_Zone)& theZone ) const
798 PointClassification aRes = POINT_OUT;
799 if ( theZone.IsNull() )
802 TopoDS_Face aZoneFace = TopoDS::Face( theZone->GetShape() );
803 if ( aZoneFace.IsNull() )
806 TopoDS_Compound aCmp;
808 aBB.MakeCompound(aCmp);
809 aBB.Add(aCmp, aZoneFace);
810 gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
811 BRepBuilderAPI_MakeVertex aMk(aPnt);
812 aBB.Add(aCmp, aMk.Vertex());
813 BRepTools::Write(aCmp, "FCL2d.brep");
815 TopAbs_State State = HYDROData_Tool::ComputePointState(thePoint, aZoneFace);
816 if (State == TopAbs_OUT)
818 else if(State == TopAbs_IN)
820 else if(State == TopAbs_ON)
825 Handle(HYDROData_Region) HYDROData_CalculationCase::addNewRegion( const Handle(HYDROData_Document)& theDoc,
826 const QString& thePrefixOrName,
829 TDF_Label aNewLab = myLab.FindChild( DataTag_ChildRegion ).NewChild();
831 Handle(HYDROData_Region) aNewRegion =
832 Handle(HYDROData_Region)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_REGION ) );
833 AddRegion( aNewRegion );
835 QString aRegionName = isPrefix ? HYDROData_Tool::GenerateObjectName( theDoc, thePrefixOrName ) : thePrefixOrName;
836 aNewRegion->SetName( aRegionName );
841 Handle(HYDROData_SplittedShapesGroup) HYDROData_CalculationCase::addNewSplittedGroup( const QString& theName )
843 TDF_Label aNewLab = myLab.FindChild( DataTag_SplittedGroups ).NewChild();
845 Handle(HYDROData_SplittedShapesGroup) aNewGroup =
846 Handle(HYDROData_SplittedShapesGroup)::DownCast(
847 HYDROData_Iterator::CreateObject( aNewLab, KIND_SPLITTED_GROUP ) );
848 AddReferenceObject( aNewGroup, DataTag_SplittedGroups );
850 aNewGroup->SetName( theName );
855 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var theGeomEngine,
856 SALOMEDS::Study_ptr theStudy ) const
858 HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroupsDefs;
860 // Get groups definitions
861 HYDROData_SequenceOfObjects aSplittedGroups = GetSplittedGroups();
863 HYDROData_SequenceOfObjects::Iterator anIter( aSplittedGroups );
864 for ( ; anIter.More(); anIter.Next() )
867 Handle(HYDROData_ShapesGroup) aGroup =
868 Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
869 if ( aGroup.IsNull() )
872 HYDROData_ShapesGroup::GroupDefinition aGroupDef;
874 aGroupDef.Name = aGroup->GetName().toLatin1().constData();
875 aGroup->GetShapes( aGroupDef.Shapes );
877 aSeqOfGroupsDefs.Append( aGroupDef );
881 TopTools_ListOfShape aFaces;
882 HYDROData_SequenceOfObjects aCaseRegions = GetRegions();
883 HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
884 for ( ; aRegionIter.More(); aRegionIter.Next() )
886 Handle(HYDROData_Region) aRegion =
887 Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
888 if( aRegion.IsNull() )
891 TopoDS_Shape aRegionShape = aRegion->GetShape( &aSeqOfGroupsDefs );
892 aFaces.Append( aRegionShape );
895 return Export( theGeomEngine, theStudy, aFaces, aSeqOfGroupsDefs );
898 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var theGeomEngine,
899 SALOMEDS::Study_ptr theStudy,
900 const TopTools_ListOfShape& theFaces,
901 const HYDROData_ShapesGroup::SeqOfGroupsDefs& theGroupsDefs ) const
904 BRepBuilderAPI_Sewing aSewing( Precision::Confusion() * 10.0 );
905 aSewing.SetNonManifoldMode( Standard_False );
906 #ifdef DEB_CALCULATION
907 TCollection_AsciiString aNam("Sh_");
910 TopTools_ListIteratorOfListOfShape aFaceIter( theFaces );
911 for ( ; aFaceIter.More(); aFaceIter.Next() )
913 TopoDS_Shape aShape = aFaceIter.Value();
914 if ( aShape.IsNull() )
917 if ( aShape.ShapeType() == TopAbs_FACE )
919 aSewing.Add( aShape );
920 #ifdef DEB_CALCULATION
921 TCollection_AsciiString aName = aNam + ++i + ".brep";
922 BRepTools::Write(aShape ,aName.ToCString());
927 #ifdef DEB_CALCULATION
930 TopExp_Explorer anExp( aShape, TopAbs_FACE );
931 for (; anExp.More(); anExp.Next() ) {
932 aSewing.Add( anExp.Current() );
933 #ifdef DEB_CALCULATION
935 TCollection_AsciiString aName = aNam + i + "_" + ++j + ".brep";
936 BRepTools::Write(anExp.Current() ,aName.ToCString());
943 TopoDS_Shape aSewedShape = aSewing.SewedShape();
945 // If the sewed shape is empty - return false
946 if ( aSewedShape.IsNull() || !TopoDS_Iterator( aSewedShape ).More() )
949 #ifdef DEB_CALCULATION
950 BRepTools::Write(aSewedShape ,"Sew.brep");
952 // Publish the sewed shape
953 QString aName = EXPORT_NAME;
954 GEOM::GEOM_Object_ptr aMainShape =
955 publishShapeInGEOM( theGeomEngine, theStudy, aSewedShape, aName );
957 if ( aMainShape->_is_nil() )
960 if ( theGroupsDefs.IsEmpty() )
964 TopTools_IndexedMapOfShape aMapOfSubShapes;
965 TopExp::MapShapes( aSewedShape, aMapOfSubShapes );
967 NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> > aGroupsData;
969 for ( int aGrId = 1, nbGroups = theGroupsDefs.Length(); aGrId <= nbGroups; ++aGrId )
971 const HYDROData_ShapesGroup::GroupDefinition& aGroupDef = theGroupsDefs.Value( aGrId );
973 NCollection_Sequence<int> aGroupIndexes;
974 for( int i = 1, n = aGroupDef.Shapes.Length(); i <= n; i++ )
976 const TopoDS_Shape& aShape = aGroupDef.Shapes.Value( i );
977 #ifdef DEB_CALCULATION
978 cout << "\nOld shape(" << i << ") = " << aShape.TShape() <<endl;
981 TopoDS_Shape aModifiedShape = aShape;
982 if ( aSewing.IsModified( aShape ) )
983 aModifiedShape = aSewing.Modified( aShape );
984 else if ( aSewing.IsModifiedSubShape( aShape ) )
985 aModifiedShape = aSewing.ModifiedSubShape( aShape );
987 #ifdef DEB_CALCULATION
988 const TopLoc_Location& aL1 = aShape.Location();
989 const TopLoc_Location& aL2 = aModifiedShape.Location();
990 cout << "\nNew shape(" << i << ") = " << aModifiedShape.TShape() << " Location is Equal = " << aL1.IsEqual(aL2)<<endl;
993 int anIndex = aMapOfSubShapes.FindIndex(aModifiedShape);
995 aGroupIndexes.Append( anIndex );
997 #ifdef DEB_CALCULATION
998 TCollection_AsciiString aNam("Lost_");
999 if(!aMapOfSubShapes.Contains(aModifiedShape)) {
1000 for ( int anIndex = 1; anIndex <= aMapOfSubShapes.Extent(); anIndex++ )
1002 const TopoDS_Shape& aS = aMapOfSubShapes.FindKey( anIndex );
1003 if ( aModifiedShape.IsPartner( aS ) )
1005 cout <<"\nIndex in Map = " << anIndex << "TShape = " << aS.TShape() <<endl;
1006 TCollection_AsciiString aName = aNam + i + "_" + anIndex + ".brep";
1007 BRepTools::Write(aS ,aName.ToCString());
1015 if ( !aGroupIndexes.IsEmpty() )
1016 aGroupsData.Bind( aGroupDef.Name, aGroupIndexes );
1019 if ( !aGroupsData.IsEmpty() )
1021 GEOM::GEOM_IGroupOperations_var aGroupOp =
1022 theGeomEngine->GetIGroupOperations( theStudy->StudyId() );
1024 NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> >::Iterator aMapIt( aGroupsData );
1025 for ( ; aMapIt.More(); aMapIt.Next() )
1027 const TCollection_AsciiString& aGroupName = aMapIt.Key();
1028 const NCollection_Sequence<int>& aGroupIndexes = aMapIt.Value();
1030 GEOM::GEOM_Object_var aGeomGroup = aGroupOp->CreateGroup( aMainShape, TopAbs_EDGE );
1031 if ( CORBA::is_nil( aGeomGroup ) || !aGroupOp->IsDone() )
1034 GEOM::ListOfLong_var aGeomIndexes = new GEOM::ListOfLong;
1035 aGeomIndexes->length( aGroupIndexes.Length() );
1037 for( int i = 1, n = aGroupIndexes.Length(); i <= n; i++ )
1038 aGeomIndexes[ i - 1 ] = aGroupIndexes.Value( i );
1040 aGroupOp->UnionIDs( aGeomGroup, aGeomIndexes );
1041 if ( aGroupOp->IsDone() )
1043 SALOMEDS::SObject_var aGroupSO =
1044 theGeomEngine->AddInStudy( theStudy, aGeomGroup, aGroupName.ToCString(), aMainShape );
1052 GEOM::GEOM_Object_ptr HYDROData_CalculationCase::publishShapeInGEOM(
1053 GEOM::GEOM_Gen_var theGeomEngine, SALOMEDS::Study_ptr theStudy,
1054 const TopoDS_Shape& theShape, const QString& theName ) const
1056 GEOM::GEOM_Object_var aGeomObj;
1058 if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ||
1059 theShape.IsNull() ) {
1060 return aGeomObj._retn();
1063 std::ostringstream aStreamShape;
1064 // Write TopoDS_Shape in ASCII format to the stream
1065 BRepTools::Write( theShape, aStreamShape );
1066 // Returns the number of bytes that have been stored in the stream's buffer.
1067 int aSize = aStreamShape.str().size();
1068 // Allocate octect buffer of required size
1069 CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize );
1070 // Copy ostrstream content to the octect buffer
1071 memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize );
1073 SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
1075 // Restore shape from the stream and get the GEOM object
1076 GEOM::GEOM_IInsertOperations_var anInsOp = theGeomEngine->GetIInsertOperations( theStudy->StudyId() );
1077 aGeomObj = anInsOp->RestoreShape( aSeqFile );
1079 // Puplish the GEOM object
1080 if ( !aGeomObj->_is_nil() ) {
1081 QString aName = GEOMBase::GetDefaultName( theName );
1083 SALOMEDS::SObject_var aResultSO =
1084 theGeomEngine->PublishInStudy( theStudy, SALOMEDS::SObject::_nil(),
1085 aGeomObj, qPrintable( aName ) );
1086 if ( aResultSO->_is_nil() ) {
1087 aGeomObj = GEOM::GEOM_Object::_nil();
1091 return aGeomObj._retn();
1094 void HYDROData_CalculationCase::ClearRules( const bool theIsSetToUpdate )
1096 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1097 HYDROData_PriorityQueue::ClearRules( aRulesLab );
1099 // Indicate model of the need to update splitting
1100 if ( theIsSetToUpdate ) {
1101 SetToUpdate( true );
1105 void HYDROData_CalculationCase::AddRule( const Handle(HYDROData_Object)& theObject1,
1106 HYDROData_PriorityType thePriority,
1107 const Handle(HYDROData_Object)& theObject2,
1108 HYDROData_Zone::MergeAltitudesType theMergeType )
1110 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1111 HYDROData_PriorityQueue::AddRule( aRulesLab, theObject1, thePriority, theObject2, theMergeType );
1113 // Indicate model of the need to update splitting
1114 SetToUpdate( true );
1117 QString HYDROData_CalculationCase::DumpRules() const
1119 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1120 return HYDROData_PriorityQueue::DumpRules( aRulesLab );
1123 void HYDROData_CalculationCase::SetAssignmentMode( AssignmentMode theMode )
1125 TDF_Label aModeLab = myLab.FindChild( DataTag_AssignmentMode );
1126 TDataStd_Integer::Set( aModeLab, ( int ) theMode );
1128 // Indicate model of the need to update splitting
1129 SetToUpdate( true );
1132 HYDROData_CalculationCase::AssignmentMode HYDROData_CalculationCase::GetAssignmentMode() const
1134 Handle(TDataStd_Integer) aModeAttr;
1135 bool isOK = myLab.FindChild( DataTag_AssignmentMode ).FindAttribute( TDataStd_Integer::GetID(), aModeAttr );
1137 return ( AssignmentMode ) aModeAttr->Get();
1142 void HYDROData_CalculationCase::DumpRulesToPython( const QString& theCalcCaseName,
1143 QStringList& theScript ) const
1145 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1146 HYDROData_PriorityQueue::DumpRulesToPython( aRulesLab, theCalcCaseName, theScript );
1149 HYDROData_Warning HYDROData_CalculationCase::GetLastWarning() const
1151 return myLastWarning;
1154 void HYDROData_CalculationCase::SetWarning( HYDROData_WarningType theType, const QString& theData )
1156 myLastWarning.Type = theType;
1157 myLastWarning.Data = theData;
1160 bool HYDROData_CalculationCase::GetRule( int theIndex,
1161 Handle(HYDROData_Object)& theObject1,
1162 HYDROData_PriorityType& thePriority,
1163 Handle(HYDROData_Object)& theObject2,
1164 HYDROData_Zone::MergeAltitudesType& theMergeType ) const
1166 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1167 return HYDROData_PriorityQueue::GetRule( aRulesLab, theIndex,
1168 theObject1, thePriority, theObject2, theMergeType );