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 ? "MANUAL" : "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();
215 // At first we remove previously created objects
217 RemoveSplittedGroups();
219 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
220 if ( aDocument.IsNull() )
223 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
224 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
225 if ( aGeomObjects.IsEmpty() )
228 HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
230 HYDROData_SplitToZonesTool::SplitDataList aSplitObjects =
231 HYDROData_SplitToZonesTool::Split( aGeomObjects, aGeomGroups, aBoundaryPolyline );
232 if ( aSplitObjects.isEmpty() )
235 HYDROData_SplitToZonesTool::SplitDataList aZonesList, anEdgesList;
237 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplitObjects );
238 while( anIter.hasNext() )
240 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
241 if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Zone )
242 aZonesList.append( aSplitData );
243 else if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Edge )
244 anEdgesList.append( aSplitData );
247 switch( GetAssignmentMode() )
250 CreateRegionsDef( aDocument, aZonesList );
253 CreateRegionsAuto( aDocument, aZonesList );
256 CreateEdgeGroupsDef( aDocument, anEdgesList );
259 void HYDROData_CalculationCase::CreateRegionsDef( const Handle(HYDROData_Document)& theDoc,
260 const HYDROData_SplitToZonesTool::SplitDataList& theZones )
262 // Create result regions for case, by default one zone for one region
263 QString aRegsPref = CALCULATION_REGIONS_PREF;
264 QString aZonesPref = CALCULATION_ZONES_PREF;
266 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theZones );
267 while( anIter.hasNext() )
269 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
271 Handle(HYDROData_Region) aRegion = addNewRegion( theDoc, aRegsPref );
273 // Add the zone for region
274 Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( theDoc, aZonesPref, aSplitData.Face(), aSplitData.ObjectNames );
278 void HYDROData_CalculationCase::CreateRegionsAuto( const Handle(HYDROData_Document)& theDoc,
279 const HYDROData_SplitToZonesTool::SplitDataList& theZones )
281 QMap<QString, Handle(HYDROData_Region)> aRegionsMap; //object name to region
282 QString aZonesPref = CALCULATION_ZONES_PREF;
283 HYDROData_PriorityQueue aPr( this );
285 // 1. First we create a default region for each object included into the calculation case
286 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
287 for( int i=aGeomObjects.Lower(), n=aGeomObjects.Upper(); i<=n; i++ )
289 Handle(HYDROData_Object) anObj = Handle(HYDROData_Object)::DownCast( aGeomObjects.Value( i ) );
292 QString anObjName = anObj->GetName();
293 QString aRegName = anObjName + "_reg";
294 Handle(HYDROData_Region) aRegion = addNewRegion( theDoc, aRegName, false );
295 aRegionsMap.insert( anObjName, aRegion );
298 // 2. Now for each zone it is necessary to determine the most priority object
299 // and assign to zone to corresponding region
300 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theZones );
301 while( anIter.hasNext() )
303 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
304 HYDROData_Zone::MergeAltitudesType aMergeType;
305 Handle(HYDROData_Object) aRegObj = aPr.GetMostPriorityObject( aSplitData.ObjectNames, aMergeType );
306 if( aRegObj.IsNull() )
308 Handle(HYDROData_Region) aRegion = aRegionsMap[aRegObj->GetName()];
309 if( aRegion.IsNull() )
311 Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( theDoc, aZonesPref, aSplitData.Face(), aSplitData.ObjectNames );
314 case HYDROData_Zone::Merge_ZMIN:
315 case HYDROData_Zone::Merge_ZMAX:
316 aRegionZone->SetMergeType( aMergeType );
318 case HYDROData_Zone::Merge_Object:
319 aRegionZone->SetMergeType( aMergeType );
320 aRegionZone->RemoveMergeAltitude();
321 aRegionZone->SetMergeAltitude( aRegObj->GetAltitudeObject() );
326 //TODO: inform if some regions remain empty
329 void HYDROData_CalculationCase::CreateEdgeGroupsDef( const Handle(HYDROData_Document)& theDoc,
330 const HYDROData_SplitToZonesTool::SplitDataList& theEdges )
332 QMap<QString,Handle(HYDROData_SplittedShapesGroup)> aSplittedEdgesGroupsMap;
334 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theEdges );
335 while( anIter.hasNext() )
337 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
338 // Create new edges group
339 if ( aSplitData.ObjectNames.isEmpty() || aSplitData.Shape.IsNull() )
342 QString anObjName = aSplitData.ObjectNames.first();
343 if ( anObjName.isEmpty() )
345 #ifdef DEB_CALCULATION
346 QString aStr = aSplitData.ObjectNames.join(" ");
347 cout << " CCase: Names = "<<aStr.toStdString() << " size = " <<aSplitData.ObjectNames.size() <<endl;
349 Handle(HYDROData_SplittedShapesGroup) aSplittedGroup;
350 if ( !aSplittedEdgesGroupsMap.contains( anObjName ) )
352 aSplittedGroup = addNewSplittedGroup( CALCULATION_GROUPS_PREF + anObjName );
353 aSplittedEdgesGroupsMap.insert( anObjName, aSplittedGroup );
357 aSplittedGroup = aSplittedEdgesGroupsMap[ anObjName ];
359 if ( aSplittedGroup.IsNull() )
362 aSplittedGroup->AddShape( aSplitData.Shape );
366 bool HYDROData_CalculationCase::AddGeometryObject( const Handle(HYDROData_Object)& theObject )
368 if ( !HYDROData_Tool::IsGeometryObject( theObject ) )
369 return false; // Wrong type of object
371 if ( HasReference( theObject, DataTag_GeometryObject ) )
372 return false; // Object is already in reference list
374 AddReferenceObject( theObject, DataTag_GeometryObject );
376 // Indicate model of the need to update splitting
382 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryObjects() const
384 return GetReferenceObjects( DataTag_GeometryObject );
387 void HYDROData_CalculationCase::RemoveGeometryObject( const Handle(HYDROData_Object)& theObject )
389 if ( theObject.IsNull() )
392 RemoveReferenceObject( theObject->Label(), DataTag_GeometryObject );
394 // Indicate model of the need to update splitting
398 void HYDROData_CalculationCase::RemoveGeometryObjects()
400 ClearReferenceObjects( DataTag_GeometryObject );
402 // Indicate model of the need to update splitting
406 bool HYDROData_CalculationCase::AddGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
408 if ( theGroup.IsNull() )
411 if ( HasReference( theGroup, DataTag_GeometryGroup ) )
412 return false; // Object is already in reference list
414 AddReferenceObject( theGroup, DataTag_GeometryGroup );
416 // Indicate model of the need to update splitting
422 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryGroups() const
424 return GetReferenceObjects( DataTag_GeometryGroup );
427 void HYDROData_CalculationCase::RemoveGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
429 if ( theGroup.IsNull() )
432 RemoveReferenceObject( theGroup->Label(), DataTag_GeometryGroup );
434 // Indicate model of the need to update splitting
438 void HYDROData_CalculationCase::RemoveGeometryGroups()
440 ClearReferenceObjects( DataTag_GeometryGroup );
442 // Indicate model of the need to update splitting
446 void HYDROData_CalculationCase::SetBoundaryPolyline( const Handle(HYDROData_PolylineXY)& thePolyline )
448 Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
450 SetReferenceObject( thePolyline, DataTag_Polyline );
452 // Indicate model of the need to update zones splitting
453 SetToUpdate( !IsEqual( aPrevPolyline, thePolyline ) || IsMustBeUpdated() );
456 Handle(HYDROData_PolylineXY) HYDROData_CalculationCase::GetBoundaryPolyline() const
458 return Handle(HYDROData_PolylineXY)::DownCast(
459 GetReferenceObject( DataTag_Polyline ) );
462 void HYDROData_CalculationCase::RemoveBoundaryPolyline()
464 Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
466 ClearReferenceObjects( DataTag_Polyline );
468 // Indicate model of the need to update zones splitting
469 SetToUpdate( !aPrevPolyline.IsNull() || IsMustBeUpdated() );
472 Handle(HYDROData_Region) HYDROData_CalculationCase::AddNewRegion( const Handle(HYDROData_Zone)& theZone )
474 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
475 Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
476 if ( aNewRegion.IsNull() )
479 aNewRegion->AddZone( theZone );
484 bool HYDROData_CalculationCase::AddRegion( const Handle(HYDROData_Region)& theRegion )
486 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
488 if ( theRegion.IsNull() )
491 if ( HasReference( theRegion, DataTag_Region ) )
492 return false; // Object is already in reference list
494 // Move the region from other calculation
495 Handle(HYDROData_CalculationCase) aFatherCalc =
496 Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
497 if ( !aFatherCalc.IsNull() && aFatherCalc->Label() != myLab )
499 Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
500 theRegion->CopyTo( aNewRegion );
502 aFatherCalc->RemoveRegion( theRegion );
504 theRegion->SetLabel( aNewRegion->Label() );
508 AddReferenceObject( theRegion, DataTag_Region );
514 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetRegions() const
516 return GetReferenceObjects( DataTag_Region );
519 void HYDROData_CalculationCase::UpdateRegionsOrder()
521 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
522 if ( aDocument.IsNull() )
525 HYDROData_SequenceOfObjects aRegions = GetRegions();
527 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
528 for ( ; anIter.More(); anIter.Next() )
530 Handle(HYDROData_Region) aRegion =
531 Handle(HYDROData_Region)::DownCast( anIter.Value() );
532 if ( aRegion.IsNull() )
535 aRegion->SetName( "" );
538 QString aRegsPref = CALCULATION_REGIONS_PREF;
540 anIter.Init( aRegions );
541 for ( ; anIter.More(); anIter.Next() )
543 Handle(HYDROData_Region) aRegion =
544 Handle(HYDROData_Region)::DownCast( anIter.Value() );
545 if ( aRegion.IsNull() )
548 QString aRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
549 aRegion->SetName( aRegionName );
553 void HYDROData_CalculationCase::RemoveRegion( const Handle(HYDROData_Region)& theRegion )
555 if ( theRegion.IsNull() )
558 RemoveReferenceObject( theRegion->Label(), DataTag_Region );
560 // Remove region from data model
561 Handle(HYDROData_CalculationCase) aFatherCalc =
562 Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
563 if ( !aFatherCalc.IsNull() && aFatherCalc->Label() == myLab )
567 void HYDROData_CalculationCase::RemoveRegions()
569 myLab.FindChild( DataTag_ChildRegion ).ForgetAllAttributes();
572 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetSplittedGroups() const
574 return GetReferenceObjects( DataTag_SplittedGroups );
577 void HYDROData_CalculationCase::RemoveSplittedGroups()
579 myLab.FindChild( DataTag_SplittedGroups ).ForgetAllAttributes();
582 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint ) const
584 Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
585 return GetAltitudeForPoint( thePoint, aZone );
588 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint,
589 const Handle(HYDROData_Region)& theRegion ) const
591 double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
593 Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
594 if ( !aZone.IsNull() )
596 Handle(HYDROData_Region) aRefRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
597 if ( IsEqual( aRefRegion, theRegion ) )
598 aResAltitude = GetAltitudeForPoint( thePoint, aZone );
604 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint,
605 const Handle(HYDROData_Zone)& theZone ) const
607 double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
608 if ( theZone.IsNull() )
611 HYDROData_Zone::MergeAltitudesType aZoneMergeType = theZone->GetMergeType();
612 if ( !theZone->IsMergingNeed() )
614 aZoneMergeType = HYDROData_Zone::Merge_UNKNOWN;
616 else if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
621 HYDROData_IInterpolator* aZoneInterpolator = theZone->GetInterpolator();
622 if ( aZoneMergeType == HYDROData_Zone::Merge_Object )
624 Handle(HYDROData_IAltitudeObject) aMergeAltitude = theZone->GetMergeAltitude();
625 if ( !aMergeAltitude.IsNull() )
627 if ( aZoneInterpolator != NULL )
629 aZoneInterpolator->SetAltitudeObject( aMergeAltitude );
630 aResAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
633 aResAltitude = aMergeAltitude->GetAltitudeForPoint( thePoint );
638 HYDROData_SequenceOfObjects aZoneObjects = theZone->GetGeometryObjects();
639 HYDROData_SequenceOfObjects::Iterator anIter( aZoneObjects );
640 for ( ; anIter.More(); anIter.Next() )
642 Handle(HYDROData_Object) aZoneObj =
643 Handle(HYDROData_Object)::DownCast( anIter.Value() );
644 if ( aZoneObj.IsNull() )
647 Handle(HYDROData_IAltitudeObject) anObjAltitude = aZoneObj->GetAltitudeObject();
648 if ( anObjAltitude.IsNull() )
651 double aPointAltitude = 0.0;
652 if ( aZoneInterpolator != NULL )
654 aZoneInterpolator->SetAltitudeObject( anObjAltitude );
655 aPointAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
658 aPointAltitude = anObjAltitude->GetAltitudeForPoint( thePoint );
660 if ( ValuesEquals( aPointAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) )
663 if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
665 aResAltitude = aPointAltitude;
668 else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMIN )
670 if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
671 aResAltitude > aPointAltitude )
673 aResAltitude = aPointAltitude;
676 else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMAX )
678 if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
679 aResAltitude < aPointAltitude )
681 aResAltitude = aPointAltitude;
690 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints(
691 const NCollection_Sequence<gp_XY>& thePoints,
692 const Handle(HYDROData_Region)& theRegion ) const
694 NCollection_Sequence<double> aResSeq;
696 for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
698 const gp_XY& thePnt = thePoints.Value( i );
700 double anAltitude = GetAltitudeForPoint( thePnt, theRegion );
701 aResSeq.Append( anAltitude );
707 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints(
708 const NCollection_Sequence<gp_XY>& thePoints,
709 const Handle(HYDROData_Zone)& theZone ) const
711 NCollection_Sequence<double> aResSeq;
713 for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
715 const gp_XY& thePnt = thePoints.Value( i );
717 double anAltitude = GetAltitudeForPoint( thePnt, theZone );
718 aResSeq.Append( anAltitude );
724 Handle(HYDROData_Region) HYDROData_CalculationCase::GetRegionFromPoint( const gp_XY& thePoint ) const
726 Handle(HYDROData_Region) aResRegion;
728 Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
729 if ( !aZone.IsNull() )
730 aResRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
735 Handle(HYDROData_Zone) HYDROData_CalculationCase::GetZoneFromPoint( const gp_XY& thePoint ) const
737 Handle(HYDROData_Zone) aResZone;
739 HYDROData_SequenceOfObjects aRegions = GetRegions();
741 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
742 for ( ; anIter.More() && aResZone.IsNull(); anIter.Next() )
744 Handle(HYDROData_Region) aRegion =
745 Handle(HYDROData_Region)::DownCast( anIter.Value() );
746 if ( aRegion.IsNull() )
749 HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
750 HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
751 for ( ; aZonesIter.More() && aResZone.IsNull(); aZonesIter.Next() )
753 Handle(HYDROData_Zone) aRegZone =
754 Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
755 if ( aRegZone.IsNull() )
758 PointClassification aPointRelation = GetPointClassification( thePoint, aRegZone );
759 if ( aPointRelation != POINT_OUT )
760 aResZone = aRegZone; // We found the desired zone
767 HYDROData_CalculationCase::PointClassification HYDROData_CalculationCase::GetPointClassification(
768 const gp_XY& thePoint,
769 const Handle(HYDROData_Zone)& theZone ) const
771 PointClassification aRes = POINT_OUT;
772 if ( theZone.IsNull() )
775 TopoDS_Face aZoneFace = TopoDS::Face( theZone->GetShape() );
776 if ( aZoneFace.IsNull() )
779 TopoDS_Compound aCmp;
781 aBB.MakeCompound(aCmp);
782 aBB.Add(aCmp, aZoneFace);
783 gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
784 BRepBuilderAPI_MakeVertex aMk(aPnt);
785 aBB.Add(aCmp, aMk.Vertex());
786 BRepTools::Write(aCmp, "FCL2d.brep");
788 TopAbs_State State = HYDROData_Tool::ComputePointState(thePoint, aZoneFace);
789 if (State == TopAbs_OUT)
791 else if(State == TopAbs_IN)
793 else if(State == TopAbs_ON)
798 Handle(HYDROData_Region) HYDROData_CalculationCase::addNewRegion( const Handle(HYDROData_Document)& theDoc,
799 const QString& thePrefixOrName,
802 TDF_Label aNewLab = myLab.FindChild( DataTag_ChildRegion ).NewChild();
804 Handle(HYDROData_Region) aNewRegion =
805 Handle(HYDROData_Region)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_REGION ) );
806 AddRegion( aNewRegion );
808 QString aRegionName = isPrefix ? HYDROData_Tool::GenerateObjectName( theDoc, thePrefixOrName ) : thePrefixOrName;
809 aNewRegion->SetName( aRegionName );
814 Handle(HYDROData_SplittedShapesGroup) HYDROData_CalculationCase::addNewSplittedGroup( const QString& theName )
816 TDF_Label aNewLab = myLab.FindChild( DataTag_SplittedGroups ).NewChild();
818 Handle(HYDROData_SplittedShapesGroup) aNewGroup =
819 Handle(HYDROData_SplittedShapesGroup)::DownCast(
820 HYDROData_Iterator::CreateObject( aNewLab, KIND_SPLITTED_GROUP ) );
821 AddReferenceObject( aNewGroup, DataTag_SplittedGroups );
823 aNewGroup->SetName( theName );
828 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var theGeomEngine,
829 SALOMEDS::Study_ptr theStudy ) const
831 HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroupsDefs;
833 // Get groups definitions
834 HYDROData_SequenceOfObjects aSplittedGroups = GetSplittedGroups();
836 HYDROData_SequenceOfObjects::Iterator anIter( aSplittedGroups );
837 for ( ; anIter.More(); anIter.Next() )
840 Handle(HYDROData_ShapesGroup) aGroup =
841 Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
842 if ( aGroup.IsNull() )
845 HYDROData_ShapesGroup::GroupDefinition aGroupDef;
847 aGroupDef.Name = aGroup->GetName().toLatin1().constData();
848 aGroup->GetShapes( aGroupDef.Shapes );
850 aSeqOfGroupsDefs.Append( aGroupDef );
854 TopTools_ListOfShape aFaces;
855 HYDROData_SequenceOfObjects aCaseRegions = GetRegions();
856 HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
857 for ( ; aRegionIter.More(); aRegionIter.Next() )
859 Handle(HYDROData_Region) aRegion =
860 Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
861 if( aRegion.IsNull() )
864 TopoDS_Shape aRegionShape = aRegion->GetShape( &aSeqOfGroupsDefs );
865 aFaces.Append( aRegionShape );
868 return Export( theGeomEngine, theStudy, aFaces, aSeqOfGroupsDefs );
871 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var theGeomEngine,
872 SALOMEDS::Study_ptr theStudy,
873 const TopTools_ListOfShape& theFaces,
874 const HYDROData_ShapesGroup::SeqOfGroupsDefs& theGroupsDefs ) const
877 BRepBuilderAPI_Sewing aSewing( Precision::Confusion() * 10.0 );
878 aSewing.SetNonManifoldMode( Standard_False );
879 #ifdef DEB_CALCULATION
880 TCollection_AsciiString aNam("Sh_");
883 TopTools_ListIteratorOfListOfShape aFaceIter( theFaces );
884 for ( ; aFaceIter.More(); aFaceIter.Next() )
886 TopoDS_Shape aShape = aFaceIter.Value();
887 if ( aShape.IsNull() )
890 if ( aShape.ShapeType() == TopAbs_FACE )
892 aSewing.Add( aShape );
893 #ifdef DEB_CALCULATION
894 TCollection_AsciiString aName = aNam + ++i + ".brep";
895 BRepTools::Write(aShape ,aName.ToCString());
900 #ifdef DEB_CALCULATION
903 TopExp_Explorer anExp( aShape, TopAbs_FACE );
904 for (; anExp.More(); anExp.Next() ) {
905 aSewing.Add( anExp.Current() );
906 #ifdef DEB_CALCULATION
908 TCollection_AsciiString aName = aNam + i + "_" + ++j + ".brep";
909 BRepTools::Write(anExp.Current() ,aName.ToCString());
916 TopoDS_Shape aSewedShape = aSewing.SewedShape();
918 // If the sewed shape is empty - return false
919 if ( aSewedShape.IsNull() || !TopoDS_Iterator( aSewedShape ).More() )
922 #ifdef DEB_CALCULATION
923 BRepTools::Write(aSewedShape ,"Sew.brep");
925 // Publish the sewed shape
926 QString aName = EXPORT_NAME;
927 GEOM::GEOM_Object_ptr aMainShape =
928 publishShapeInGEOM( theGeomEngine, theStudy, aSewedShape, aName );
930 if ( aMainShape->_is_nil() )
933 if ( theGroupsDefs.IsEmpty() )
937 TopTools_IndexedMapOfShape aMapOfSubShapes;
938 TopExp::MapShapes( aSewedShape, aMapOfSubShapes );
940 NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> > aGroupsData;
942 for ( int aGrId = 1, nbGroups = theGroupsDefs.Length(); aGrId <= nbGroups; ++aGrId )
944 const HYDROData_ShapesGroup::GroupDefinition& aGroupDef = theGroupsDefs.Value( aGrId );
946 NCollection_Sequence<int> aGroupIndexes;
947 for( int i = 1, n = aGroupDef.Shapes.Length(); i <= n; i++ )
949 const TopoDS_Shape& aShape = aGroupDef.Shapes.Value( i );
950 #ifdef DEB_CALCULATION
951 cout << "\nOld shape(" << i << ") = " << aShape.TShape() <<endl;
954 TopoDS_Shape aModifiedShape = aShape;
955 if ( aSewing.IsModified( aShape ) )
956 aModifiedShape = aSewing.Modified( aShape );
957 else if ( aSewing.IsModifiedSubShape( aShape ) )
958 aModifiedShape = aSewing.ModifiedSubShape( aShape );
960 #ifdef DEB_CALCULATION
961 const TopLoc_Location& aL1 = aShape.Location();
962 const TopLoc_Location& aL2 = aModifiedShape.Location();
963 cout << "\nNew shape(" << i << ") = " << aModifiedShape.TShape() << " Location is Equal = " << aL1.IsEqual(aL2)<<endl;
966 int anIndex = aMapOfSubShapes.FindIndex(aModifiedShape);
968 aGroupIndexes.Append( anIndex );
970 #ifdef DEB_CALCULATION
971 TCollection_AsciiString aNam("Lost_");
972 if(!aMapOfSubShapes.Contains(aModifiedShape)) {
973 for ( int anIndex = 1; anIndex <= aMapOfSubShapes.Extent(); anIndex++ )
975 const TopoDS_Shape& aS = aMapOfSubShapes.FindKey( anIndex );
976 if ( aModifiedShape.IsPartner( aS ) )
978 cout <<"\nIndex in Map = " << anIndex << "TShape = " << aS.TShape() <<endl;
979 TCollection_AsciiString aName = aNam + i + "_" + anIndex + ".brep";
980 BRepTools::Write(aS ,aName.ToCString());
988 if ( !aGroupIndexes.IsEmpty() )
989 aGroupsData.Bind( aGroupDef.Name, aGroupIndexes );
992 if ( !aGroupsData.IsEmpty() )
994 GEOM::GEOM_IGroupOperations_var aGroupOp =
995 theGeomEngine->GetIGroupOperations( theStudy->StudyId() );
997 NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> >::Iterator aMapIt( aGroupsData );
998 for ( ; aMapIt.More(); aMapIt.Next() )
1000 const TCollection_AsciiString& aGroupName = aMapIt.Key();
1001 const NCollection_Sequence<int>& aGroupIndexes = aMapIt.Value();
1003 GEOM::GEOM_Object_var aGeomGroup = aGroupOp->CreateGroup( aMainShape, TopAbs_EDGE );
1004 if ( CORBA::is_nil( aGeomGroup ) || !aGroupOp->IsDone() )
1007 GEOM::ListOfLong_var aGeomIndexes = new GEOM::ListOfLong;
1008 aGeomIndexes->length( aGroupIndexes.Length() );
1010 for( int i = 1, n = aGroupIndexes.Length(); i <= n; i++ )
1011 aGeomIndexes[ i - 1 ] = aGroupIndexes.Value( i );
1013 aGroupOp->UnionIDs( aGeomGroup, aGeomIndexes );
1014 if ( aGroupOp->IsDone() )
1016 SALOMEDS::SObject_var aGroupSO =
1017 theGeomEngine->AddInStudy( theStudy, aGeomGroup, aGroupName.ToCString(), aMainShape );
1025 GEOM::GEOM_Object_ptr HYDROData_CalculationCase::publishShapeInGEOM(
1026 GEOM::GEOM_Gen_var theGeomEngine, SALOMEDS::Study_ptr theStudy,
1027 const TopoDS_Shape& theShape, const QString& theName ) const
1029 GEOM::GEOM_Object_var aGeomObj;
1031 if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ||
1032 theShape.IsNull() ) {
1033 return aGeomObj._retn();
1036 std::ostringstream aStreamShape;
1037 // Write TopoDS_Shape in ASCII format to the stream
1038 BRepTools::Write( theShape, aStreamShape );
1039 // Returns the number of bytes that have been stored in the stream's buffer.
1040 int aSize = aStreamShape.str().size();
1041 // Allocate octect buffer of required size
1042 CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize );
1043 // Copy ostrstream content to the octect buffer
1044 memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize );
1046 SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
1048 // Restore shape from the stream and get the GEOM object
1049 GEOM::GEOM_IInsertOperations_var anInsOp = theGeomEngine->GetIInsertOperations( theStudy->StudyId() );
1050 aGeomObj = anInsOp->RestoreShape( aSeqFile );
1052 // Puplish the GEOM object
1053 if ( !aGeomObj->_is_nil() ) {
1054 QString aName = GEOMBase::GetDefaultName( theName );
1056 SALOMEDS::SObject_var aResultSO =
1057 theGeomEngine->PublishInStudy( theStudy, SALOMEDS::SObject::_nil(),
1058 aGeomObj, qPrintable( aName ) );
1059 if ( aResultSO->_is_nil() ) {
1060 aGeomObj = GEOM::GEOM_Object::_nil();
1064 return aGeomObj._retn();
1067 void HYDROData_CalculationCase::ClearRules()
1069 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1070 HYDROData_PriorityQueue::ClearRules( aRulesLab );
1073 void HYDROData_CalculationCase::AddRule( const Handle(HYDROData_Object)& theObject1,
1074 HYDROData_PriorityType thePriority,
1075 const Handle(HYDROData_Object)& theObject2,
1076 HYDROData_Zone::MergeAltitudesType theMergeType )
1078 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1079 HYDROData_PriorityQueue::AddRule( aRulesLab, theObject1, thePriority, theObject2, theMergeType );
1082 QString HYDROData_CalculationCase::DumpRules()
1084 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1085 return HYDROData_PriorityQueue::DumpRules( aRulesLab );
1088 void HYDROData_CalculationCase::SetAssignmentMode( AssignmentMode theMode )
1090 TDF_Label aModeLab = myLab.FindChild( DataTag_AssignmentMode );
1091 TDataStd_Integer::Set( aModeLab, ( int ) theMode );
1094 HYDROData_CalculationCase::AssignmentMode HYDROData_CalculationCase::GetAssignmentMode() const
1096 Handle(TDataStd_Integer) aModeAttr;
1097 bool isOK = myLab.FindChild( DataTag_AssignmentMode ).FindAttribute( TDataStd_Integer::GetID(), aModeAttr );
1099 return ( AssignmentMode ) aModeAttr->Get();
1104 void HYDROData_CalculationCase::DumpRulesToPython( const QString& theCalcCaseName,
1105 QStringList& theScript ) const
1107 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1108 HYDROData_PriorityQueue::DumpRulesToPython( aRulesLab, theCalcCaseName, theScript );