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"
14 #include "HYDROData_GeomTool.h"
21 #include <TopoDS_Shell.hxx>
22 #include <TopoDS_Edge.hxx>
24 #include <BRep_Builder.hxx>
25 #include <BRepBuilderAPI_Sewing.hxx>
26 #include <BRepTopAdaptor_FClass2d.hxx>
28 #include <BRepTools.hxx>
31 #include <TopExp_Explorer.hxx>
33 #include <TopTools_ListOfShape.hxx>
34 #include <TopTools_ListIteratorOfListOfShape.hxx>
35 #include <TDataStd_Integer.hxx>
37 //#define DEB_CALCULATION 1
38 #ifdef DEB_CALCULATION
39 #include <BRepTools.hxx>
40 #include <TopLoc_Location.hxx>
42 #define CALCULATION_REGIONS_PREF GetName() + "_Reg"
43 #define CALCULATION_ZONES_PREF GetName() + "_Zone"
44 #define CALCULATION_GROUPS_PREF GetName() + "_"
45 //#define DEB_CLASS2D 1
47 #include <BRepBuilderAPI_MakeVertex.hxx>
50 #define EXPORT_NAME "HYDRO_" + GetName()
52 IMPLEMENT_STANDARD_HANDLE(HYDROData_CalculationCase, HYDROData_Entity)
53 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_CalculationCase, HYDROData_Entity)
55 HYDROData_CalculationCase::HYDROData_CalculationCase()
60 HYDROData_CalculationCase::~HYDROData_CalculationCase()
64 void HYDROData_CalculationCase::SetName( const QString& theName )
66 QString anOldCaseName = GetName();
67 if ( anOldCaseName != theName )
69 HYDROData_SequenceOfObjects aRegions = GetRegions();
71 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
72 for ( ; anIter.More(); anIter.Next() )
74 Handle(HYDROData_Region) aRegion =
75 Handle(HYDROData_Region)::DownCast( anIter.Value() );
76 if ( aRegion.IsNull() )
79 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aRegion );
81 HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
82 HYDROData_SequenceOfObjects::Iterator anIter( aZones );
83 for ( ; anIter.More(); anIter.Next() )
85 Handle(HYDROData_Zone) aRegZone =
86 Handle(HYDROData_Zone)::DownCast( anIter.Value() );
87 if ( aRegZone.IsNull() )
90 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aRegZone );
94 HYDROData_SequenceOfObjects aGroups = GetSplittedGroups();
96 anIter.Init( aGroups );
97 for ( ; anIter.More(); anIter.Next() )
99 Handle(HYDROData_SplittedShapesGroup) aGroup =
100 Handle(HYDROData_SplittedShapesGroup)::DownCast( anIter.Value() );
101 if ( aGroup.IsNull() )
104 HYDROData_Tool::UpdateChildObjectName( anOldCaseName, theName, aGroup );
108 HYDROData_Entity::SetName( theName );
111 QStringList HYDROData_CalculationCase::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
113 QStringList aResList = dumpObjectCreation( theTreatedObjects );
114 aResList.prepend( "# Calculation case" );
116 QString aCalculName = GetObjPyName();
118 AssignmentMode aMode = GetAssignmentMode();
119 QString aModeStr = aMode==MANUAL ? "HYDROData_CalculationCase.MANUAL" : "HYDROData_CalculationCase.AUTOMATIC";
120 aResList << QString( "%0.SetAssignmentMode( %1 )" ).arg( aCalculName ).arg( aModeStr );
122 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
123 HYDROData_SequenceOfObjects::Iterator anIter( aGeomObjects );
124 for ( ; anIter.More(); anIter.Next() )
126 Handle(HYDROData_Object) aRefGeomObj =
127 Handle(HYDROData_Object)::DownCast( anIter.Value() );
128 setPythonReferenceObject( theTreatedObjects, aResList, aRefGeomObj, "AddGeometryObject" );
130 aResList << QString( "" );
132 QString aGroupName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "case_geom_group" );
134 HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
135 anIter.Init( aGeomGroups );
136 for ( ; anIter.More(); anIter.Next() )
138 Handle(HYDROData_ShapesGroup) aGeomGroup =
139 Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
140 if ( aGeomGroup.IsNull() )
143 Handle(HYDROData_Object) aFatherGeom =
144 Handle(HYDROData_Object)::DownCast( aGeomGroup->GetFatherObject() );
145 if ( aFatherGeom.IsNull() )
148 int aGroupId = aFatherGeom->GetGroupId( aGeomGroup );
149 aResList << QString( "%1 = %2.GetGroup( %3 );" )
150 .arg( aGroupName ).arg( aFatherGeom->GetObjPyName() ).arg( aGroupId );
152 aResList << QString( "%1.AddGeometryGroup( %2 );" ).arg( aCalculName ).arg( aGroupName );
155 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
156 setPythonReferenceObject( theTreatedObjects, aResList, aBoundaryPolyline, "SetBoundaryPolyline" );
158 if( aMode==AUTOMATIC )
159 DumpRulesToPython( aCalculName, aResList );
161 aResList << QString( "" );
162 aResList << "# Start the algorithm of the partition and assignment";
163 aResList << QString( "%1.Update();" ).arg( aCalculName );
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 theTreatedObjects.insert( aRegion->GetName(), aRegion );
178 QStringList aRegDump = aRegion->DumpToPython( theTreatedObjects );
179 aResList << aRegDump;
183 // Export calculation case
184 aResList << QString( "" );
185 aResList << "# Export of the calculation case";
186 QString aStudyName = "theStudy";
187 QString anEntryVar = aCalculName + "_entry";
188 aResList << QString( "%1 = %2.Export( %3._get_StudyId() )" ).arg( anEntryVar ).arg( aCalculName ).arg( aStudyName );
190 // Get geometry object and print debug information
192 aResList << "# Get geometry shape and print debug information";
193 aResList << "import GEOM";
194 aResList << QString( "print \"Entry:\", %1" ).arg( anEntryVar );
195 QString aGeomShapeName = aCalculName + "_geom";
196 aResList << QString( "%1 = salome.IDToObject( str( %2 ) )" ).arg( aGeomShapeName ).arg( anEntryVar );
197 aResList << QString( "print \"Geom shape:\", %1" ).arg( aGeomShapeName );
198 aResList << QString( "print \"Geom shape name:\", %1.GetName()" ).arg( aGeomShapeName );
200 DumpSampleMeshing( aResList, aStudyName, aGeomShapeName, aCalculName+"_mesh" );
202 aResList << QString( "" );
206 void HYDROData_CalculationCase::DumpSampleMeshing( QStringList& theResList,
207 const QString& theStudyName,
208 const QString& theGeomShapeName,
209 const QString& theMeshName ) const
212 theResList << "# Meshing";
213 theResList << "import SMESH, SALOMEDS";
214 theResList << "from salome.smesh import smeshBuilder";
215 theResList << "from salome.geom import geomBuilder";
217 theResList << QString( "smesh = smeshBuilder.New( %1 )" ).arg( theStudyName );
218 theResList << QString( "%1 = smesh.Mesh( %2 )" ).arg( theMeshName ).arg( theGeomShapeName );
219 theResList << QString( "MEFISTO_2D = %1.Triangle( algo=smeshBuilder.MEFISTO )" ).arg( theMeshName );
220 theResList << "Max_Element_Area_1 = MEFISTO_2D.MaxElementArea( 10 )";
221 theResList << QString( "Regular_1D = %1.Segment()" ).arg( theMeshName );
222 theResList << "Max_Size_1 = Regular_1D.MaxSize(10)";
223 theResList << QString( "isDone = %1.Compute()" ).arg( theMeshName );
226 theResList << "# Set names of Mesh objects";
227 theResList << "smesh.SetName( MEFISTO_2D.GetAlgorithm(), 'MEFISTO_2D' )";
228 theResList << "smesh.SetName( Regular_1D.GetAlgorithm(), 'Regular_1D' )";
229 theResList << "smesh.SetName( Max_Size_1, 'Max Size_1' )";
230 theResList << "smesh.SetName( Max_Element_Area_1, 'Max. Element Area_1' )";
231 theResList << QString( "smesh.SetName( %1.GetMesh(), '%1' )" ).arg( theMeshName );
234 theResList << "# Greate SMESH groups";
235 theResList << QString( "geompy = geomBuilder.New( %1 )" ).arg( theStudyName );
236 theResList << QString( "geom_groups = geompy.GetGroups( %1 )" ).arg( theGeomShapeName );
237 theResList << QString( "for group in geom_groups:" );
238 theResList << QString( " smesh_group = %1.GroupOnGeom(group, group.GetName(), SMESH.EDGE)" )
240 theResList << QString( " smesh.SetName(smesh_group, group.GetName())" );
241 theResList << QString( " print \"SMESH group '%s': %s\" % (smesh_group.GetName(), smesh_group)" );
244 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetAllReferenceObjects() const
246 HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
248 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
249 if ( !aBoundaryPolyline.IsNull() )
250 aResSeq.Append( aBoundaryPolyline );
252 HYDROData_SequenceOfObjects aSeqOfRegions = GetRegions();
253 aResSeq.Append( aSeqOfRegions );
258 void HYDROData_CalculationCase::Update()
260 HYDROData_Entity::Update();
263 // At first we remove previously created objects
265 RemoveSplittedGroups();
267 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
268 if ( aDocument.IsNull() )
271 Handle(HYDROData_PolylineXY) aBoundaryPolyline = GetBoundaryPolyline();
272 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
273 if ( aGeomObjects.IsEmpty() )
276 HYDROData_SequenceOfObjects aGeomGroups = GetGeometryGroups();
278 HYDROData_SplitToZonesTool::SplitDataList aSplitObjects =
279 HYDROData_SplitToZonesTool::Split( aGeomObjects, aGeomGroups, aBoundaryPolyline );
280 if ( aSplitObjects.isEmpty() )
283 HYDROData_SplitToZonesTool::SplitDataList aZonesList, anEdgesList;
285 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplitObjects );
286 while( anIter.hasNext() )
288 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
289 if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Zone )
290 aZonesList.append( aSplitData );
291 else if ( aSplitData.Type == HYDROData_SplitToZonesTool::SplitData::Data_Edge )
292 anEdgesList.append( aSplitData );
295 switch( GetAssignmentMode() )
298 CreateRegionsDef( aDocument, aZonesList );
301 CreateRegionsAuto( aDocument, aZonesList );
304 CreateEdgeGroupsDef( aDocument, anEdgesList );
307 void HYDROData_CalculationCase::CreateRegionsDef( const Handle(HYDROData_Document)& theDoc,
308 const HYDROData_SplitToZonesTool::SplitDataList& theZones )
310 // Create result regions for case, by default one zone for one region
311 QString aRegsPref = CALCULATION_REGIONS_PREF;
312 QString aZonesPref = CALCULATION_ZONES_PREF;
314 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theZones );
315 while( anIter.hasNext() )
317 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
319 Handle(HYDROData_Region) aRegion = addNewRegion( theDoc, aRegsPref );
321 // Add the zone for region
322 Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( theDoc, aZonesPref, aSplitData.Face(), aSplitData.ObjectNames );
326 void HYDROData_CalculationCase::CreateRegionsAuto( const Handle(HYDROData_Document)& theDoc,
327 const HYDROData_SplitToZonesTool::SplitDataList& theZones )
329 QMap<QString, Handle(HYDROData_Region)> aRegionsMap; //object name to region
330 QMap<QString, QString> aRegionNameToObjNameMap;
331 QString aZonesPref = CALCULATION_ZONES_PREF;
332 HYDROData_PriorityQueue aPr( this );
334 // 1. First we create a default region for each object included into the calculation case
335 HYDROData_SequenceOfObjects aGeomObjects = GetGeometryObjects();
336 for( int i=aGeomObjects.Lower(), n=aGeomObjects.Upper(); i<=n; i++ )
338 Handle(HYDROData_Object) anObj = Handle(HYDROData_Object)::DownCast( aGeomObjects.Value( i ) );
341 QString anObjName = anObj->GetName();
342 QString aRegName = anObjName + "_reg";
343 Handle(HYDROData_Region) aRegion = addNewRegion( theDoc, aRegName, false );
344 aRegionsMap.insert( anObjName, aRegion );
345 aRegionNameToObjNameMap.insert( aRegName, anObjName );
348 // 2. Now for each zone it is necessary to determine the most priority object
349 // and assign to zone to corresponding region
350 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theZones );
351 while( anIter.hasNext() )
353 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
354 HYDROData_Zone::MergeAltitudesType aMergeType;
355 Handle(HYDROData_Object) aRegObj = aPr.GetMostPriorityObject( aSplitData.ObjectNames, aMergeType );
356 if( aRegObj.IsNull() )
358 Handle(HYDROData_Region) aRegion = aRegionsMap[aRegObj->GetName()];
359 if( aRegion.IsNull() )
361 Handle(HYDROData_Zone) aRegionZone = aRegion->addNewZone( theDoc, aZonesPref, aSplitData.Face(), aSplitData.ObjectNames );
363 if( aSplitData.ObjectNames.count() > 1 && aMergeType==HYDROData_Zone::Merge_UNKNOWN )
365 qDebug( "Error in algorithm: unresolved conflicts" );
370 case HYDROData_Zone::Merge_ZMIN:
371 case HYDROData_Zone::Merge_ZMAX:
372 aRegionZone->SetMergeType( aMergeType );
374 case HYDROData_Zone::Merge_Object:
375 aRegionZone->SetMergeType( aMergeType );
376 aRegionZone->RemoveMergeAltitude();
377 aRegionZone->SetMergeAltitude( aRegObj->GetAltitudeObject() );
382 QStringList anObjectsWithEmptyRegions;
383 QMap<QString, Handle(HYDROData_Region)>::const_iterator
384 anIt = aRegionsMap.begin(), aLast = aRegionsMap.end();
385 for( ; anIt!=aLast; anIt++ )
387 Handle(HYDROData_Region) aRegion = anIt.value();
388 if( aRegion->GetZones().IsEmpty() )
390 QString aRegName = aRegion->GetName();
391 QString anObjName = aRegionNameToObjNameMap[aRegName];
392 anObjectsWithEmptyRegions.append( anObjName );
396 if( !anObjectsWithEmptyRegions.empty() )
398 QString aData = anObjectsWithEmptyRegions.join( ", " );
399 SetWarning( WARN_EMPTY_REGIONS, aData );
403 void HYDROData_CalculationCase::CreateEdgeGroupsDef( const Handle(HYDROData_Document)& theDoc,
404 const HYDROData_SplitToZonesTool::SplitDataList& theEdges )
406 QMap<QString,Handle(HYDROData_SplittedShapesGroup)> aSplittedEdgesGroupsMap;
408 HYDROData_SplitToZonesTool::SplitDataListIterator anIter( theEdges );
409 while( anIter.hasNext() )
411 const HYDROData_SplitToZonesTool::SplitData& aSplitData = anIter.next();
412 // Create new edges group
413 if ( aSplitData.ObjectNames.isEmpty() || aSplitData.Shape.IsNull() )
416 QString anObjName = aSplitData.ObjectNames.first();
417 if ( anObjName.isEmpty() )
419 #ifdef DEB_CALCULATION
420 QString aStr = aSplitData.ObjectNames.join(" ");
421 cout << " CCase: Names = "<<aStr.toStdString() << " size = " <<aSplitData.ObjectNames.size() <<endl;
423 Handle(HYDROData_SplittedShapesGroup) aSplittedGroup;
424 if ( !aSplittedEdgesGroupsMap.contains( anObjName ) )
426 aSplittedGroup = addNewSplittedGroup( CALCULATION_GROUPS_PREF + anObjName );
427 aSplittedEdgesGroupsMap.insert( anObjName, aSplittedGroup );
431 aSplittedGroup = aSplittedEdgesGroupsMap[ anObjName ];
433 if ( aSplittedGroup.IsNull() )
436 aSplittedGroup->AddShape( aSplitData.Shape );
440 bool HYDROData_CalculationCase::AddGeometryObject( const Handle(HYDROData_Object)& theObject )
442 if ( !HYDROData_Tool::IsGeometryObject( theObject ) )
443 return false; // Wrong type of object
445 if ( HasReference( theObject, DataTag_GeometryObject ) )
446 return false; // Object is already in reference list
448 AddReferenceObject( theObject, DataTag_GeometryObject );
450 // Indicate model of the need to update splitting
456 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryObjects() const
458 return GetReferenceObjects( DataTag_GeometryObject );
461 void HYDROData_CalculationCase::RemoveGeometryObject( const Handle(HYDROData_Object)& theObject )
463 if ( theObject.IsNull() )
466 RemoveReferenceObject( theObject->Label(), DataTag_GeometryObject );
468 // Indicate model of the need to update splitting
472 void HYDROData_CalculationCase::RemoveGeometryObjects()
474 ClearReferenceObjects( DataTag_GeometryObject );
476 // Indicate model of the need to update splitting
480 bool HYDROData_CalculationCase::AddGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
482 if ( theGroup.IsNull() )
485 if ( HasReference( theGroup, DataTag_GeometryGroup ) )
486 return false; // Object is already in reference list
488 AddReferenceObject( theGroup, DataTag_GeometryGroup );
490 // Indicate model of the need to update splitting
496 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetGeometryGroups() const
498 return GetReferenceObjects( DataTag_GeometryGroup );
501 void HYDROData_CalculationCase::RemoveGeometryGroup( const Handle(HYDROData_ShapesGroup)& theGroup )
503 if ( theGroup.IsNull() )
506 RemoveReferenceObject( theGroup->Label(), DataTag_GeometryGroup );
508 // Indicate model of the need to update splitting
512 void HYDROData_CalculationCase::RemoveGeometryGroups()
514 ClearReferenceObjects( DataTag_GeometryGroup );
516 // Indicate model of the need to update splitting
520 void HYDROData_CalculationCase::SetBoundaryPolyline( const Handle(HYDROData_PolylineXY)& thePolyline )
522 Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
524 SetReferenceObject( thePolyline, DataTag_Polyline );
526 // Indicate model of the need to update zones splitting
527 SetToUpdate( !IsEqual( aPrevPolyline, thePolyline ) || IsMustBeUpdated() );
530 Handle(HYDROData_PolylineXY) HYDROData_CalculationCase::GetBoundaryPolyline() const
532 return Handle(HYDROData_PolylineXY)::DownCast(
533 GetReferenceObject( DataTag_Polyline ) );
536 void HYDROData_CalculationCase::RemoveBoundaryPolyline()
538 Handle(HYDROData_PolylineXY) aPrevPolyline = GetBoundaryPolyline();
540 ClearReferenceObjects( DataTag_Polyline );
542 // Indicate model of the need to update zones splitting
543 SetToUpdate( !aPrevPolyline.IsNull() || IsMustBeUpdated() );
546 Handle(HYDROData_Region) HYDROData_CalculationCase::AddNewRegion( const Handle(HYDROData_Zone)& theZone )
548 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
549 Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
550 if ( aNewRegion.IsNull() )
553 aNewRegion->AddZone( theZone );
558 bool HYDROData_CalculationCase::AddRegion( const Handle(HYDROData_Region)& theRegion )
560 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
562 if ( theRegion.IsNull() )
565 if ( HasReference( theRegion, DataTag_Region ) )
566 return false; // Object is already in reference list
568 // Move the region from other calculation
569 Handle(HYDROData_CalculationCase) aFatherCalc =
570 Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
571 if ( !aFatherCalc.IsNull() && aFatherCalc->Label() != myLab )
573 Handle(HYDROData_Region) aNewRegion = addNewRegion( aDocument, CALCULATION_REGIONS_PREF );
574 theRegion->CopyTo( aNewRegion );
576 aFatherCalc->RemoveRegion( theRegion );
578 theRegion->SetLabel( aNewRegion->Label() );
582 AddReferenceObject( theRegion, DataTag_Region );
588 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetRegions() const
590 return GetReferenceObjects( DataTag_Region );
593 void HYDROData_CalculationCase::UpdateRegionsOrder()
595 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
596 if ( aDocument.IsNull() )
599 HYDROData_SequenceOfObjects aRegions = GetRegions();
601 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
602 for ( ; anIter.More(); anIter.Next() )
604 Handle(HYDROData_Region) aRegion =
605 Handle(HYDROData_Region)::DownCast( anIter.Value() );
606 if ( aRegion.IsNull() )
609 aRegion->SetName( "" );
612 QString aRegsPref = CALCULATION_REGIONS_PREF;
614 anIter.Init( aRegions );
615 for ( ; anIter.More(); anIter.Next() )
617 Handle(HYDROData_Region) aRegion =
618 Handle(HYDROData_Region)::DownCast( anIter.Value() );
619 if ( aRegion.IsNull() )
622 QString aRegionName = HYDROData_Tool::GenerateObjectName( aDocument, aRegsPref );
623 aRegion->SetName( aRegionName );
627 void HYDROData_CalculationCase::RemoveRegion( const Handle(HYDROData_Region)& theRegion )
629 if ( theRegion.IsNull() )
632 RemoveReferenceObject( theRegion->Label(), DataTag_Region );
634 // Remove region from data model
635 Handle(HYDROData_CalculationCase) aFatherCalc =
636 Handle(HYDROData_CalculationCase)::DownCast( theRegion->GetFatherObject() );
637 if ( !aFatherCalc.IsNull() && aFatherCalc->Label() == myLab )
641 void HYDROData_CalculationCase::RemoveRegions()
643 myLab.FindChild( DataTag_ChildRegion ).ForgetAllAttributes();
646 HYDROData_SequenceOfObjects HYDROData_CalculationCase::GetSplittedGroups() const
648 return GetReferenceObjects( DataTag_SplittedGroups );
651 void HYDROData_CalculationCase::RemoveSplittedGroups()
653 myLab.FindChild( DataTag_SplittedGroups ).ForgetAllAttributes();
656 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint ) const
658 Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
659 return GetAltitudeForPoint( thePoint, aZone );
662 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint,
663 const Handle(HYDROData_Region)& theRegion ) const
665 double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
667 Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
668 if ( !aZone.IsNull() )
670 Handle(HYDROData_Region) aRefRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
671 if ( IsEqual( aRefRegion, theRegion ) )
672 aResAltitude = GetAltitudeForPoint( thePoint, aZone );
678 double HYDROData_CalculationCase::GetAltitudeForPoint( const gp_XY& thePoint,
679 const Handle(HYDROData_Zone)& theZone ) const
681 double aResAltitude = HYDROData_IAltitudeObject::GetInvalidAltitude();
682 if ( theZone.IsNull() )
685 HYDROData_Zone::MergeAltitudesType aZoneMergeType = theZone->GetMergeType();
686 if ( !theZone->IsMergingNeed() )
688 aZoneMergeType = HYDROData_Zone::Merge_UNKNOWN;
690 else if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
695 HYDROData_IInterpolator* aZoneInterpolator = theZone->GetInterpolator();
696 if ( aZoneMergeType == HYDROData_Zone::Merge_Object )
698 Handle(HYDROData_IAltitudeObject) aMergeAltitude = theZone->GetMergeAltitude();
699 if ( !aMergeAltitude.IsNull() )
701 if ( aZoneInterpolator != NULL )
703 aZoneInterpolator->SetAltitudeObject( aMergeAltitude );
704 aResAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
707 aResAltitude = aMergeAltitude->GetAltitudeForPoint( thePoint );
712 HYDROData_SequenceOfObjects aZoneObjects = theZone->GetGeometryObjects();
713 HYDROData_SequenceOfObjects::Iterator anIter( aZoneObjects );
714 for ( ; anIter.More(); anIter.Next() )
716 Handle(HYDROData_Object) aZoneObj =
717 Handle(HYDROData_Object)::DownCast( anIter.Value() );
718 if ( aZoneObj.IsNull() )
721 Handle(HYDROData_IAltitudeObject) anObjAltitude = aZoneObj->GetAltitudeObject();
722 if ( anObjAltitude.IsNull() )
725 double aPointAltitude = 0.0;
726 if ( aZoneInterpolator != NULL )
728 aZoneInterpolator->SetAltitudeObject( anObjAltitude );
729 aPointAltitude = aZoneInterpolator->GetAltitudeForPoint( thePoint );
732 aPointAltitude = anObjAltitude->GetAltitudeForPoint( thePoint );
734 if ( ValuesEquals( aPointAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) )
737 if ( aZoneMergeType == HYDROData_Zone::Merge_UNKNOWN )
739 aResAltitude = aPointAltitude;
742 else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMIN )
744 if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
745 aResAltitude > aPointAltitude )
747 aResAltitude = aPointAltitude;
750 else if ( aZoneMergeType == HYDROData_Zone::Merge_ZMAX )
752 if ( ValuesEquals( aResAltitude, HYDROData_IAltitudeObject::GetInvalidAltitude() ) ||
753 aResAltitude < aPointAltitude )
755 aResAltitude = aPointAltitude;
764 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints(
765 const NCollection_Sequence<gp_XY>& thePoints,
766 const Handle(HYDROData_Region)& theRegion ) const
768 NCollection_Sequence<double> aResSeq;
770 for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
772 const gp_XY& thePnt = thePoints.Value( i );
774 double anAltitude = GetAltitudeForPoint( thePnt, theRegion );
775 aResSeq.Append( anAltitude );
781 NCollection_Sequence<double> HYDROData_CalculationCase::GetAltitudesForPoints(
782 const NCollection_Sequence<gp_XY>& thePoints,
783 const Handle(HYDROData_Zone)& theZone ) const
785 NCollection_Sequence<double> aResSeq;
787 for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
789 const gp_XY& thePnt = thePoints.Value( i );
791 double anAltitude = GetAltitudeForPoint( thePnt, theZone );
792 aResSeq.Append( anAltitude );
798 Handle(HYDROData_Region) HYDROData_CalculationCase::GetRegionFromPoint( const gp_XY& thePoint ) const
800 Handle(HYDROData_Region) aResRegion;
802 Handle(HYDROData_Zone) aZone = GetZoneFromPoint( thePoint );
803 if ( !aZone.IsNull() )
804 aResRegion = Handle(HYDROData_Region)::DownCast( aZone->GetFatherObject() );
809 Handle(HYDROData_Zone) HYDROData_CalculationCase::GetZoneFromPoint( const gp_XY& thePoint ) const
811 Handle(HYDROData_Zone) aResZone;
813 HYDROData_SequenceOfObjects aRegions = GetRegions();
815 HYDROData_SequenceOfObjects::Iterator anIter( aRegions );
816 for ( ; anIter.More() && aResZone.IsNull(); anIter.Next() )
818 Handle(HYDROData_Region) aRegion =
819 Handle(HYDROData_Region)::DownCast( anIter.Value() );
820 if ( aRegion.IsNull() )
823 HYDROData_SequenceOfObjects aZones = aRegion->GetZones();
824 HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
825 for ( ; aZonesIter.More() && aResZone.IsNull(); aZonesIter.Next() )
827 Handle(HYDROData_Zone) aRegZone =
828 Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
829 if ( aRegZone.IsNull() )
832 PointClassification aPointRelation = GetPointClassification( thePoint, aRegZone );
833 if ( aPointRelation != POINT_OUT )
834 aResZone = aRegZone; // We found the desired zone
841 HYDROData_CalculationCase::PointClassification HYDROData_CalculationCase::GetPointClassification(
842 const gp_XY& thePoint,
843 const Handle(HYDROData_Zone)& theZone ) const
845 PointClassification aRes = POINT_OUT;
846 if ( theZone.IsNull() )
849 TopoDS_Face aZoneFace = TopoDS::Face( theZone->GetShape() );
850 if ( aZoneFace.IsNull() )
853 TopoDS_Compound aCmp;
855 aBB.MakeCompound(aCmp);
856 aBB.Add(aCmp, aZoneFace);
857 gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
858 BRepBuilderAPI_MakeVertex aMk(aPnt);
859 aBB.Add(aCmp, aMk.Vertex());
860 BRepTools::Write(aCmp, "FCL2d.brep");
862 TopAbs_State State = HYDROData_Tool::ComputePointState(thePoint, aZoneFace);
863 if (State == TopAbs_OUT)
865 else if(State == TopAbs_IN)
867 else if(State == TopAbs_ON)
872 Handle(HYDROData_Region) HYDROData_CalculationCase::addNewRegion( const Handle(HYDROData_Document)& theDoc,
873 const QString& thePrefixOrName,
876 TDF_Label aNewLab = myLab.FindChild( DataTag_ChildRegion ).NewChild();
877 int aTag = aNewLab.Tag();
879 Handle(HYDROData_Region) aNewRegion =
880 Handle(HYDROData_Region)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_REGION ) );
881 AddRegion( aNewRegion );
883 QString aRegionName = isPrefix ? HYDROData_Tool::GenerateObjectName( theDoc, thePrefixOrName ) : thePrefixOrName;
884 aNewRegion->SetName( aRegionName );
889 Handle(HYDROData_SplittedShapesGroup) HYDROData_CalculationCase::addNewSplittedGroup( const QString& theName )
891 TDF_Label aNewLab = myLab.FindChild( DataTag_SplittedGroups ).NewChild();
893 Handle(HYDROData_SplittedShapesGroup) aNewGroup =
894 Handle(HYDROData_SplittedShapesGroup)::DownCast(
895 HYDROData_Iterator::CreateObject( aNewLab, KIND_SPLITTED_GROUP ) );
896 AddReferenceObject( aNewGroup, DataTag_SplittedGroups );
898 aNewGroup->SetName( theName );
903 QString HYDROData_CalculationCase::Export( int theStudyId ) const
905 GEOM::GEOM_Gen_var aGEOMEngine = HYDROData_GeomTool::GetGeomGen();
906 SALOMEDS::Study_var aDSStudy = HYDROData_GeomTool::GetStudyByID( theStudyId );
908 QString aGeomObjEntry;
909 bool isOK = Export( aGEOMEngine, aDSStudy, aGeomObjEntry );
910 return isOK ? aGeomObjEntry : QString();
913 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var theGeomEngine,
914 SALOMEDS::Study_ptr theStudy,
915 QString& theGeomObjEntry ) const
917 HYDROData_ShapesGroup::SeqOfGroupsDefs aSeqOfGroupsDefs;
919 // Get groups definitions
920 HYDROData_SequenceOfObjects aSplittedGroups = GetSplittedGroups();
922 HYDROData_SequenceOfObjects::Iterator anIter( aSplittedGroups );
923 for ( ; anIter.More(); anIter.Next() )
926 Handle(HYDROData_ShapesGroup) aGroup =
927 Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
928 if ( aGroup.IsNull() )
931 HYDROData_ShapesGroup::GroupDefinition aGroupDef;
933 aGroupDef.Name = aGroup->GetName().toLatin1().constData();
934 aGroup->GetShapes( aGroupDef.Shapes );
936 aSeqOfGroupsDefs.Append( aGroupDef );
940 TopTools_ListOfShape aFaces;
941 HYDROData_SequenceOfObjects aCaseRegions = GetRegions();
942 HYDROData_SequenceOfObjects::Iterator aRegionIter( aCaseRegions );
943 for ( ; aRegionIter.More(); aRegionIter.Next() )
945 Handle(HYDROData_Region) aRegion =
946 Handle(HYDROData_Region)::DownCast( aRegionIter.Value() );
947 if( aRegion.IsNull() )
950 TopoDS_Shape aRegionShape = aRegion->GetShape( &aSeqOfGroupsDefs );
951 aFaces.Append( aRegionShape );
954 return Export( theGeomEngine, theStudy, aFaces, aSeqOfGroupsDefs, theGeomObjEntry );
957 bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var theGeomEngine,
958 SALOMEDS::Study_ptr theStudy,
959 const TopTools_ListOfShape& theFaces,
960 const HYDROData_ShapesGroup::SeqOfGroupsDefs& theGroupsDefs,
961 QString& theGeomObjEntry ) const
964 BRepBuilderAPI_Sewing aSewing( Precision::Confusion() * 10.0 );
965 aSewing.SetNonManifoldMode( Standard_False );
966 #ifdef DEB_CALCULATION
967 TCollection_AsciiString aNam("Sh_");
970 TopTools_ListIteratorOfListOfShape aFaceIter( theFaces );
971 for ( ; aFaceIter.More(); aFaceIter.Next() )
973 TopoDS_Shape aShape = aFaceIter.Value();
974 if ( aShape.IsNull() )
977 if ( aShape.ShapeType() == TopAbs_FACE )
979 aSewing.Add( aShape );
980 #ifdef DEB_CALCULATION
981 TCollection_AsciiString aName = aNam + ++i + ".brep";
982 BRepTools::Write(aShape ,aName.ToCString());
987 #ifdef DEB_CALCULATION
990 TopExp_Explorer anExp( aShape, TopAbs_FACE );
991 for (; anExp.More(); anExp.Next() ) {
992 aSewing.Add( anExp.Current() );
993 #ifdef DEB_CALCULATION
995 TCollection_AsciiString aName = aNam + i + "_" + ++j + ".brep";
996 BRepTools::Write(anExp.Current() ,aName.ToCString());
1003 TopoDS_Shape aSewedShape = aSewing.SewedShape();
1005 // If the sewed shape is empty - return false
1006 if ( aSewedShape.IsNull() || !TopoDS_Iterator( aSewedShape ).More() )
1009 #ifdef DEB_CALCULATION
1010 BRepTools::Write(aSewedShape ,"Sew.brep");
1012 // Publish the sewed shape
1013 QString aName = EXPORT_NAME;
1014 GEOM::GEOM_Object_ptr aMainShape =
1015 publishShapeInGEOM( theGeomEngine, theStudy, aSewedShape, aName, theGeomObjEntry );
1017 if ( aMainShape->_is_nil() )
1020 if ( theGroupsDefs.IsEmpty() )
1024 TopTools_IndexedMapOfShape aMapOfSubShapes;
1025 TopExp::MapShapes( aSewedShape, aMapOfSubShapes );
1027 NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> > aGroupsData;
1029 for ( int aGrId = 1, nbGroups = theGroupsDefs.Length(); aGrId <= nbGroups; ++aGrId )
1031 const HYDROData_ShapesGroup::GroupDefinition& aGroupDef = theGroupsDefs.Value( aGrId );
1033 NCollection_Sequence<int> aGroupIndexes;
1034 for( int i = 1, n = aGroupDef.Shapes.Length(); i <= n; i++ )
1036 const TopoDS_Shape& aShape = aGroupDef.Shapes.Value( i );
1037 #ifdef DEB_CALCULATION
1038 cout << "\nOld shape(" << i << ") = " << aShape.TShape() <<endl;
1041 TopoDS_Shape aModifiedShape = aShape;
1042 if ( aSewing.IsModified( aShape ) )
1043 aModifiedShape = aSewing.Modified( aShape );
1044 else if ( aSewing.IsModifiedSubShape( aShape ) )
1045 aModifiedShape = aSewing.ModifiedSubShape( aShape );
1047 #ifdef DEB_CALCULATION
1048 const TopLoc_Location& aL1 = aShape.Location();
1049 const TopLoc_Location& aL2 = aModifiedShape.Location();
1050 cout << "\nNew shape(" << i << ") = " << aModifiedShape.TShape() << " Location is Equal = " << aL1.IsEqual(aL2)<<endl;
1053 int anIndex = aMapOfSubShapes.FindIndex(aModifiedShape);
1054 if ( anIndex > 0 ) {
1055 aGroupIndexes.Append( anIndex );
1057 #ifdef DEB_CALCULATION
1058 TCollection_AsciiString aNam("Lost_");
1059 if(!aMapOfSubShapes.Contains(aModifiedShape)) {
1060 for ( int anIndex = 1; anIndex <= aMapOfSubShapes.Extent(); anIndex++ )
1062 const TopoDS_Shape& aS = aMapOfSubShapes.FindKey( anIndex );
1063 if ( aModifiedShape.IsPartner( aS ) )
1065 cout <<"\nIndex in Map = " << anIndex << "TShape = " << aS.TShape() <<endl;
1066 TCollection_AsciiString aName = aNam + i + "_" + anIndex + ".brep";
1067 BRepTools::Write(aS ,aName.ToCString());
1075 if ( !aGroupIndexes.IsEmpty() )
1076 aGroupsData.Bind( aGroupDef.Name, aGroupIndexes );
1079 if ( !aGroupsData.IsEmpty() )
1081 GEOM::GEOM_IGroupOperations_var aGroupOp =
1082 theGeomEngine->GetIGroupOperations( theStudy->StudyId() );
1084 NCollection_DataMap< TCollection_AsciiString, NCollection_Sequence<int> >::Iterator aMapIt( aGroupsData );
1085 for ( ; aMapIt.More(); aMapIt.Next() )
1087 const TCollection_AsciiString& aGroupName = aMapIt.Key();
1088 const NCollection_Sequence<int>& aGroupIndexes = aMapIt.Value();
1090 GEOM::GEOM_Object_var aGeomGroup = aGroupOp->CreateGroup( aMainShape, TopAbs_EDGE );
1091 if ( CORBA::is_nil( aGeomGroup ) || !aGroupOp->IsDone() )
1094 GEOM::ListOfLong_var aGeomIndexes = new GEOM::ListOfLong;
1095 aGeomIndexes->length( aGroupIndexes.Length() );
1097 for( int i = 1, n = aGroupIndexes.Length(); i <= n; i++ )
1098 aGeomIndexes[ i - 1 ] = aGroupIndexes.Value( i );
1100 aGroupOp->UnionIDs( aGeomGroup, aGeomIndexes );
1101 if ( aGroupOp->IsDone() )
1103 SALOMEDS::SObject_var aGroupSO =
1104 theGeomEngine->AddInStudy( theStudy, aGeomGroup, aGroupName.ToCString(), aMainShape );
1112 GEOM::GEOM_Object_ptr HYDROData_CalculationCase::publishShapeInGEOM(
1113 GEOM::GEOM_Gen_var theGeomEngine, SALOMEDS::Study_ptr theStudy,
1114 const TopoDS_Shape& theShape, const QString& theName,
1115 QString& theGeomObjEntry ) const
1117 theGeomObjEntry = "";
1118 GEOM::GEOM_Object_var aGeomObj;
1120 if ( theGeomEngine->_is_nil() || theStudy->_is_nil() ||
1121 theShape.IsNull() ) {
1122 return aGeomObj._retn();
1125 std::ostringstream aStreamShape;
1126 // Write TopoDS_Shape in ASCII format to the stream
1127 BRepTools::Write( theShape, aStreamShape );
1128 // Returns the number of bytes that have been stored in the stream's buffer.
1129 int aSize = aStreamShape.str().size();
1130 // Allocate octect buffer of required size
1131 CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize );
1132 // Copy ostrstream content to the octect buffer
1133 memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize );
1135 SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 );
1137 // Restore shape from the stream and get the GEOM object
1138 GEOM::GEOM_IInsertOperations_var anInsOp = theGeomEngine->GetIInsertOperations( theStudy->StudyId() );
1139 aGeomObj = anInsOp->RestoreShape( aSeqFile );
1141 // Puplish the GEOM object
1142 if ( !aGeomObj->_is_nil() ) {
1143 QString aName = HYDROData_GeomTool::GetFreeName( theStudy, theName );
1145 SALOMEDS::SObject_var aResultSO =
1146 theGeomEngine->PublishInStudy( theStudy, SALOMEDS::SObject::_nil(),
1147 aGeomObj, qPrintable( aName ) );
1148 if ( aResultSO->_is_nil() ) {
1149 aGeomObj = GEOM::GEOM_Object::_nil();
1152 theGeomObjEntry = aResultSO->GetID();
1155 return aGeomObj._retn();
1158 void HYDROData_CalculationCase::ClearRules( const bool theIsSetToUpdate )
1160 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1161 HYDROData_PriorityQueue::ClearRules( aRulesLab );
1163 // Indicate model of the need to update splitting
1164 if ( theIsSetToUpdate ) {
1165 SetToUpdate( true );
1169 void HYDROData_CalculationCase::AddRule( const Handle(HYDROData_Object)& theObject1,
1170 HYDROData_PriorityType thePriority,
1171 const Handle(HYDROData_Object)& theObject2,
1172 HYDROData_Zone::MergeAltitudesType theMergeType )
1174 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1175 HYDROData_PriorityQueue::AddRule( aRulesLab, theObject1, thePriority, theObject2, theMergeType );
1177 // Indicate model of the need to update splitting
1178 SetToUpdate( true );
1181 QString HYDROData_CalculationCase::DumpRules() const
1183 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1184 return HYDROData_PriorityQueue::DumpRules( aRulesLab );
1187 void HYDROData_CalculationCase::SetAssignmentMode( AssignmentMode theMode )
1189 TDF_Label aModeLab = myLab.FindChild( DataTag_AssignmentMode );
1190 TDataStd_Integer::Set( aModeLab, ( int ) theMode );
1192 // Indicate model of the need to update splitting
1193 SetToUpdate( true );
1196 HYDROData_CalculationCase::AssignmentMode HYDROData_CalculationCase::GetAssignmentMode() const
1198 Handle(TDataStd_Integer) aModeAttr;
1199 bool isOK = myLab.FindChild( DataTag_AssignmentMode ).FindAttribute( TDataStd_Integer::GetID(), aModeAttr );
1201 return ( AssignmentMode ) aModeAttr->Get();
1206 void HYDROData_CalculationCase::DumpRulesToPython( const QString& theCalcCaseName,
1207 QStringList& theScript ) const
1209 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1210 HYDROData_PriorityQueue::DumpRulesToPython( aRulesLab, theCalcCaseName, theScript );
1213 HYDROData_Warning HYDROData_CalculationCase::GetLastWarning() const
1215 return myLastWarning;
1218 void HYDROData_CalculationCase::SetWarning( HYDROData_WarningType theType, const QString& theData )
1220 myLastWarning.Type = theType;
1221 myLastWarning.Data = theData;
1224 bool HYDROData_CalculationCase::GetRule( int theIndex,
1225 Handle(HYDROData_Object)& theObject1,
1226 HYDROData_PriorityType& thePriority,
1227 Handle(HYDROData_Object)& theObject2,
1228 HYDROData_Zone::MergeAltitudesType& theMergeType ) const
1230 TDF_Label aRulesLab = myLab.FindChild( DataTag_CustomRules );
1231 return HYDROData_PriorityQueue::GetRule( aRulesLab, theIndex,
1232 theObject1, thePriority, theObject2, theMergeType );