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>
35 //#define DEB_CALCULATION 1
36 #ifdef DEB_CALCULATION
37 #include <BRepTools.hxx>
38 #include <TopLoc_Location.hxx>
40 #define CALCULATION_REGIONS_PREF GetName() + "_Reg"
41 #define CALCULATION_ZONES_PREF GetName() + "_Zone"
42 #define CALCULATION_GROUPS_PREF GetName() + "_"
43 //#define DEB_CLASS2D 1
45 #include <BRepBuilderAPI_MakeVertex.hxx>
48 #define EXPORT_NAME "HYDRO_" + GetName()
50 IMPLEMENT_STANDARD_HANDLE(HYDROData_CalculationCase, HYDROData_Entity)
51 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_CalculationCase, HYDROData_Entity)
53 HYDROData_CalculationCase::HYDROData_CalculationCase()
58 HYDROData_CalculationCase::~HYDROData_CalculationCase()
62 void HYDROData_CalculationCase::SetName( const QString& theName )
64 QString anOldCaseName = GetName();
65 if ( anOldCaseName != theName )
67 HYDROData_SequenceOfObjects aRegions = GetRegions();
69 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
70 for ( ; anIter.More(); anIter.Next() )
72 Handle(HYDROData_Region) aRegion =
73 Handle(HYDROData_Region)::DownCast( anIter.Value() );
74 if ( aRegion.IsNull() )
77 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aRegion );
79 HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
80 HYDROData_SequenceOfObjects::Iterator anIter( aZones );
81 for ( ; anIter.More(); anIter.Next() )
83 Handle(HYDROData_Zone) aRegZone =
84 Handle(HYDROData_Zone)::DownCast( anIter.Value() );
85 if ( aRegZone.IsNull() )
88 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aRegZone );
92 HYDROData_SequenceOfObjects aGroups = GetSplittedGroups();
94 anIter.Init( aGroups );
95 for ( ; anIter.More(); anIter.Next() )
97 Handle(HYDROData_SplittedShapesGroup) aGroup =
98 Handle(HYDROData_SplittedShapesGroup)::DownCast( anIter.Value() );
99 if ( aGroup.IsNull() )
102 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aGroup );
106 HYDROData_Entity::SetName( theName );
109 QStringList HYDROData_CalculationCase::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
111 QStringList aResList = dumpObjectCreation( theTreatedObjects );
113 QString aCalculName = GetObjPyName();
115 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
116 HYDROData_SequenceOfObjects::Iterator anIter( aGeomObjects );
117 for ( ; anIter.More(); anIter.Next() )
119 Handle(HYDROData_Object) aRefGeomObj =
120 Handle(HYDROData_Object)::DownCast( anIter.Value() );
121 setPythonReferenceObject( theTreatedObjects, aResList, aRefGeomObj, "AddGeometryObject" );
123 aResList << QString( "" );
125 QString aGroupName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "case_geom_group" );
127 HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
128 anIter.Init( aGeomGroups );
129 for ( ; anIter.More(); anIter.Next() )
131 Handle(HYDROData_ShapesGroup) aGeomGroup =
132 Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
133 if ( aGeomGroup.IsNull() )
136 Handle(HYDROData_Object) aFatherGeom =
137 Handle(HYDROData_Object)::DownCast( aGeomGroup->GetFatherObject() );
138 if ( aFatherGeom.IsNull() )
141 int aGroupId = aFatherGeom->GetGroupId( aGeomGroup );
142 aResList << QString( "%1 = %2.GetGroup( %3 );" )
143 .arg( aGroupName ).arg( aFatherGeom->GetObjPyName() ).arg( aGroupId );
145 aResList << QString( "%1.AddGeometryGroup( %2 );" ).arg( aCalculName ).arg( aGroupName );
147 aResList << QString( "" );
149 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
150 setPythonReferenceObject( theTreatedObjects, aResList, aBoundaryPolyline, "SetBoundaryPolyline" );
151 aResList << QString( "" );
153 aResList << QString( "%1.Update();" ).arg( aCalculName );
154 aResList << QString( "" );
156 // Now we restore the regions and zones order
157 HYDROData_SequenceOfObjects aRegions = GetRegions();
158 anIter.Init( aRegions );
159 for ( ; anIter.More(); anIter.Next() )
161 Handle(HYDROData_Region) aRegion =
162 Handle(HYDROData_Region)::DownCast( anIter.Value() );
163 if ( aRegion.IsNull() )
166 QString aRegionName = aRegion->GetName();
168 HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
169 HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
170 for ( ; aZonesIter.More(); aZonesIter.Next() )
172 Handle(HYDROData_Zone) aRegZone =
173 Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
174 if ( aRegZone.IsNull() )
180 aResList << QString( "" );
185 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetAllReferenceObjects() const
187 HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
189 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
190 if ( !aBoundaryPolyline.IsNull() )
191 aResSeq.Append( aBoundaryPolyline );
193 HYDROData_SequenceOfObjects aSeqOfRegions = GetRegions();
194 aResSeq.Append( aSeqOfRegions );
199 void HYDROData_CalculationCase::Update()
201 HYDROData_Entity::Update();
203 // At first we remove previously created objects
205 RemoveSplittedGroups();
207 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
208 if ( aDocument.IsNull() )
211 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
212 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
213 if ( aGeomObjects.IsEmpty() )
216 HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
218 HYDROData_SplitToZonesTool::SplitDataList aSplitObjects =
219 HYDROData_SplitToZonesTool::Split( aGeomObjects, aGeomGroups, aBoundaryPolyline );
220 if ( aSplitObjects.isEmpty() )
223 HYDROData_SplitToZonesTool::SplitDataList aZonesList, anEdgesList;
225 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplitObjects );
226 while( anIter.hasNext() )
228 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
229 if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Zone )
230 aZonesList.append( aSplitData );
231 else if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Edge )
232 anEdgesList.append( aSplitData );
235 //CreateRegionsDef( aDocument, aZonesList );
236 CreateRegionsAuto( aDocument, aZonesList );
237 CreateEdgeGroupsDef( aDocument, anEdgesList );
240 void HYDROData_CalculationCase::CreateRegionsDef( const Handle(HYDROData_Document)& theDoc,
241 const HYDROData_SplitToZonesTool::SplitDataList& theZones )
243 // Create result regions for case, by default one zone for one region
244 QString aRegsPref = CALCULATION_REGIONS_PREF;
245 QString aZonesPref = CALCULATION_ZONES_PREF;
247 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theZones );
248 while( anIter.hasNext() )
250 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
252 Handle(HYDROData_Region) aRegion = addNewRegion( theDoc, aRegsPref );
254 // Add the zone for region
255 Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( theDoc, aZonesPref, aSplitData.Face(), aSplitData.ObjectNames );
259 void HYDROData_CalculationCase::CreateRegionsAuto( const Handle(HYDROData_Document)& theDoc,
260 const HYDROData_SplitToZonesTool::SplitDataList& theZones )
262 QMap<QString, Handle(HYDROData_Region)> aRegionsMap; //object name to region
263 QString aZonesPref = CALCULATION_ZONES_PREF;
264 HYDROData_PriorityQueue aPr( this );
266 // 1. First we create a default region for each object included into the calculation case
267 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
268 for( int i=aGeomObjects.Lower(), n=aGeomObjects.Upper(); i<=n; i++ )
270 Handle(HYDROData_Object) anObj = Handle(HYDROData_Object)::DownCast( aGeomObjects.Value( i ) );
273 QString anObjName = anObj->GetName();
274 QString aRegName = anObjName + "_reg";
275 Handle(HYDROData_Region) aRegion = addNewRegion( theDoc, aRegName, false );
276 aRegionsMap.insert( anObjName, aRegion );
279 // 2. Now for each zone it is necessary to determine the most priority object
280 // and assign to zone to corresponding region
281 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theZones );
282 while( anIter.hasNext() )
284 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
285 HYDROData_Zone::MergeAltitudesType aMergeType;
286 Handle(HYDROData_Object) aRegObj = aPr.GetMostPriorityObject( aSplitData.ObjectNames, aMergeType );
287 if( aRegObj.IsNull() )
289 Handle(HYDROData_Region) aRegion = aRegionsMap[aRegObj->GetName()];
290 if( aRegion.IsNull() )
292 Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( theDoc, aZonesPref, aSplitData.Face(), aSplitData.ObjectNames );
295 case HYDROData_Zone::Merge_ZMIN:
296 case HYDROData_Zone::Merge_ZMAX:
297 aRegionZone->SetMergeType( aMergeType );
299 case HYDROData_Zone::Merge_Object:
300 aRegionZone->SetMergeType( aMergeType );
301 aRegionZone->RemoveMergeAltitude();
302 aRegionZone->SetMergeAltitude( aRegObj->GetAltitudeObject() );
307 //TODO: inform if some regions remain empty
310 void HYDROData_CalculationCase::CreateEdgeGroupsDef( const Handle(HYDROData_Document)& theDoc,
311 const HYDROData_SplitToZonesTool::SplitDataList& theEdges )
313 QMap<QString,Handle(HYDROData_SplittedShapesGroup)> aSplittedEdgesGroupsMap;
315 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theEdges );
316 while( anIter.hasNext() )
318 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
319 // Create new edges group
320 if ( aSplitData.ObjectNames.isEmpty() || aSplitData.Shape.IsNull() )
323 QString anObjName = aSplitData.ObjectNames.first();
324 if ( anObjName.isEmpty() )
326 #ifdef DEB_CALCULATION
327 QString aStr = aSplitData.ObjectNames.join(" ");
328 cout << " CCase: Names = "<<aStr.toStdString() << " size = " <<aSplitData.ObjectNames.size() <<endl;
330 Handle(HYDROData_SplittedShapesGroup) aSplittedGroup;
331 if ( !aSplittedEdgesGroupsMap.contains( anObjName ) )
333 aSplittedGroup = addNewSplittedGroup( CALCULATION_GROUPS_PREF + anObjName );
334 aSplittedEdgesGroupsMap.insert( anObjName, aSplittedGroup );
338 aSplittedGroup = aSplittedEdgesGroupsMap[ anObjName ];
340 if ( aSplittedGroup.IsNull() )
343 aSplittedGroup->AddShape( aSplitData.Shape );
347 bool HYDROData_CalculationCase::AddGeometryObject( const Handle(HYDROData_Object)& theObject )
349 if ( !HYDROData_Tool::IsGeometryObject( theObject ) )
350 return false; // Wrong type of object
352 if ( HasReference( theObject, DataTag_GeometryObject ) )
353 return false; // Object is already in reference list
355 AddReferenceObject( theObject, DataTag_GeometryObject );
357 // Indicate model of the need to update splitting
363 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryObjects() const
365 return GetReferenceObjects( DataTag_GeometryObject );
368 void HYDROData_CalculationCase::RemoveGeometryObject( const Handle(HYDROData_Object)& theObject )
370 if ( theObject.IsNull() )
373 RemoveReferenceObject( theObject->Label(), DataTag_GeometryObject );
375 // Indicate model of the need to update splitting
379 void HYDROData_CalculationCase::RemoveGeometryObjects()
381 ClearReferenceObjects( DataTag_GeometryObject );
383 // Indicate model of the need to update splitting
387 bool HYDROData_CalculationCase::AddGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
389 if ( theGroup.IsNull() )
392 if ( HasReference( theGroup, DataTag_GeometryGroup ) )
393 return false; // Object is already in reference list
395 AddReferenceObject( theGroup, DataTag_GeometryGroup );
397 // Indicate model of the need to update splitting
403 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryGroups() const
405 return GetReferenceObjects( DataTag_GeometryGroup );
408 void HYDROData_CalculationCase::RemoveGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
410 if ( theGroup.IsNull() )
413 RemoveReferenceObject( theGroup->Label(), DataTag_GeometryGroup );
415 // Indicate model of the need to update splitting
419 void HYDROData_CalculationCase::RemoveGeometryGroups()
421 ClearReferenceObjects( DataTag_GeometryGroup );
423 // Indicate model of the need to update splitting
427 void HYDROData_CalculationCase::SetBoundaryPolyline( const Handle(HYDROData_PolylineXY)& thePolyline )
429 Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
431 SetReferenceObject( thePolyline, DataTag_Polyline );
433 // Indicate model of the need to update zones splitting
434 SetToUpdate( !IsEqual( aPrevPolyline, thePolyline ) || IsMustBeUpdated() );
437 Handle(HYDROData_PolylineXY) HYDROData_CalculationCase::GetBoundaryPolyline() const
439 return Handle(HYDROData_PolylineXY)::DownCast(
440 GetReferenceObject( DataTag_Polyline ) );
443 void HYDROData_CalculationCase::RemoveBoundaryPolyline()
445 Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
447 ClearReferenceObjects( DataTag_Polyline );
449 // Indicate model of the need to update zones splitting
450 SetToUpdate( !aPrevPolyline.IsNull() || IsMustBeUpdated() );
453 Handle(HYDROData_Region) HYDROData_CalculationCase::AddNewRegion( const Handle(HYDROData_Zone)& theZone )
455 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
456 Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
457 if ( aNewRegion.IsNull() )
460 aNewRegion->AddZone( theZone );
465 bool HYDROData_CalculationCase::AddRegion( const Handle(HYDROData_Region)& theRegion )
467 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
469 if ( theRegion.IsNull() )
472 if ( HasReference( theRegion, DataTag_Region ) )
473 return false; // Object is already in reference list
475 // Move the region from other calculation
476 Handle(HYDROData_CalculationCase) aFatherCalc =
477 Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
478 if ( !aFatherCalc.IsNull() && aFatherCalc->Label() != myLab )
480 Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
481 theRegion->CopyTo( aNewRegion );
483 aFatherCalc->RemoveRegion( theRegion );
485 theRegion->SetLabel( aNewRegion->Label() );
489 AddReferenceObject( theRegion, DataTag_Region );
495 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetRegions() const
497 return GetReferenceObjects( DataTag_Region );
500 void HYDROData_CalculationCase::UpdateRegionsOrder()
502 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
503 if ( aDocument.IsNull() )
506 HYDROData_SequenceOfObjects aRegions = GetRegions();
508 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
509 for ( ; anIter.More(); anIter.Next() )
511 Handle(HYDROData_Region) aRegion =
512 Handle(HYDROData_Region)::DownCast( anIter.Value() );
513 if ( aRegion.IsNull() )
516 aRegion->SetName( "" );
519 QString aRegsPref = CALCULATION_REGIONS_PREF;
521 anIter.Init( aRegions );
522 for ( ; anIter.More(); anIter.Next() )
524 Handle(HYDROData_Region) aRegion =
525 Handle(HYDROData_Region)::DownCast( anIter.Value() );
526 if ( aRegion.IsNull() )
529 QString aRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
530 aRegion->SetName( aRegionName );
534 void HYDROData_CalculationCase::RemoveRegion( const Handle(HYDROData_Region)& theRegion )
536 if ( theRegion.IsNull() )
539 RemoveReferenceObject( theRegion->Label(), DataTag_Region );
541 // Remove region from data model
542 Handle(HYDROData_CalculationCase) aFatherCalc =
543 Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
544 if ( !aFatherCalc.IsNull() && aFatherCalc->Label() == myLab )
548 void HYDROData_CalculationCase::RemoveRegions()
550 myLab.FindChild( DataTag_ChildRegion ).ForgetAllAttributes();
553 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetSplittedGroups() const
555 return GetReferenceObjects( DataTag_SplittedGroups );
558 void HYDROData_CalculationCase::RemoveSplittedGroups()
560 myLab.FindChild( DataTag_SplittedGroups ).ForgetAllAttributes();
563 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint ) const
565 Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
566 return GetAltitudeForPoint( thePoint, aZone );
569 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint,
570 const Handle(HYDROData_Region)& theRegion ) const
572 double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
574 Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
575 if ( !aZone.IsNull() )
577 Handle(HYDROData_Region) aRefRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
578 if ( IsEqual( aRefRegion, theRegion ) )
579 aResAltitude = GetAltitudeForPoint( thePoint, aZone );
585 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint,
586 const Handle(HYDROData_Zone)& theZone ) const
588 double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
589 if ( theZone.IsNull() )
592 HYDROData_Zone::MergeAltitudesType aZoneMergeType = theZone->GetMergeType();
593 if ( !theZone->IsMergingNeed() )
595 aZoneMergeType = HYDROData_Zone::Merge_UNKNOWN;
597 else if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
602 HYDROData_IInterpolator* aZoneInterpolator = theZone->GetInterpolator();
603 if ( aZoneMergeType == HYDROData_Zone::Merge_Object )
605 Handle(HYDROData_IAltitudeObject) aMergeAltitude = theZone->GetMergeAltitude();
606 if ( !aMergeAltitude.IsNull() )
608 if ( aZoneInterpolator != NULL )
610 aZoneInterpolator->SetAltitudeObject( aMergeAltitude );
611 aResAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
614 aResAltitude = aMergeAltitude->GetAltitudeForPoint( thePoint );
619 HYDROData_SequenceOfObjects aZoneObjects = theZone->GetGeometryObjects();
620 HYDROData_SequenceOfObjects::Iterator anIter( aZoneObjects );
621 for ( ; anIter.More(); anIter.Next() )
623 Handle(HYDROData_Object) aZoneObj =
624 Handle(HYDROData_Object)::DownCast( anIter.Value() );
625 if ( aZoneObj.IsNull() )
628 Handle(HYDROData_IAltitudeObject) anObjAltitude = aZoneObj->GetAltitudeObject();
629 if ( anObjAltitude.IsNull() )
632 double aPointAltitude = 0.0;
633 if ( aZoneInterpolator != NULL )
635 aZoneInterpolator->SetAltitudeObject( anObjAltitude );
636 aPointAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
639 aPointAltitude = anObjAltitude->GetAltitudeForPoint( thePoint );
641 if ( ValuesEquals( aPointAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) )
644 if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
646 aResAltitude = aPointAltitude;
649 else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMIN )
651 if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
652 aResAltitude > aPointAltitude )
654 aResAltitude = aPointAltitude;
657 else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMAX )
659 if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
660 aResAltitude < aPointAltitude )
662 aResAltitude = aPointAltitude;
671 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints(
672 const NCollection_Sequence<gp_XY>& thePoints,
673 const Handle(HYDROData_Region)& theRegion ) const
675 NCollection_Sequence<double> aResSeq;
677 for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
679 const gp_XY& thePnt = thePoints.Value( i );
681 double anAltitude = GetAltitudeForPoint( thePnt, theRegion );
682 aResSeq.Append( anAltitude );
688 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints(
689 const NCollection_Sequence<gp_XY>& thePoints,
690 const Handle(HYDROData_Zone)& theZone ) const
692 NCollection_Sequence<double> aResSeq;
694 for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
696 const gp_XY& thePnt = thePoints.Value( i );
698 double anAltitude = GetAltitudeForPoint( thePnt, theZone );
699 aResSeq.Append( anAltitude );
705 Handle(HYDROData_Region) HYDROData_CalculationCase::GetRegionFromPoint( const gp_XY& thePoint ) const
707 Handle(HYDROData_Region) aResRegion;
709 Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
710 if ( !aZone.IsNull() )
711 aResRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
716 Handle(HYDROData_Zone) HYDROData_CalculationCase::GetZoneFromPoint( const gp_XY& thePoint ) const
718 Handle(HYDROData_Zone) aResZone;
720 HYDROData_SequenceOfObjects aRegions = GetRegions();
722 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
723 for ( ; anIter.More() && aResZone.IsNull(); anIter.Next() )
725 Handle(HYDROData_Region) aRegion =
726 Handle(HYDROData_Region)::DownCast( anIter.Value() );
727 if ( aRegion.IsNull() )
730 HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
731 HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
732 for ( ; aZonesIter.More() && aResZone.IsNull(); aZonesIter.Next() )
734 Handle(HYDROData_Zone) aRegZone =
735 Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
736 if ( aRegZone.IsNull() )
739 PointClassification aPointRelation = GetPointClassification( thePoint, aRegZone );
740 if ( aPointRelation != POINT_OUT )
741 aResZone = aRegZone; // We found the desired zone
748 HYDROData_CalculationCase::PointClassification HYDROData_CalculationCase::GetPointClassification(
749 const gp_XY& thePoint,
750 const Handle(HYDROData_Zone)& theZone ) const
752 PointClassification aRes = POINT_OUT;
753 if ( theZone.IsNull() )
756 TopoDS_Face aZoneFace = TopoDS::Face( theZone->GetShape() );
757 if ( aZoneFace.IsNull() )
760 TopoDS_Compound aCmp;
762 aBB.MakeCompound(aCmp);
763 aBB.Add(aCmp, aZoneFace);
764 gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
765 BRepBuilderAPI_MakeVertex aMk(aPnt);
766 aBB.Add(aCmp, aMk.Vertex());
767 BRepTools::Write(aCmp, "FCL2d.brep");
769 TopAbs_State State = HYDROData_Tool::ComputePointState(thePoint, aZoneFace);
770 if (State == TopAbs_OUT)
772 else if(State == TopAbs_IN)
774 else if(State == TopAbs_ON)
779 Handle(HYDROData_Region) HYDROData_CalculationCase::addNewRegion( const Handle(HYDROData_Document)& theDoc,
780 const QString& thePrefixOrName,
783 TDF_Label aNewLab = myLab.FindChild( DataTag_ChildRegion ).NewChild();
785 Handle(HYDROData_Region) aNewRegion =
786 Handle(HYDROData_Region)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_REGION ) );
787 AddRegion( aNewRegion );
789 QString aRegionName = isPrefix ? HYDROData_Tool::GenerateObjectName( theDoc, thePrefixOrName ) : thePrefixOrName;
790 aNewRegion->SetName( aRegionName );
795 Handle(HYDROData_SplittedShapesGroup) HYDROData_CalculationCase::addNewSplittedGroup( const QString& theName )
797 TDF_Label aNewLab = myLab.FindChild( DataTag_SplittedGroups ).NewChild();
799 Handle(HYDROData_SplittedShapesGroup) aNewGroup =
800 Handle(HYDROData_SplittedShapesGroup)::DownCast(
801 HYDROData_Iterator::CreateObject( aNewLab, KIND_SPLITTED_GROUP ) );
802 AddReferenceObject( aNewGroup, DataTag_SplittedGroups );
804 aNewGroup->SetName( theName );
809 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var theGeomEngine,
810 SALOMEDS::Study_ptr theStudy ) const
812 HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroupsDefs;
814 // Get groups definitions
815 HYDROData_SequenceOfObjects aSplittedGroups = GetSplittedGroups();
817 HYDROData_SequenceOfObjects::Iterator anIter( aSplittedGroups );
818 for ( ; anIter.More(); anIter.Next() )
821 Handle(HYDROData_ShapesGroup) aGroup =
822 Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
823 if ( aGroup.IsNull() )
826 HYDROData_ShapesGroup::GroupDefinition aGroupDef;
828 aGroupDef.Name = aGroup->GetName().toLatin1().constData();
829 aGroup->GetShapes( aGroupDef.Shapes );
831 aSeqOfGroupsDefs.Append( aGroupDef );
835 TopTools_ListOfShape aFaces;
836 HYDROData_SequenceOfObjects aCaseRegions = GetRegions();
837 HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
838 for ( ; aRegionIter.More(); aRegionIter.Next() )
840 Handle(HYDROData_Region) aRegion =
841 Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
842 if( aRegion.IsNull() )
845 TopoDS_Shape aRegionShape = aRegion->GetShape( &aSeqOfGroupsDefs );
846 aFaces.Append( aRegionShape );
849 return Export( theGeomEngine, theStudy, aFaces, aSeqOfGroupsDefs );
852 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var theGeomEngine,
853 SALOMEDS::Study_ptr theStudy,
854 const TopTools_ListOfShape& theFaces,
855 const HYDROData_ShapesGroup::SeqOfGroupsDefs& theGroupsDefs ) const
858 BRepBuilderAPI_Sewing aSewing( Precision::Confusion() * 10.0 );
859 aSewing.SetNonManifoldMode( Standard_False );
860 #ifdef DEB_CALCULATION
861 TCollection_AsciiString aNam("Sh_");
864 TopTools_ListIteratorOfListOfShape aFaceIter( theFaces );
865 for ( ; aFaceIter.More(); aFaceIter.Next() )
867 TopoDS_Shape aShape = aFaceIter.Value();
868 if ( aShape.IsNull() )
871 if ( aShape.ShapeType() == TopAbs_FACE )
873 aSewing.Add( aShape );
874 #ifdef DEB_CALCULATION
875 TCollection_AsciiString aName = aNam + ++i + ".brep";
876 BRepTools::Write(aShape ,aName.ToCString());
881 #ifdef DEB_CALCULATION
884 TopExp_Explorer anExp( aShape, TopAbs_FACE );
885 for (; anExp.More(); anExp.Next() ) {
886 aSewing.Add( anExp.Current() );
887 #ifdef DEB_CALCULATION
889 TCollection_AsciiString aName = aNam + i + "_" + ++j + ".brep";
890 BRepTools::Write(anExp.Current() ,aName.ToCString());
897 TopoDS_Shape aSewedShape = aSewing.SewedShape();
899 // If the sewed shape is empty - return false
900 if ( aSewedShape.IsNull() || !TopoDS_Iterator( aSewedShape ).More() )
903 #ifdef DEB_CALCULATION
904 BRepTools::Write(aSewedShape ,"Sew.brep");
906 // Publish the sewed shape
907 QString aName = EXPORT_NAME;
908 GEOM::GEOM_Object_ptr aMainShape =
909 publishShapeInGEOM( theGeomEngine, theStudy, aSewedShape, aName );
911 if ( aMainShape->_is_nil() )
914 if ( theGroupsDefs.IsEmpty() )
918 TopTools_IndexedMapOfShape aMapOfSubShapes;
919 TopExp::MapShapes( aSewedShape, aMapOfSubShapes );
921 NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> > aGroupsData;
923 for ( int aGrId = 1, nbGroups = theGroupsDefs.Length(); aGrId <= nbGroups; ++aGrId )
925 const HYDROData_ShapesGroup::GroupDefinition& aGroupDef = theGroupsDefs.Value( aGrId );
927 NCollection_Sequence<int> aGroupIndexes;
928 for( int i = 1, n = aGroupDef.Shapes.Length(); i <= n; i++ )
930 const TopoDS_Shape& aShape = aGroupDef.Shapes.Value( i );
931 #ifdef DEB_CALCULATION
932 cout << "\nOld shape(" << i << ") = " << aShape.TShape() <<endl;
935 TopoDS_Shape aModifiedShape = aShape;
936 if ( aSewing.IsModified( aShape ) )
937 aModifiedShape = aSewing.Modified( aShape );
938 else if ( aSewing.IsModifiedSubShape( aShape ) )
939 aModifiedShape = aSewing.ModifiedSubShape( aShape );
941 #ifdef DEB_CALCULATION
942 const TopLoc_Location& aL1 = aShape.Location();
943 const TopLoc_Location& aL2 = aModifiedShape.Location();
944 cout << "\nNew shape(" << i << ") = " << aModifiedShape.TShape() << " Location is Equal = " << aL1.IsEqual(aL2)<<endl;
947 int anIndex = aMapOfSubShapes.FindIndex(aModifiedShape);
949 aGroupIndexes.Append( anIndex );
951 #ifdef DEB_CALCULATION
952 TCollection_AsciiString aNam("Lost_");
953 if(!aMapOfSubShapes.Contains(aModifiedShape)) {
954 for ( int anIndex = 1; anIndex <= aMapOfSubShapes.Extent(); anIndex++ )
956 const TopoDS_Shape& aS = aMapOfSubShapes.FindKey( anIndex );
957 if ( aModifiedShape.IsPartner( aS ) )
959 cout <<"\nIndex in Map = " << anIndex << "TShape = " << aS.TShape() <<endl;
960 TCollection_AsciiString aName = aNam + i + "_" + anIndex + ".brep";
961 BRepTools::Write(aS ,aName.ToCString());
969 if ( !aGroupIndexes.IsEmpty() )
970 aGroupsData.Bind( aGroupDef.Name, aGroupIndexes );
973 if ( !aGroupsData.IsEmpty() )
975 GEOM::GEOM_IGroupOperations_var aGroupOp =
976 theGeomEngine->GetIGroupOperations( theStudy->StudyId() );
978 NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> >::Iterator aMapIt( aGroupsData );
979 for ( ; aMapIt.More(); aMapIt.Next() )
981 const TCollection_AsciiString& aGroupName = aMapIt.Key();
982 const NCollection_Sequence<int>& aGroupIndexes = aMapIt.Value();
984 GEOM::GEOM_Object_var aGeomGroup = aGroupOp->CreateGroup( aMainShape, TopAbs_EDGE );
985 if ( CORBA::is_nil( aGeomGroup ) || !aGroupOp->IsDone() )
988 GEOM::ListOfLong_var aGeomIndexes = new GEOM::ListOfLong;
989 aGeomIndexes->length( aGroupIndexes.Length() );
991 for( int i = 1, n = aGroupIndexes.Length(); i <= n; i++ )
992 aGeomIndexes[ i - 1 ] = aGroupIndexes.Value( i );
994 aGroupOp->UnionIDs( aGeomGroup, aGeomIndexes );
995 if ( aGroupOp->IsDone() )
997 SALOMEDS::SObject_var aGroupSO =
998 theGeomEngine->AddInStudy( theStudy, aGeomGroup, aGroupName.ToCString(), aMainShape );
1006 GEOM::GEOM_Object_ptr HYDROData_CalculationCase::publishShapeInGEOM(
1007 GEOM::GEOM_Gen_var theGeomEngine, SALOMEDS::Study_ptr theStudy,
1008 const TopoDS_Shape& theShape, const QString& theName ) const
1010 GEOM::GEOM_Object_var aGeomObj;
1012 if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ||
1013 theShape.IsNull() ) {
1014 return aGeomObj._retn();
1017 std::ostringstream aStreamShape;
1018 // Write TopoDS_Shape in ASCII format to the stream
1019 BRepTools::Write( theShape, aStreamShape );
1020 // Returns the number of bytes that have been stored in the stream's buffer.
1021 int aSize = aStreamShape.str().size();
1022 // Allocate octect buffer of required size
1023 CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize );
1024 // Copy ostrstream content to the octect buffer
1025 memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize );
1027 SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
1029 // Restore shape from the stream and get the GEOM object
1030 GEOM::GEOM_IInsertOperations_var anInsOp = theGeomEngine->GetIInsertOperations( theStudy->StudyId() );
1031 aGeomObj = anInsOp->RestoreShape( aSeqFile );
1033 // Puplish the GEOM object
1034 if ( !aGeomObj->_is_nil() ) {
1035 QString aName = GEOMBase::GetDefaultName( theName );
1037 SALOMEDS::SObject_var aResultSO =
1038 theGeomEngine->PublishInStudy( theStudy, SALOMEDS::SObject::_nil(),
1039 aGeomObj, qPrintable( aName ) );
1040 if ( aResultSO->_is_nil() ) {
1041 aGeomObj = GEOM::GEOM_Object::_nil();
1045 return aGeomObj._retn();
1048 void HYDROData_CalculationCase::ClearRules()
1050 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1051 HYDROData_PriorityQueue::ClearRules( aRulesLab );
1054 void HYDROData_CalculationCase::AddRule( const Handle(HYDROData_Object)& theObject1,
1055 HYDROData_PriorityType thePriority,
1056 const Handle(HYDROData_Object)& theObject2,
1057 HYDROData_Zone::MergeAltitudesType theMergeType )
1059 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1060 HYDROData_PriorityQueue::AddRule( aRulesLab, theObject1, thePriority, theObject2, theMergeType );
1063 QString HYDROData_CalculationCase::DumpRules()
1065 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1066 return HYDROData_PriorityQueue::DumpRules( aRulesLab );